Skip to content

Commit

Permalink
launch: check prerequisites in a pre-launch task
Browse files Browse the repository at this point in the history
Fixes #103708
  • Loading branch information
connor4312 committed Jul 31, 2020
1 parent 98dd195 commit 262ab80
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 60 deletions.
7 changes: 4 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,10 @@
"port": 9222,
"timeout": 20000,
"env": {
"VSCODE_EXTHOST_WILL_SEND_SOCKET": null
"VSCODE_EXTHOST_WILL_SEND_SOCKET": null,
"VSCODE_SKIP_PRELAUNCH": "1"
},
"cleanUp": "wholeBrowser",
"breakOnLoad": false,
"urlFilter": "*workbench.html*",
"runtimeArgs": [
"--inspect=5875",
Expand All @@ -232,7 +232,8 @@
"outFiles": [
"${workspaceFolder}/out/**/*.js"
],
"browserLaunchLocation": "workspace"
"browserLaunchLocation": "workspace",
"preLaunchTask": "Ensure Prelaunch Dependencies",
},
{
"type": "node",
Expand Down
10 changes: 9 additions & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@
"source": "eslint",
"base": "$eslint-stylish"
}
}
},
{
"type": "shell",
"command": "node build/lib/prelaunch.js",
"label": "Ensure Prelaunch Dependencies",
"presentation": {
"reveal": "silent"
}
},
]
}
24 changes: 13 additions & 11 deletions build/lib/builtInExtensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function writeControlFile(control) {
fs.writeFileSync(controlFilePath, JSON.stringify(control, null, 2));
}

