Skip to content

Commit 9b935a2

Browse files
clydinangular-robot[bot]
authored andcommitted
Revert "build: remove no longer used scripts"
This reverts commit 8f4e101.
1 parent 04b2b09 commit 9b935a2

File tree

6 files changed

+734
-83
lines changed

6 files changed

+734
-83
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
],
2020
"scripts": {
2121
"admin": "node ./bin/devkit-admin",
22-
"test": "bazel test //packages/...",
22+
"bazel:test": "bazel test //packages/...",
2323
"build": "node ./bin/devkit-admin build",
24+
"build:bazel": "node ./bin/devkit-admin build-bazel",
2425
"build-tsc": "tsc -p tsconfig.json",
2526
"lint": "eslint --cache --max-warnings=0 \"**/*.ts\"",
2627
"ng-dev": "cross-env TS_NODE_PROJECT=$PWD/.ng-dev/tsconfig.json TS_NODE_TRANSPILE_ONLY=1 node --no-warnings --loader ts-node/esm node_modules/@angular/ng-dev/bundles/cli.mjs",

scripts/build-bazel.ts

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import { logging } from '@angular-devkit/core';
10+
import { spawn } from 'child_process';
11+
import fs from 'fs';
12+
import { dirname, join, relative, resolve } from 'path';
13+
14+
const baseDir = resolve(`${__dirname}/..`);
15+
const bazelCmd = process.env.BAZEL ?? `yarn --cwd "${baseDir}" --silent bazel`;
16+
const distRoot = join(baseDir, '/dist');
17+
18+
type BuildMode = 'local' | 'snapshot' | 'release';
19+
20+
function _copy(from: string, to: string) {
21+
// Create parent folder if necessary.
22+
if (!fs.existsSync(dirname(to))) {
23+
fs.mkdirSync(dirname(to), { recursive: true });
24+
}
25+
26+
// Error out if destination already exists.
27+
if (fs.existsSync(to)) {
28+
throw new Error(`Path ${to} already exist...`);
29+
}
30+
31+
from = relative(process.cwd(), from);
32+
to = relative(process.cwd(), to);
33+
34+
const buffer = fs.readFileSync(from);
35+
fs.writeFileSync(to, buffer);
36+
}
37+
38+
function _recursiveCopy(from: string, to: string, logger: logging.Logger) {
39+
if (!fs.existsSync(from)) {
40+
logger.error(`File "${from}" does not exist.`);
41+
process.exit(4);
42+
}
43+
if (fs.statSync(from).isDirectory()) {
44+
fs.readdirSync(from).forEach((fileName) => {
45+
_recursiveCopy(join(from, fileName), join(to, fileName), logger);
46+
});
47+
} else {
48+
_copy(from, to);
49+
}
50+
}
51+
52+
function rimraf(location: string) {
53+
fs.rmSync(location, { force: true, recursive: true, maxRetries: 3 });
54+
}
55+
56+
function _clean(logger: logging.Logger) {
57+
logger.info('Cleaning...');
58+
logger.info(' Removing dist/...');
59+
rimraf(join(__dirname, '../dist'));
60+
}
61+
62+
function _exec(cmd: string, captureStdout: boolean, logger: logging.Logger): Promise<string> {
63+
return new Promise((resolve, reject) => {
64+
const proc = spawn(cmd, {
65+
stdio: 'pipe',
66+
shell: true,
67+
});
68+
69+
let output = '';
70+
proc.stdout.on('data', (data) => {
71+
logger.info(data.toString().trim());
72+
if (captureStdout) {
73+
output += data.toString().trim();
74+
}
75+
});
76+
proc.stderr.on('data', (data) => logger.info(data.toString().trim()));
77+
78+
proc.on('error', (error) => {
79+
logger.error(error.message);
80+
});
81+
82+
proc.on('exit', (status) => {
83+
if (status !== 0) {
84+
reject(`Command failed: ${cmd}`);
85+
} else {
86+
resolve(output);
87+
}
88+
});
89+
});
90+
}
91+
92+
async function _build(logger: logging.Logger, mode: BuildMode): Promise<string[]> {
93+
logger.info(`Building (mode=${mode})...`);
94+
95+
const queryLogger = logger.createChild('query');
96+
const queryTargetsCmd = `${bazelCmd} query --output=label "attr(name, npm_package_archive, //packages/...)"`;
97+
const targets = (await _exec(queryTargetsCmd, true, queryLogger)).split(/\r?\n/);
98+
99+
const buildLogger = logger.createChild('build');
100+
101+
// If we are in release mode, run `bazel clean` to ensure the execroot and action cache
102+
// are not populated. This is necessary because targets using `npm_package` rely on
103+
// workspace status variables for the package version. Such NPM package targets are not
104+
// rebuilt if only the workspace status variables change. This could result in accidental
105+
// re-use of previously built package output with a different `version` in the `package.json`.
106+
if (mode == 'release') {
107+
buildLogger.info('Building in release mode. Resetting the Bazel execroot and action cache.');
108+
await _exec(`${bazelCmd} clean`, false, buildLogger);
109+
}
110+
111+
await _exec(`${bazelCmd} build --config=${mode} ${targets.join(' ')}`, false, buildLogger);
112+
113+
return targets;
114+
}
115+
116+
export default async function (
117+
argv: { local?: boolean; snapshot?: boolean } = {},
118+
logger: logging.Logger = new logging.Logger('build-logger'),
119+
): Promise<{ name: string; outputPath: string }[]> {
120+
const bazelBin = await _exec(`${bazelCmd} info bazel-bin`, true, logger);
121+
122+
_clean(logger);
123+
124+
let buildMode: BuildMode;
125+
if (argv.local) {
126+
buildMode = 'local';
127+
} else if (argv.snapshot) {
128+
buildMode = 'snapshot';
129+
} else {
130+
buildMode = 'release';
131+
}
132+
133+
const targets = await _build(logger, buildMode);
134+
const output: { name: string; outputPath: string }[] = [];
135+
136+
logger.info('Moving packages and tars to dist/');
137+
const packageLogger = logger.createChild('packages');
138+
139+
for (const target of targets) {
140+
const packageDir = target.replace(/\/\/packages\/(.*):npm_package_archive/, '$1');
141+
const bazelOutDir = join(bazelBin, 'packages', packageDir, 'npm_package');
142+
const tarPath = `${bazelBin}/packages/${packageDir}/npm_package_archive.tgz`;
143+
const packageJsonPath = `${bazelOutDir}/package.json`;
144+
const packageName = require(packageJsonPath).name;
145+
const destDir = `${distRoot}/${packageName}`;
146+
147+
packageLogger.info(packageName);
148+
149+
_recursiveCopy(bazelOutDir, destDir, logger);
150+
_copy(tarPath, `${distRoot}/${packageName.replace('@', '_').replace('/', '_')}.tgz`);
151+
152+
output.push({ name: packageDir, outputPath: destDir });
153+
}
154+
155+
return output;
156+
}

0 commit comments

Comments
 (0)