forked from angular/components
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-packages-dist.js
More file actions
executable file
·124 lines (105 loc) · 4.41 KB
/
Copy pathbuild-packages-dist.js
File metadata and controls
executable file
·124 lines (105 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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(false, join(projectDir, 'dist/releases'));
}
/**
* Builds the release packages with the given compile mode and copies
* the package output into the given directory.
*/
function buildReleasePackages(useIvy, distPath) {
console.log('######################################');
console.log(' Building release packages...');
console.log(` Compiling with Ivy: ${useIvy}`);
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 --config=${useIvy ? 'ivy' : 'view-engine'} ${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();
}
}