Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/build/gulpfile.vscode.linux.ts
5241 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 gulp from 'gulp';
7
import replace from 'gulp-replace';
8
import rename from 'gulp-rename';
9
import es from 'event-stream';
10
import vfs from 'vinyl-fs';
11
import { rimraf } from './lib/util.ts';
12
import { getVersion } from './lib/getVersion.ts';
13
import * as task from './lib/task.ts';
14
import packageJson from '../package.json' with { type: 'json' };
15
import product from '../product.json' with { type: 'json' };
16
import { getDependencies } from './linux/dependencies-generator.ts';
17
import { recommendedDeps as debianRecommendedDependencies } from './linux/debian/dep-lists.ts';
18
import * as path from 'path';
19
import * as cp from 'child_process';
20
import { promisify } from 'util';
21
22
const exec = promisify(cp.exec);
23
const root = path.dirname(import.meta.dirname);
24
const commit = getVersion(root);
25
26
const linuxPackageRevision = Math.floor(new Date().getTime() / 1000);
27
28
function getDebPackageArch(arch: string): string {
29
switch (arch) {
30
case 'x64': return 'amd64';
31
case 'armhf': return 'armhf';
32
case 'arm64': return 'arm64';
33
default: throw new Error(`Unknown arch: ${arch}`);
34
}
35
}
36
37
function prepareDebPackage(arch: string) {
38
const binaryDir = '../VSCode-linux-' + arch;
39
const debArch = getDebPackageArch(arch);
40
const destination = '.build/linux/deb/' + debArch + '/' + product.applicationName + '-' + debArch;
41
42
return async function () {
43
const dependencies = await getDependencies('deb', binaryDir, product.applicationName, debArch);
44
45
const desktop = gulp.src('resources/linux/code.desktop', { base: '.' })
46
.pipe(rename('usr/share/applications/' + product.applicationName + '.desktop'));
47
48
const desktopUrlHandler = gulp.src('resources/linux/code-url-handler.desktop', { base: '.' })
49
.pipe(rename('usr/share/applications/' + product.applicationName + '-url-handler.desktop'));
50
51
const desktops = es.merge(desktop, desktopUrlHandler)
52
.pipe(replace('@@NAME_LONG@@', product.nameLong))
53
.pipe(replace('@@NAME_SHORT@@', product.nameShort))
54
.pipe(replace('@@NAME@@', product.applicationName))
55
.pipe(replace('@@EXEC@@', `/usr/share/${product.applicationName}/${product.applicationName}`))
56
.pipe(replace('@@ICON@@', product.linuxIconName))
57
.pipe(replace('@@URLPROTOCOL@@', product.urlProtocol));
58
59
const appdata = gulp.src('resources/linux/code.appdata.xml', { base: '.' })
60
.pipe(replace('@@NAME_LONG@@', product.nameLong))
61
.pipe(replace('@@NAME@@', product.applicationName))
62
.pipe(replace('@@LICENSE@@', product.licenseName))
63
.pipe(rename('usr/share/appdata/' + product.applicationName + '.appdata.xml'));
64
65
const workspaceMime = gulp.src('resources/linux/code-workspace.xml', { base: '.' })
66
.pipe(replace('@@NAME_LONG@@', product.nameLong))
67
.pipe(replace('@@NAME@@', product.applicationName))
68
.pipe(rename('usr/share/mime/packages/' + product.applicationName + '-workspace.xml'));
69
70
const icon = gulp.src('resources/linux/code.png', { base: '.' })
71
.pipe(rename('usr/share/pixmaps/' + product.linuxIconName + '.png'));
72
73
const bash_completion = gulp.src('resources/completions/bash/code')
74
.pipe(replace('@@APPNAME@@', product.applicationName))
75
.pipe(rename('usr/share/bash-completion/completions/' + product.applicationName));
76
77
const zsh_completion = gulp.src('resources/completions/zsh/_code')
78
.pipe(replace('@@APPNAME@@', product.applicationName))
79
.pipe(rename('usr/share/zsh/vendor-completions/_' + product.applicationName));
80
81
const code = gulp.src(binaryDir + '/**/*', { base: binaryDir })
82
.pipe(rename(function (p) { p.dirname = 'usr/share/' + product.applicationName + '/' + p.dirname; }));
83
84
let size = 0;
85
const control = code.pipe(es.through(
86
function (f) { size += f.isDirectory() ? 4096 : f.contents.length; },
87
function () {
88
const that = this;
89
gulp.src('resources/linux/debian/control.template', { base: '.' })
90
.pipe(replace('@@NAME@@', product.applicationName))
91
.pipe(replace('@@VERSION@@', packageJson.version + '-' + linuxPackageRevision))
92
.pipe(replace('@@ARCHITECTURE@@', debArch))
93
.pipe(replace('@@DEPENDS@@', dependencies.join(', ')))
94
.pipe(replace('@@RECOMMENDS@@', debianRecommendedDependencies.join(', ')))
95
.pipe(replace('@@INSTALLEDSIZE@@', Math.ceil(size / 1024).toString()))
96
.pipe(rename('DEBIAN/control'))
97
.pipe(es.through(function (f) { that.emit('data', f); }, function () { that.emit('end'); }));
98
}));
99
100
const prerm = gulp.src('resources/linux/debian/prerm.template', { base: '.' })
101
.pipe(replace('@@NAME@@', product.applicationName))
102
.pipe(rename('DEBIAN/prerm'));
103
104
const postrm = gulp.src('resources/linux/debian/postrm.template', { base: '.' })
105
.pipe(replace('@@NAME@@', product.applicationName))
106
.pipe(rename('DEBIAN/postrm'));
107
108
const postinst = gulp.src('resources/linux/debian/postinst.template', { base: '.' })
109
.pipe(replace('@@NAME@@', product.applicationName))
110
.pipe(replace('@@ARCHITECTURE@@', debArch))
111
.pipe(rename('DEBIAN/postinst'));
112
113
const templates = gulp.src('resources/linux/debian/templates.template', { base: '.' })
114
.pipe(replace('@@NAME@@', product.applicationName))
115
.pipe(rename('DEBIAN/templates'));
116
117
const all = es.merge(control, templates, postinst, postrm, prerm, desktops, appdata, workspaceMime, icon, bash_completion, zsh_completion, code);
118
119
return all.pipe(vfs.dest(destination));
120
};
121
}
122
123
function buildDebPackage(arch: string) {
124
const debArch = getDebPackageArch(arch);
125
const cwd = `.build/linux/deb/${debArch}`;
126
127
return async () => {
128
await exec(`chmod 755 ${product.applicationName}-${debArch}/DEBIAN/postinst ${product.applicationName}-${debArch}/DEBIAN/prerm ${product.applicationName}-${debArch}/DEBIAN/postrm`, { cwd });
129
await exec('mkdir -p deb', { cwd });
130
await exec(`fakeroot dpkg-deb -Zxz -b ${product.applicationName}-${debArch} deb`, { cwd });
131
};
132
}
133
134
function getRpmBuildPath(rpmArch: string): string {
135
return '.build/linux/rpm/' + rpmArch + '/rpmbuild';
136
}
137
138
function getRpmPackageArch(arch: string): string {
139
switch (arch) {
140
case 'x64': return 'x86_64';
141
case 'armhf': return 'armv7hl';
142
case 'arm64': return 'aarch64';
143
default: throw new Error(`Unknown arch: ${arch}`);
144
}
145
}
146
147
function prepareRpmPackage(arch: string) {
148
const binaryDir = '../VSCode-linux-' + arch;
149
const rpmArch = getRpmPackageArch(arch);
150
const stripBinary = process.env['STRIP'] ?? '/usr/bin/strip';
151
152
return async function () {
153
const dependencies = await getDependencies('rpm', binaryDir, product.applicationName, rpmArch);
154
155
const desktop = gulp.src('resources/linux/code.desktop', { base: '.' })
156
.pipe(rename('BUILD/usr/share/applications/' + product.applicationName + '.desktop'));
157
158
const desktopUrlHandler = gulp.src('resources/linux/code-url-handler.desktop', { base: '.' })
159
.pipe(rename('BUILD/usr/share/applications/' + product.applicationName + '-url-handler.desktop'));
160
161
const desktops = es.merge(desktop, desktopUrlHandler)
162
.pipe(replace('@@NAME_LONG@@', product.nameLong))
163
.pipe(replace('@@NAME_SHORT@@', product.nameShort))
164
.pipe(replace('@@NAME@@', product.applicationName))
165
.pipe(replace('@@EXEC@@', `/usr/share/${product.applicationName}/${product.applicationName}`))
166
.pipe(replace('@@ICON@@', product.linuxIconName))
167
.pipe(replace('@@URLPROTOCOL@@', product.urlProtocol));
168
169
const appdata = gulp.src('resources/linux/code.appdata.xml', { base: '.' })
170
.pipe(replace('@@NAME_LONG@@', product.nameLong))
171
.pipe(replace('@@NAME@@', product.applicationName))
172
.pipe(replace('@@LICENSE@@', product.licenseName))
173
.pipe(rename('BUILD/usr/share/appdata/' + product.applicationName + '.appdata.xml'));
174
175
const workspaceMime = gulp.src('resources/linux/code-workspace.xml', { base: '.' })
176
.pipe(replace('@@NAME_LONG@@', product.nameLong))
177
.pipe(replace('@@NAME@@', product.applicationName))
178
.pipe(rename('BUILD/usr/share/mime/packages/' + product.applicationName + '-workspace.xml'));
179
180
const icon = gulp.src('resources/linux/code.png', { base: '.' })
181
.pipe(rename('BUILD/usr/share/pixmaps/' + product.linuxIconName + '.png'));
182
183
const bash_completion = gulp.src('resources/completions/bash/code')
184
.pipe(replace('@@APPNAME@@', product.applicationName))
185
.pipe(rename('BUILD/usr/share/bash-completion/completions/' + product.applicationName));
186
187
const zsh_completion = gulp.src('resources/completions/zsh/_code')
188
.pipe(replace('@@APPNAME@@', product.applicationName))
189
.pipe(rename('BUILD/usr/share/zsh/site-functions/_' + product.applicationName));
190
191
const code = gulp.src(binaryDir + '/**/*', { base: binaryDir })
192
.pipe(rename(function (p) { p.dirname = 'BUILD/usr/share/' + product.applicationName + '/' + p.dirname; }));
193
194
const spec = gulp.src('resources/linux/rpm/code.spec.template', { base: '.' })
195
.pipe(replace('@@NAME@@', product.applicationName))
196
.pipe(replace('@@NAME_LONG@@', product.nameLong))
197
.pipe(replace('@@ICON@@', product.linuxIconName))
198
.pipe(replace('@@VERSION@@', packageJson.version))
199
.pipe(replace('@@RELEASE@@', linuxPackageRevision.toString()))
200
.pipe(replace('@@ARCHITECTURE@@', rpmArch))
201
.pipe(replace('@@LICENSE@@', product.licenseName))
202
.pipe(replace('@@QUALITY@@', (product as typeof product & { quality?: string }).quality || '@@QUALITY@@'))
203
.pipe(replace('@@UPDATEURL@@', (product as typeof product & { updateUrl?: string }).updateUrl || '@@UPDATEURL@@'))
204
.pipe(replace('@@DEPENDENCIES@@', dependencies.join(', ')))
205
.pipe(replace('@@STRIP@@', stripBinary))
206
.pipe(rename('SPECS/' + product.applicationName + '.spec'));
207
208
const specIcon = gulp.src('resources/linux/rpm/code.xpm', { base: '.' })
209
.pipe(rename('SOURCES/' + product.applicationName + '.xpm'));
210
211
const all = es.merge(code, desktops, appdata, workspaceMime, icon, bash_completion, zsh_completion, spec, specIcon);
212
213
return all.pipe(vfs.dest(getRpmBuildPath(rpmArch)));
214
};
215
}
216
217
function buildRpmPackage(arch: string) {
218
const rpmArch = getRpmPackageArch(arch);
219
const rpmBuildPath = getRpmBuildPath(rpmArch);
220
const rpmOut = `${rpmBuildPath}/RPMS/${rpmArch}`;
221
const destination = `.build/linux/rpm/${rpmArch}`;
222
223
return async () => {
224
await exec(`mkdir -p ${destination}`);
225
await exec(`HOME="$(pwd)/${destination}" rpmbuild -bb ${rpmBuildPath}/SPECS/${product.applicationName}.spec --target=${rpmArch}`);
226
await exec(`cp "${rpmOut}/$(ls ${rpmOut})" ${destination}/`);
227
};
228
}
229
230
function getSnapBuildPath(arch: string): string {
231
return `.build/linux/snap/${arch}/${product.applicationName}-${arch}`;
232
}
233
234
function prepareSnapPackage(arch: string) {
235
const binaryDir = '../VSCode-linux-' + arch;
236
const destination = getSnapBuildPath(arch);
237
238
return function () {
239
// A desktop file that is placed in snap/gui will be placed into meta/gui verbatim.
240
const desktop = gulp.src('resources/linux/code.desktop', { base: '.' })
241
.pipe(rename(`snap/gui/${product.applicationName}.desktop`));
242
243
// A desktop file that is placed in snap/gui will be placed into meta/gui verbatim.
244
const desktopUrlHandler = gulp.src('resources/linux/code-url-handler.desktop', { base: '.' })
245
.pipe(rename(`snap/gui/${product.applicationName}-url-handler.desktop`));
246
247
const desktops = es.merge(desktop, desktopUrlHandler)
248
.pipe(replace('@@NAME_LONG@@', product.nameLong))
249
.pipe(replace('@@NAME_SHORT@@', product.nameShort))
250
.pipe(replace('@@NAME@@', product.applicationName))
251
.pipe(replace('@@EXEC@@', `${product.applicationName} --force-user-env`))
252
.pipe(replace('@@ICON@@', `\${SNAP}/meta/gui/${product.linuxIconName}.png`))
253
.pipe(replace('@@URLPROTOCOL@@', product.urlProtocol));
254
255
// An icon that is placed in snap/gui will be placed into meta/gui verbatim.
256
const icon = gulp.src('resources/linux/code.png', { base: '.' })
257
.pipe(rename(`snap/gui/${product.linuxIconName}.png`));
258
259
const code = gulp.src(binaryDir + '/**/*', { base: binaryDir })
260
.pipe(rename(function (p) { p.dirname = `usr/share/${product.applicationName}/${p.dirname}`; }));
261
262
const snapcraft = gulp.src('resources/linux/snap/snapcraft.yaml', { base: '.' })
263
.pipe(replace('@@NAME@@', product.applicationName))
264
.pipe(replace('@@VERSION@@', commit!.substr(0, 8)))
265
// Possible run-on values https://snapcraft.io/docs/architectures
266
.pipe(replace('@@ARCHITECTURE@@', arch === 'x64' ? 'amd64' : arch))
267
.pipe(rename('snap/snapcraft.yaml'));
268
269
const electronLaunch = gulp.src('resources/linux/snap/electron-launch', { base: '.' })
270
.pipe(rename('electron-launch'));
271
272
const all = es.merge(desktops, icon, code, snapcraft, electronLaunch);
273
274
return all.pipe(vfs.dest(destination));
275
};
276
}
277
278
function buildSnapPackage(arch: string) {
279
const cwd = getSnapBuildPath(arch);
280
return () => exec('snapcraft', { cwd });
281
}
282
283
const BUILD_TARGETS = [
284
{ arch: 'x64' },
285
{ arch: 'armhf' },
286
{ arch: 'arm64' },
287
];
288
289
BUILD_TARGETS.forEach(({ arch }) => {
290
const debArch = getDebPackageArch(arch);
291
const prepareDebTask = task.define(`vscode-linux-${arch}-prepare-deb`, task.series(rimraf(`.build/linux/deb/${debArch}`), prepareDebPackage(arch)));
292
gulp.task(prepareDebTask);
293
const buildDebTask = task.define(`vscode-linux-${arch}-build-deb`, buildDebPackage(arch));
294
gulp.task(buildDebTask);
295
296
const rpmArch = getRpmPackageArch(arch);
297
const prepareRpmTask = task.define(`vscode-linux-${arch}-prepare-rpm`, task.series(rimraf(`.build/linux/rpm/${rpmArch}`), prepareRpmPackage(arch)));
298
gulp.task(prepareRpmTask);
299
const buildRpmTask = task.define(`vscode-linux-${arch}-build-rpm`, buildRpmPackage(arch));
300
gulp.task(buildRpmTask);
301
302
const prepareSnapTask = task.define(`vscode-linux-${arch}-prepare-snap`, task.series(rimraf(`.build/linux/snap/${arch}`), prepareSnapPackage(arch)));
303
gulp.task(prepareSnapTask);
304
const buildSnapTask = task.define(`vscode-linux-${arch}-build-snap`, task.series(prepareSnapTask, buildSnapPackage(arch)));
305
gulp.task(buildSnapTask);
306
});
307
308