Skip to content

Commit

Permalink
Fully version FaustWP during changeset versioning (wpengine#704)
Browse files Browse the repository at this point in the history
* fully version FaustWP during changeset versioning

* update the development docs

* use promisified file system module

* add missing param types
  • Loading branch information
apmatthews authored Dec 8, 2021
1 parent 0371b31 commit 2ddd4ff
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 84 deletions.
31 changes: 19 additions & 12 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,28 +187,35 @@ After releasing the packages or plugins, you'll need to update faustjs.org with

## Deployment

Developers with full GitHub repository access can create public releases:
Developers with full GitHub repository access can create public releases. We use [Changesets](https://github.com/atlassian/changesets) to automate the versioning and deployment process for all of our packages and plugins.

### Release the FaustWP plugin
### Versioning

1. Update the `Version` in the file header at `plugins/faustwp/faustwp.php`.
2. Update the 'stable tag' in `plugins/faustwp/readme.txt`.
3. Open a PR to merge your changes into the `canary` branch.
4. Once your PR is merged, create a new release on GitHub with a tag of `plugin/faustwp/v[version]`. This will kick off our GitHub Action to deploy the `faustwp` plugin to WordPress.org.
When you are ready to release, you should first create the new package and plugin versions.

### Release the @faustjs packages
1. Go to [pull requests](https://github.com/wpengine/faustjs/pulls), and view the "Version Packages" PR.
2. Review the PR:
- [ ] Changelog entries were created in all updated packages or plugins.
- [ ] Version numbers were appropriately bumped in the relevant package.json files.
- [ ] All `.changeset/*.md` files were removed.
- [ ] Version number updated in the main plugin file and readme.txt (Plugin versioning only)
- [ ] The plugin's readme.txt changelog has been updated with the latest 3 versions (Plugin versioning only)
3. Approve, then "Squash and merge" the "Version Packages" PR into `canary`.

We use [Changesets](https://github.com/atlassian/changesets) to automate our deployment process for the @faustjs packages.
### Publishing the @faustjs packages

1. When you are ready to release the @faustjs packages, go to [pull requests](https://github.com/wpengine/faustjs/pulls), and view the "Version Packages" PR.
2. Review the changes and make sure that the packages are versioned appropriately, and that the changelogs reflect the changes accurately.
3. When ready to release, merge the PR into `canary`. This will kick off the GitHub Action to publish to NPM.
The @faustjs packages are automatically published to NPM through a GitHub action once the "Version Packages" PR is merged.

Once deployed, the updated packages will be visible here:
### Publishing the FaustWP plugin

Once the "Version Packages" PR is merged, create a new release on GitHub with a tag of `plugin/faustwp/v[version]`. This will kick off our GitHub Action to deploy the `faustwp` plugin to WordPress.org.

Once deployed, the updated packages and plugin will be visible here:

- https://www.npmjs.com/package/@faustjs/core
- https://www.npmjs.com/package/@faustjs/react
- https://www.npmjs.com/package/@faustjs/next
- https://plugins.trac.wordpress.org/browser/faustwp/tags

### Update the docs

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"wp:destroy": "wp-env destroy",
"wpe-build": "cd internal/website && npm i && cd .. && npm run docs:build",
"changeset": "changeset",
"version": "changeset version && node scripts/buildPluginReadme.js",
"version": "changeset version && node scripts/versionPlugin.js",
"version:status": "changeset status",
"release": "npm run build && changeset publish"
},
Expand Down
71 changes: 0 additions & 71 deletions scripts/buildPluginReadme.js

This file was deleted.

167 changes: 167 additions & 0 deletions scripts/versionPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/**
* Versions the FaustWP plugin.
*/

const fs = require('fs/promises');
const path = require("path");

const readFile = (filename) => fs.readFile(filename, { encoding: "utf8" });
const writeFile = fs.writeFile;

/**
* Runs all WordPress plugin versioning operations for FaustWP
* including version bumps and readme.txt changelog updates.
*/
async function versionPlugin() {
const pluginPath = path.join(__dirname, '../plugins/faustwp');
const pluginFile = path.join(pluginPath, 'faustwp.php');
const readmeTxt = path.join(pluginPath, 'readme.txt');
const changelog = path.join(pluginPath, 'CHANGELOG.md');

const version = await getNewVersion(pluginPath);

if ( version ) {
bumpPluginHeader(pluginFile, version);
await bumpStableTag(readmeTxt, version);
generateReadmeChangelog(readmeTxt, changelog);
}
}

/**
* Updates the version number found in the header comment of a given
* WordPress plugin's main PHP file.
*
* @param {String} pluginFile Full path to a file containing a WordPress
* plugin header comment.
* @param {String} version The new version number.
*/
async function bumpPluginHeader(pluginFile, version) {
return bumpVersion(pluginFile, /^\s*\*\s*Version:\s*([0-9.]+)$/gm, version);
}

/**
* Updates the stable tag found in a given WordPress plugin's readme.txt file.
*
* @param {String} pluginFile Full path to a file containing a WordPress
* plugin header comment.
* @param {String} version The new version number.
*/
async function bumpStableTag(readmeTxt, version) {
return bumpVersion(readmeTxt, /^Stable tag:\s*([0-9.]+)$/gm, version);
}

/**
* Replaces the version number in the first line of a file matching the given
* regular expression.
*
* Note that this function depends on a properly formatted regular expression.
* The given regex should meet the following criteria:
*
* 1. Begins with ^ and ends with $ so that we can match an entire line.
* 2. Contains one and only one capturing group that matches only the version
* number portion of the line. For example, in the line " * Version: 1.0.0"
* capturing group 1 of the regex must resolve to "1.0.0".
*
* @param {String} file Full path to the file to update.
* @param {RegExp} regex A valid regular expression as noted above.
* @param {String} version The new version number.
*/
async function bumpVersion(file, regex, version) {
try {
let data = await readFile(file);
const matches = regex.exec(data);

if ( ! matches ) {
throw new Error(`Version string does not exist in ${file}`);
}

// Replace the version number in the captured line.
let versionString = matches[0].replace(matches[1], version);

// Replace the captured line with the new version string.
data = data.replace(matches[0], versionString);

return writeFile(file, data);
} catch (e) {
console.warn(e);
}
}

/**
* Get the current version number from a plugin's package.json file.
*
* @param {String} pluginPath Full path to the directory containing the plugin's
* package.json file.
* @returns The version number string found in the plugin's package.json.
*/
async function getNewVersion(pluginPath) {
const packageJsonFile = path.join(pluginPath, 'package.json');

try {
let packageJson = await readFile(packageJsonFile);

return JSON.parse(packageJson)?.version;
} catch (e) {
if (e instanceof SyntaxError) {
e.message = `${e.message} in ${packageJsonFile}.\n`;
}

console.warn(e);
}
}

/**
* Updates the FaustWP plugin's readme.txt changelog with the latest 3 releases
* found in the plugin's CHANGELOG.md file.
*
* @param {String} readmeTxtFile Full path to the plugin's readme.txt file.
* @param {String} changelog Full path to the plugin's CHANGELOG.md file.
*/
async function generateReadmeChangelog(readmeTxtFile, changelog) {
let output = "";

try {
let readmeTxt = await readFile(readmeTxtFile);
changelog = await readFile(changelog);

changelog = changelog.replace(
"# FaustWP",
"== Changelog =="
);

// split the contents by new line
const changelogLines = changelog.split(/\r?\n/);
const processedLines = [];
let versionCount = 0;

// print all lines in current version
changelogLines.every((line) => {
// Version numbers in CHANGELOG.md are h2
if (line.startsWith("## ")) {
if (versionCount == 3) {
return false;
}
// Format version number for WordPress
line = line.replace("## ", "= ") + " =";
versionCount++;
}

processedLines.push(line);

return true;
});

changelog = processedLines.join("\n");

const changelogStart = readmeTxt.indexOf('== Changelog ==');

output = readmeTxt.substring(0, changelogStart) + changelog;
output += "\n[View the full changelog](https://faustjs.org/docs/changelog/faustwp)";

return writeFile(readmeTxtFile, output);
} catch(e) {
console.warn(e);
}
}

versionPlugin();

0 comments on commit 2ddd4ff

Please sign in to comment.