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
This commit is contained in:
Joey Perrott 2020-12-11 12:34:44 -08:00 committed by Alex Rickabaugh
parent dcb3d176a6
commit c44baa4c5c
5 changed files with 111 additions and 18 deletions

View File

@ -6540,13 +6540,13 @@ const ReleaseSetDistTagCommand = {
* Note: git operations, especially git status, take a long time inside mounted docker volumes * 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). * 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_BRANCH ${getCurrentBranch()}`);
console.info(`BUILD_SCM_COMMIT_SHA ${getCurrentSha()}`); console.info(`BUILD_SCM_COMMIT_SHA ${getCurrentSha()}`);
console.info(`BUILD_SCM_HASH ${getCurrentSha()}`); console.info(`BUILD_SCM_HASH ${getCurrentSha()}`);
console.info(`BUILD_SCM_LOCAL_CHANGES ${hasLocalChanges()}`); console.info(`BUILD_SCM_LOCAL_CHANGES ${hasLocalChanges()}`);
console.info(`BUILD_SCM_USER ${getCurrentGitUser()}`); console.info(`BUILD_SCM_USER ${getCurrentGitUser()}`);
console.info(`BUILD_SCM_VERSION ${getSCMVersion()}`); console.info(`BUILD_SCM_VERSION ${getSCMVersion(mode)}`);
process.exit(0); process.exit(0);
} }
/** Run the exec command and return the stdout as a trimmed string. */ /** Run the exec command and return the stdout as a trimmed string. */
@ -6557,10 +6557,23 @@ function exec$1(cmd) {
function hasLocalChanges() { function hasLocalChanges() {
return !!exec$1(`git status --untracked-files=no --porcelain`); return !!exec$1(`git status --untracked-files=no --porcelain`);
} }
/** Get the version based on the most recent semver tag. */ /**
function getSCMVersion() { * 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`); 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 `${version.replace(/-([0-9]+)-g/, '+$1.sha-')}${(hasLocalChanges() ? '.with-local-changes' : '')}`;
}
return '0.0.0';
} }
/** Get the current SHA of HEAD. */ /** Get the current SHA of HEAD. */
function getCurrentSha() { function getCurrentSha() {
@ -6577,6 +6590,33 @@ function getCurrentGitUser() {
return `${userName} <${userEmail}>`; 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. */ /** Build the parser for the release commands. */
function buildReleaseParser(localYargs) { function buildReleaseParser(localYargs) {
return localYargs.help() return localYargs.help()
@ -6585,7 +6625,7 @@ function buildReleaseParser(localYargs) {
.command(ReleasePublishCommandModule) .command(ReleasePublishCommandModule)
.command(ReleaseBuildCommandModule) .command(ReleaseBuildCommandModule)
.command(ReleaseSetDistTagCommand) .command(ReleaseSetDistTagCommand)
.command('build-env-stamp', 'Build the environment stamping information', {}, () => buildEnvStamp()); .command(BuildEnvStampCommand);
} }
/** /**

View File

@ -12,6 +12,7 @@ ts_library(
"//dev-infra/release/publish", "//dev-infra/release/publish",
"//dev-infra/release/set-dist-tag", "//dev-infra/release/set-dist-tag",
"//dev-infra/utils", "//dev-infra/utils",
"@npm//@types/node",
"@npm//@types/yargs", "@npm//@types/yargs",
"@npm//yargs", "@npm//yargs",
], ],

View File

@ -10,7 +10,7 @@ import * as yargs from 'yargs';
import {ReleaseBuildCommandModule} from './build/cli'; import {ReleaseBuildCommandModule} from './build/cli';
import {ReleasePublishCommandModule} from './publish/cli'; import {ReleasePublishCommandModule} from './publish/cli';
import {ReleaseSetDistTagCommand} from './set-dist-tag/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. */ /** Build the parser for the release commands. */
export function buildReleaseParser(localYargs: yargs.Argv) { export function buildReleaseParser(localYargs: yargs.Argv) {
@ -20,7 +20,5 @@ export function buildReleaseParser(localYargs: yargs.Argv) {
.command(ReleasePublishCommandModule) .command(ReleasePublishCommandModule)
.command(ReleaseBuildCommandModule) .command(ReleaseBuildCommandModule)
.command(ReleaseSetDistTagCommand) .command(ReleaseSetDistTagCommand)
.command( .command(BuildEnvStampCommand);
'build-env-stamp', 'Build the environment stamping information', {},
() => buildEnvStamp());
} }

View File

@ -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<Options> {
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<Options>) {
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',
};

View File

@ -6,8 +6,13 @@
* found in the LICENSE file at https://angular.io/license * 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'; import {exec as _exec} from '../../utils/shelljs';
export type EnvStampMode = 'snapshot'|'release';
/** /**
* Log the environment variables expected by bazel for stamping. * 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 * 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). * 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_BRANCH ${getCurrentBranch()}`);
console.info(`BUILD_SCM_COMMIT_SHA ${getCurrentSha()}`); console.info(`BUILD_SCM_COMMIT_SHA ${getCurrentSha()}`);
console.info(`BUILD_SCM_HASH ${getCurrentSha()}`); console.info(`BUILD_SCM_HASH ${getCurrentSha()}`);
console.info(`BUILD_SCM_LOCAL_CHANGES ${hasLocalChanges()}`); console.info(`BUILD_SCM_LOCAL_CHANGES ${hasLocalChanges()}`);
console.info(`BUILD_SCM_USER ${getCurrentGitUser()}`); console.info(`BUILD_SCM_USER ${getCurrentGitUser()}`);
console.info(`BUILD_SCM_VERSION ${getSCMVersion()}`); console.info(`BUILD_SCM_VERSION ${getSCMVersion(mode)}`);
process.exit(0); process.exit(0);
} }
@ -38,11 +43,24 @@ function hasLocalChanges() {
return !!exec(`git status --untracked-files=no --porcelain`); return !!exec(`git status --untracked-files=no --porcelain`);
} }
/** Get the version based on the most recent semver tag. */ /**
function getSCMVersion() { * 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`); const version = exec(`git describe --match [0-9]*.[0-9]*.[0-9]* --abbrev=7 --tags HEAD`);
return `${version.replace(/-([0-9]+)-g/, '+$1.sha-')}${ return `${version.replace(/-([0-9]+)-g/, '+$1.sha-')}${
(hasLocalChanges() ? '.with-local-changes' : '')}`; (hasLocalChanges() ? '.with-local-changes' : '')}`;
}
return '0.0.0';
} }
/** Get the current SHA of HEAD. */ /** Get the current SHA of HEAD. */