theme-utils.mjs 24 KB

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