From c44baa4c5ce319da73f448b99778aeb736d30b26 Mon Sep 17 00:00:00 2001 From: Joey Perrott Date: Fri, 11 Dec 2020 12:34:44 -0800 Subject: [PATCH] feat(dev-infra): support --mode flag for building environment stamp (#40095) When building the environment stamp, support two modes: release and snapshot The release mode will always stamp using the current version of in the root package.json and in snapshot mode will use a version stamp expressing a version based on the tag and the number of commits from the tag. PR Close #40095 --- dev-infra/ng-dev.js | 54 +++++++++++++++++++++---- dev-infra/release/BUILD.bazel | 1 + dev-infra/release/cli.ts | 6 +-- dev-infra/release/stamping/cli.ts | 36 +++++++++++++++++ dev-infra/release/stamping/env-stamp.ts | 32 +++++++++++---- 5 files changed, 111 insertions(+), 18 deletions(-) create mode 100644 dev-infra/release/stamping/cli.ts diff --git a/dev-infra/ng-dev.js b/dev-infra/ng-dev.js index e616a137ec..3c464e170a 100755 --- a/dev-infra/ng-dev.js +++ b/dev-infra/ng-dev.js @@ -6540,13 +6540,13 @@ const ReleaseSetDistTagCommand = { * Note: git operations, especially git status, take a long time inside mounted docker volumes * in Windows or OSX hosts (https://github.com/docker/for-win/issues/188). */ -function buildEnvStamp() { +function buildEnvStamp(mode) { console.info(`BUILD_SCM_BRANCH ${getCurrentBranch()}`); console.info(`BUILD_SCM_COMMIT_SHA ${getCurrentSha()}`); console.info(`BUILD_SCM_HASH ${getCurrentSha()}`); console.info(`BUILD_SCM_LOCAL_CHANGES ${hasLocalChanges()}`); console.info(`BUILD_SCM_USER ${getCurrentGitUser()}`); - console.info(`BUILD_SCM_VERSION ${getSCMVersion()}`); + console.info(`BUILD_SCM_VERSION ${getSCMVersion(mode)}`); process.exit(0); } /** Run the exec command and return the stdout as a trimmed string. */ @@ -6557,10 +6557,23 @@ function exec$1(cmd) { function hasLocalChanges() { return !!exec$1(`git status --untracked-files=no --porcelain`); } -/** Get the version based on the most recent semver tag. */ -function getSCMVersion() { - const version = exec$1(`git describe --match [0-9]*.[0-9]*.[0-9]* --abbrev=7 --tags HEAD`); - return `${version.replace(/-([0-9]+)-g/, '+$1.sha-')}${(hasLocalChanges() ? '.with-local-changes' : '')}`; +/** + * Get the version for generated packages. + * + * In snapshot mode, the version is based on the most recent semver tag. + * In release mode, the version is based on the base package.json version. + */ +function getSCMVersion(mode) { + if (mode === 'release') { + const packageJsonPath = path.join(getRepoBaseDir(), 'package.json'); + const { version } = require(packageJsonPath); + return version; + } + if (mode === 'snapshot') { + const version = exec$1(`git describe --match [0-9]*.[0-9]*.[0-9]* --abbrev=7 --tags HEAD`); + return `${version.replace(/-([0-9]+)-g/, '+$1.sha-')}${(hasLocalChanges() ? '.with-local-changes' : '')}`; + } + return '0.0.0'; } /** Get the current SHA of HEAD. */ function getCurrentSha() { @@ -6577,6 +6590,33 @@ function getCurrentGitUser() { return `${userName} <${userEmail}>`; } +/** + * @license + * Copyright Google LLC All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ +function builder$a(args) { + return args.option('mode', { + demandOption: true, + description: 'Whether the env-stamp should be built for a snapshot or release', + choices: ['snapshot', 'release'] + }); +} +function handler$a({ mode }) { + return tslib.__awaiter(this, void 0, void 0, function* () { + buildEnvStamp(mode); + }); +} +/** CLI command module for building the environment stamp. */ +const BuildEnvStampCommand = { + builder: builder$a, + handler: handler$a, + command: 'build-env-stamp', + describe: 'Build the environment stamping information', +}; + /** Build the parser for the release commands. */ function buildReleaseParser(localYargs) { return localYargs.help() @@ -6585,7 +6625,7 @@ function buildReleaseParser(localYargs) { .command(ReleasePublishCommandModule) .command(ReleaseBuildCommandModule) .command(ReleaseSetDistTagCommand) - .command('build-env-stamp', 'Build the environment stamping information', {}, () => buildEnvStamp()); + .command(BuildEnvStampCommand); } /** diff --git a/dev-infra/release/BUILD.bazel b/dev-infra/release/BUILD.bazel index 16e0a5146a..316d39a627 100644 --- a/dev-infra/release/BUILD.bazel +++ b/dev-infra/release/BUILD.bazel @@ -12,6 +12,7 @@ ts_library( "//dev-infra/release/publish", "//dev-infra/release/set-dist-tag", "//dev-infra/utils", + "@npm//@types/node", "@npm//@types/yargs", "@npm//yargs", ], diff --git a/dev-infra/release/cli.ts b/dev-infra/release/cli.ts index 6281e4ab88..4e8531cf3b 100644 --- a/dev-infra/release/cli.ts +++ b/dev-infra/release/cli.ts @@ -10,7 +10,7 @@ import * as yargs from 'yargs'; import {ReleaseBuildCommandModule} from './build/cli'; import {ReleasePublishCommandModule} from './publish/cli'; import {ReleaseSetDistTagCommand} from './set-dist-tag/cli'; -import {buildEnvStamp} from './stamping/env-stamp'; +import {BuildEnvStampCommand} from './stamping/cli'; /** Build the parser for the release commands. */ export function buildReleaseParser(localYargs: yargs.Argv) { @@ -20,7 +20,5 @@ export function buildReleaseParser(localYargs: yargs.Argv) { .command(ReleasePublishCommandModule) .command(ReleaseBuildCommandModule) .command(ReleaseSetDistTagCommand) - .command( - 'build-env-stamp', 'Build the environment stamping information', {}, - () => buildEnvStamp()); + .command(BuildEnvStampCommand); } diff --git a/dev-infra/release/stamping/cli.ts b/dev-infra/release/stamping/cli.ts new file mode 100644 index 0000000000..f6343df956 --- /dev/null +++ b/dev-infra/release/stamping/cli.ts @@ -0,0 +1,36 @@ +/** + * @license + * Copyright Google LLC All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ + +import {Arguments, Argv, CommandModule} from 'yargs'; + +import {buildEnvStamp, EnvStampMode} from './env-stamp'; + + +export interface Options { + mode: EnvStampMode; +} + +function builder(args: Argv): Argv { + return args.option('mode', { + demandOption: true, + description: 'Whether the env-stamp should be built for a snapshot or release', + choices: ['snapshot' as const, 'release' as const] + }); +} + +async function handler({mode}: Arguments) { + buildEnvStamp(mode); +} + +/** CLI command module for building the environment stamp. */ +export const BuildEnvStampCommand: CommandModule<{}, Options> = { + builder, + handler, + command: 'build-env-stamp', + describe: 'Build the environment stamping information', +}; diff --git a/dev-infra/release/stamping/env-stamp.ts b/dev-infra/release/stamping/env-stamp.ts index 184b63d7d6..5e6e993be0 100644 --- a/dev-infra/release/stamping/env-stamp.ts +++ b/dev-infra/release/stamping/env-stamp.ts @@ -6,8 +6,13 @@ * found in the LICENSE file at https://angular.io/license */ +import {join} from 'path'; + +import {getRepoBaseDir} from '../../utils/config'; import {exec as _exec} from '../../utils/shelljs'; +export type EnvStampMode = 'snapshot'|'release'; + /** * Log the environment variables expected by bazel for stamping. * @@ -18,13 +23,13 @@ import {exec as _exec} from '../../utils/shelljs'; * Note: git operations, especially git status, take a long time inside mounted docker volumes * in Windows or OSX hosts (https://github.com/docker/for-win/issues/188). */ -export function buildEnvStamp() { +export function buildEnvStamp(mode: EnvStampMode) { console.info(`BUILD_SCM_BRANCH ${getCurrentBranch()}`); console.info(`BUILD_SCM_COMMIT_SHA ${getCurrentSha()}`); console.info(`BUILD_SCM_HASH ${getCurrentSha()}`); console.info(`BUILD_SCM_LOCAL_CHANGES ${hasLocalChanges()}`); console.info(`BUILD_SCM_USER ${getCurrentGitUser()}`); - console.info(`BUILD_SCM_VERSION ${getSCMVersion()}`); + console.info(`BUILD_SCM_VERSION ${getSCMVersion(mode)}`); process.exit(0); } @@ -38,11 +43,24 @@ function hasLocalChanges() { return !!exec(`git status --untracked-files=no --porcelain`); } -/** Get the version based on the most recent semver tag. */ -function getSCMVersion() { - const version = exec(`git describe --match [0-9]*.[0-9]*.[0-9]* --abbrev=7 --tags HEAD`); - return `${version.replace(/-([0-9]+)-g/, '+$1.sha-')}${ - (hasLocalChanges() ? '.with-local-changes' : '')}`; +/** + * Get the version for generated packages. + * + * In snapshot mode, the version is based on the most recent semver tag. + * In release mode, the version is based on the base package.json version. + */ +function getSCMVersion(mode: EnvStampMode) { + if (mode === 'release') { + const packageJsonPath = join(getRepoBaseDir(), 'package.json'); + const {version} = require(packageJsonPath); + return version; + } + if (mode === 'snapshot') { + const version = exec(`git describe --match [0-9]*.[0-9]*.[0-9]* --abbrev=7 --tags HEAD`); + return `${version.replace(/-([0-9]+)-g/, '+$1.sha-')}${ + (hasLocalChanges() ? '.with-local-changes' : '')}`; + } + return '0.0.0'; } /** Get the current SHA of HEAD. */