Skip to content

Commit f6ba177

Browse files
committed
Clone and build VS Code if no pre-built is available
Also change the tar name since old pre-builts won't be compatible with the new version.
1 parent 0d94bd8 commit f6ba177

File tree

5 files changed

+74
-52
lines changed

5 files changed

+74
-52
lines changed

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@
77
"private": true,
88
"workspaces": ["packages/*", "packages/app/*"],
99
"scripts": {
10-
"postinstall": "npm-run-all --parallel vscode:install build:rules",
10+
"postinstall": "yarn build:rules",
1111
"build": "yarn task build",
1212
"start": "npm-run-all --parallel watch build:run",
1313
"watch": "yarn task build true",
1414
"build:run": "cd ./out && node ./packages/server/src/cli # TODO: restart on change",
1515
"build:rules": "cd ./rules && tsc -p .",
16-
"vscode:install": "yarn task vscode:install",
1716
"lint": "tslint --project .",
1817
"task": "ts-node -r tsconfig-paths/register scripts/tasks.ts",
1918
"test": "jest"

packages/protocol/src/node/modules/node-pty.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/// <reference path="../../../../../lib/vscode/src/typings/node-pty.d.ts" />
21
import { EventEmitter } from "events";
32
import * as pty from "node-pty";
43
import { ServerProxy } from "../../common/proxy";

packages/runner/src/runner.ts

+13-15
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const execute = (command: string, args: string[] = [], options: cp.SpawnOptions,
4343
};
4444

4545
// tslint:disable-next-line no-any
46-
export type TaskFunction = (runner: Runner, ...args: any[]) => void | Promise<void>;
46+
export type TaskFunction = (runner: Runner, logger: Logger, ...args: any[]) => void | Promise<void>;
4747

4848
export interface Runner {
4949
cwd: string;
@@ -93,25 +93,23 @@ export const run = (name: string = process.argv[2]): void | Promise<void> => {
9393
set cwd(path: string) {
9494
cwd = path;
9595
},
96-
execute(command: string, args: string[] = [], env?: object): Promise<CommandResult> {
97-
const prom = execute(command, args, {
96+
async execute(command: string, args: string[] = [], env?: object): Promise<CommandResult> {
97+
const result = await execute(command, args, {
9898
cwd,
9999
env: env as NodeJS.ProcessEnv,
100100
}, log);
101101

102-
return prom.then((result: CommandResult) => {
103-
if (result.exitCode != 0) {
104-
log.error("failed",
105-
field("exitCode", result.exitCode),
106-
field("stdout", result.stdout),
107-
field("stderr", result.stderr)
108-
);
109-
}
110-
111-
return result;
112-
});
102+
if (result.exitCode != 0) {
103+
log.error("failed",
104+
field("exitCode", result.exitCode),
105+
field("stdout", result.stdout),
106+
field("stderr", result.stderr),
107+
);
108+
}
109+
110+
return result;
113111
},
114-
}, ...process.argv.slice(3));
112+
}, log, ...process.argv.slice(3));
115113

116114
if (prom) {
117115
activated.set(name, prom);

scripts/tasks.ts

+56-31
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Binary } from "@coder/nbin";
2-
import { logger, field } from "@coder/logger";
2+
import { field } from "@coder/logger";
33
import { register, run } from "@coder/runner";
44

55
import * as fs from "fs";
@@ -11,25 +11,25 @@ import * as tar from "tar";
1111

1212
import { platform } from "./platform";
1313

14-
const libDir = path.join(__dirname, "../lib");
14+
const libPath = path.join(__dirname, "../lib");
1515
const releasePath = path.resolve(__dirname, "../release");
1616
const target = `${platform()}-${os.arch()}`;
17-
const vscodeVersion = process.env.VSCODE_VERSION || "1.34.0";
17+
const vscodeVersion = process.env.VSCODE_VERSION || "1.35.0";
1818

1919
/**
2020
* Build source.
2121
*/
22-
register("build", async (runner, shouldWatch: string) => {
22+
register("build", async (runner, logger, shouldWatch: string) => {
2323
const watch = shouldWatch === "true";
2424

2525
logger.info("Building", field("env", {
2626
NODE_ENV: process.env.NODE_ENV,
2727
VERSION: process.env.VERSION,
2828
}), field("vscode", vscodeVersion), field("watch", watch));
2929

30-
const outDir = path.join(__dirname, "../out");
30+
const outPath = path.join(__dirname, "../out");
3131
const compile = async (): Promise<void> => {
32-
fse.removeSync(path.resolve(outDir));
32+
fse.removeSync(path.resolve(outPath));
3333

3434
runner.cwd = path.resolve(__dirname, "..");
3535
const resp = await runner.execute(
@@ -48,15 +48,15 @@ register("build", async (runner, shouldWatch: string) => {
4848
["tsconfig.runtime.json", "tsconfig.json"],
4949
].map((p) => fse.copy(
5050
path.resolve(__dirname, "..", Array.isArray(p) ? p[0] : p),
51-
path.resolve(outDir, Array.isArray(p) ? p[1] : p),
51+
path.resolve(outPath, Array.isArray(p) ? p[1] : p),
5252
)));
53-
fse.unlinkSync(path.resolve(outDir, "packages/protocol/src/proto/index.ts"));
53+
fse.unlinkSync(path.resolve(outPath, "packages/protocol/src/proto/index.ts"));
5454
};
5555

56+
await ensureInstalled(),
5657
await Promise.all([
5758
await compile(),
5859
await copy(),
59-
await ensureInstalled(),
6060
]);
6161
});
6262

@@ -70,8 +70,9 @@ register("bundle", async () => {
7070
target: platform() === "darwin" ? "darwin" : platform() === "musl" ? "alpine" : "linux",
7171
});
7272

73+
bin.writeFiles(path.join(root, "lib/**"));
7374
bin.writeFiles(path.join(root, "out/**"));
74-
// TODO: dependencies (node_modules).
75+
bin.writeFiles(path.join(root, "**/node_modules"));
7576

7677
fse.mkdirpSync(releasePath);
7778

@@ -89,41 +90,65 @@ register("bundle", async () => {
8990
/**
9091
* Make sure the expected VS code version has been downloaded.
9192
*/
92-
const ensureInstalled = register("vscode:install", async (runner) => {
93-
runner.cwd = libDir;
93+
const ensureInstalled = register("vscode:install", async (runner, logger) => {
94+
const vscodePath = path.join(libPath, "vscode");
95+
96+
const build = async (): Promise<void> => {
97+
runner.cwd = vscodePath;
98+
if (!fs.existsSync(path.join(vscodePath, "node_modules"))) {
99+
await runner.execute("yarn");
100+
}
101+
if (!fs.existsSync(path.join(vscodePath, "out"))) {
102+
await runner.execute("yarn", ["compile"]);
103+
}
104+
};
105+
106+
const clone = async (): Promise<void> => {
107+
runner.cwd = libPath;
108+
await runner.execute("git", [
109+
"clone", "https://github.com/microsoft/vscode",
110+
"--branch", `${vscodeVersion}`, "--single-branch", "--depth=1",
111+
]);
112+
};
94113

95114
// See if we already have the correct version downloaded.
96-
const vscodePath = path.join(libDir, "vscode");
97115
if (fs.existsSync(vscodePath)) {
98116
const pkgVersion = JSON.parse(
99117
fs.readFileSync(path.join(vscodePath, "package.json")).toString("utf8"),
100118
).version;
101119
if (pkgVersion === vscodeVersion) {
102-
// TODO: check that it has been properly built along with dependencies.
103-
return;
120+
logger.info(`Found existing VS Code ${vscodeVersion} installation`);
121+
122+
return build();
104123
}
105124
}
106125

107-
fse.removeSync(libDir);
108-
fse.mkdirpSync(libDir);
126+
fse.removeSync(libPath);
127+
fse.mkdirpSync(libPath);
109128

129+
// If we can, fetch the pre-built version to save time. Otherwise we'll need
130+
// to clone and build.
110131
await new Promise<void>((resolve, reject): void => {
111-
const vsSourceUrl = `https://codesrv-ci.cdr.sh/vstar-${vscodeVersion}.tar.gz`;
132+
const vsSourceUrl = `https://codesrv-ci.cdr.sh/vscode-${vscodeVersion}-prebuilt.tar.gz`;
112133
https.get(vsSourceUrl, (res) => {
113134
switch (res.statusCode) {
114-
case 200: break;
115-
// TODO: if it hasn't been packaged, clone and build it instead.
116-
case 404: return reject(new Error(`${vscodeVersion} has not been packaged yet`));
117-
default: return reject(new Error(res.statusMessage));
135+
case 200:
136+
logger.info(`Downloading pre-built VS Code ${vscodeVersion}`);
137+
res.pipe(tar.x({
138+
C: libPath,
139+
}).on("finish", () => {
140+
resolve();
141+
}).on("error", (err: Error) => {
142+
reject(err);
143+
}));
144+
break;
145+
case 404:
146+
logger.info(`VS Code ${vscodeVersion} hasn't been packaged yet`);
147+
clone().then(() => build()).catch(reject);
148+
break;
149+
default:
150+
return reject(new Error(res.statusMessage));
118151
}
119-
120-
res.pipe(tar.x({
121-
C: libDir,
122-
}).on("finish", () => {
123-
resolve();
124-
}).on("error", (err: Error) => {
125-
reject(err);
126-
}));
127152
}).on("error", (err) => {
128153
reject(err);
129154
});
@@ -133,7 +158,7 @@ const ensureInstalled = register("vscode:install", async (runner) => {
133158
/**
134159
* Package the binary, readme, and license into an archive.
135160
*/
136-
register("package", async (runner, releaseTag) => {
161+
register("package", async (runner, _logger, releaseTag) => {
137162
if (!releaseTag) {
138163
throw new Error("Please specify the release tag.");
139164
}

scripts/vstar.bash

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,18 @@ function main() {
1111
fi
1212

1313
local dir=/tmp/vstar
14-
local outfile="/tmp/vstar-${version}.tar.gz"
14+
local outfile="/tmp/vscode-${version}-prebuilt.tar.gz"
1515
rm -rf "${dir}"
1616
mkdir -p "${dir}"
1717

1818
cd "${dir}"
19-
git clone https://github.com/microsoft/vscode --branch "${version}" --single-branch --depth=1
19+
git clone https://github.com/microsoft/vscode \
20+
--branch "${version}" --single-branch --depth=1
2021
cd vscode
2122

2223
yarn
23-
2424
npx gulp vscode-linux-x64 --max-old-space-size=32384
25+
2526
tar -czvf "${outfile}" "${dir}"
2627
}
2728

0 commit comments

Comments
 (0)