Skip to content

Commit

Permalink
CLI: Add version to Jetpack Release (#21863)
Browse files Browse the repository at this point in the history
Co-authored-by: Brad Jorsch <[email protected]>
  • Loading branch information
sdixon194 and anomiex authored Dec 6, 2021
1 parent fd78e83 commit c538436
Showing 1 changed file with 117 additions and 24 deletions.
141 changes: 117 additions & 24 deletions tools/cli/commands/release.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export function releaseDefine( yargs ) {
type: 'string',
} )
.positional( 'script', {
alias: 's',
describe: 'The release script to run',
type: 'string',
choices: [ 'changelog', 'readme', 'release-branch', 'amend', 'version' ],
Expand All @@ -45,6 +44,11 @@ export function releaseDefine( yargs ) {
alias: 'b',
describe: 'Is this a beta?',
type: 'boolean',
} )
.option( 'stable', {
alias: 's',
describe: 'Is this a stable release?',
type: 'boolean',
} );
},
async argv => {
Expand Down Expand Up @@ -76,13 +80,14 @@ export async function releaseCli( argv ) {
argv = await promptForScript( argv );
}

// Check if we're working with a beta version and only if generating changlog or release-branch.
// Check if we're working with a beta/alpha version when necessary.
if (
! argv.devRelease &&
typeof argv.beta === 'undefined' &&
( argv.script === 'changelog' || argv.script === 'release-branch' )
typeof argv.stable === 'undefined' &&
( argv.script !== 'readme' || argv.script !== 'amend' )
) {
argv = await promptBeta( argv );
argv = await promptDevBeta( argv );
}

// Get the info we need for the script.
Expand Down Expand Up @@ -146,7 +151,8 @@ export async function scriptRouter( argv ) {
jetpack release ${ argv.project } release-branch \n`.replace( /^\t+/gm, '' );
break;
case 'release-branch':
argv = await getReleaseVersion( argv );
argv.version = await getReleaseVersion( argv );
argv = await promptForVersion( argv );
argv.script = `tools/create-release-branch.sh`;
argv.scriptArgs = [ argv.project, argv.version ];
argv.next = `Finished! Next:
Expand All @@ -166,7 +172,18 @@ export async function scriptRouter( argv ) {
jetpack release ${ argv.project } readme \n`.replace( /^\t+/gm, '' );
break;
case 'version':
console.log( `${ argv.script } is not implemented yet!` );
argv.version = await getReleaseVersion( argv );
argv = await promptForVersion( argv );
argv.script = 'tools/project-version.sh';
argv.scriptArgs = [ '-u', argv.version, argv.project ];
argv.next = `Finished! Next, you will likely want to check the following project files to make sure versions were updated correctly:
- The main php file
- package.json
- composer.json (the autoloader-suffix filed)
- changelog.md and the changelog part of readme.txt \n`.replace( /^\t+/gm, '' );
break;
default:
console.log( 'Not a valid release command!' );
process.exit( 1 );
}
}
Expand Down Expand Up @@ -220,7 +237,7 @@ export async function parseProj( argv ) {
}

/**
* Prompts for and suggests a version number for the release branch.
* Get a potential version that we might need when creating a release branch or bumping versions.
*
* @param {object} argv - the arguments passed
* @returns {object} argv
Expand All @@ -230,33 +247,93 @@ export async function getReleaseVersion( argv ) {
.execSync( `tools/plugin-version.sh ${ argv.project }` )
.toString()
.trim();
potentialVersion = potentialVersion.split( '-' );
potentialVersion = potentialVersion[ 0 ].split( '.' ).splice( 0, 2 );
potentialVersion = potentialVersion.join( '.' );
potentialVersion = potentialVersion.split( '-' ); // e.g., split 10.4-a.8 into [10.4, a.8]
let stableVersion = potentialVersion[ 0 ];
let devReleaseVersion = potentialVersion[ 1 ];

if ( argv.stable || argv.s ) {
return stableVersion;
}

// Append '-beta' if necessary.
if ( argv.b || argv.beta ) {
potentialVersion += '-beta';
return `${ stableVersion }-beta`;
}
argv = await promptForVersion( argv, potentialVersion );

return argv;
// Handle alpha/dev-release version if necessary.
if ( argv.a || argv.devRelease ) {
// Check if dev-releases is specified in project's composer.json
const hasDevReleases = await readComposerJson( argv.project ).extra[ 'dev-releases' ];
if ( hasDevReleases ) {
if ( devReleaseVersion ) {
devReleaseVersion = await getVersionBump( devReleaseVersion, argv.project );
potentialVersion = `${ stableVersion }-${ devReleaseVersion }`;
} else {
stableVersion = await getVersionBump( stableVersion, argv.project );
potentialVersion = `${ stableVersion }-a.0`;
}
} else {
stableVersion = await getVersionBump( stableVersion, argv.project );
potentialVersion = `${ stableVersion }-alpha`;
}
return potentialVersion;
}
}

/**
* Bumps the correct number.
*
* @param {Array} version - the arguments passed
* @param {string} project - the project we're working with.
* @returns {Array} the bumped version.
*/
export async function getVersionBump( version, project ) {
version = version.split( '.' );

// If we we're bumping just a dev-release version, e.g. x.y-a.z
if ( version[ 0 ] === 'a' ) {
version[ 1 ] = parseInt( version[ 1 ] ) + 1;
return version.join( '.' );
}

const changeloggerConfig = await readComposerJson( project ).extra.changelogger;

// If WordPress versioning, i.e. x.(y+1) or (x+1).0
if ( changeloggerConfig && changeloggerConfig.versioning === 'wordpress' ) {
// If there was a point release, remove the last number before bumping.
if ( version[ 2 ] ) {
version.pop();
}

if ( version[ 1 ] === '9' ) {
// e.g, if the current version is 10.9 and we want 11.0
version[ 0 ] = parseInt( version[ 0 ] ) + 1;
version[ 1 ] = '0';
} else {
// e.g, if the current version is 10.8 and we want 10.9
version[ 1 ] = parseInt( version[ 1 ] ) + 1;
}
return version.join( '.' );
}

// Default semver, bumping a minor patch, i.e. x.y.(z+1)
version[ 2 ] = parseInt( version[ 2 ] ) + 1;
return version.join( '.' );
}

/**
* Prompts for what version we're releasing
*
* @param {object} argv - the arguments passed.
* @param {string} version - the version we think might be used.
* @returns {string} version
*/
export async function promptForVersion( argv, version ) {
export async function promptForVersion( argv ) {
const response = await inquirer.prompt( [
{
type: 'input',
name: 'version',
message: `What version are you releasing for ${ argv.project }?`,
default: version,
default: argv.version,
},
] );
argv.version = response.version;
Expand All @@ -269,17 +346,29 @@ export async function promptForVersion( argv, version ) {
* @param {object} argv - the arguments passed
* @returns {object} argv
*/
export async function promptBeta( argv ) {
export async function promptDevBeta( argv ) {
const response = await inquirer.prompt( [
{
type: 'confirm',
name: 'beta',
message: `Are you releasing a beta version of ${ argv.project }?`,
default: false,
type: 'list',
name: 'version_type',
message: `What kind of release is this?`,
choices: [ 'alpha (including Atomic)', 'beta', 'stable' ],
},
] );
argv.beta = response.beta;
argv.b = response.beta;

switch ( response.version_type ) {
case 'beta':
argv.b = true;
argv.beta = true;
break;
case 'alpha (including Atomic)':
argv.devRelease = true;
argv.a = true;
break;
default:
argv.stable = true;
argv.s = true;
}
return argv;
}

Expand All @@ -305,13 +394,17 @@ export async function promptForScript( argv ) {
value: 'readme',
},
{
name: `[Create Release Branch] - Create a release branch for ${ argv.project }`,
name: `[Create Release Branch] - Create a release branch for ${ argv.project }`,
value: 'release-branch',
},
{
name: `[Amend Changelog.md ] - Updates changelog.md with any files cherry picked to release branch prior to release.`,
value: 'amend',
},
{
name: `[Update Version ] - Update version number for ${ argv.project }.`,
value: 'version',
},
],
},
] );
Expand Down

0 comments on commit c538436

Please sign in to comment.