Skip to content

Commit a5686a8

Browse files
Lightning00BladeDevtools-frontend LUCI CQ
authored and
Devtools-frontend LUCI CQ
committed
Split the prepare and build function
Split the logic in to to better define what is happening when. Bug: none Change-Id: Ibb5d7c7832b6cb1b38009a426bafb1be4a6b1ffa Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6439492 Reviewed-by: Benedikt Meurer <bmeurer@chromium.org> Commit-Queue: Nikolay Vitkov <nvitkov@chromium.org>
1 parent 06a73da commit a5686a8

File tree

2 files changed

+69
-52
lines changed

2 files changed

+69
-52
lines changed

scripts/devtools_build.mjs

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55
import childProcess from 'node:child_process';
66
import fs from 'node:fs/promises';
77
import path from 'node:path';
8-
import {performance} from 'node:perf_hooks';
8+
import { performance } from 'node:perf_hooks';
99
import util from 'node:util';
1010

11-
import {autoninjaExecutablePath, gnExecutablePath, rootPath} from './devtools_paths.js';
11+
import {
12+
autoninjaExecutablePath,
13+
gnExecutablePath,
14+
rootPath,
15+
} from './devtools_paths.js';
1216

1317
const execFile = util.promisify(childProcess.execFile);
1418

@@ -74,7 +78,7 @@ export class FeatureSet {
7478
* Yields the command line parameters to pass to the invocation of
7579
* a Chrome binary for achieving the state of the feature set.
7680
*/
77-
* [Symbol.iterator]() {
81+
*[Symbol.iterator]() {
7882
const disabledFeatures = [...this.#disabled];
7983
if (disabledFeatures.length) {
8084
yield `--disable-features=${disabledFeatures.sort().join(',')}`;
@@ -104,15 +108,17 @@ export class FeatureSet {
104108
if (parts.length > 1) {
105109
const args = parts[1].split('/');
106110
if (args.length % 2 !== 0) {
107-
throw new Error(`Invalid parameters '${parts[1]}' for feature ${feature}`);
111+
throw new Error(
112+
`Invalid parameters '${parts[1]}' for feature ${feature}`,
113+
);
108114
}
109115
for (let i = 0; i < args.length; i += 2) {
110116
const key = args[i + 0];
111117
const value = args[i + 1];
112118
parameters[key] = value;
113119
}
114120
}
115-
features.push({feature, parameters});
121+
features.push({ feature, parameters });
116122
}
117123
return features;
118124
}
@@ -134,16 +140,16 @@ export class BuildError extends Error {
134140
* @param {string} options.target the target relative to `//out`.
135141
*/
136142
constructor(step, options) {
137-
const {cause, outDir, target} = options;
138-
super(`Failed to build target ${target} in ${outDir}`, {cause});
143+
const { cause, outDir, target } = options;
144+
super(`Failed to build target ${target} in ${outDir}`, { cause });
139145
this.name = 'BuildError';
140146
this.step = step;
141147
this.target = target;
142148
this.outDir = outDir;
143149
}
144150

145151
toString() {
146-
const {stdout} = this.cause;
152+
const { stdout } = this.cause;
147153
return stdout;
148154
}
149155
}
@@ -156,11 +162,9 @@ export class BuildError extends Error {
156162

157163
/**
158164
* @param {string} target
159-
* @param {AbortSignal=} signal
160-
* @return {Promise<BuildResult>} a `BuildResult` with statistics for the build.
165+
* @return {Promise<void>}
161166
*/
162-
export async function build(target, signal) {
163-
const startTime = performance.now();
167+
export async function prepareBuild(target) {
164168
const outDir = path.join(rootPath(), 'out', target);
165169

166170
// Prepare the build directory first.
@@ -170,30 +174,37 @@ export async function build(target, signal) {
170174
try {
171175
const gnExe = gnExecutablePath();
172176
const gnArgs = ['-q', 'gen', outDir];
173-
await execFile(gnExe, gnArgs, {signal});
177+
await execFile(gnExe, gnArgs);
174178
} catch (cause) {
175-
if (cause.name === 'AbortError') {
176-
throw cause;
177-
}
178-
throw new BuildError(BuildStep.GN, {cause, outDir, target});
179+
throw new BuildError(BuildStep.GN, { cause, outDir, target });
179180
}
180181
}
182+
}
183+
184+
/**
185+
* @param {string} target
186+
* @param {AbortSignal=} signal
187+
* @return {Promise<BuildResult>} a `BuildResult` with statistics for the build.
188+
*/
189+
export async function build(target, signal) {
190+
const startTime = performance.now();
191+
const outDir = path.join(rootPath(), 'out', target);
181192

182193
// Build just the devtools-frontend resources in |outDir|. This is important
183194
// since we might be running in a full Chromium checkout and certainly don't
184195
// want to build all of Chromium first.
185196
try {
186197
const autoninjaExe = autoninjaExecutablePath();
187198
const autoninjaArgs = ['-C', outDir, '--quiet', 'devtools_all_files'];
188-
await execFile(autoninjaExe, autoninjaArgs, {signal});
199+
await execFile(autoninjaExe, autoninjaArgs, { signal });
189200
} catch (cause) {
190201
if (cause.name === 'AbortError') {
191202
throw cause;
192203
}
193-
throw new BuildError(BuildStep.AUTONINJA, {cause, outDir, target});
204+
throw new BuildError(BuildStep.AUTONINJA, { cause, outDir, target });
194205
}
195206

196207
// Report the build result.
197208
const time = (performance.now() - startTime) / 1000;
198-
return {time};
209+
return { time };
199210
}

scripts/run_build.mjs

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,55 @@
55
import fs from 'node:fs';
66
import path from 'node:path';
77
import yargs from 'yargs';
8-
import {hideBin} from 'yargs/helpers';
8+
import { hideBin } from 'yargs/helpers';
99

10-
import {build} from './devtools_build.mjs';
10+
import { build, prepareBuild } from './devtools_build.mjs';
1111

1212
const argv = yargs(hideBin(process.argv))
13-
.option('target', {
14-
alias: 't',
15-
type: 'string',
16-
default: 'Default',
17-
description: 'Specify the target build subdirectory under //out',
18-
})
19-
.option('skip-initial-build', {
20-
type: 'boolean',
21-
default: false,
22-
description: 'Skip the initial build (use with --watch)',
23-
implies: 'watch',
24-
})
25-
.option('watch', {
26-
alias: 'w',
27-
type: 'boolean',
28-
default: false,
29-
description: 'Monitor for changes and automatically rebuild',
30-
})
31-
.usage('npm run build -- [options]')
32-
.help('help')
33-
.version(false)
34-
.parseSync();
13+
.option('target', {
14+
alias: 't',
15+
type: 'string',
16+
default: 'Default',
17+
description: 'Specify the target build subdirectory under //out',
18+
})
19+
.option('skip-initial-build', {
20+
type: 'boolean',
21+
default: false,
22+
description: 'Skip the initial build (use with --watch)',
23+
implies: 'watch',
24+
})
25+
.option('watch', {
26+
alias: 'w',
27+
type: 'boolean',
28+
default: false,
29+
description: 'Monitor for changes and automatically rebuild',
30+
})
31+
.usage('npm run build -- [options]')
32+
.help('help')
33+
.version(false)
34+
.parseSync();
3535

36-
const {target, watch, skipInitialBuild} = argv;
36+
const { target, watch, skipInitialBuild } = argv;
3737
const timeFormatter = new Intl.NumberFormat('en-US', {
3838
style: 'unit',
3939
unit: 'second',
4040
unitDisplay: 'narrow',
4141
maximumFractionDigits: 2,
4242
});
4343

44+
// Prepare the build target if not initialized.
45+
try {
46+
await prepareBuild(target);
47+
} catch (error) {
48+
console.log(error.toString());
49+
process.exit(1);
50+
}
51+
4452
// Perform an initial build (unless we should skip).
4553
if (!skipInitialBuild) {
4654
console.log('Building…');
4755
try {
48-
const {time} = await build(target);
56+
const { time } = await build(target);
4957
console.log(`Build ready (${timeFormatter.format(time)})`);
5058
} catch (error) {
5159
console.log(error.toString());
@@ -73,12 +81,12 @@ if (watch) {
7381
// Abort any currently running build.
7482
abortController.abort();
7583
abortController = new AbortController();
76-
const {signal} = abortController;
84+
const { signal } = abortController;
7785

7886
buildPromise = buildPromise.then(async () => {
7987
try {
8088
console.log('Rebuilding...');
81-
const {time} = await build(target, signal);
89+
const { time } = await build(target, signal);
8290
console.log(`Rebuild successfully (${timeFormatter.format(time)})`);
8391
} catch (error) {
8492
if (error.name !== 'AbortError') {
@@ -90,10 +98,8 @@ if (watch) {
9098

9199
const WATCHLIST = ['front_end', 'test'];
92100
for (const dirname of WATCHLIST) {
93-
fs.watch(
94-
dirname,
95-
{recursive: true},
96-
(eventType, filename) => watchCallback(eventType, path.join(dirname, filename)),
101+
fs.watch(dirname, { recursive: true }, (eventType, filename) =>
102+
watchCallback(eventType, path.join(dirname, filename)),
97103
);
98104
}
99105
}

0 commit comments

Comments
 (0)