Skip to content

Commit c30ec68

Browse files
committed
feat(ng-dev): create tool for automatically discovering and updating generated files (#2359)
Automatically query bazel and find all generated files and run the updates for them. PR Close #2359
1 parent d836a5a commit c30ec68

File tree

6 files changed

+97
-44
lines changed

6 files changed

+97
-44
lines changed

ng-dev/misc/cli.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {Argv} from 'yargs';
1010
import {BuildAndLinkCommandModule} from './build-and-link/cli.js';
1111
import {UpdateYarnCommandModule} from './update-yarn/cli.js';
1212
import {ValidateLicensesModule} from './validate-licenses/cli.js';
13+
import {GeneratedFilesModule} from './generated-files/cli.js';
1314

1415
/** Build the parser for the misc commands. */
1516
export function buildMiscParser(localYargs: Argv) {
@@ -18,5 +19,6 @@ export function buildMiscParser(localYargs: Argv) {
1819
.strict()
1920
.command(BuildAndLinkCommandModule)
2021
.command(UpdateYarnCommandModule)
21-
.command(ValidateLicensesModule);
22+
.command(ValidateLicensesModule)
23+
.command(GeneratedFilesModule);
2224
}

ng-dev/misc/generated-files/cli.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC
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 {Argv, CommandModule} from 'yargs';
10+
import {updateGeneratedFileTargets} from './update-generated-files.js';
11+
12+
async function builder(argv: Argv) {
13+
return argv;
14+
}
15+
async function handler() {
16+
await updateGeneratedFileTargets();
17+
}
18+
19+
/** CLI command module. */
20+
export const GeneratedFilesModule: CommandModule = {
21+
builder,
22+
handler,
23+
command: 'update-generated-files',
24+
describe: 'Automatically discover all bazel generated file targets and update them.',
25+
};
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC
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 {ChildProcess} from '../../utils/child-process.js';
10+
import {Spinner} from '../../utils/spinner.js';
11+
import {getBazelBin} from '../../utils/bazel-bin.js';
12+
import {green, Log} from '../../utils/logging.js';
13+
14+
export async function updateGeneratedFileTargets(): Promise<void> {
15+
const spinner = new Spinner('Querying for all generated file targets');
16+
17+
// Query for all of the generated file targets
18+
const result = await ChildProcess.spawn(
19+
getBazelBin(),
20+
['query', `"kind(nodejs_binary, //...) intersect attr(name, '.update$', //...)"`],
21+
{mode: 'silent'},
22+
);
23+
24+
if (result.status !== 0) {
25+
spinner.complete();
26+
throw Error(`Unexpected error: ${result.stderr}`);
27+
}
28+
29+
const targets = result.stdout.trim().split(/\r?\n/);
30+
spinner.update(`Found ${targets.length} generated file targets to update`);
31+
32+
// Build all of the generated file targets in parallel.
33+
await ChildProcess.spawn(getBazelBin(), ['build', targets.join(' ')], {mode: 'silent'});
34+
35+
// Individually run the generated file update targets.
36+
for (let idx = 0; idx < targets.length; idx++) {
37+
const target = targets[idx];
38+
spinner.update(`${idx + 1} of ${targets.length} updates completed`);
39+
const updateResult = await ChildProcess.spawn(getBazelBin(), ['run', target], {mode: 'silent'});
40+
if (updateResult.status !== 0) {
41+
spinner.complete();
42+
throw Error(`Unexpected error while updating: ${target}.`);
43+
}
44+
}
45+
46+
spinner.complete();
47+
Log.info(` ${green('✔')} Updated all generated files (${targets.length} targets)`);
48+
}

ng-dev/utils/bazel-bin.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC
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 {join} from 'path';
10+
import {determineRepoBaseDirFromCwd} from './repo-directory.js';
11+
12+
let BAZEL_BIN: undefined | string = undefined;
13+
14+
export function getBazelBin() {
15+
if (BAZEL_BIN === undefined) {
16+
BAZEL_BIN = process.env.BAZEL || join(determineRepoBaseDirFromCwd(), 'node_modules/.bin/bazel');
17+
}
18+
19+
return BAZEL_BIN;
20+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"ng-dev": "bash ./tools/local-dev.sh",
88
"lint": "yarn tslint -c tslint.json --project tsconfig.json",
99
"build-env-stamp": "tsx --tsconfig tsconfig.json ./ng-dev/release/stamping/_private_main.ts build-env-stamp",
10-
"update-generated-files": "tsx --tsconfig tsconfig.json ./tools/update-generated-files.ts",
10+
"update-generated-files": "yarn ng-dev misc update-generated-files",
1111
"pack": "bazel run //:npm_package.pack --config=release"
1212
},
1313
"exports": {

tools/update-generated-files.ts

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)