Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/build/lib/electron.ts
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
import fs from 'fs';
7
import path from 'path';
8
import vfs from 'vinyl-fs';
9
import filter from 'gulp-filter';
10
import * as util from './util';
11
import { getVersion } from './getVersion';
12
13
type DarwinDocumentSuffix = 'document' | 'script' | 'file' | 'source code';
14
type DarwinDocumentType = {
15
name: string;
16
role: string;
17
ostypes: string[];
18
extensions: string[];
19
iconFile: string;
20
utis?: string[];
21
};
22
23
function isDocumentSuffix(str?: string): str is DarwinDocumentSuffix {
24
return str === 'document' || str === 'script' || str === 'file' || str === 'source code';
25
}
26
27
const root = path.dirname(path.dirname(__dirname));
28
const product = JSON.parse(fs.readFileSync(path.join(root, 'product.json'), 'utf8'));
29
const commit = getVersion(root);
30
31
function createTemplate(input: string): (params: Record<string, string>) => string {
32
return (params: Record<string, string>) => {
33
return input.replace(/<%=\s*([^\s]+)\s*%>/g, (match, key) => {
34
return params[key] || match;
35
});
36
};
37
}
38
39
const darwinCreditsTemplate = product.darwinCredits && createTemplate(fs.readFileSync(path.join(root, product.darwinCredits), 'utf8'));
40
41
/**
42
* Generate a `DarwinDocumentType` given a list of file extensions, an icon name, and an optional suffix or file type name.
43
* @param extensions A list of file extensions, such as `['bat', 'cmd']`
44
* @param icon A sentence-cased file type name that matches the lowercase name of a darwin icon resource.
45
* For example, `'HTML'` instead of `'html'`, or `'Java'` instead of `'java'`.
46
* This parameter is lowercased before it is used to reference an icon file.
47
* @param nameOrSuffix An optional suffix or a string to use as the file type. If a suffix is provided,
48
* it is used with the icon parameter to generate a file type string. If nothing is provided,
49
* `'document'` is used with the icon parameter to generate file type string.
50
*
51
* For example, if you call `darwinBundleDocumentType(..., 'HTML')`, the resulting file type is `"HTML document"`,
52
* and the `'html'` darwin icon is used.
53
*
54
* If you call `darwinBundleDocumentType(..., 'Javascript', 'file')`, the resulting file type is `"Javascript file"`.
55
* and the `'javascript'` darwin icon is used.
56
*
57
* If you call `darwinBundleDocumentType(..., 'bat', 'Windows command script')`, the file type is `"Windows command script"`,
58
* and the `'bat'` darwin icon is used.
59
*/
60
function darwinBundleDocumentType(extensions: string[], icon: string, nameOrSuffix?: string | DarwinDocumentSuffix, utis?: string[]): DarwinDocumentType {
61
// If given a suffix, generate a name from it. If not given anything, default to 'document'
62
if (isDocumentSuffix(nameOrSuffix) || !nameOrSuffix) {
63
nameOrSuffix = icon.charAt(0).toUpperCase() + icon.slice(1) + ' ' + (nameOrSuffix ?? 'document');
64
}
65
66
return {
67
name: nameOrSuffix,
68
role: 'Editor',
69
ostypes: ['TEXT', 'utxt', 'TUTX', '****'],
70
extensions,
71
iconFile: 'resources/darwin/' + icon.toLowerCase() + '.icns',
72
utis
73
};
74
}
75
76
/**
77
* Generate several `DarwinDocumentType`s with unique names and a shared icon.
78
* @param types A map of file type names to their associated file extensions.
79
* @param icon A darwin icon resource to use. For example, `'HTML'` would refer to `resources/darwin/html.icns`
80
*
81
* Examples:
82
* ```
83
* darwinBundleDocumentTypes({ 'C header file': 'h', 'C source code': 'c' },'c')
84
* darwinBundleDocumentTypes({ 'React source code': ['jsx', 'tsx'] }, 'react')
85
* ```
86
*/
87
function darwinBundleDocumentTypes(types: { [name: string]: string | string[] }, icon: string): DarwinDocumentType[] {
88
return Object.keys(types).map((name: string): DarwinDocumentType => {
89
const extensions = types[name];
90
return {
91
name,
92
role: 'Editor',
93
ostypes: ['TEXT', 'utxt', 'TUTX', '****'],
94
extensions: Array.isArray(extensions) ? extensions : [extensions],
95
iconFile: 'resources/darwin/' + icon + '.icns'
96
};
97
});
98
}
99
100
const { electronVersion, msBuildId } = util.getElectronVersion();
101
102
export const config = {
103
version: electronVersion,
104
tag: product.electronRepository ? `v${electronVersion}-${msBuildId}` : undefined,
105
productAppName: product.nameLong,
106
companyName: 'Microsoft Corporation',
107
copyright: 'Copyright (C) 2024 Microsoft. All rights reserved',
108
darwinIcon: 'resources/darwin/code.icns',
109
darwinBundleIdentifier: product.darwinBundleIdentifier,
110
darwinApplicationCategoryType: 'public.app-category.developer-tools',
111
darwinHelpBookFolder: 'VS Code HelpBook',
112
darwinHelpBookName: 'VS Code HelpBook',
113
darwinBundleDocumentTypes: [
114
...darwinBundleDocumentTypes({ 'C header file': 'h', 'C source code': 'c' }, 'c'),
115
...darwinBundleDocumentTypes({ 'Git configuration file': ['gitattributes', 'gitconfig', 'gitignore'] }, 'config'),
116
...darwinBundleDocumentTypes({ 'HTML template document': ['asp', 'aspx', 'cshtml', 'jshtm', 'jsp', 'phtml', 'shtml'] }, 'html'),
117
darwinBundleDocumentType(['bat', 'cmd'], 'bat', 'Windows command script'),
118
darwinBundleDocumentType(['bowerrc'], 'Bower'),
119
darwinBundleDocumentType(['config', 'editorconfig', 'ini', 'cfg'], 'config', 'Configuration file'),
120
darwinBundleDocumentType(['hh', 'hpp', 'hxx', 'h++'], 'cpp', 'C++ header file'),
121
darwinBundleDocumentType(['cc', 'cpp', 'cxx', 'c++'], 'cpp', 'C++ source code'),
122
darwinBundleDocumentType(['m'], 'default', 'Objective-C source code'),
123
darwinBundleDocumentType(['mm'], 'cpp', 'Objective-C++ source code'),
124
darwinBundleDocumentType(['cs', 'csx'], 'csharp', 'C# source code'),
125
darwinBundleDocumentType(['css'], 'css', 'CSS'),
126
darwinBundleDocumentType(['go'], 'go', 'Go source code'),
127
darwinBundleDocumentType(['htm', 'html', 'xhtml'], 'HTML'),
128
darwinBundleDocumentType(['jade'], 'Jade'),
129
darwinBundleDocumentType(['jav', 'java'], 'Java'),
130
darwinBundleDocumentType(['js', 'jscsrc', 'jshintrc', 'mjs', 'cjs'], 'Javascript', 'file'),
131
darwinBundleDocumentType(['json'], 'JSON'),
132
darwinBundleDocumentType(['less'], 'Less'),
133
darwinBundleDocumentType(['markdown', 'md', 'mdoc', 'mdown', 'mdtext', 'mdtxt', 'mdwn', 'mkd', 'mkdn'], 'Markdown'),
134
darwinBundleDocumentType(['php'], 'PHP', 'source code'),
135
darwinBundleDocumentType(['ps1', 'psd1', 'psm1'], 'Powershell', 'script'),
136
darwinBundleDocumentType(['py', 'pyi'], 'Python', 'script'),
137
darwinBundleDocumentType(['gemspec', 'rb', 'erb'], 'Ruby', 'source code'),
138
darwinBundleDocumentType(['scss', 'sass'], 'SASS', 'file'),
139
darwinBundleDocumentType(['sql'], 'SQL', 'script'),
140
darwinBundleDocumentType(['ts'], 'TypeScript', 'file'),
141
darwinBundleDocumentType(['tsx', 'jsx'], 'React', 'source code'),
142
darwinBundleDocumentType(['vue'], 'Vue', 'source code'),
143
darwinBundleDocumentType(['ascx', 'csproj', 'dtd', 'plist', 'wxi', 'wxl', 'wxs', 'xml', 'xaml'], 'XML'),
144
darwinBundleDocumentType(['eyaml', 'eyml', 'yaml', 'yml'], 'YAML'),
145
darwinBundleDocumentType([
146
'bash', 'bash_login', 'bash_logout', 'bash_profile', 'bashrc',
147
'profile', 'rhistory', 'rprofile', 'sh', 'zlogin', 'zlogout',
148
'zprofile', 'zsh', 'zshenv', 'zshrc'
149
], 'Shell', 'script'),
150
// Default icon with specified names
151
...darwinBundleDocumentTypes({
152
'Clojure source code': ['clj', 'cljs', 'cljx', 'clojure'],
153
'VS Code workspace file': 'code-workspace',
154
'CoffeeScript source code': 'coffee',
155
'Comma Separated Values': 'csv',
156
'CMake script': 'cmake',
157
'Dart script': 'dart',
158
'Diff file': 'diff',
159
'Dockerfile': 'dockerfile',
160
'Gradle file': 'gradle',
161
'Groovy script': 'groovy',
162
'Makefile': ['makefile', 'mk'],
163
'Lua script': 'lua',
164
'Pug document': 'pug',
165
'Jupyter': 'ipynb',
166
'Lockfile': 'lock',
167
'Log file': 'log',
168
'Plain Text File': 'txt',
169
'Xcode project file': 'xcodeproj',
170
'Xcode workspace file': 'xcworkspace',
171
'Visual Basic script': 'vb',
172
'R source code': 'r',
173
'Rust source code': 'rs',
174
'Restructured Text document': 'rst',
175
'LaTeX document': ['tex', 'cls'],
176
'F# source code': 'fs',
177
'F# signature file': 'fsi',
178
'F# script': ['fsx', 'fsscript'],
179
'SVG document': ['svg'],
180
'TOML document': 'toml',
181
'Swift source code': 'swift',
182
}, 'default'),
183
// Default icon with default name
184
darwinBundleDocumentType([
185
'containerfile', 'ctp', 'dot', 'edn', 'handlebars', 'hbs', 'ml', 'mli',
186
'pl', 'pl6', 'pm', 'pm6', 'pod', 'pp', 'properties', 'psgi', 'rt', 't'
187
], 'default', product.nameLong + ' document'),
188
// Folder support ()
189
darwinBundleDocumentType([], 'default', 'Folder', ['public.folder'])
190
],
191
darwinBundleURLTypes: [{
192
role: 'Viewer',
193
name: product.nameLong,
194
urlSchemes: [product.urlProtocol]
195
}],
196
darwinForceDarkModeSupport: true,
197
darwinCredits: darwinCreditsTemplate ? Buffer.from(darwinCreditsTemplate({ commit: commit, date: new Date().toISOString() })) : undefined,
198
linuxExecutableName: product.applicationName,
199
winIcon: 'resources/win32/code.ico',
200
token: process.env['GITHUB_TOKEN'],
201
repo: product.electronRepository || undefined,
202
validateChecksum: true,
203
checksumFile: path.join(root, 'build', 'checksums', 'electron.txt'),
204
};
205
206
function getElectron(arch: string): () => NodeJS.ReadWriteStream {
207
return () => {
208
const electron = require('@vscode/gulp-electron');
209
const json = require('gulp-json-editor') as typeof import('gulp-json-editor');
210
211
const electronOpts = {
212
...config,
213
platform: process.platform,
214
arch: arch === 'armhf' ? 'arm' : arch,
215
ffmpegChromium: false,
216
keepDefaultApp: true
217
};
218
219
return vfs.src('package.json')
220
.pipe(json({ name: product.nameShort }))
221
.pipe(electron(electronOpts))
222
.pipe(filter(['**', '!**/app/package.json']))
223
.pipe(vfs.dest('.build/electron'));
224
};
225
}
226
227
async function main(arch: string = process.arch): Promise<void> {
228
const version = electronVersion;
229
const electronPath = path.join(root, '.build', 'electron');
230
const versionFile = path.join(electronPath, 'version');
231
const isUpToDate = fs.existsSync(versionFile) && fs.readFileSync(versionFile, 'utf8') === `${version}`;
232
233
if (!isUpToDate) {
234
await util.rimraf(electronPath)();
235
await util.streamToPromise(getElectron(arch)());
236
}
237
}
238
239
if (require.main === module) {
240
main(process.argv[2]).catch(err => {
241
console.error(err);
242
process.exit(1);
243
});
244
}
245
246