Skip to content

Commit

Permalink
build: use cross platform workspace_status_command
Browse files Browse the repository at this point in the history
  • Loading branch information
filipesilva committed Dec 5, 2018
1 parent 2a39425 commit 76a1b92
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 46 deletions.
4 changes: 3 additions & 1 deletion .bazelrc
Expand Up @@ -35,7 +35,9 @@ test --nolegacy_external_runfiles
###############################

# Releases should always be stamped with version control info
build:release --workspace_status_command=./tools/bazel_stamp_vars.sh
# This command assumes node on the path and is a workaround for
# https://github.com/bazelbuild/bazel/issues/4802
build:release --workspace_status_command="node ./tools/bazel_stamp_vars.js"

###############################
# Output #
Expand Down
2 changes: 1 addition & 1 deletion docs/BAZEL.md
Expand Up @@ -149,7 +149,7 @@ Bazel supports the ability to include non-hermetic information from the version
You can see an overview at https://www.kchodorow.com/blog/2017/03/27/stamping-your-builds/
In our repo, here is how it's configured:

1) In `tools/bazel_stamp_vars.sh` we run the `git` commands to generate our versioning info.
1) In `tools/bazel_stamp_vars.js` we run the `git` commands to generate our versioning info.
1) In `.bazelrc` we register this script as the value for the `workspace_status_command` flag. Bazel will run the script when it needs to stamp a binary.

Note that Bazel has a `--stamp` argument to `yarn bazel build`, but this has no effect since our stamping takes place in Skylark rules. See https://github.com/bazelbuild/bazel/issues/1054
Expand Down
56 changes: 56 additions & 0 deletions tools/bazel_stamp_vars.js
@@ -0,0 +1,56 @@
/**
* @license
* Copyright Google Inc. 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
*/

// tslint:disable:no-console
// Generates the data used by the stamping feature in bazel.
// See the section on stamping in docs / BAZEL.md
// This script must be a NodeJS script in order to be cross-platform.
// See https://github.com/bazelbuild/bazel/issues/5958
// 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).
const execSync = require('child_process').execSync;
function _exec(str) {
return execSync(str).toString().trim();
}

console.error('Running', process.argv.join(' '));

function onError() {
console.log('Failed to execute:,', process.argv.join(' '));
console.log('');
}

// Setup crash handler
process.on('uncaughtException', onError);

const BUILD_SCM_HASH = _exec(`git rev-parse HEAD`);
console.log(`BUILD_SCM_HASH ${BUILD_SCM_HASH}`);

if (_exec(`git tag`) == '') {
console.error(`No git tags found, can't stamp the build.`);
console.error('Please fetch the tags first:');
console.error(' git fetch git@github.com:angular/angular.git --tags');
}

// Find out if there are any uncommitted local changes
const LOCAL_CHANGES = _exec(`git status --untracked-files=no --porcelain`) != '';
console.log(`BUILD_SCM_LOCAL_CHANGES ${LOCAL_CHANGES}`);

// Only match the latest tag that is a version such as 6.0.0, 6.0.0-rc.5, etc...
// This will ignore non-version tags which would break unit tests expecting a valid version
// number in the package headers
const BUILD_SCM_VERSION_RAW =
_exec(`git describe --match [0-9].[0-9].[0-9]* --abbrev=7 --tags HEAD`);

// Reformat `git describe` version string into a more semver-ish string
// From: 5.2.0-rc.0-57-g757f886
// To: 5.2.0-rc.0+57.sha-757f886
// Or: 5.2.0-rc.0+57.sha-757f886.with-local-changes
const BUILD_SCM_VERSION = BUILD_SCM_VERSION_RAW.replace(/-([0-9]+)-g/, '+$1.sha-') +
(LOCAL_CHANGES ? '.with-local-changes' : '');
console.log(`BUILD_SCM_VERSION ${BUILD_SCM_VERSION}`);
44 changes: 0 additions & 44 deletions tools/bazel_stamp_vars.sh

This file was deleted.

0 comments on commit 76a1b92

Please sign in to comment.