فهرست منبع

Fix/variation versioning (#5406)

* Adjusted variation script to keep existing version when running
* Removed unused 'template-mods' feature of variations
* Added option to git add changes while building variations
* Added the building of variations to deployment process
Jason Crist 3 سال پیش
والد
کامیت
2c8be5f238
3فایلهای تغییر یافته به همراه50 افزوده شده و 35 حذف شده
  1. 26 3
      theme-utils.mjs
  2. 24 15
      variations/build-variations.mjs
  3. 0 17
      variations/variation-readme.md

+ 26 - 3
theme-utils.mjs

@@ -107,6 +107,30 @@ async function pushButtonDeploy(repoType) {
 			await cleanSandboxSvn();
 		}
 
+		//build variations
+		console.log('Building Variations');
+		await executeCommand(`node ./variations/build-variations.mjs git-add-changes`)
+		prompt = await inquirer.prompt([{
+			type: 'confirm',
+			message: 'Are you good with any staged theme variations changes? Make any manual adjustments now if necessary.',
+			name: "continue",
+			default: false
+		}]);
+	
+		if(!prompt.continue){
+			console.log(`Aborted Automated Deploy Process at variations building.` );
+			return;
+		}
+
+		try {
+			await executeCommand(`
+				git commit -m "Building Variations"
+			`);
+		} catch (err) {
+			// Most likely the error is that there are no variation changes to commit.
+			// Just swallowing that error for now
+		}
+
 		let hash = await getLastDeployedHash();
 		let diffUrl;
 
@@ -378,7 +402,6 @@ async function versionBumpThemes() {
 
 	//version bump the root project if there were changes to any of the themes
 	let rootHasVersionBump = await checkProjectForVersionBump(hash);
-	console.log('root check', rootHasVersionBump, versionBumpCount, changesWereMade);
 	if ( versionBumpCount > 0 && ! rootHasVersionBump ) {
 		await executeCommand(`npm version patch --no-git-tag-version && git add package.json package-lock.json`);
 		changesWereMade = true;
@@ -387,7 +410,7 @@ async function versionBumpThemes() {
 	return changesWereMade;
 }
 
-function getThemeMetadata(styleCss, attribute) {
+export function getThemeMetadata(styleCss, attribute) {
 	if ( !styleCss || !attribute ) {
 		return null;
 	}
@@ -829,7 +852,7 @@ EOF`, logResponse);
 /*
  Execute a command locally.
 */
-async function executeCommand(command, logResponse) {
+export async function executeCommand(command, logResponse) {
 	return new Promise((resolove, reject) => {
 
 		let child;

+ 24 - 15
variations/build-variations.mjs

@@ -3,18 +3,21 @@ import fsorig from 'fs';
 import deepmerge from 'deepmerge';
 import { dirname } from 'path';
 import { fileURLToPath } from 'url';
+import { executeCommand, getThemeMetadata } from '../theme-utils.mjs';
 
 const localpath = dirname( fileURLToPath( import.meta.url ) );
+const args = process.argv.slice(2);
 
-(async function start() {
-	let args = process.argv.slice(2);
+async function start() {
 	let source = args?.[0];
 	let variation = args?.[1];
 	if ( source && variation ) {
 		return await buildVariation(source, variation);
 	}
 	return await buildAllVariations();
-})();
+}
+
+start();
 
 async function buildAllVariations(){
 	for (let source of getDirectories( localpath )){
@@ -32,8 +35,16 @@ async function buildVariation(source, variation) {
 	console.log( `Copying the source ${source} to the variation ${variation}` );
 
 	try {
+		// First grab the existing version if the variation exists already
+		let variationExists = fs.existsSync(`${destDir}/style.css`);
+		let currentVersion = null;
 
-		// First empty the old directory.
+		if( variationExists ) {
+			let styleCss = fs.readFileSync(`${destDir}/style.css`, 'utf8');
+			currentVersion = getThemeMetadata(styleCss, 'Version');
+		}
+
+		// then empty the old directory.
 		await fs.emptyDir( destDir );
 
 		// Then copy the source directory.
@@ -42,7 +53,6 @@ async function buildVariation(source, variation) {
 		// remove unneeded resources
 		await fs.remove( destDir + '/sass');
 		await fs.remove( destDir + '/node_modules' );
-		await fs.remove( destDir + '/template-mods.json');
 		await fs.remove( destDir + '/package.json');
 		await fs.remove( destDir + '/package-lock.json');
 
@@ -52,16 +62,6 @@ async function buildVariation(source, variation) {
 		// copy the readme
 		await fs.copy( localpath + '/variation-readme.md', destDir + '/variation-readme.md' );
 
-		// make template modifications
-		const hasMods = fs.existsSync( `${srcVariationDir}/template-mods.json`);
-		if(hasMods) {
-			const srcModsFile = await fs.readFile( `${srcVariationDir}/template-mods.json`, 'utf8' );
-			const modsJson = JSON.parse( srcModsFile );
-			modsJson.forEach(mod => {
-				modifyTemplates(mod.from, mod.to, destDir + '/block-templates');
-			});
-		}
-	
 		// merge the theme.json files
 		const srcJsonFile = await fs.readFile( srcDir + '/theme.json', 'utf8' );
 		const srcVariationJsonFile = await fs.readFile( srcVariationDir + '/theme.json', 'utf8' )
@@ -72,6 +72,15 @@ async function buildVariation(source, variation) {
 		});
 		await fs.writeFile ( destDir + '/theme.json', JSON.stringify( mergedJson, null, '\t' ), 'utf8' );
 
+		// replace the with current version
+		if ( currentVersion != null ) {
+			await executeCommand(`perl -pi -e 's/Version: (.*)$/"Version: '${currentVersion}'"/ge' ${destDir}/style.css`);
+		}
+
+		if ( args[0] == 'git-add-changes') {
+			await executeCommand(`git add ${destDir}`, true);
+		}
+	
 		console.log('Finished sucessfully.\n\n');
 	}
 	catch (err){

+ 0 - 17
variations/variation-readme.md

@@ -24,20 +24,3 @@ For a variation in that folder `/variations/geologist/geologist-banana`
 * Lastly the theme.json files in `/geologist` and `/variations/geologist/geologist-banana` will be merged.
 
 Any resource in the /variations folder will replace the resources in the source theme (with the exception of theme.json);
-
-### Template Mods
-
-A variation can have a `template-mods.json` file that lists strings in the templates to replace.  The use-case this mechanism was build
-for was so that the variation theme could have a different header by defining that in one place.  There are plenty of gaps in this
-implementation but should cover the basic use-cases we need.
-
-The format for this file is:
-
-```
-[
-	{
-		"from": "STRING TO REPLACE",
-		"to": "STRING TO BE USED AS REPLACEMENT"
-	}
-]
-```