feat(dev-infra): include experimental version stamp in env stamps (#42934)

Include the determined experimental version for stamping in the env stamps
created for release and snapshot builds.

PR Close #42934
This commit is contained in:
Joey Perrott 2021-07-22 12:15:50 -07:00 committed by Dylan Hunn
parent e462743969
commit 99f833869d
4 changed files with 43 additions and 21 deletions

View File

@ -6201,6 +6201,7 @@ function semverInc(version, release, identifier) {
} }
/** Creates the equivalent experimental version for a provided SemVer. */ /** Creates the equivalent experimental version for a provided SemVer. */
function createExperimentalSemver(version) { function createExperimentalSemver(version) {
version = new semver.SemVer(version);
var experimentalVersion = new semver.SemVer(version.format()); var experimentalVersion = new semver.SemVer(version.format());
experimentalVersion.major = 0; experimentalVersion.major = 0;
experimentalVersion.minor = version.major * 100 + version.minor; experimentalVersion.minor = version.major * 100 + version.minor;
@ -7681,7 +7682,9 @@ function buildEnvStamp(mode) {
console.info(`BUILD_SCM_HASH ${getCurrentBranchOrRevision()}`); console.info(`BUILD_SCM_HASH ${getCurrentBranchOrRevision()}`);
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(mode)}`); const { version, experimentalVersion } = getSCMVersions(mode);
console.info(`BUILD_SCM_VERSION ${version}`);
console.info(`BUILD_SCM_EXPERIMENTAL_VERSION ${experimentalVersion}`);
process.exit(); process.exit();
} }
/** Whether the repo has local changes. */ /** Whether the repo has local changes. */
@ -7690,24 +7693,30 @@ function hasLocalChanges() {
return git.hasUncommittedChanges(); return git.hasUncommittedChanges();
} }
/** /**
* Get the version for generated packages. * Get the versions for generated packages.
* *
* In snapshot mode, the version is based on the most recent semver tag. * 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. * In release mode, the version is based on the base package.json version.
*/ */
function getSCMVersion(mode) { function getSCMVersions(mode) {
const git = GitClient.get(); const git = GitClient.get();
if (mode === 'release') { if (mode === 'release') {
const packageJsonPath = path.join(git.baseDir, 'package.json'); const packageJsonPath = path.join(git.baseDir, 'package.json');
const { version } = require(packageJsonPath); const { version } = new semver.SemVer(require(packageJsonPath).version);
return version; const { version: experimentalVersion } = createExperimentalSemver(new semver.SemVer(version));
return { version, experimentalVersion };
} }
if (mode === 'snapshot') { if (mode === 'snapshot') {
const version = git.run(['describe', '--match', '[0-9]*.[0-9]*.[0-9]*', '--abbrev=7', '--tags', 'HEAD']) const localChanges = hasLocalChanges() ? '.with-local-changes' : '';
.stdout.trim(); const { stdout: rawVersion } = git.run(['describe', '--match', '*[0-9]*.[0-9]*.[0-9]*', '--abbrev=7', '--tags', 'HEAD~100']);
return `${version.replace(/-([0-9]+)-g/, '+$1.sha-')}${(hasLocalChanges() ? '.with-local-changes' : '')}`; const { version } = new semver.SemVer(rawVersion);
const { version: experimentalVersion } = createExperimentalSemver(version);
return {
version: `${version.replace(/-([0-9]+)-g/, '+$1.sha-')}${localChanges}`,
experimentalVersion: `${experimentalVersion.replace(/-([0-9]+)-g/, '+$1.sha-')}${localChanges}`,
};
} }
return '0.0.0'; throw Error('No environment stamp mode was provided.');
} }
/** Get the current branch or revision of HEAD. */ /** Get the current branch or revision of HEAD. */
function getCurrentBranchOrRevision() { function getCurrentBranchOrRevision() {

View File

@ -14,7 +14,9 @@ ts_library(
"//dev-infra/release/set-dist-tag", "//dev-infra/release/set-dist-tag",
"//dev-infra/utils", "//dev-infra/utils",
"@npm//@types/node", "@npm//@types/node",
"@npm//@types/semver",
"@npm//@types/yargs", "@npm//@types/yargs",
"@npm//semver",
"@npm//yargs", "@npm//yargs",
], ],
) )

View File

@ -7,7 +7,9 @@
*/ */
import {join} from 'path'; import {join} from 'path';
import {SemVer} from 'semver';
import {GitClient} from '../../utils/git/git-client'; import {GitClient} from '../../utils/git/git-client';
import {createExperimentalSemver} from '../../utils/semver';
export type EnvStampMode = 'snapshot'|'release'; export type EnvStampMode = 'snapshot'|'release';
@ -27,7 +29,9 @@ export function buildEnvStamp(mode: EnvStampMode) {
console.info(`BUILD_SCM_HASH ${getCurrentBranchOrRevision()}`); console.info(`BUILD_SCM_HASH ${getCurrentBranchOrRevision()}`);
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(mode)}`); const {version, experimentalVersion} = getSCMVersions(mode);
console.info(`BUILD_SCM_VERSION ${version}`);
console.info(`BUILD_SCM_EXPERIMENTAL_VERSION ${experimentalVersion}`);
process.exit(); process.exit();
} }
@ -38,26 +42,32 @@ function hasLocalChanges() {
} }
/** /**
* Get the version for generated packages. * Get the versions for generated packages.
* *
* In snapshot mode, the version is based on the most recent semver tag. * 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. * In release mode, the version is based on the base package.json version.
*/ */
function getSCMVersion(mode: EnvStampMode) { function getSCMVersions(mode: EnvStampMode): {version: string, experimentalVersion: string} {
const git = GitClient.get(); const git = GitClient.get();
if (mode === 'release') { if (mode === 'release') {
const packageJsonPath = join(git.baseDir, 'package.json'); const packageJsonPath = join(git.baseDir, 'package.json');
const {version} = require(packageJsonPath); const {version} = new SemVer(require(packageJsonPath).version);
return version; const {version: experimentalVersion} = createExperimentalSemver(new SemVer(version));
return {version, experimentalVersion};
} }
if (mode === 'snapshot') { if (mode === 'snapshot') {
const version = const localChanges = hasLocalChanges() ? '.with-local-changes' : '';
git.run(['describe', '--match', '[0-9]*.[0-9]*.[0-9]*', '--abbrev=7', '--tags', 'HEAD']) const {stdout: rawVersion} = git.run(
.stdout.trim(); ['describe', '--match', '*[0-9]*.[0-9]*.[0-9]*', '--abbrev=7', '--tags', 'HEAD~100']);
return `${version.replace(/-([0-9]+)-g/, '+$1.sha-')}${ const {version} = new SemVer(rawVersion);
(hasLocalChanges() ? '.with-local-changes' : '')}`; const {version: experimentalVersion} = createExperimentalSemver(version);
return {
version: `${version.replace(/-([0-9]+)-g/, '+$1.sha-')}${localChanges}`,
experimentalVersion:
`${experimentalVersion.replace(/-([0-9]+)-g/, '+$1.sha-')}${localChanges}`,
};
} }
return '0.0.0'; throw Error('No environment stamp mode was provided.');
} }
/** Get the current branch or revision of HEAD. */ /** Get the current branch or revision of HEAD. */

View File

@ -19,7 +19,8 @@ export function semverInc(
} }
/** Creates the equivalent experimental version for a provided SemVer. */ /** Creates the equivalent experimental version for a provided SemVer. */
export function createExperimentalSemver(version: semver.SemVer): semver.SemVer { export function createExperimentalSemver(version: string|semver.SemVer): semver.SemVer {
version = new semver.SemVer(version);
const experimentalVersion = new semver.SemVer(version.format()); const experimentalVersion = new semver.SemVer(version.format());
experimentalVersion.major = 0; experimentalVersion.major = 0;
experimentalVersion.minor = version.major * 100 + version.minor; experimentalVersion.minor = version.major * 100 + version.minor;