function main() {
exports.getBuiltInExtensions = function getBuiltInExtensions() {
log('Syncronizing built-in extensions...');
log(`You can manage built-in extensions with the ${ansiColors.cyan('--builtin')} flag`);

Expand All @@ -116,14 +116,16 @@ function main() {

writeControlFile(control);

es.merge(streams)
.on('error', err => {
console.error(err);
process.exit(1);
})
.on('end', () => {
process.exit(0);
});
return new Promise((resolve, reject) => {
es.merge(streams)
.on('error', reject)
.on('end', resolve);
});
};

if (require.main === module) {
main().then(() => process.exit(0)).catch(err => {
console.error(err);
process.exit(1);
});
}

main();
55 changes: 55 additions & 0 deletions build/lib/preLaunch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
// @ts-check
const path = require("path");
const child_process_1 = require("child_process");
const fs_1 = require("fs");
const yarn = process.platform === 'win32' ? 'yarn.cmd' : 'yarn';
const rootDir = path.resolve(__dirname, '..', '..');
function runProcess(command, args = []) {
return new Promise((resolve, reject) => {
const child = child_process_1.spawn(command, args, { cwd: rootDir, stdio: 'inherit', env: process.env });
child.on('exit', err => !err ? resolve() : process.exit(err !== null && err !== void 0 ? err : 1));
child.on('error', reject);
});
}
async function exists(subdir) {
try {
await fs_1.promises.stat(path.join(rootDir, subdir));
return true;
}
catch (_a) {
return false;
}
}
async function ensureNodeModules() {
if (!(await exists('node_modules'))) {
await runProcess(yarn);
}
}
async function getElectron() {
await runProcess(yarn, ['electron']);
}
async function ensureCompiled() {
if (!(await exists('out'))) {
await runProcess(yarn, ['compile']);
}
}
async function main() {
await ensureNodeModules();
await getElectron();
await ensureCompiled();
// Can't require this until after dependencies are installed
const { getBuiltInExtensions } = require('./builtInExtensions');
await getBuiltInExtensions();
}
if (require.main === module) {
main().catch(err => {
console.error(err);
process.exit(1);
});
}
65 changes: 65 additions & 0 deletions build/lib/preLaunch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

'use strict';

// @ts-check

import * as path from 'path';
import { spawn } from 'child_process';
import { promises as fs } from 'fs';

const yarn = process.platform === 'win32' ? 'yarn.cmd' : 'yarn';
const rootDir = path.resolve(__dirname, '..', '..');

function runProcess(command: string, args: ReadonlyArray<string> = []) {
return new Promise((resolve, reject) => {
const child = spawn(command, args, { cwd: rootDir, stdio: 'inherit', env: process.env });
child.on('exit', err => !err ? resolve() : process.exit(err ?? 1));
child.on('error', reject);
});
}

async function exists(subdir: string) {
try {
await fs.stat(path.join(rootDir, subdir));
return true;
} catch {
return false;
}
}

async function ensureNodeModules() {
if (!(await exists('node_modules'))) {
await runProcess(yarn);
}
}

async function getElectron() {
await runProcess(yarn, ['electron']);
}

async function ensureCompiled() {
if (!(await exists('out'))) {
await runProcess(yarn, ['compile']);
}
}

async function main() {
await ensureNodeModules();
await getElectron();
await ensureCompiled();

// Can't require this until after dependencies are installed
const { getBuiltInExtensions } = require('./builtInExtensions');
await getBuiltInExtensions();
}

if (require.main === module) {
main().catch(err => {
console.error(err);
process.exit(1);
});
}
14 changes: 2 additions & 12 deletions scripts/code-cli.bat
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,17 @@ title VSCode Dev

pushd %~dp0\..

:: Node modules
if not exist node_modules call yarn
:: Get electron, compile, built-in extensions
if "%VSCODE_SKIP_PRELAUNCH%"=="" node build/lib/preLaunch.js

for /f "tokens=2 delims=:," %%a in ('findstr /R /C:"\"nameShort\":.*" product.json') do set NAMESHORT=%%~a
set NAMESHORT=%NAMESHORT: "=%
set NAMESHORT=%NAMESHORT:"=%.exe
set CODE=".build\electron\%NAMESHORT%"

:: Download Electron if needed
node build\lib\electron.js
if %errorlevel% neq 0 node .\node_modules\gulp\bin\gulp.js electron

:: Manage built-in extensions
if "%1"=="--builtin" goto builtin

:: Sync built-in extensions
node build\lib\builtInExtensions.js

:: Build
if not exist out yarn compile

:: Configuration
set ELECTRON_RUN_AS_NODE=1
set NODE_ENV=development
Expand Down
15 changes: 4 additions & 11 deletions scripts/code-cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,17 @@ function code() {
CODE=".build/electron/$NAME"
fi

# Node modules
test -d node_modules || yarn

# Get electron
yarn electron
# Get electron, compile, built-in extensions
if [[ -z "${VSCODE_SKIP_PRELAUNCH}" ]]; then
node build/lib/preLaunch.js
fi

# Manage built-in extensions
if [[ "$1" == "--builtin" ]]; then
exec "$CODE" build/builtin
return
fi

# Sync built-in extensions
node build/lib/builtInExtensions.js

# Build
test -d out || yarn compile

ELECTRON_RUN_AS_NODE=1 \
NODE_ENV=development \
VSCODE_DEV=1 \
Expand Down
13 changes: 2 additions & 11 deletions scripts/code.bat
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,17 @@ title VSCode Dev

pushd %~dp0\..

:: Node modules
if not exist node_modules call yarn
:: Get electron, compile, built-in extensions
if "%VSCODE_SKIP_PRELAUNCH%"=="" node build/lib/preLaunch.js

for /f "tokens=2 delims=:," %%a in ('findstr /R /C:"\"nameShort\":.*" product.json') do set NAMESHORT=%%~a
set NAMESHORT=%NAMESHORT: "=%
set NAMESHORT=%NAMESHORT:"=%.exe
set CODE=".build\electron\%NAMESHORT%"

:: Get electron
call yarn electron

:: Manage built-in extensions
if "%1"=="--builtin" goto builtin

:: Sync built-in extensions
node build\lib\builtInExtensions.js

:: Build
if not exist out yarn compile

:: Configuration
set NODE_ENV=development
set VSCODE_DEV=1
Expand Down
15 changes: 4 additions & 11 deletions scripts/code.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,17 @@ function code() {
CODE=".build/electron/$NAME"
fi

# Node modules
test -d node_modules || yarn

# Get electron
yarn electron
# Get electron, compile, built-in extensions
if [[ -z "${VSCODE_SKIP_PRELAUNCH}" ]]; then
node build/lib/preLaunch.js
fi

# Manage built-in extensions
if [[ "$1" == "--builtin" ]]; then
exec "$CODE" build/builtin
return
fi

# Sync built-in extensions
node build/lib/builtInExtensions.js

# Build
test -d out || yarn compile

# Configuration
export NODE_ENV=development
export VSCODE_DEV=1
Expand Down

0 comments on commit 262ab80

Please sign in to comment.