This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
gulpfile.js
403 lines (335 loc) · 13.6 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
require('dotenv').config({silent: true});
const _ = require('underscore-plus');
const gulp = require('gulp');
const gutil = require('gulp-util');
const shell = require('shelljs');
const Client = require('ssh2').Client;
const fs = require('fs');
const os = require('os');
const path = require('path');
const decompress = require('decompress');
const request = require('request');
const del = require('del');
const runSequence = require('run-sequence');
const cp = require('./utils/child-process-wrapper');
const pkg = require('./package.json')
var buildBeta;
var buildDir = path.join(__dirname, 'build')
console.log('build directory', buildDir)
function productName() {
var name = 'Learn IDE';
if (buildBeta) {
name += ' Beta';
}
return name;
}
function executableName() {
var name = productName().toLowerCase();
return name.replace(/ /g, '_');
}
function windowsInstallerName() {
return productName().replace(/ /g, '') + 'Setup.exe';
}
gulp.task('setup', function() {
shell.cp('./.env.example', './.env');
});
gulp.task('download-atom', function(done) {
var tarballURL = `https://github.com/atom/atom/archive/v${ pkg.atomVersion }.tar.gz`
console.log(`Downloading Atom from ${ tarballURL }`)
var tarballPath = path.join(buildDir, 'atom.tar.gz')
var r = request(tarballURL)
r.on('end', function() {
decompress(tarballPath, buildDir, {strip: 1}).then(function(files) {
fs.unlinkSync(tarballPath)
done()
}).catch(function(err) {
console.error(err)
})
})
r.pipe(fs.createWriteStream(tarballPath))
})
gulp.task('build-atom', function(done) {
process.chdir(buildDir)
var cmd = path.join(buildDir, 'script', 'build')
var args = []
switch (process.platform) {
case 'win32':
args.push('--create-windows-installer');
break;
case 'darwin':
args.push('--compress-artifacts');
args.push('--code-sign');
break;
case 'linux':
args.push('--create-rpm-package');
args.push('--create-debian-package');
break;
}
if (process.platform == 'win32') {
args = ['/s', '/c', cmd].concat(args);
cmd = 'cmd';
}
console.log('running command: ' + cmd + ' ' + args.join(' '))
cp.safeSpawn(cmd, args, function() {
done()
})
})
gulp.task('reset', function() {
del.sync(['build/**/*', '!build/.gitkeep'], {dot: true})
})
gulp.task('sleep', function(done) {
setTimeout(function() { done() }, 1000 * 60)
})
gulp.task('inject-packages', function() {
function rmPackage(name) {
var packageJSON = path.join(buildDir, 'package.json')
var packages = JSON.parse(fs.readFileSync(packageJSON))
delete packages.packageDependencies[name]
fs.writeFileSync(packageJSON, JSON.stringify(packages, null, ' '))
}
function injectPackage(name, version) {
var packageJSON = path.join(buildDir, 'package.json')
var packages = JSON.parse(fs.readFileSync(packageJSON))
packages.packageDependencies[name] = version
fs.writeFileSync(packageJSON, JSON.stringify(packages, null, ' '))
}
var pkg = require('./package.json')
rmPackage('welcome')
rmPackage('tree-view')
rmPackage('about')
rmPackage('notifications')
injectPackage(pkg.name, pkg.version)
_.each(pkg.packageDependencies, (version, name) => {
injectPackage(name, version)
})
})
gulp.task('replace-files', function() {
var iconSrc = path.join('resources', 'app-icons', '**', '*');
var iconDest = path.join(buildDir, 'resources', 'app-icons', 'stable')
gulp.src([iconSrc]).pipe(gulp.dest(iconDest));
var winSrc = path.join('resources', 'win', '**', '*');
var winDest = path.join(buildDir, 'resources', 'win');
gulp.src([winSrc]).pipe(gulp.dest(winDest));
var scriptSrc = path.join('resources', 'script-replacements', '**', '*');
var scriptDest = path.join(buildDir, 'script', 'lib')
gulp.src([scriptSrc]).pipe(gulp.dest(scriptDest));
})
gulp.task('alter-files', function() {
function replaceInFile(filepath, replaceArgs) {
var data = fs.readFileSync(filepath, 'utf8');
replaceArgs.forEach(function(args) {
data = data.replace(args[0], args[1]);
});
fs.writeFileSync(filepath, data)
}
replaceInFile(path.join(buildDir, 'script', 'lib', 'create-windows-installer.js'), [
[
'https://raw.githubusercontent.com/atom/atom/master/resources/app-icons/${CONFIG.channel}/atom.ico',
'https://raw.githubusercontent.com/learn-co/learn-ide/master/resources/app-icons/atom.ico'
]
])
replaceInFile(path.join(buildDir, 'script', 'lib', 'create-rpm-package.js'), [
['atom.${generatedArch}.rpm', executableName() + '.${generatedArch}.rpm'],
[/'Atom Beta' : 'Atom'/g, "'" + productName() + "' : '" + productName() + "'"]
]);
replaceInFile(path.join(buildDir, 'script', 'lib', 'create-debian-package.js'), [
['atom-${arch}.deb', executableName() + '-${arch}.deb'],
[/'Atom Beta' : 'Atom'/g, "'" + productName() + "' : '" + productName() + "'"]
]);
replaceInFile(path.join(buildDir, 'script', 'lib', 'package-application.js'), [
[/'Atom Beta' : 'Atom'/g, "'" + productName() + "' : '" + productName() + "'"]
]);
replaceInFile(path.join(buildDir, 'script', 'lib', 'package-application.js'), [
[/'Atom'/g, `'${productName()}'`]
]);
if (process.platform != 'linux') {
replaceInFile(path.join(buildDir, 'script', 'lib', 'package-application.js'), [
[/return 'atom'/, "return '" + executableName() + "'"],
[/'atom-beta' : 'atom'/g, "'" + executableName() + "' : '" + executableName() + "'"]
]);
}
replaceInFile(path.join(buildDir, 'script', 'lib', 'compress-artifacts.js'), [
[/atom-/g, executableName() + '-']
]);
replaceInFile(path.join(buildDir, 'src', 'main-process', 'atom-application.coffee'), [
[
/options.socketPath = "\\\\\\\\.\\\\pipe\\\\atom-#{options.version}-#{userNameSafe}-#{process.arch}-sock"/,
'options.socketPath = "\\\\\\\\.\\\\pipe\\\\' + executableName() + '-#{options.version}-#{userNameSafe}-#{process.arch}-sock"'
],
[
'options.socketPath = path.join(os.tmpdir(), "atom-#{options.version}-#{process.env.USER}.sock")',
'options.socketPath = path.join(os.tmpdir(), "' + executableName() + '-#{options.version}-#{process.env.USER}.sock")'
]
]);
replaceInFile(path.join(buildDir, 'resources', 'mac', 'atom-Info.plist'), [
[
/(CFBundleURLSchemes.+\n.+\n.+)(atom)(.+)/,
'$1learn-ide$3'
]
]);
replaceInFile(path.join(buildDir, 'src', 'main-process', 'atom-protocol-handler.coffee'), [
[
/(registerFileProtocol.+)(atom)(.+)/,
'$1learn-ide$3'
]
]);
replaceInFile(path.join(buildDir, 'src', 'main-process', 'parse-command-line.js'), [
[
/(urlsToOpen.+)/,
"$1\n if (args['url-to-open']) { urlsToOpen.push(args['url-to-open']) }\n"
],
[
/(const args)/,
"options.string('url-to-open')\n $1"
]
]);
replaceInFile(path.join(buildDir, 'menus', 'darwin.cson'), [
[
"{ label: 'Check for Update', command: 'application:check-for-update', visible: false}",
"{ label: 'Check for Update', command: 'learn-ide:update-check'}"
],
[
"{ label: 'VERSION', enabled: false }\n { label: 'Restart and Install Update', command: 'application:install-update', visible: false}",
"{ label: 'View Version', command: 'learn-ide:view-version'}"
],
[/About Atom/, 'About'],
[/application:about/, 'learn-ide:about'],
[/application:open-faq/, 'learn-ide:faq'],
[/application:report-issue/, 'learn-ide:report-issue'],
["\n { label: 'Search Issues', command: 'application:search-issues' }", ""],
]);
replaceInFile(path.join(buildDir, 'menus', 'win32.cson'), [
[
"{ label: 'Check for Update', command: 'application:check-for-update', visible: false}",
"{ label: 'Check for Update', command: 'learn-ide:update-check'}"
],
[
"{ label: 'VERSION', enabled: false }\n { label: 'Restart and Install Update', command: 'application:install-update', visible: false}",
"{ label: 'View Version', command: 'learn-ide:view-version'}"
],
[
"\n { label: 'Checking for Update', enabled: false, visible: false}\n { label: 'Downloading Update', enabled: false, visible: false}",
''
],
[/About Atom/, 'About'],
[/application:about/, 'learn-ide:about'],
[/application:open-faq/, 'learn-ide:faq'],
[/application:report-issue/, 'learn-ide:report-issue'],
["\n { label: 'Search Issues', command: 'application:search-issues' }", ""],
]);
replaceInFile(path.join(buildDir, 'menus', 'linux.cson'), [
[/About Atom/, 'About'],
[
'{ label: "VERSION", enabled: false }',
"{ label: 'View Version', command: 'learn-ide:view-version'}"
],
[/application:about/, 'learn-ide:about'],
[/application:open-faq/, 'learn-ide:faq'],
[/application:report-issue/, 'learn-ide:report-issue'],
["\n { label: 'Search Issues', command: 'application:search-issues' }", ""],
]);
replaceInFile(path.join(buildDir, 'src', 'config-schema.js'), [
[
"automaticallyUpdate: {\n description: 'Automatically update Atom when a new release is available.',\n type: 'boolean',\n default: true\n }",
"automaticallyUpdate: {\n description: 'Automatically update Atom when a new release is available.',\n type: 'boolean',\n default: false\n }",
],
[
"openEmptyEditorOnStart: {\n description: 'When checked opens an untitled editor when loading a blank environment (such as with _File > New Window_ or when \"Restore Previous Windows On Start\" is unchecked); otherwise no editor is opened when loading a blank environment. This setting has no effect when restoring a previous state.',\n type: 'boolean',\n default: true",
"openEmptyEditorOnStart: {\n description: 'When checked opens an untitled editor when loading a blank environment (such as with _File > New Window_ or when \"Restore Previous Windows On Start\" is unchecked); otherwise no editor is opened when loading a blank environment. This setting has no effect when restoring a previous state.',\n type: 'boolean',\n default: false"
],
[
"restorePreviousWindowsOnStart: {\n description: 'When checked restores the last state of all Atom windows when started from the icon or `atom` by itself from the command line; otherwise a blank environment is loaded.',\n type: 'boolean',\n default: true",
"restorePreviousWindowsOnStart: {\n description: 'When checked restores the last state of all Atom windows when started from the icon or `atom` by itself from the command line; otherwise a blank environment is loaded.',\n type: 'boolean',\n default: false"
],
[
"['one-dark-ui', 'one-dark-syntax']", "['learn-ide-material-ui', 'atom-material-syntax']"
]
]);
})
gulp.task('update-package-json', function() {
var packageJSON = path.join(buildDir, 'package.json')
var atomPkg = JSON.parse(fs.readFileSync(packageJSON))
var learnPkg = require('./package.json')
atomPkg.name = executableName()
atomPkg.productName = productName()
atomPkg.version = learnPkg.version
atomPkg.description = learnPkg.description
fs.writeFileSync(packageJSON, JSON.stringify(atomPkg, null, ' '))
})
gulp.task('rename-installer', function(done) {
var src = path.join(buildDir, 'out', productName() + 'Setup.exe');
var des = path.join(buildDir, 'out', windowsInstallerName());
fs.rename(src, des, function (err) {
if (err) {
console.log('error while renaming: ', err.message)
}
done()
})
})
gulp.task('sign-installer', function() {
var certPath = process.env.FLATIRON_P12KEY_PATH;
var password = process.env.FLATIRON_P12KEY_PASSWORD;
if (!certPath || !password) {
console.log('unable to sign installer, must provide FLATIRON_P12KEY_PATH and FLATIRON_P12KEY_PASSWORD environment variables')
return
}
var cmd = path.join(buildDir, 'script', 'node_modules', 'electron-winstaller', 'vendor', 'signtool.exe')
var installer = path.join(buildDir, 'out', windowsInstallerName());
args = ['sign', '/a', '/f', certPath, '/p', "'" + password + "'", installer]
console.log('running command: ' + cmd + ' ' + args.join(' '))
cp.safeSpawn(cmd, args, function() {
done()
})
})
gulp.task('cleanup', function(done) {
switch (process.platform) {
case 'win32':
runSequence('rename-installer', 'sign-installer', done)
break;
case 'darwin':
done()
break;
case 'linux':
done()
break;
}
})
gulp.task('prep-build', function(done) {
runSequence(
'inject-packages',
'replace-files',
'alter-files',
'update-package-json',
done
)
})
gulp.task('build', function(done) {
var pkg = require('./package.json')
if (pkg.version.match(/beta/)) { buildBeta = true }
runSequence(
'reset',
'download-atom',
'prep-build',
'build-atom',
'cleanup',
done
)
})
gulp.task('mastermind', function(done) {
// update package.json
var pkg = require('./package.json')
pkg.name = 'mastermind'
pkg.description = 'The Learn IDE\'s evil twin that we use for testing'
pkg.packageDependencies['mirage'] = 'learn-co/mirage#master'
pkg.repository = pkg.repository.replace('learn-ide', 'mastermind')
delete pkg.packageDependencies['learn-ide-tree']
fs.writeFileSync('./package.json', JSON.stringify(pkg, null, ' '))
// update gulpfile
var gf = fs.readFileSync('./gulpfile.js', 'utf-8')
var updated = gf.replace('Learn IDE', 'Mastermind IDE')
fs.writeFileSync('./gulpfile.js', updated)
// update menus
var menu = fs.readFileSync('./menus/learn-ide.cson', 'utf-8')
var updated = menu.replace(/Learn IDE/g, 'Mastermind')
fs.writeFileSync('./menus/learn-ide.cson', updated)
})