Skip to content

build: add version stamp to release output #12418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions tools/gulp/tasks/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const argv = minimist(process.argv.slice(3));
task('publish', sequenceTask(
':publish:whoami',
':publish:build-releases',
'validate-release:check-remote-tag',
'validate-release:check-bundles',
':publish',
':publish:logout',
Expand Down Expand Up @@ -52,8 +53,8 @@ task(':publish', async () => {
if (!version.match(validVersionRegex)) {
console.log(red(`Error: Cannot publish due to an invalid version name. Version "${version}" ` +
`is not following our semver format.`));
console.log(yellow(`A version should follow this format: d.d.d, d.d.d-beta.x, d.d.d-alpha.x, ` +
`d.d.d-rc.x`));
console.log(yellow(`A version should follow this format: X.X.X, X.X.X-beta.X, X.X.X-alpha.X, ` +
`X.X.X-rc.X`));
return;
}

Expand Down
20 changes: 19 additions & 1 deletion tools/gulp/tasks/validate-release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@ import {join} from 'path';
import {green, red} from 'chalk';
import {releasePackages} from './publish';
import {sync as glob} from 'glob';
import {spawnSync} from 'child_process';
import {buildConfig, sequenceTask} from 'material2-build-tools';

const {projectDir, projectVersion, outputDir} = buildConfig;

/** Git repository URL that has been read out from the project package.json file. */
const repositoryGitUrl = require('../../../package.json').repository.url;

/** Path to the directory where all releases are created. */
const releasesDir = join(buildConfig.outputDir, 'releases');
const releasesDir = join(outputDir, 'releases');

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

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

task('validate-release:check-remote-tag', () => {
// Since we cannot assume that every developer uses `origin` as the default name for the upstream
// remote, we just pass in the Git URL that refers to angular/material2 repository on Github.
const tagCommitSha = spawnSync('git', ['ls-remote', '--tags', repositoryGitUrl, projectVersion],
{cwd: projectDir}).stdout.toString().trim();

if (!tagCommitSha) {
throw Error(red(`Cannot publish v${projectVersion} because the release is not ` +
`tagged on upstream yet. Please tag the release before publishing to NPM.`));
}
});

/** Task that checks the release bundles for any common mistakes before releasing to the public. */
task('validate-release:check-bundles', () => {
const releaseFailures = releasePackages
Expand Down
3 changes: 3 additions & 0 deletions tools/package-tools/build-release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {copyFiles} from './copy-files';
import {createEntryPointPackageJson} from './entry-point-package-json';
import {inlinePackageMetadataFiles} from './metadata-inlining';
import {createMetadataReexportFile} from './metadata-reexport';
import {insertPackageJsonVersionStamp} from './package-version-stamp';
import {createTypingsReexportFile} from './typings-reexport';
import {replaceVersionPlaceholders} from './version-placeholders';

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

replaceVersionPlaceholders(releasePath);
insertPackageJsonVersionStamp(join(releasePath, 'package.json'));

createTypingsReexportFile(releasePath, './typings/index', name);
createMetadataReexportFile(releasePath, './typings/index', name, importAsName);

Expand Down
42 changes: 42 additions & 0 deletions tools/package-tools/package-version-stamp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {writeFileSync} from 'fs';
import {spawnSync} from 'child_process';
import {buildConfig} from './build-config';

/**
* Inserts the a version stamp, which consists of the current commit SHA and the current
* branch name, into the specified package.json file.
*
* This makes it easy to quickly verify the exact snapshot from which the release originated.
*/
export function insertPackageJsonVersionStamp(packageJsonPath: string) {
const packageJson = require(packageJsonPath);

packageJson['releaseGitCommitSha'] = getCurrentCommitSha();
packageJson['releaseGitBranch'] = getCurrentBranchName();
packageJson['releaseGitUser'] = getCurrentGitUser();

writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
}

/** Returns the commit SHA for the current git HEAD of the project. */
function getCurrentCommitSha(): string {
return spawnSync('git', ['rev-parse', 'HEAD'], {cwd: buildConfig.projectDir})
.stdout.toString().trim();
}

/** Returns the name of the currently checked out branch of the project. */
function getCurrentBranchName(): string {
return spawnSync('git', ['symbolic-ref', '--short', 'HEAD'], {cwd: buildConfig.projectDir})
.stdout.toString().trim();
}

/** Returns the name and email of the Git user that creates this release build. */
function getCurrentGitUser() {
const userName = spawnSync('git', ['config', 'user.name'], {cwd: buildConfig.projectDir})
.stdout.toString().trim();

const userEmail = spawnSync('git', ['config', 'user.email'], {cwd: buildConfig.projectDir})
.stdout.toString().trim();

return `${userName} <${userEmail}>`;
}