Skip to content

Commit da02c2e

Browse files
authored
chore: add --dry-run to install-deps CLI command (microsoft#10520)
1 parent 5d19f16 commit da02c2e

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

packages/playwright-core/src/cli/innerCli.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ program
107107
if (!args.length) {
108108
const executables = registry.defaultExecutables();
109109
if (options.withDeps)
110-
await registry.installDeps(executables);
110+
await registry.installDeps(executables, false);
111111
await registry.install(executables);
112112
} else {
113113
const installDockerImage = args.some(arg => arg === 'docker-image');
@@ -123,7 +123,7 @@ program
123123

124124
const executables = checkBrowsersToInstall(args);
125125
if (options.withDeps)
126-
await registry.installDeps(executables);
126+
await registry.installDeps(executables, false);
127127
await registry.install(executables);
128128
}
129129
} catch (e) {
@@ -143,12 +143,13 @@ Examples:
143143
program
144144
.command('install-deps [browser...]')
145145
.description('install dependencies necessary to run browsers (will ask for sudo permissions)')
146-
.action(async function(args: string[]) {
146+
.option('--dry-run', 'Do not execute installation commands, only print them')
147+
.action(async function(args: string[], options: { dryRun?: boolean }) {
147148
try {
148149
if (!args.length)
149-
await registry.installDeps(registry.defaultExecutables());
150+
await registry.installDeps(registry.defaultExecutables(), !!options.dryRun);
150151
else
151-
await registry.installDeps(checkBrowsersToInstall(args));
152+
await registry.installDeps(checkBrowsersToInstall(args), !!options.dryRun);
152153
} catch (e) {
153154
console.log(`Failed to install browser dependencies\n${e}`);
154155
process.exit(1);

packages/playwright-core/src/utils/dependencies.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,21 @@ function isSupportedWindowsVersion(): boolean {
3838

3939
export type DependencyGroup = 'chromium' | 'firefox' | 'webkit' | 'tools';
4040

41-
export async function installDependenciesWindows(targets: Set<DependencyGroup>) {
41+
export async function installDependenciesWindows(targets: Set<DependencyGroup>, dryRun: boolean): Promise<void> {
4242
if (targets.has('chromium')) {
43-
const { code } = await utils.spawnAsync('powershell.exe', ['-ExecutionPolicy', 'Bypass', '-File', path.join(BIN_DIRECTORY, 'install_media_pack.ps1')], { cwd: BIN_DIRECTORY, stdio: 'inherit' });
43+
const command = 'powershell.exe';
44+
const args = ['-ExecutionPolicy', 'Bypass', '-File', path.join(BIN_DIRECTORY, 'install_media_pack.ps1')];
45+
if (dryRun) {
46+
console.log(`${command} ${quoteProcessArgs(args).join(' ')}`); // eslint-disable-line no-console
47+
return;
48+
}
49+
const { code } = await utils.spawnAsync(command, args, { cwd: BIN_DIRECTORY, stdio: 'inherit' });
4450
if (code !== 0)
4551
throw new Error('Failed to install windows dependencies!');
4652
}
4753
}
4854

49-
export async function installDependenciesLinux(targets: Set<DependencyGroup>) {
55+
export async function installDependenciesLinux(targets: Set<DependencyGroup>, dryRun: boolean) {
5056
const libraries: string[] = [];
5157
for (const target of targets) {
5258
const info = deps[utils.hostPlatform];
@@ -57,13 +63,18 @@ export async function installDependenciesLinux(targets: Set<DependencyGroup>) {
5763
libraries.push(...info[target]);
5864
}
5965
const uniqueLibraries = Array.from(new Set(libraries));
60-
console.log('Installing Ubuntu dependencies...'); // eslint-disable-line no-console
66+
if (!dryRun)
67+
console.log('Installing Ubuntu dependencies...'); // eslint-disable-line no-console
6168
const commands: string[] = [];
6269
commands.push('apt-get update');
6370
commands.push(['apt-get', 'install', '-y', '--no-install-recommends',
6471
...uniqueLibraries,
6572
].join(' '));
6673
const [command, args] = await buildAptProcessArgs(commands);
74+
if (dryRun) {
75+
console.log(`${command} ${quoteProcessArgs(args).join(' ')}`); // eslint-disable-line no-console
76+
return;
77+
}
6778
const child = childProcess.spawn(command, args, { stdio: 'inherit' });
6879
await new Promise((resolve, reject) => {
6980
child.on('exit', resolve);
@@ -293,3 +304,11 @@ const MANUAL_LIBRARY_TO_PACKAGE_NAME_UBUNTU: { [s: string]: string} = {
293304
// gstreamer1.0-libav -> libavcodec57 -> libx264-152
294305
'libx264.so': 'gstreamer1.0-libav',
295306
};
307+
308+
function quoteProcessArgs(args: string[]): string[] {
309+
return args.map(arg => {
310+
if (arg.includes(' '))
311+
return `"${arg}"`;
312+
return arg;
313+
});
314+
}

packages/playwright-core/src/utils/registry.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ export class Registry {
526526
return await validateDependenciesWindows(windowsExeAndDllDirectories.map(d => path.join(browserDirectory, d)));
527527
}
528528

529-
async installDeps(executablesToInstallDeps: Executable[]) {
529+
async installDeps(executablesToInstallDeps: Executable[], dryRun: boolean) {
530530
const executables = this._addRequirementsAndDedupe(executablesToInstallDeps);
531531
const targets = new Set<DependencyGroup>();
532532
for (const executable of executables) {
@@ -535,9 +535,9 @@ export class Registry {
535535
}
536536
targets.add('tools');
537537
if (os.platform() === 'win32')
538-
return await installDependenciesWindows(targets);
538+
return await installDependenciesWindows(targets, dryRun);
539539
if (os.platform() === 'linux')
540-
return await installDependenciesLinux(targets);
540+
return await installDependenciesLinux(targets, dryRun);
541541
}
542542

543543
async install(executablesToInstall: Executable[]) {

0 commit comments

Comments
 (0)