forked from gitterHQ/desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
306 lines (259 loc) · 10.8 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
/* jshint node: true */
'use strict';
var gutil = require('gulp-util');
var SUPPORTED_PLATFORMS = ['win32', 'linux32', 'linux64', 'osx64'];
var gulp = require('gulp');
var shell = require('gulp-shell');
var os = require('os');
var fs = require('fs');
var s3 = require('s3');
var path = require('path');
var rimraf = require('rimraf');
var manifest = require('./package.json');
var template = require('lodash.template');
var Builder = require('node-webkit-builder');
var appmanifest = require('./nwapp/package.json');
var LATEST_HTML_TEMPLATE = template(fs.readFileSync('./latest-template.html').toString());
var CURRENT_OS = (os.platform().match(/darwin/) ? 'osx' : os.platform());
var YEAR = new Date().getFullYear();
var CACHE_DIR = './cache';
var OUTPUT_DIR = './opt';
var SOURCE_DIR = './nwapp';
var SIGN_IDENTITY = 'Developer ID Application: Troupe Technology Limited (A86QBWJ43W)'; // To use this identity you must have the appropriate Developer ID cert in your keychain
var CERTIFICATES_DIR = './certificates';
var CERTIFICATES_FORMAT = {
'win': 'troupe-cert.pfx',
'osx': 'DeveloperID.p12'
};
var ARTEFACTS_DIR = './artefacts';
var LINUX_ARTEFACT_TEMPLATE = template('<%= name %>_<%= version %>_<%= arch %>.deb');
var ARTEFACTS = {
'win': template('<%= name %>Setup-<%= version %>.exe')({ name: appmanifest.name, version: appmanifest.version }),
'osx': template('<%= name %>-<%= version %>.dmg')({ name: appmanifest.name, version: appmanifest.version }),
'linux32': LINUX_ARTEFACT_TEMPLATE({ name: appmanifest.name.toLowerCase(), version: appmanifest.version, arch: 'i386' }),
'linux64': LINUX_ARTEFACT_TEMPLATE({ name: appmanifest.name.toLowerCase(), version: appmanifest.version, arch: 'amd64' }),
};
var ARTEFACTS_URL = Object.keys(ARTEFACTS).reduce(function (fold, item) {
fold[item] = path.join(item, ARTEFACTS[item]);
return fold;
}, {});
var S3_CONSTS = {
buckets: {
certificates: 'troupe-certs',
updates: 'update.gitter.im'
},
handleError: function (err) {
gutil.log(err.stack);
},
handleProgress: function (msg, file) {
if (!this) {
return gutil.log(gutil.colors.red('no progress report'));
}
var progress = ~~((this.progressAmount / this.progressTotal) * 100);
if (progress % 50 !== 0) return;
gutil.log(msg, gutil.colors.cyan('\'' + file + '\''), progress + '%');
}
};
var s3Client = s3.createClient({
s3Options: {
accessKeyId: S3_CONSTS.credentials && S3_CONSTS.credentials.key,
secretAccessKey: S3_CONSTS.credentials && S3_CONSTS.credentials.secret
},
});
function namespace(/* args */) {
return [].slice.call(arguments).join(':');
}
function fetchS3(params, done) {
var downloader = s3Client.downloadFile(params);
downloader.on('error', S3_CONSTS.handleError.bind(downloader));
downloader.on('progress', S3_CONSTS.handleProgress.bind(downloader, 'Downloading', params.localFile));
downloader.on('end', done);
}
function pushS3(params, done) {
var uploader = s3Client.uploadFile(params);
uploader.on('error', S3_CONSTS.handleError.bind(uploader));
uploader.on('progress', S3_CONSTS.handleProgress.bind(uploader, 'Uploading', params.localFile));
uploader.on('end', done);
}
[OUTPUT_DIR, ARTEFACTS_DIR, CACHE_DIR].forEach(function (dir) {
gulp.task('clean:' + path.basename(dir).toLowerCase(), function (done) {
rimraf(dir, done);
});
});
gulp.task('clean', [OUTPUT_DIR, ARTEFACTS_DIR, CACHE_DIR].map(function (dir) { return 'clean:' + path.basename(dir); }));
/* certificate:fetch:{{ OS }} */
Object.keys(CERTIFICATES_FORMAT).forEach(function (OS) {
var file = CERTIFICATES_FORMAT[OS];
gulp.task('cert:fetch:' + OS, fetchS3.bind(null, {
localFile: path.join(CERTIFICATES_DIR, file),
s3Params: {
Bucket: S3_CONSTS.buckets.certificates,
Key: file,
},
}));
});
/* fetches the current os certificate */
gulp.task('cert:fetch', ['cert:fetch:' + CURRENT_OS]);
gulp.task('build', ['clean:opt', 'clean:artefacts'], function (done) {
fs.mkdirSync(ARTEFACTS_DIR);
var builder = new Builder({
buildDir: OUTPUT_DIR,
version: manifest.nwversion,
files: [path.join(SOURCE_DIR, '**')],
platforms: SUPPORTED_PLATFORMS,
winIco: './icons/gitter.ico',
macIcns: './icons/AppIcon.icns',
macPlist: {
NSHumanReadableCopyright: 'Copyright © 2013-' + YEAR + ' Troupe Technology Limited. All rights reserved.',
CFBundleIdentifier: 'com.gitter.desktop'
}
});
builder.on('log', gutil.log.bind(gutil, 'nw-builder:'));
builder.build().nodeify(done);
});
gulp.task('build:linux', ['pack:deb:32', 'pack:deb:64']);
gulp.task('build:osx', ['pack:osx']);
gulp.task('build:all', ['build:linux', 'build:osx']);
gulp.task('run', function (done){
var builder = new Builder({
version: manifest.nwversion,
files: path.join(SOURCE_DIR, '**')
});
builder.on('log', gutil.log.bind(gutil, 'nw-builder:'));
builder.run().nodeify(done);
});
// generated Debian/Ubuntu packages for 32 and 64 bit archs
[32, 64].forEach(function (arch) {
var fpm = template('fpm -s dir -t deb -a <%= port %> -n <%= name %> --category Utility --after-install ./opt/Gitter/linux<%= arch %>/after-install.sh --after-remove ./opt/Gitter/linux<%= arch %>/after-remove.sh --url "https://gitter.im" --description "Where developers come to talk" --maintainer "Troupe Technology <[email protected]>" -p <%= output %> -v <%= version %> ./opt/Gitter/linux<%= arch %>/');
var catsed = gutil.template('cat ./linux/<%= file %> | sed "s/{{arch}}/linux<%= arch %>/g" > ./opt/Gitter/linux<%= arch %>/<%= file %>');
gulp.task('pack:templates:' + arch, ['build'], shell.task([ // this probably waits for build
catsed({ arch: arch, file: 'gitter.desktop' }) ,
catsed({ arch: arch, file: 'after-install.sh' }),
catsed({ arch: arch, file: 'after-remove.sh' }),
'cp ./icons/logo.png ./opt/Gitter/linux' + arch + '/logo.png'
]));
gulp.task('pack:deb:' + arch, ['pack:templates:' + arch], shell.task(
fpm({
arch: arch,
port: arch === 32 ? 'i386' : 'amd64',
version: appmanifest.version,
name: appmanifest.name.toLowerCase(),
output: path.join(ARTEFACTS_DIR, ARTEFACTS['linux' + arch])
})
));
});
var dmg_cmd = template('./osx/create-dmg/create-dmg --icon "<%= name %>" 311 50 --icon-size 32 --app-drop-link 311 364 --window-size 622 683 --volname "<%= name %>" --volicon "./icons/AppIcon.icns" --background "./icons/dmg-bg.png" "<%= output %>" "<%= path %>/Gitter/osx64/Gitter.app"');
// Only runs on OSX (requires XCode properly configured)
gulp.task('sign:osx', ['build'], shell.task([
'codesign -v -f -s "'+ SIGN_IDENTITY +'" '+ OUTPUT_DIR +'/Gitter/osx64/Gitter.app/Contents/Frameworks/*',
'codesign -v -f -s "'+ SIGN_IDENTITY +'" '+ OUTPUT_DIR +'/Gitter/osx64/Gitter.app',
'codesign -v --display '+ OUTPUT_DIR +'/Gitter/osx64/Gitter.app',
'codesign -v --verify '+ OUTPUT_DIR +'/Gitter/osx64/Gitter.app',
]));
// Only runs on OSX
if (ARTEFACTS.osx) {
gulp.task('pack:osx', ['sign:osx'], shell.task(dmg_cmd({
name: appmanifest.name,
version: appmanifest.version,
path: OUTPUT_DIR,
output: path.join(ARTEFACTS_DIR, ARTEFACTS.osx)
})));
}
// Generate auto-updater packages for Windows
gulp.task('autoupdate:zip:win', shell.task([
'cd '+ OUTPUT_DIR + '/Gitter/win32; zip win32.zip ./* > /dev/null 2>&1',
'mv '+ OUTPUT_DIR + '/Gitter/win32/win32.zip ' + ARTEFACTS_DIR + '/win32.zip',
]));
// Generate auto-updater packages for OSX
gulp.task('autoupdate:zip:osx', shell.task([
'cd '+ OUTPUT_DIR + '/Gitter/osx64; zip -r osx.zip ./Gitter.app > /dev/null 2>&1',
'mv '+ OUTPUT_DIR + '/Gitter/osx64/osx.zip '+ ARTEFACTS_DIR + '/osx.zip',
]));
gulp.task('autoupdate:push:osx', function (done) {
pushS3({
localFile: ARTEFACTS_DIR + '/osx.zip',
s3Params: {
Bucket: S3_CONSTS.buckets.updates,
Key: 'osx/osx.zip',
CacheControl: 'public, max-age=0, no-cache',
ACL: 'public-read'
}
}, done);
});
gulp.task('autoupdate:push:win', function (done) {
pushS3({
localFile: ARTEFACTS_DIR + '/win32.zip',
s3Params: {
Bucket: S3_CONSTS.buckets.updates,
Key: 'win/win32.zip',
CacheControl: 'public, max-age=0, no-cache',
ACL: 'public-read'
}
}, done);
});
gulp.task('manifest:push', function (done) {
pushS3({
localFile: SOURCE_DIR + '/package.json',
s3Params: {
Bucket: S3_CONSTS.buckets.updates,
Key: 'desktop/package.json',
CacheControl: 'public, max-age=0, no-cache',
ACL: 'public-read'
}
}, done);
});
gulp.task('identity', function (done) {
done();
});
Object.keys(ARTEFACTS).forEach(function (platform) {
console.log(platform);
var LATEST_TEMPLATE = template('latest_<%= platform %>.html');
gulp.task(namespace('artefacts', 'push', platform), function (done) {
pushS3({
localFile: path.join(ARTEFACTS_DIR, ARTEFACTS[platform]),
s3Params: {
Bucket: S3_CONSTS.buckets.updates,
Key: path.join(platform, ARTEFACTS[platform]),
CacheControl: 'public, max-age=0, no-cache',
ACL: 'public-read'
}
}, done);
});
// LATEST_HTML_TEMPLATE
gulp.task(namespace('redirect', 'source', platform), function (done) {
fs.writeFile(path.join(ARTEFACTS_DIR, LATEST_TEMPLATE({ platform: platform })), LATEST_HTML_TEMPLATE({ url: ARTEFACTS_URL[platform] }), done);
});
gulp.task(namespace('redirect', 'push', platform), function (done) {
pushS3({
localFile: path.join(ARTEFACTS_DIR, LATEST_TEMPLATE({ platform: platform })),
s3Params: {
Bucket: S3_CONSTS.buckets.updates,
Key: path.join(platform, 'latest'),
CacheControl: 'public, max-age=0, no-cache',
ACL: 'public-read'
}
}, done);
});
});
gulp.task('artefacts:push', Object.keys(ARTEFACTS).map(function (platform) { return namespace('artefacts', 'push', platform); }));
gulp.task('redirect:source', Object.keys(ARTEFACTS).map(function (platform) { return namespace('redirect', 'source', platform); }));
gulp.task('redirect:push', Object.keys(ARTEFACTS).map(function (platform) { return namespace('redirect', 'push', platform); }));
// gulp.task('build:win', ['build'], function (done) {
// // shell.task([
// // s3cmd+'./'+ OUTPUT_DIR +'/Gitter/'+winInstaller+' '+s3bucket+'/win/',
// // s3cmd+'./'+ OUTPUT_DIR +'/Gitter/win32/win32.zip '+s3bucket+'/win/',
// // s3cmd+'--content-type "text/html" ./latest '+s3bucket+'/win/',
// // s3cmd+'./nwapp/package.json '+s3bucket+'/desktop/'
// // ]));
// // var winInstaller = "GitterSetup-"+appmanifest.version+".exe";
// // var winDownloadUrl = "https://update.gitter.im/win/" + winInstaller;
// // var uploader = s3Client.uploadFile({
// // localFile: 'README.md',
// // s3Params: {
// // Bucket: S3_CONSTS.buckets.updates,
// // Key: 'nwdev/README.md',
// // CacheControl: 'public, max-age=0, no-cache',
// // ACL: 'public-read'
// // },
// // });
// });