theme-utils.mjs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. import { spawn } from 'child_process';
  2. import fs from 'fs';
  3. import open from 'open';
  4. import inquirer from 'inquirer';
  5. const remoteSSH = 'wpcom-sandbox';
  6. const sandboxPublicThemesFolder = '/home/wpdev/public_html/wp-content/themes/pub';
  7. const sandboxPremiumThemesFolder = '/home/wpdev/public_html/wp-content/themes/premium';
  8. const sandboxRootFolder = '/home/wpdev/public_html/';
  9. const isWin = process.platform === 'win32';
  10. const premiumThemes = [ 'videomaker', 'videomaker-white' ];
  11. (async function start() {
  12. let args = process.argv.slice(2);
  13. let command = args?.[0];
  14. switch (command) {
  15. case "push-button-deploy-git": return pushButtonDeploy('git');
  16. case "push-button-deploy-svn": return pushButtonDeploy('svn');
  17. case "clean-sandbox-git": return cleanSandboxGit();
  18. case "clean-sandbox-svn": return cleanSandboxSvn();
  19. case "clean-premium-sandbox-git": return cleanPremiumSandboxGit();
  20. case "clean-premium-sandbox-svn": return cleanPremiumSandboxSvn();
  21. case "clean-all-sandbox-git": return cleanAllSandboxGit();
  22. case "clean-all-sandbox-svn": return cleanAllSandboxSvn();
  23. case "push-to-sandbox": return pushToSandbox();
  24. case "push-changes-to-sandbox": return pushChangesToSandbox();
  25. case "push-premium-to-sandbox": return pushPremiumToSandbox();
  26. case "version-bump-themes": return versionBumpThemes();
  27. case "land-diff-git": return landChangesGit(args?.[1]);
  28. case "land-diff-svn": return landChangesSvn(args?.[1]);
  29. case "deploy-preview": return deployPreview();
  30. case "deploy-theme": return deployThemes([args?.[1]]);
  31. case "build-com-zip": return buildComZip([args?.[1]]);
  32. }
  33. return showHelp();
  34. })();
  35. function showHelp(){
  36. // TODO: make this helpful
  37. console.log('Help info can go here');
  38. }
  39. /*
  40. Determine what changes would be deployed
  41. */
  42. async function deployPreview() {
  43. console.clear();
  44. console.log('To ensure accuracy clean your sandbox before previewing. (It is not automatically done).');
  45. console.log('npm run sandbox:clean:git OR npm run sandbox:clean:svn')
  46. let message = await checkForDeployability();
  47. if (message) {
  48. console.log(`\n${message}\n\n`);
  49. }
  50. let hash = await getLastDeployedHash();
  51. console.log(`Last deployed hash: ${hash}`);
  52. let changedThemes = await getChangedThemes(hash);
  53. console.log(`The following themes have changes:\n${changedThemes}`);
  54. let logs = await executeCommand(`git log --reverse --pretty=format:%s ${hash}..HEAD`);
  55. console.log(`\n\nCommit log of changes to be deployed:\n\n${logs}\n\n`);
  56. }
  57. /*
  58. Execute the first phase of a deployment.
  59. Leverages git on the sandbox.
  60. * Gets the last deployed hash from the sandbox
  61. * Version bump all themes have have changes since the last deployment
  62. * Commit the version bump change to github
  63. * Clean the sandbox and ensure it is up-to-date
  64. * Push all changed files (including removal of deleted files) since the last deployment
  65. * Update the 'last deployed' hash on the sandbox
  66. * Create a phabricator diff based on the changes since the last deployment. The description including the commit messages since the last deployment.
  67. * Open the Phabricator Diff in your browser
  68. * Create a tag in the github repository at this point of change which includes the phabricator link in the description
  69. */
  70. async function pushButtonDeploy(repoType) {
  71. console.clear();
  72. let prompt = await inquirer.prompt([{
  73. type: 'confirm',
  74. message: 'You are about to deploy /trunk. Are you ready to continue?',
  75. name: "continue",
  76. default: false
  77. }]);
  78. if(!prompt.continue){
  79. return;
  80. }
  81. if (repoType != 'svn' && repoType != 'git' ) {
  82. return console.log('Specify a repo type to use push-button deploy');
  83. }
  84. let message = await checkForDeployability();
  85. if (message) {
  86. return console.log(`\n\n${message}\n\n`);
  87. }
  88. try {
  89. if (repoType === 'git' ) {
  90. await cleanSandboxGit();
  91. }
  92. else {
  93. await cleanSandboxSvn();
  94. }
  95. //build variations
  96. console.log('Building Variations');
  97. await executeCommand(`node ./variations/build-variations.mjs git-add-changes`)
  98. prompt = await inquirer.prompt([{
  99. type: 'confirm',
  100. message: 'Are you good with any staged theme variations changes? Make any manual adjustments now if necessary.',
  101. name: "continue",
  102. default: false
  103. }]);
  104. if(!prompt.continue){
  105. console.log(`Aborted Automated Deploy Process at variations building.` );
  106. return;
  107. }
  108. try {
  109. await executeCommand(`
  110. git commit -m "Building Variations"
  111. `);
  112. } catch (err) {
  113. // Most likely the error is that there are no variation changes to commit.
  114. // Just swallowing that error for now
  115. }
  116. let hash = await getLastDeployedHash();
  117. let diffUrl;
  118. let thingsWentBump = await versionBumpThemes();
  119. if( thingsWentBump ){
  120. prompt = await inquirer.prompt([{
  121. type: 'confirm',
  122. message: 'Are you good with the version bump changes? Make any manual adjustments now if necessary.',
  123. name: "continue",
  124. default: false
  125. }]);
  126. if(!prompt.continue){
  127. console.log(`Aborted Automated Deploy Process at version bump changes.` );
  128. return;
  129. }
  130. }
  131. let changedThemes = await getChangedThemes(hash);
  132. await pushChangesToSandbox();
  133. //push changes (from version bump)
  134. if( thingsWentBump ){
  135. prompt = await inquirer.prompt([{
  136. type: 'confirm',
  137. message: 'Are you ready to push this version bump change to the source repository (Github)?',
  138. name: "continue",
  139. default: false
  140. }]);
  141. if(!prompt.continue){
  142. console.log(`Aborted Automated Deploy Process at version bump push change.` );
  143. return;
  144. }
  145. await executeCommand(`
  146. git commit -m "Version Bump";
  147. git push
  148. `, true);
  149. }
  150. await updateLastDeployedHash();
  151. if (repoType === 'git' ) {
  152. diffUrl = await createGitPhabricatorDiff(hash);
  153. }
  154. else {
  155. diffUrl = await createSvnPhabricatorDiff(hash);
  156. }
  157. let diffId = diffUrl.split('a8c.com/')[1];
  158. await tagDeployment({
  159. hash: hash,
  160. diffId: diffId
  161. });
  162. console.log(`\n\nPhase One Complete\n\nYour sandbox has been updated and the diff is available for review.\nPlease give your sandbox a smoke test to determine that the changes work as expected.\nThe following themes have had changes: \n\n${changedThemes.join(' ')}\n\n\n`);
  163. prompt = await inquirer.prompt([{
  164. type: 'confirm',
  165. message: 'Are you ready to land these changes?',
  166. name: "continue",
  167. default: false
  168. }]);
  169. if(!prompt.continue){
  170. console.log(`Aborted Automated Deploy Process Landing Phase\n\nYou will have to land these changes manually. The ID of the diff to land: ${diffId}` );
  171. return;
  172. }
  173. if (repoType === 'git' ) {
  174. await landChangesGit(diffId);
  175. }
  176. else {
  177. await landChangesSvn(diffId);
  178. }
  179. await deployThemes(changedThemes);
  180. await buildComZips(changedThemes);
  181. console.log(`The following themes have changed:\n${changedThemes.join('\n')}`)
  182. console.log('\n\nAll Done!!\n\n');
  183. }
  184. catch (err) {
  185. console.log("ERROR with deply script: ", err);
  186. }
  187. }
  188. /*
  189. Build .zip file for .com
  190. */
  191. async function buildComZip(themeSlug) {
  192. console.log( `Building ${themeSlug} .zip` );
  193. let styleCss = fs.readFileSync(`${themeSlug}/style.css`, 'utf8');
  194. // Gets the theme version (Version:) and minimum WP version (Tested up to:) from the theme's style.css
  195. let themeVersion = getThemeMetadata(styleCss, 'Version');
  196. let wpVersionCompat = getThemeMetadata(styleCss, 'Requires at least');
  197. if (themeVersion && wpVersionCompat) {
  198. await executeOnSandbox(`php ${sandboxRootFolder}bin/themes/theme-downloads/build-theme-zip.php --stylesheet=pub/${themeSlug} --themeversion=${themeVersion} --wpversioncompat=${wpVersionCompat}`, true);
  199. }
  200. else {
  201. console.log('Unable to build theme .zip.');
  202. if (!themeVersion) {
  203. console.log('Could not find theme version (Version:) in the theme style.css.');
  204. }
  205. if (!wpVersionCompat) {
  206. console.log('Could not find WP compat version (Tested up to:) in the theme style.css.');
  207. }
  208. console.log('Please build the .zip file for the theme manually.', themeSlug);
  209. open('https://mc.a8c.com/themes/downloads/');
  210. }
  211. }
  212. async function buildComZips(themes) {
  213. for ( let theme of themes ) {
  214. await buildComZip(theme);
  215. }
  216. }
  217. /*
  218. Check to ensure that:
  219. * The current branch is /trunk
  220. * That trunk is up-to-date with origin/trunk
  221. */
  222. async function checkForDeployability(){
  223. let branchName = await executeCommand('git symbolic-ref --short HEAD');
  224. if(branchName !== 'trunk' ) {
  225. return 'Only the /trunk branch can be deployed.';
  226. }
  227. await executeCommand('git remote update', true);
  228. let localMasterHash = await executeCommand('git rev-parse trunk')
  229. let remoteMasterHash = await executeCommand('git rev-parse origin/trunk')
  230. if(localMasterHash !== remoteMasterHash) {
  231. return 'Local /trunk is out-of-date. Pull changes to continue.'
  232. }
  233. return null;
  234. }
  235. /*
  236. Land the changes from the given diff ID. This is the "production merge".
  237. This is the git version of that action.
  238. */
  239. async function landChangesGit(diffId){
  240. return await executeOnSandbox(`cd ${sandboxPublicThemesFolder};arc patch ${diffId};arc land;exit;`, true, true);
  241. }
  242. /*
  243. Land the changes from the given diff ID. This is the "production merge".
  244. This is the svn version of that action.
  245. */
  246. async function landChangesSvn(diffId){
  247. return await executeOnSandbox(`
  248. cd ${sandboxPublicThemesFolder};
  249. svn ci -m ${diffId}
  250. `, true );
  251. }
  252. async function getChangedThemes(hash) {
  253. console.log('Determining all changed themes');
  254. let themes = await getActionableThemes();
  255. let changedThemes = [];
  256. for (let theme of themes) {
  257. let hasChanges = await checkThemeForChanges(theme, hash);
  258. if(hasChanges){
  259. changedThemes.push(theme);
  260. }
  261. }
  262. return changedThemes;
  263. }
  264. /*
  265. Deploy a collection of themes.
  266. Part of the push-button-deploy process.
  267. Can also be triggered to deploy a single theme with the command:
  268. node ./theme-utils.mjs deploy-theme THEMENAME
  269. */
  270. async function deployThemes( themes ) {
  271. let response;
  272. for ( let theme of themes ) {
  273. console.log( `Deploying ${theme}` );
  274. let deploySuccess = false;
  275. let attempt = 0;
  276. while ( ! deploySuccess) {
  277. attempt++;
  278. console.log(`\nattempt #${attempt}\n\n`);
  279. response = await executeOnSandbox( `deploy pub ${theme};exit;`, true, true );
  280. deploySuccess = response.includes( 'successfully deployed to' );
  281. if( ! deploySuccess ) {
  282. console.log( 'Deploy was not successful. Trying again in 10 seconds...' );
  283. await new Promise(resolve => setTimeout(resolve, 10000));
  284. }
  285. else {
  286. console.log( "Deploy successful." );
  287. }
  288. }
  289. }
  290. }
  291. /*
  292. Provide the hash of the last managed deployment.
  293. This hash is used to determine all the changes that have happened between that point and the current point.
  294. */
  295. async function getLastDeployedHash() {
  296. let result = await executeOnSandbox(`
  297. cat ${sandboxPublicThemesFolder}/.pub-git-hash
  298. `);
  299. return result;
  300. }
  301. /*
  302. Update the 'last deployed hash' on the server with the current hash.
  303. */
  304. async function updateLastDeployedHash() {
  305. let hash = await executeCommand(`git rev-parse HEAD`);
  306. await executeOnSandbox(`
  307. echo '${hash}' > ${sandboxPublicThemesFolder}/.pub-git-hash
  308. `);
  309. }
  310. /*
  311. Version bump (increment version patch) any theme project that has had changes since the last deployment.
  312. If a theme's version has already been changed since that last deployment then do not version bump it.
  313. If any theme projects have had a version bump also version bump the parent project.
  314. Commit the change.
  315. */
  316. async function versionBumpThemes() {
  317. console.log("Version Bumping");
  318. let themes = await getActionableThemes();
  319. let hash = await getLastDeployedHash();
  320. let changesWereMade = false;
  321. let versionBumpCount = 0;
  322. for (let theme of themes) {
  323. let hasChanges = await checkThemeForChanges(theme, hash);
  324. if( ! hasChanges){
  325. // console.log(`${theme} has no changes`);
  326. continue;
  327. }
  328. versionBumpCount++;
  329. let hasVersionBump = await checkThemeForVersionBump(theme, hash);
  330. if( hasVersionBump ){
  331. continue;
  332. }
  333. await versionBumpTheme(theme, true);
  334. changesWereMade = true;
  335. }
  336. //version bump the root project if there were changes to any of the themes
  337. let rootHasVersionBump = await checkProjectForVersionBump(hash);
  338. if ( versionBumpCount > 0 && ! rootHasVersionBump ) {
  339. await executeCommand(`npm version patch --no-git-tag-version && git add package.json package-lock.json`);
  340. changesWereMade = true;
  341. }
  342. return changesWereMade;
  343. }
  344. export function getThemeMetadata(styleCss, attribute) {
  345. if ( !styleCss || !attribute ) {
  346. return null;
  347. }
  348. switch ( attribute ) {
  349. case 'Version':
  350. return styleCss
  351. .match(/(?<=Version:\s*).*?(?=\s*\r?\n|\rg)/gs)[0]
  352. .trim()
  353. .replace('-wpcom', '');
  354. case 'Requires at least':
  355. return styleCss
  356. .match(/(?<=Requires at least:\s*).*?(?=\s*\r?\n|\rg)/gs);
  357. }
  358. }
  359. /*
  360. Version Bump a Theme.
  361. Used by versionBumpThemes to do the work of version bumping.
  362. First increment the patch version in style.css
  363. Then update any of these files with the new version: [package.json, style.scss, style-child-theme.scss]
  364. */
  365. async function versionBumpTheme(theme, addChanges){
  366. console.log(`${theme} needs a version bump`);
  367. await executeCommand(`perl -pi -e 's/Version: ((\\d+\\.)*)(\\d+)(.*)$/"Version: ".$1.($3+1).$4/ge' ${theme}/style.css`, true);
  368. await executeCommand(`git add ${theme}/style.css`);
  369. let styleCss = fs.readFileSync(`${theme}/style.css`, 'utf8');
  370. let currentVersion = getThemeMetadata(styleCss, 'Version');
  371. let filesToUpdate = await executeCommand(`find ${theme} -name package.json -o -name style.scss -o -name style-child-theme.scss -maxdepth 2`);
  372. filesToUpdate = filesToUpdate.split('\n').filter(item => item != '');
  373. for ( let file of filesToUpdate ) {
  374. await executeCommand(`perl -pi -e 's/Version: (.*)$/"Version: '${currentVersion}'"/ge' ${file}`);
  375. await executeCommand(`perl -pi -e 's/\\"version\\": (.*)$/"\\"version\\": \\"'${currentVersion}'\\","/ge' ${file}`);
  376. if (addChanges){
  377. await executeCommand(`git add ${file}`);
  378. }
  379. }
  380. }
  381. /*
  382. Determine if a theme has had a version bump since a given hash.
  383. Used by versionBumpThemes
  384. Compares the value of 'version' in style.css between the hash and current value
  385. */
  386. async function checkThemeForVersionBump(theme, hash){
  387. return executeCommand(`
  388. git show ${hash}:${theme}/style.css 2>/dev/null
  389. `)
  390. .catch( ( error ) => {
  391. //This is a new theme, no need to bump versions so we'll just say we've already done it
  392. return true;
  393. } )
  394. .then( ( previousStyleString ) => {
  395. if( previousStyleString === true) {
  396. return previousStyleString;
  397. }
  398. let previousVersion = getThemeMetadata(previousStyleString, 'Version');
  399. let styleCss = fs.readFileSync(`${theme}/style.css`, 'utf8');
  400. let currentVersion = getThemeMetadata(styleCss, 'Version');
  401. return previousVersion != currentVersion;
  402. });
  403. }
  404. /*
  405. Determine if the project has had a version bump since a given hash.
  406. Used by versionBumpThemes
  407. Compares the value of 'version' in package.json between the hash and current value
  408. */
  409. async function checkProjectForVersionBump(hash){
  410. let previousPackageString = await executeCommand(`
  411. git show ${hash}:./package.json 2>/dev/null
  412. `);
  413. let previousPackage = JSON.parse(previousPackageString);
  414. let currentPackage = JSON.parse(fs.readFileSync(`./package.json`))
  415. return previousPackage.version != currentPackage.version;
  416. }
  417. /*
  418. Determine if a theme has had changes since a given hash.
  419. Used by versionBumpThemes
  420. */
  421. async function checkThemeForChanges(theme, hash){
  422. let uncomittedChanges = await executeCommand(`git diff-index --name-only HEAD -- ${theme}`);
  423. let comittedChanges = await executeCommand(`git diff --name-only ${hash} HEAD -- ${theme}`);
  424. return uncomittedChanges != '' || comittedChanges != '';
  425. }
  426. /*
  427. Provide a list of 'actionable' themes (those themes that have style.css files)
  428. */
  429. async function getActionableThemes() {
  430. let result = await executeCommand(`for d in */; do
  431. if test -f "./$d/style.css"; then
  432. echo $d;
  433. fi
  434. done`);
  435. return result
  436. .split('\n')
  437. .map(item=>item.replace('/', ''));
  438. }
  439. /*
  440. Clean the theme sandbox.
  441. Assumes sandbox is in 'git' mode
  442. checkout origin/develop and ensure it's up-to-date.
  443. Remove any other changes.
  444. */
  445. async function cleanSandboxGit() {
  446. console.log('Cleaning the Themes Sandbox');
  447. await executeOnSandbox(`
  448. cd ${sandboxPublicThemesFolder};
  449. git reset --hard HEAD;
  450. git clean -fd;
  451. git checkout develop;
  452. git pull;
  453. echo;
  454. git status
  455. `, true);
  456. console.log('All done cleaning.');
  457. }
  458. /*
  459. Clean the premium theme sandbox.
  460. Assumes sandbox is in 'git' mode
  461. checkout origin/develop and ensure it's up-to-date.
  462. Remove any other changes.
  463. */
  464. async function cleanPremiumSandboxGit() {
  465. console.log('Cleaning the Themes Sandbox');
  466. await executeOnSandbox(`
  467. cd ${sandboxPremiumThemesFolder};
  468. git reset --hard HEAD;
  469. git clean -fd;
  470. git checkout develop;
  471. git pull;
  472. echo;
  473. git status
  474. `, true);
  475. console.log('All done cleaning.');
  476. }
  477. /*
  478. Clean the entire sandbox.
  479. Assumes sandbox is in 'git' mode
  480. checkout origin/develop and ensure it's up-to-date.
  481. Remove any other changes.
  482. */
  483. async function cleanAllSandboxGit() {
  484. console.log('Cleaning the Entire Sandbox');
  485. let response = await executeOnSandbox(`
  486. cd ${sandboxRootFolder};
  487. git reset --hard HEAD;
  488. git clean -fd;
  489. git checkout develop;
  490. git pull;
  491. echo;
  492. git status
  493. `, true);
  494. console.log('All done cleaning.');
  495. }
  496. /*
  497. Clean the theme sandbox.
  498. Assumes sandbox is in 'svn' mode
  499. ensure trunk is up-to-date
  500. Remove any other changes
  501. */
  502. async function cleanSandboxSvn() {
  503. console.log('Cleaning the theme sandbox');
  504. await executeOnSandbox(`
  505. cd ${sandboxPublicThemesFolder};
  506. svn revert -R .;
  507. svn cleanup --remove-unversioned;
  508. svn up;
  509. `, true);
  510. console.log('All done cleaning.');
  511. }
  512. /*
  513. Clean the premium theme sandbox.
  514. Assumes sandbox is in 'svn' mode
  515. ensure trunk is up-to-date
  516. Remove any other changes
  517. */
  518. async function cleanPremiumSandboxSvn() {
  519. console.log('Cleaning the premium theme sandbox');
  520. await executeOnSandbox(`
  521. cd ${sandboxPremiumThemesFolder};
  522. svn revert -R .;
  523. svn cleanup --remove-unversioned;
  524. svn up;
  525. `, true);
  526. console.log('All done cleaning.');
  527. }
  528. /*
  529. Clean the entire sandbox.
  530. Assumes sandbox is in 'svn' mode
  531. ensure trunk is up-to-date
  532. Remove any other changes
  533. */
  534. async function cleanAllSandboxSvn() {
  535. console.log('Cleaning the entire sandbox');
  536. await executeOnSandbox(`
  537. cd ${sandboxRootFolder};
  538. svn revert -R .;
  539. svn cleanup --remove-unversioned;
  540. svn up .;
  541. `, true);
  542. console.log('All done cleaning.');
  543. }
  544. /*
  545. Push exactly what is here (all files) up to the sandbox (with the exclusion of files noted in .sandbox-ignore)
  546. */
  547. function pushToSandbox() {
  548. executeCommand(`
  549. rsync -av --no-p --no-times --exclude-from='.sandbox-ignore' ./ wpcom-sandbox:${sandboxPublicThemesFolder}/
  550. `);
  551. }
  552. /*
  553. Push exactly what is here (all files) up to the sandbox (with the exclusion of files noted in .sandbox-ignore)
  554. This pushes only the folders noted as "premiumThemes" into the premium themes directory.
  555. This is the only part of the deploy process that is automated; the rest must be done manually including:
  556. * Creating a Phabricator Diff
  557. * Landing (comitting) the change
  558. * Deploying the theme
  559. * Triggering the .zip builds
  560. */
  561. async function pushPremiumToSandbox() {
  562. //TODO: It would be nice to determine this list programatically
  563. const filesToModify = [
  564. 'style.css',
  565. 'block-templates/404.html',
  566. 'block-template-parts/header.html',
  567. 'block-template-parts/footer.html'
  568. ];
  569. // Change 'blockbase' to 'blockbase-premium' in the files noted
  570. for ( let theme of premiumThemes ) {
  571. for ( let file of filesToModify ) {
  572. await executeCommand(`perl -pi -e 's/blockbase/blockbase-premium/' ${theme}/${file}`, true);
  573. }
  574. }
  575. // Push the changes in the premium themes to the sandbox
  576. await executeCommand(`
  577. rsync -avR --no-p --no-times --delete -m --exclude-from='.sandbox-ignore' --exclude='sass' ./${premiumThemes.join(' ./')} wpcom-sandbox:${sandboxPremiumThemesFolder}/
  578. `, true);
  579. // revert the local blockbase-premium changes
  580. for ( let theme of premiumThemes ) {
  581. for ( let file of filesToModify ) {
  582. await executeCommand(`
  583. git restore --source=HEAD --staged --worktree ./${theme}/${file}
  584. `);
  585. }
  586. }
  587. }
  588. /*
  589. Push only (and every) change since the point-of-diversion from /trunk
  590. Remove files from the sandbox that have been removed since the last deployed hash
  591. */
  592. async function pushChangesToSandbox() {
  593. console.log("Pushing Changes to Sandbox.");
  594. let hash = await getLastDeployedHash();
  595. let changedThemes = await getChangedThemes(hash);
  596. changedThemes = changedThemes.filter( item=> ! premiumThemes.includes( item ) );
  597. console.log(`Syncing ${changedThemes.length} themes`);
  598. for ( let theme of changedThemes ) {
  599. console.log( `Syncing ${theme}` );
  600. await executeCommand(`
  601. rsync -avR --no-p --no-times --delete -m --exclude-from='.sandbox-ignore' ./${theme}/ wpcom-sandbox:${sandboxPublicThemesFolder}/
  602. `, true);
  603. }
  604. }
  605. /*
  606. Build the Phabricator commit message.
  607. This message contains the logs from all of the commits since the given hash.
  608. Used by create*PhabricatorDiff
  609. */
  610. async function buildPhabricatorCommitMessageSince(hash){
  611. let projectVersion = await executeCommand(`node -p "require('./package.json').version"`);
  612. let logs = await executeCommand(`git log --reverse --pretty=format:%s ${hash}..HEAD`);
  613. // Remove any double quotes from commit messages
  614. logs.replace(/"/g, '');
  615. return `Deploy Themes ${projectVersion} to wpcom
  616. Summary:
  617. ${logs}
  618. Test Plan: Execute Smoke Test
  619. Reviewers:
  620. Subscribers:
  621. `;
  622. }
  623. /*
  624. Create a (git) Phabricator diff from a given hash.
  625. Open the phabricator diff in your browser.
  626. Provide the URL of the phabricator diff.
  627. */
  628. async function createGitPhabricatorDiff(hash) {
  629. console.log('creating Phabricator Diff');
  630. let commitMessage = await buildPhabricatorCommitMessageSince(hash);
  631. let result = await executeOnSandbox(`
  632. cd ${sandboxPublicThemesFolder};
  633. git branch -D deploy
  634. git checkout -b deploy
  635. git add --all
  636. git commit -m "${commitMessage}"
  637. arc diff --create --verbatim
  638. `, true);
  639. let phabricatorUrl = getPhabricatorUrlFromResponse(result);
  640. console.log('Diff Created at: ', phabricatorUrl);
  641. if(phabricatorUrl) {
  642. open(phabricatorUrl);
  643. }
  644. return phabricatorUrl;
  645. }
  646. /*
  647. Create a (svn) Phabricator diff from a given hash.
  648. Open the phabricator diff in your browser.
  649. Provide the URL of the phabricator diff.
  650. */
  651. async function createSvnPhabricatorDiff(hash) {
  652. console.log('creating Phabricator Diff');
  653. const commitTempFileLocation = '/tmp/theme-deploy-comment.txt';
  654. const commitMessage = await buildPhabricatorCommitMessageSince(hash);
  655. console.log(commitMessage);
  656. const result = await executeOnSandbox(`
  657. cd ${sandboxPublicThemesFolder};
  658. echo "${commitMessage}" > ${commitTempFileLocation};
  659. svn add --force * --auto-props --parents --depth infinity -q;
  660. svn status | grep "^\!" | sed 's/^\! *//g' | xargs svn rm;
  661. arc diff --create --message-file ${commitTempFileLocation}
  662. `, true);
  663. const phabricatorUrl = getPhabricatorUrlFromResponse(result);
  664. console.log('Diff Created at: ', phabricatorUrl);
  665. if(phabricatorUrl) {
  666. open(phabricatorUrl);
  667. }
  668. return phabricatorUrl;
  669. }
  670. /*
  671. Utility to pull the Phabricator URL from the diff creation command.
  672. Used by createGitPhabricatorDiff
  673. */
  674. function getPhabricatorUrlFromResponse(response){
  675. return response
  676. ?.split('\n')
  677. ?.find( item => {
  678. return item.includes('Revision URI: ');
  679. })
  680. ?.split("Revision URI: ")[1];
  681. }
  682. /*
  683. Create a git tag at the current hash.
  684. In the description include the commit logs since the given hash.
  685. Include the (cleansed) Phabricator link.
  686. */
  687. async function tagDeployment(options={}) {
  688. console.log('tagging deployment');
  689. let hash = options.hash || await getLastDeployedHash();
  690. let workInTheOpenPhabricatorUrl = '';
  691. if (options.diffId) {
  692. workInTheOpenPhabricatorUrl = `Phabricator: ${options.diffId}-code`;
  693. }
  694. let projectVersion = await executeCommand(`node -p "require('./package.json').version"`);
  695. let logs = await executeCommand(`git log --reverse --pretty=format:%s ${hash}..HEAD`);
  696. // Remove any double quotes from commit messages
  697. logs.replace(/"/g, '');
  698. let tag = `v${projectVersion}`;
  699. let message = `Deploy Themes ${tag} to wpcom. \n\n${logs} \n\n${workInTheOpenPhabricatorUrl}`;
  700. await executeCommand(`
  701. git tag -a ${tag} -m "${message}"
  702. git push origin ${tag}
  703. `, true);
  704. }
  705. /*
  706. Execute a command on the sandbox.
  707. Expects the following to be configured in your ~/.ssh/config file:
  708. Host wpcom-sandbox
  709. User wpdev
  710. HostName SANDBOXURL.wordpress.com
  711. ForwardAgent yes
  712. */
  713. function executeOnSandbox(command, logResponse, enablePsudoterminal){
  714. if(enablePsudoterminal){
  715. return executeCommand(`ssh -tt -A ${remoteSSH} << EOF
  716. ${command}
  717. EOF`, logResponse);
  718. }
  719. return executeCommand(`ssh -TA ${remoteSSH} << EOF
  720. ${command}
  721. EOF`, logResponse);
  722. }
  723. /*
  724. Execute a command locally.
  725. */
  726. export async function executeCommand(command, logResponse) {
  727. return new Promise((resolove, reject) => {
  728. let child;
  729. let response = '';
  730. let errResponse = '';
  731. if (isWin) {
  732. child = spawn('cmd.exe', ['/s', '/c', '"' + command + '"'], {
  733. windowsVerbatimArguments: true,
  734. stdio: [process.stdin, 'pipe', 'pipe'],
  735. })
  736. } else {
  737. child = spawn(process.env.SHELL, ['-c', command]);
  738. }
  739. child.stdout.on('data', (data) => {
  740. response += data;
  741. if(logResponse){
  742. console.log(data.toString());
  743. }
  744. });
  745. child.stderr.on('data', (data) => {
  746. errResponse += data;
  747. if(logResponse){
  748. console.log(data.toString());
  749. }
  750. });
  751. child.on('exit', (code) => {
  752. if (code !== 0) {
  753. reject(errResponse.trim());
  754. }
  755. resolove(response.trim());
  756. });
  757. });
  758. }