|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Script that builds the release output of all packages which have the "release-package |
| 5 | + * bazel tag set. The script builds all those packages and copies the release output to the |
| 6 | + * distribution folder within the project. |
| 7 | + */ |
| 8 | + |
| 9 | +const {execSync} = require('child_process'); |
| 10 | +const {join} = require('path'); |
| 11 | +const {chmod, cp, mkdir, rm, set, test} = require('shelljs'); |
| 12 | + |
| 13 | +// ShellJS should exit if a command fails. |
| 14 | +set('-e'); |
| 15 | + |
| 16 | +/** Name of the Bazel tag that will be used to find release package targets. */ |
| 17 | +const releaseTargetTag = 'release-package'; |
| 18 | + |
| 19 | +/** Path to the project directory. */ |
| 20 | +const projectDir = join(__dirname, '../'); |
| 21 | + |
| 22 | +/** Command that runs Bazel. */ |
| 23 | +const bazelCmd = process.env.BAZEL_COMMAND || `yarn -s bazel`; |
| 24 | + |
| 25 | +/** Command that queries Bazel for all release package targets. */ |
| 26 | +const queryPackagesCmd = |
| 27 | + `${bazelCmd} query --output=label "attr('tags', '\\[.*${releaseTargetTag}.*\\]', //src/...) ` + |
| 28 | + `intersect kind('.*_package', //src/...)"`; |
| 29 | + |
| 30 | +// Export the methods for building the release packages. These |
| 31 | +// can be consumed by the release tool. |
| 32 | +exports.buildReleasePackages = buildReleasePackages; |
| 33 | +exports.defaultBuildReleasePackages = defaultBuildReleasePackages; |
| 34 | + |
| 35 | +if (module === require.main) { |
| 36 | + defaultBuildReleasePackages(); |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Builds the release packages with the default compile mode and |
| 41 | + * output directory. |
| 42 | + */ |
| 43 | +function defaultBuildReleasePackages() { |
| 44 | + buildReleasePackages('legacy', join(projectDir, 'dist/releases')); |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Builds the release packages with the given compile mode and copies |
| 49 | + * the package output into the given directory. |
| 50 | + */ |
| 51 | +function buildReleasePackages(compileMode, distPath) { |
| 52 | + console.log('######################################'); |
| 53 | + console.log(' Building release packages...'); |
| 54 | + console.log(` Compile mode: ${compileMode}`); |
| 55 | + console.log('######################################'); |
| 56 | + |
| 57 | + // List of targets to build. e.g. "src/cdk:npm_package", or "src/material:npm_package". |
| 58 | + const targets = exec(queryPackagesCmd, true).split(/\r?\n/); |
| 59 | + const packageNames = getPackageNamesOfTargets(targets); |
| 60 | + const bazelBinPath = exec(`${bazelCmd} info bazel-bin`, true); |
| 61 | + const getOutputPath = pkgName => join(bazelBinPath, 'src', pkgName, 'npm_package'); |
| 62 | + |
| 63 | + // Walk through each release package and clear previous "npm_package" outputs. This is |
| 64 | + // a workaround for: https://github.com/bazelbuild/rules_nodejs/issues/1219. We need to |
| 65 | + // do this to ensure that the version placeholders are properly populated. |
| 66 | + packageNames.forEach(pkgName => { |
| 67 | + const outputPath = getOutputPath(pkgName); |
| 68 | + if (test('-d', outputPath)) { |
| 69 | + chmod('-R', 'u+w', outputPath); |
| 70 | + rm('-rf', outputPath); |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + // Build with "--config=release" so that Bazel runs the workspace stamping script. The |
| 75 | + // stamping script ensures that the version placeholder is populated in the release output. |
| 76 | + exec(`${bazelCmd} build --config=release --define=compile=${compileMode} ${targets.join(' ')}`); |
| 77 | + |
| 78 | + // Delete the distribution directory so that the output is guaranteed to be clean. Re-create |
| 79 | + // the empty directory so that we can copy the release packages into it later. |
| 80 | + rm('-rf', distPath); |
| 81 | + mkdir('-p', distPath); |
| 82 | + |
| 83 | + // Copy the package output into the specified distribution folder. |
| 84 | + packageNames.forEach(pkgName => { |
| 85 | + const outputPath = getOutputPath(pkgName); |
| 86 | + const targetFolder = join(distPath, pkgName); |
| 87 | + console.log(`> Copying package output to "${targetFolder}"`); |
| 88 | + cp('-R', outputPath, targetFolder); |
| 89 | + chmod('-R', 'u+w', targetFolder); |
| 90 | + }); |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * Gets the package names of the specified Bazel targets. |
| 95 | + * e.g. //src/material:npm_package -> material |
| 96 | + */ |
| 97 | +function getPackageNamesOfTargets(targets) { |
| 98 | + return targets.map(targetName => { |
| 99 | + const matches = targetName.match(/\/\/src\/(.*):npm_package/); |
| 100 | + if (matches === null) { |
| 101 | + throw Error(`Found Bazel target with "${releaseTargetTag}" tag, but could not ` + |
| 102 | + `determine release output name: ${targetName}`); |
| 103 | + } |
| 104 | + return matches[1]; |
| 105 | + }); |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * Executes the given command in the project directory. |
| 110 | + * @param {string} command The command to run |
| 111 | + * @param {boolean=} captureStdout Whether the stdout should be captured and |
| 112 | + * returned. |
| 113 | + */ |
| 114 | +function exec(command, captureStdout) { |
| 115 | + const stdout = execSync(command, { |
| 116 | + cwd: projectDir, |
| 117 | + stdio: ['inherit', captureStdout ? 'pipe' : 'inherit', 'inherit'], |
| 118 | + }); |
| 119 | + |
| 120 | + if (captureStdout) { |
| 121 | + process.stdout.write(stdout); |
| 122 | + return stdout.toString().trim(); |
| 123 | + } |
| 124 | +} |
0 commit comments