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