Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/build/gulpfile.cli.ts
5237 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import es from 'event-stream';
7
import gulp from 'gulp';
8
import * as path from 'path';
9
import fancyLog from 'fancy-log';
10
import ansiColors from 'ansi-colors';
11
import * as cp from 'child_process';
12
import { tmpdir } from 'os';
13
import { existsSync, mkdirSync, rmSync } from 'fs';
14
import * as task from './lib/task.ts';
15
import watcher from './lib/watch/index.ts';
16
import { debounce, untar } from './lib/util.ts';
17
import { createReporter } from './lib/reporter.ts';
18
import gunzip from 'gulp-gunzip';
19
20
const root = 'cli';
21
const rootAbs = path.resolve(import.meta.dirname, '..', root);
22
const src = `${root}/src`;
23
24
const platformOpensslDirName =
25
process.platform === 'win32' ? (
26
process.arch === 'arm64'
27
? 'arm64-windows-static-md'
28
: 'x64-windows-static-md')
29
: process.platform === 'darwin' ? (
30
process.arch === 'arm64'
31
? 'arm64-osx'
32
: 'x64-osx')
33
: (process.arch === 'arm64'
34
? 'arm64-linux'
35
: process.arch === 'arm'
36
? 'arm-linux'
37
: 'x64-linux');
38
const platformOpensslDir = path.join(rootAbs, 'openssl', 'package', 'out', platformOpensslDirName);
39
40
const hasLocalRust = (() => {
41
let result: boolean | undefined = undefined;
42
return () => {
43
if (result !== undefined) {
44
return result;
45
}
46
47
try {
48
const r = cp.spawnSync('cargo', ['--version']);
49
result = r.status === 0;
50
} catch (e) {
51
result = false;
52
}
53
54
return result;
55
};
56
})();
57
58
const compileFromSources = (callback: (err?: string) => void) => {
59
const proc = cp.spawn('cargo', ['--color', 'always', 'build'], {
60
cwd: root,
61
stdio: ['ignore', 'pipe', 'pipe'],
62
env: existsSync(platformOpensslDir) ? { OPENSSL_DIR: platformOpensslDir, ...process.env } : process.env
63
});
64
65
const stdoutErr: Buffer[] = [];
66
proc.stdout.on('data', d => stdoutErr.push(d));
67
proc.stderr.on('data', d => stdoutErr.push(d));
68
proc.on('error', callback);
69
proc.on('exit', code => {
70
if (code !== 0) {
71
callback(Buffer.concat(stdoutErr).toString());
72
} else {
73
callback();
74
}
75
});
76
};
77
78
const acquireBuiltOpenSSL = (callback: (err?: unknown) => void) => {
79
const dir = path.join(tmpdir(), 'vscode-openssl-download');
80
mkdirSync(dir, { recursive: true });
81
82
cp.spawnSync(
83
process.platform === 'win32' ? 'npm.cmd' : 'npm',
84
['pack', '@vscode/openssl-prebuilt'],
85
{ stdio: ['ignore', 'ignore', 'inherit'], cwd: dir }
86
);
87
88
gulp.src('*.tgz', { cwd: dir })
89
.pipe(gunzip())
90
.pipe(untar())
91
.pipe(gulp.dest(`${root}/openssl`))
92
.on('error', callback)
93
.on('end', () => {
94
rmSync(dir, { recursive: true, force: true });
95
callback();
96
});
97
};
98
99
const compileWithOpenSSLCheck = (reporter: import('./lib/reporter.ts').IReporter) => es.map((_, callback) => {
100
compileFromSources(err => {
101
if (!err) {
102
// no-op
103
} else if (err.toString().includes('Could not find directory of OpenSSL installation') && !existsSync(platformOpensslDir)) {
104
fancyLog(ansiColors.yellow(`[cli]`), 'OpenSSL libraries not found, acquiring prebuilt bits...');
105
acquireBuiltOpenSSL(err => {
106
if (err) {
107
callback(err as Error);
108
} else {
109
compileFromSources(err => {
110
if (err) {
111
reporter(err.toString());
112
}
113
callback(undefined, '');
114
});
115
}
116
});
117
} else {
118
reporter(err.toString());
119
}
120
callback(undefined, '');
121
});
122
});
123
124
const warnIfRustNotInstalled = () => {
125
if (!hasLocalRust()) {
126
fancyLog(ansiColors.yellow(`[cli]`), 'No local Rust install detected, compilation may fail.');
127
fancyLog(ansiColors.yellow(`[cli]`), 'Get rust from: https://rustup.rs/');
128
}
129
};
130
131
const compileCliTask = task.define('compile-cli', () => {
132
warnIfRustNotInstalled();
133
const reporter = createReporter('cli');
134
return gulp.src(`${root}/Cargo.toml`)
135
.pipe(compileWithOpenSSLCheck(reporter))
136
.pipe(reporter.end(true));
137
});
138
139
140
const watchCliTask = task.define('watch-cli', () => {
141
warnIfRustNotInstalled();
142
return watcher(`${src}/**`, { read: false })
143
.pipe(debounce(compileCliTask as task.StreamTask));
144
});
145
146
gulp.task(compileCliTask);
147
gulp.task(watchCliTask);
148
149