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