Skip to content

Commit 390afa3

Browse files
devversionjelbourn
authored andcommitted
build: add version stamp to release output (#12418)
1 parent ee63518 commit 390afa3

File tree

4 files changed

+67
-3
lines changed

4 files changed

+67
-3
lines changed

tools/gulp/tasks/publish.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const argv = minimist(process.argv.slice(3));
2525
task('publish', sequenceTask(
2626
':publish:whoami',
2727
':publish:build-releases',
28+
'validate-release:check-remote-tag',
2829
'validate-release:check-bundles',
2930
':publish',
3031
':publish:logout',
@@ -52,8 +53,8 @@ task(':publish', async () => {
5253
if (!version.match(validVersionRegex)) {
5354
console.log(red(`Error: Cannot publish due to an invalid version name. Version "${version}" ` +
5455
`is not following our semver format.`));
55-
console.log(yellow(`A version should follow this format: d.d.d, d.d.d-beta.x, d.d.d-alpha.x, ` +
56-
`d.d.d-rc.x`));
56+
console.log(yellow(`A version should follow this format: X.X.X, X.X.X-beta.X, X.X.X-alpha.X, ` +
57+
`X.X.X-rc.X`));
5758
return;
5859
}
5960

tools/gulp/tasks/validate-release.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@ import {join} from 'path';
44
import {green, red} from 'chalk';
55
import {releasePackages} from './publish';
66
import {sync as glob} from 'glob';
7+
import {spawnSync} from 'child_process';
78
import {buildConfig, sequenceTask} from 'material2-build-tools';
89

10+
const {projectDir, projectVersion, outputDir} = buildConfig;
11+
12+
/** Git repository URL that has been read out from the project package.json file. */
13+
const repositoryGitUrl = require('../../../package.json').repository.url;
14+
915
/** Path to the directory where all releases are created. */
10-
const releasesDir = join(buildConfig.outputDir, 'releases');
16+
const releasesDir = join(outputDir, 'releases');
1117

1218
/** RegExp that matches Angular component inline styles that contain a sourcemap reference. */
1319
const inlineStylesSourcemapRegex = /styles: ?\[["'].*sourceMappingURL=.*["']/;
@@ -17,6 +23,18 @@ const externalReferencesRegex = /(templateUrl|styleUrls): *["'[]/;
1723

1824
task('validate-release', sequenceTask(':publish:build-releases', 'validate-release:check-bundles'));
1925

26+
task('validate-release:check-remote-tag', () => {
27+
// Since we cannot assume that every developer uses `origin` as the default name for the upstream
28+
// remote, we just pass in the Git URL that refers to angular/material2 repository on Github.
29+
const tagCommitSha = spawnSync('git', ['ls-remote', '--tags', repositoryGitUrl, projectVersion],
30+
{cwd: projectDir}).stdout.toString().trim();
31+
32+
if (!tagCommitSha) {
33+
throw Error(red(`Cannot publish v${projectVersion} because the release is not ` +
34+
`tagged on upstream yet. Please tag the release before publishing to NPM.`));
35+
}
36+
});
37+
2038
/** Task that checks the release bundles for any common mistakes before releasing to the public. */
2139
task('validate-release:check-bundles', () => {
2240
const releaseFailures = releasePackages

tools/package-tools/build-release.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {copyFiles} from './copy-files';
77
import {createEntryPointPackageJson} from './entry-point-package-json';
88
import {inlinePackageMetadataFiles} from './metadata-inlining';
99
import {createMetadataReexportFile} from './metadata-reexport';
10+
import {insertPackageJsonVersionStamp} from './package-version-stamp';
1011
import {createTypingsReexportFile} from './typings-reexport';
1112
import {replaceVersionPlaceholders} from './version-placeholders';
1213

@@ -48,6 +49,8 @@ export function composeRelease(buildPackage: BuildPackage) {
4849
copyFiles(sourceDir, 'package.json', releasePath);
4950

5051
replaceVersionPlaceholders(releasePath);
52+
insertPackageJsonVersionStamp(join(releasePath, 'package.json'));
53+
5154
createTypingsReexportFile(releasePath, './typings/index', name);
5255
createMetadataReexportFile(releasePath, './typings/index', name, importAsName);
5356

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import {writeFileSync} from 'fs';
2+
import {spawnSync} from 'child_process';
3+
import {buildConfig} from './build-config';
4+
5+
/**
6+
* Inserts the a version stamp, which consists of the current commit SHA and the current
7+
* branch name, into the specified package.json file.
8+
*
9+
* This makes it easy to quickly verify the exact snapshot from which the release originated.
10+
*/
11+
export function insertPackageJsonVersionStamp(packageJsonPath: string) {
12+
const packageJson = require(packageJsonPath);
13+
14+
packageJson['releaseGitCommitSha'] = getCurrentCommitSha();
15+
packageJson['releaseGitBranch'] = getCurrentBranchName();
16+
packageJson['releaseGitUser'] = getCurrentGitUser();
17+
18+
writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
19+
}
20+
21+
/** Returns the commit SHA for the current git HEAD of the project. */
22+
function getCurrentCommitSha(): string {
23+
return spawnSync('git', ['rev-parse', 'HEAD'], {cwd: buildConfig.projectDir})
24+
.stdout.toString().trim();
25+
}
26+
27+
/** Returns the name of the currently checked out branch of the project. */
28+
function getCurrentBranchName(): string {
29+
return spawnSync('git', ['symbolic-ref', '--short', 'HEAD'], {cwd: buildConfig.projectDir})
30+
.stdout.toString().trim();
31+
}
32+
33+
/** Returns the name and email of the Git user that creates this release build. */
34+
function getCurrentGitUser() {
35+
const userName = spawnSync('git', ['config', 'user.name'], {cwd: buildConfig.projectDir})
36+
.stdout.toString().trim();
37+
38+
const userEmail = spawnSync('git', ['config', 'user.email'], {cwd: buildConfig.projectDir})
39+
.stdout.toString().trim();
40+
41+
return `${userName} <${userEmail}>`;
42+
}

0 commit comments

Comments
 (0)