Skip to content

Commit 950c571

Browse files
authored
Merge pull request #710 from WordPress/fix/703-improve-manual-workflow
Use dynamic version from `plugins.json` for manual workflow
2 parents 6a3bd22 + 52c6eb1 commit 950c571

3 files changed

Lines changed: 107 additions & 6 deletions

File tree

‎.github/workflows/deploy-standalone-plugins.yml‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ on:
88
slug:
99
type: string
1010
description: 'The slug of the plugin to deploy'
11-
version:
12-
type: string
13-
description: 'The version of the plugin to deploy'
14-
1511
jobs:
1612
release:
1713
name: Prepare Deployment
@@ -21,12 +17,30 @@ jobs:
2117
steps:
2218
- name: Checkout repository
2319
uses: actions/checkout@v3
20+
- name: Setup Node.js (.nvmrc)
21+
uses: actions/setup-node@v3
22+
with:
23+
node-version-file: '.nvmrc'
24+
cache: npm
25+
- name: Install npm dependencies
26+
run: npm ci
27+
- name: Get plugin version
28+
id: get-version
29+
if: ${{ github.event_name == 'workflow_dispatch' }}
30+
run: |
31+
echo "version=$(node ./bin/plugin/cli.js get-plugin-version --slug=${{ inputs.slug }})" >> $GITHUB_OUTPUT
2432
- name: Set matrix
2533
id: set-matrix
2634
run: |
2735
if ${{ github.event_name == 'workflow_dispatch' }}; then
28-
# Set the manual input values in JSON format for use in the matrix.
29-
echo "matrix={\"include\":[{\"slug\":\"${{ inputs.slug }}\",\"version\":\"${{ inputs.version }}\"}]}" >> $GITHUB_OUTPUT
36+
result=$(echo "${{ steps.get-version.outputs.version }}" | awk '/^(\*|[0-9]+(\.[0-9]+){0,2}(-[a-zA-Z0-9.]+)?)$/ {print "Matched"}')
37+
if [[ -n "$result" ]]; then
38+
# Set the manual input values in JSON format for use in the matrix.
39+
echo "matrix={\"include\":[{\"slug\":\"${{ inputs.slug }}\",\"version\":\"${{ steps.get-version.outputs.version }}\"}]}" >> $GITHUB_OUTPUT
40+
else
41+
echo "The ${{ inputs.slug }} module slug is missing in the file plugins.json."
42+
exit 1
43+
fi
3044
else
3145
# Load the JSON file and parse from "{name: {slug, version}, ...}" to "include: [{ name, slug, version }, ...]"
3246
# for use in the matrix.

‎bin/plugin/cli.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ const {
4646
handler: testPluginsHandler,
4747
options: testPluginsOptions,
4848
} = require( './commands/test-plugins' );
49+
const {
50+
handler: getPluginVersionHandler,
51+
options: getPluginVersionOptions,
52+
} = require( './commands/get-plugin-version' );
4953
const {
5054
handler: enabledModulesHandler,
5155
options: enabledModulesOptions,
@@ -92,6 +96,13 @@ withOptions( program.command( 'test-standalone-plugins' ), testPluginsOptions )
9296
)
9397
.action( catchException( testPluginsHandler ) );
9498

99+
withOptions( program.command( 'get-standalone-plugin-version' ), getPluginVersionOptions )
100+
.alias( 'get-plugin-version' )
101+
.description(
102+
'Get standalone plugin version'
103+
)
104+
.action( catchException( getPluginVersionHandler ) );
105+
95106
withOptions(
96107
program.command( 'default-enabled-modules' ),
97108
enabledModulesOptions
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* External dependencies
3+
*/
4+
const fs = require( 'fs' );
5+
const path = require( 'path' );
6+
7+
/**
8+
* Internal dependencies
9+
*/
10+
const { log } = require( '../lib/logger' );
11+
12+
exports.options = [
13+
{
14+
argname: '-s, --slug <slug>',
15+
description: 'Standalone plugin slug to get version from plugins.json',
16+
},
17+
];
18+
19+
/**
20+
* Command to get the plugin version based on the slug.
21+
*
22+
* @param {Object} opt Command options.
23+
*/
24+
exports.handler = async ( opt ) => {
25+
doRunGetPluginVersion( {
26+
pluginsJsonFile: 'plugins.json', // Path to plugins.json file.
27+
slug: opt.slug, // Plugin slug.
28+
} );
29+
};
30+
31+
/**
32+
* Returns the match plugin version from plugins.json file.
33+
*
34+
* @param {Object} settings Plugin settings.
35+
*/
36+
function doRunGetPluginVersion( settings ) {
37+
if ( settings.slug === undefined ) {
38+
throw Error( 'A slug must be provided via the --slug (-s) argument.' );
39+
}
40+
41+
const pluginsFile = path.join( '.', settings.pluginsJsonFile );
42+
43+
// Buffer contents of plugins JSON file.
44+
let pluginsFileContent = '';
45+
46+
try {
47+
pluginsFileContent = fs.readFileSync( pluginsFile, 'utf-8' );
48+
} catch ( e ) {
49+
throw Error( `Error reading file at "${ pluginsFile }": ${ e }` );
50+
}
51+
52+
// Validate that the plugins JSON file contains content before proceeding.
53+
if ( ! pluginsFileContent ) {
54+
throw Error( `Contents of file at "${ pluginsFile }" could not be read, or are empty.` );
55+
}
56+
57+
const plugins = JSON.parse( pluginsFileContent );
58+
59+
// Check for valid and not empty object resulting from plugins JSON file parse.
60+
if (
61+
'object' !== typeof plugins ||
62+
0 === Object.keys( plugins ).length
63+
) {
64+
throw Error( `File at "${ pluginsFile }" parsed, but detected empty/non valid JSON object.` );
65+
}
66+
67+
for ( const moduleDir in plugins ) {
68+
const pluginVersion = plugins[ moduleDir ]?.version;
69+
const pluginSlug = plugins[ moduleDir ]?.slug;
70+
if ( pluginVersion && pluginSlug && ( settings.slug === pluginSlug ) ) {
71+
return log( pluginVersion );
72+
}
73+
}
74+
75+
throw Error( `The "${ settings.slug }" module slug is missing in the file "${ pluginsFile }".` );
76+
}

0 commit comments

Comments
 (0)