-
Notifications
You must be signed in to change notification settings - Fork 6.8k
build: rework package-builder script to node script #17758
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
jelbourn
merged 2 commits into
angular:master
from
devversion:build/rework-package-builder-script-to-js
Nov 20, 2019
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
#!/usr/bin/env node | ||
|
||
/** | ||
* Script that builds the release output of all packages which have the "release-package | ||
* bazel tag set. The script builds all those packages and copies the release output to the | ||
* distribution folder within the project. | ||
*/ | ||
|
||
const {execSync} = require('child_process'); | ||
const {join} = require('path'); | ||
const {chmod, cp, mkdir, rm, set, test} = require('shelljs'); | ||
|
||
// ShellJS should exit if a command fails. | ||
set('-e'); | ||
|
||
/** Name of the Bazel tag that will be used to find release package targets. */ | ||
const releaseTargetTag = 'release-package'; | ||
|
||
/** Path to the project directory. */ | ||
const projectDir = join(__dirname, '../'); | ||
|
||
/** Command that runs Bazel. */ | ||
const bazelCmd = process.env.BAZEL_COMMAND || `yarn -s bazel`; | ||
|
||
/** Command that queries Bazel for all release package targets. */ | ||
const queryPackagesCmd = | ||
`${bazelCmd} query --output=label "attr('tags', '\\[.*${releaseTargetTag}.*\\]', //src/...) ` + | ||
`intersect kind('.*_package', //src/...)"`; | ||
|
||
// Export the methods for building the release packages. These | ||
// can be consumed by the release tool. | ||
exports.buildReleasePackages = buildReleasePackages; | ||
exports.defaultBuildReleasePackages = defaultBuildReleasePackages; | ||
|
||
if (module === require.main) { | ||
defaultBuildReleasePackages(); | ||
} | ||
|
||
/** | ||
* Builds the release packages with the default compile mode and | ||
* output directory. | ||
*/ | ||
function defaultBuildReleasePackages() { | ||
buildReleasePackages('legacy', join(projectDir, 'dist/releases')); | ||
} | ||
|
||
/** | ||
* Builds the release packages with the given compile mode and copies | ||
* the package output into the given directory. | ||
*/ | ||
function buildReleasePackages(compileMode, distPath) { | ||
console.log('######################################'); | ||
console.log(' Building release packages...'); | ||
console.log(` Compile mode: ${compileMode}`); | ||
console.log('######################################'); | ||
|
||
// List of targets to build. e.g. "src/cdk:npm_package", or "src/material:npm_package". | ||
const targets = exec(queryPackagesCmd, true).split(/\r?\n/); | ||
const packageNames = getPackageNamesOfTargets(targets); | ||
const bazelBinPath = exec(`${bazelCmd} info bazel-bin`, true); | ||
const getOutputPath = pkgName => join(bazelBinPath, 'src', pkgName, 'npm_package'); | ||
|
||
// Walk through each release package and clear previous "npm_package" outputs. This is | ||
// a workaround for: https://github.com/bazelbuild/rules_nodejs/issues/1219. We need to | ||
// do this to ensure that the version placeholders are properly populated. | ||
packageNames.forEach(pkgName => { | ||
const outputPath = getOutputPath(pkgName); | ||
if (test('-d', outputPath)) { | ||
chmod('-R', 'u+w', outputPath); | ||
rm('-rf', outputPath); | ||
} | ||
}); | ||
|
||
// Build with "--config=release" so that Bazel runs the workspace stamping script. The | ||
// stamping script ensures that the version placeholder is populated in the release output. | ||
exec(`${bazelCmd} build --config=release --define=compile=${compileMode} ${targets.join(' ')}`); | ||
|
||
// Delete the distribution directory so that the output is guaranteed to be clean. Re-create | ||
// the empty directory so that we can copy the release packages into it later. | ||
rm('-rf', distPath); | ||
mkdir('-p', distPath); | ||
|
||
// Copy the package output into the specified distribution folder. | ||
packageNames.forEach(pkgName => { | ||
const outputPath = getOutputPath(pkgName); | ||
const targetFolder = join(distPath, pkgName); | ||
console.log(`> Copying package output to "${targetFolder}"`); | ||
cp('-R', outputPath, targetFolder); | ||
chmod('-R', 'u+w', targetFolder); | ||
}); | ||
} | ||
|
||
/** | ||
* Gets the package names of the specified Bazel targets. | ||
* e.g. //src/material:npm_package -> material | ||
*/ | ||
function getPackageNamesOfTargets(targets) { | ||
return targets.map(targetName => { | ||
const matches = targetName.match(/\/\/src\/(.*):npm_package/); | ||
if (matches === null) { | ||
throw Error(`Found Bazel target with "${releaseTargetTag}" tag, but could not ` + | ||
`determine release output name: ${targetName}`); | ||
} | ||
return matches[1]; | ||
}); | ||
} | ||
|
||
/** | ||
* Executes the given command in the project directory. | ||
* @param {string} command The command to run | ||
* @param {boolean=} captureStdout Whether the stdout should be captured and | ||
* returned. | ||
*/ | ||
function exec(command, captureStdout) { | ||
const stdout = execSync(command, { | ||
cwd: projectDir, | ||
stdio: ['inherit', captureStdout ? 'pipe' : 'inherit', 'inherit'], | ||
}); | ||
|
||
if (captureStdout) { | ||
process.stdout.write(stdout); | ||
return stdout.toString().trim(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.