From e2d139e91869e2ae45bfe08e18efc2494874452a Mon Sep 17 00:00:00 2001 From: valexandrov Date: Mon, 23 Dec 2019 20:12:10 +0200 Subject: [PATCH 1/3] Version bumping is done through a simple recursive filemtime check on all asset files. --- assets-version.js | 46 ---------------------------------------------- functions.php | 16 +++++++++++++--- 2 files changed, 13 insertions(+), 49 deletions(-) delete mode 100644 assets-version.js diff --git a/assets-version.js b/assets-version.js deleted file mode 100644 index 72446fe..0000000 --- a/assets-version.js +++ /dev/null @@ -1,46 +0,0 @@ -/** - * Currently this script will generate a new version - * number for the theme's asset files. It writes in inc/theme-version.php - */ - -// The two packages required to write in the theme-version.php file -var fs = require('fs'); -var moment = require('moment'); - -// Generate version for theme-version.php for the assets -let someFile = './inc/theme-version.php'; - -fs.readFile(someFile, 'utf8', function (err,data) { - if (err) { - return console.log(err); - } - - // The REGEX to find the assets version - let versionRegex = /define\( 'DX_ASSETS_VERSION', '*(.*?)' \);/g; - - // Find the line where the version is at. - let found = data.match(versionRegex); - - // This will get and increment the version of the assets file. - // 39 must be replaced with automated find - let verIndexOf = found[0].indexOf(', \'') + 3 + 9; // 3 for the ", '" bit and 9 for the date + dash - currentVersion = parseInt(found[0].substring(verIndexOf, found[0].length - 4)); - currentVersion++; - - // Create the date format. Of course you can change that to anything you like - // or even remove it so that you have only the version number and nothing else. - let time = moment().format("YYYYMMDD"); - - // Create the string that we will replace with the existing one. - let themeVersionString = "define( 'DX_ASSETS_VERSION', '" + time + '-' + currentVersion + "' );"; - - // Once again find the string to replace and set the new version - let result = data.replace(versionRegex, themeVersionString); - - // End it all by writing in the file (or throw error :) - fs.writeFile(someFile, result, 'utf8', function (err) { - if (err) return console.log(err); - }); - - console.log("Assets version has been updated!"); -}); diff --git a/functions.php b/functions.php index caf25f0..8733dfc 100644 --- a/functions.php +++ b/functions.php @@ -6,10 +6,20 @@ * * @package DevriX_Starter */ +function dx_get_assets_version( $dir ) { + $rii = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $dir ) ); + $files = array(); + $assets_m_time = array(); + foreach ($rii as $file) { + if ( ! $file->isDir() ) { + array_push( $assets_m_time, date( "YmdHi", filemtime( $file->getPathname() ) ) ); + } + } + + return $assets_m_time; +} -// Dynamic grab master CSS mod time. -$master_modified_time = filemtime( get_theme_file_path() . '/assets/dist/css/master.min.css' ); -define( 'DX_ASSETS_VERSION', $master_modified_time . '-0000' ); +define( 'DX_ASSETS_VERSION', max( dx_get_assets_version( get_template_directory() . '/assets/dist/' ) ) ); /** * Implement the Custom Header feature. From 887eb0d7ff12ee8ead72a8010fdc2debd6920675 Mon Sep 17 00:00:00 2001 From: valexandrov Date: Mon, 23 Dec 2019 20:14:41 +0200 Subject: [PATCH 2/3] Moved "max" function call, so now the recursive asset versiom bump function is completely self-contained. --- functions.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/functions.php b/functions.php index 8733dfc..4a4df01 100644 --- a/functions.php +++ b/functions.php @@ -16,10 +16,10 @@ function dx_get_assets_version( $dir ) { } } - return $assets_m_time; + return max( $assets_m_time ); } -define( 'DX_ASSETS_VERSION', max( dx_get_assets_version( get_template_directory() . '/assets/dist/' ) ) ); +define( 'DX_ASSETS_VERSION', dx_get_assets_version( get_template_directory() . '/assets/dist/' ) ); /** * Implement the Custom Header feature. From 030e7c1b2f936cd9ff8a00106bffae5905e60193 Mon Sep 17 00:00:00 2001 From: valexandrov Date: Mon, 23 Dec 2019 21:01:24 +0200 Subject: [PATCH 3/3] Code refactor. --- functions.php | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/functions.php b/functions.php index 4a4df01..3bccfcd 100644 --- a/functions.php +++ b/functions.php @@ -6,17 +6,23 @@ * * @package DevriX_Starter */ + +/* + * Recursive auto version bump function. + * Loops through all assets in the supplied folder and returns the + * latest date of modification. +*/ function dx_get_assets_version( $dir ) { - $rii = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $dir ) ); - $files = array(); - $assets_m_time = array(); - foreach ($rii as $file) { - if ( ! $file->isDir() ) { - array_push( $assets_m_time, date( "YmdHi", filemtime( $file->getPathname() ) ) ); - } - } - - return max( $assets_m_time ); + $riterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $dir ) ); + $files = array(); + $assets_modification_time = array(); + foreach ($riterator as $file) { + if ( ! $file->isDir() ) { + array_push( $assets_modification_time, date( "YmdHi", filemtime( $file->getPathname() ) ) ); + } + } + + return max( $assets_modification_time ); } define( 'DX_ASSETS_VERSION', dx_get_assets_version( get_template_directory() . '/assets/dist/' ) );