forked from Jigsaw-Code/outline-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
259 lines (237 loc) · 9.23 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
// Copyright 2018 The Outline Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict';
const browserify = require('browserify');
const babel = require('gulp-babel');
const babelify = require('babelify');
const babel_preset_env = require('babel-preset-env');
const child_process = require('child_process');
const fs = require('fs');
const generateRtlCss = require('./scripts/generate_rtl_css.js');
const gulp = require('gulp');
const gulpif = require('gulp-if');
const gutil = require('gulp-util');
const merge_stream = require('merge-stream');
const polymer_build = require('polymer-build');
const source = require('vinyl-source-stream');
const watchify = require('watchify');
const SRC_DIR = 'www';
const CONFIG_BY_PLATFORM = {
android: {
targetDir: `platforms/android/app/src/main/assets/${SRC_DIR}`,
platformArgs: '--gradleArg=-PcdvBuildMultipleApks=true'
},
browser: {targetDir: `platforms/browser/${SRC_DIR}`},
ios: {targetDir: `platforms/ios/${SRC_DIR}`, compileArgs: '--device'},
osx: {targetDir: `platforms/osx/${SRC_DIR}`}
};
function getConfigByPlatform(platform) {
const config = CONFIG_BY_PLATFORM[platform];
if (!config) {
throw new Error(`Unexpected platform: ${platform}`);
}
return config;
}
// Transpiles to |src| to ES5, copying the output to |dest|.
function transpile(src, dest) {
const sourcesHtmlSplitter = new polymer_build.HtmlSplitter();
return gulp.src(src)
.pipe(sourcesHtmlSplitter.split())
.pipe(gulpif(/\.js$/,
babel({
presets: [babel_preset_env.default()]
})))
.pipe(sourcesHtmlSplitter.rejoin())
.pipe(gulp.dest(dest));
}
function transpileBowerComponents(config){
gutil.log('Transpiling Bower components');
// Transpile bower_components with the exception of webcomponentsjs, which contains transpiled
// polyfills, and minified files, which are generally already transpiled.
const bowerComponentsSrc = [
'www/bower_components/**/*.html',
'www/bower_components/**/*.js',
'!www/bower_components/webcomponentsjs/**/*.js',
'!www/bower_components/webcomponentsjs/**/*.html',
'!www/bower_components/**/*.min.js'
];
const bowerComponentsDest = `${config.targetDir}/bower_components`;
return transpile(bowerComponentsSrc, bowerComponentsDest);
}
function transpileUiComponents(config) {
gutil.log('Transpiling UI components');
return transpile(['www/ui_components/*.html'], `${config.targetDir}/ui_components`);
}
// See https://www.typescriptlang.org/docs/handbook/gulp.html
function getBrowserifyInstance() {
return browserify({
basedir: '.',
debug: true,
entries: [`${SRC_DIR}/app/cordova_main.js`],
cache: {},
packageCache: {}
});
}
function bundleJs(browserifyInstance) {
gutil.log('Running browserify');
const notWatch = !gutil.env.watch;
const log = gutil.log.bind(gutil, 'Browserify Error:');
const bundle = browserifyInstance.bundle();
bundle.once('error', function (...args) {
log(...args);
if (notWatch) {
return process.exit(1);
}
});
return bundle.pipe(source('cordova_main.js'));
}
// TODO: watch for changes in .html files too, not just .ts files
function watch(browserifyInstance, config) {
const log = gutil.log.bind(gutil, 'Browserify:');
const watchified = watchify(browserifyInstance);
watchified.on('log', log);
watchified.on('update', function (deps) {
gutil.log(`The following dependencies were modified, rebuilding...
${deps}`);
bundleJs(watchified).pipe(gulp.dest(config.targetDir));
gutil.log('Rebuilt');
});
return browserifyInstance;
}
function runCommand(command, options, done) {
gutil.log(`Running ${command}`);
let child = child_process.exec(command, options, function(err, stdout, stderr) {
if (done) { done(err); }
});
child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);
}
// Copies Babel polyfill from node_modules, as it needs to be included by cordova_index.html.
function copyBabelPolyfill(config) {
const babelPolyfill = 'node_modules/babel-polyfill/dist/polyfill.min.js';
runCommand(`cp -v ${babelPolyfill} ${config.targetDir}/babel-polyfill.min.js`, {}, function () {
gutil.log(`Copied Babel polyfill.`);
});
}
// Writes a JSON file accessible to environment.ts containing environment variables.
function writeEnvJson(platform, config, isRelease) {
const platformPath = `platforms/${platform}`;
if (!fs.existsSync(platformPath)) {
throw new Error(`Failed to set up environment, no such path: ${platformPath}`);
}
if (!fs.existsSync(config.targetDir)) {
throw new Error(`Failed to set up environment, no such path: ${config.targetDir}`);
}
const envFile = `${config.targetDir}/environment.json`;
let envScript = 'scripts/environment_json.sh';
if (process.platform.includes('win')) {
envScript = `sh ${envScript}`;
}
runCommand(`${envScript} -p ${platform} ${isRelease ? '-r' : ''} > ${envFile}`, {}, function() {
gutil.log(`Wrote ${envFile}`);
});
}
// Expected environment variables when the release flag is set.
const RELEASE_ENVIRONMENT_KEYS = {
KEYALIAS: 'KEYALIAS',
KEYPASS: 'KEYPASS',
KEYSTORE: 'KEYSTORE',
STOREPASS: 'STOREPASS'
};
// Retrieves the environment variables passed as arguments to gulp.
// Validates that the expected variables exist on the given environment.
function getReleaseEnvironmentVariables(platform) {
let envVars = {};
envVars.RELEASE = gutil.env.release;
if (platform === 'android' && envVars.RELEASE) {
for (const envKey in RELEASE_ENVIRONMENT_KEYS) {
const envVar = gutil.env[envKey];
if (!envVar) {
throw new Error(`Missing environment variable ${envKey}`);
}
envVars[envKey] = envVar;
}
}
return envVars;
}
function getReleaseCompileArgs(platform, envVars) {
if (platform !== 'android') {
return '--release';
}
return `--release -- --keystore=${envVars.KEYSTORE} ` +
`--storePassword=${envVars.STOREPASS} --alias=${envVars.KEYALIAS} ` +
`--password=${envVars.KEYPASS}`;
}
const DEFAULT_PLATFORM = 'android';
gulp.task('build', function () {
const platform = gutil.env.platform || DEFAULT_PLATFORM;
const config = getConfigByPlatform(platform);
gutil.log(`Building for platform ${platform}...`);
build(platform, config);
});
function build(platform, config) {
const envVars = getReleaseEnvironmentVariables(platform);
const shouldWatch = !!gutil.env.watch;
let browserifyInstance = getBrowserifyInstance().transform("babelify", {
global: true, // Transpile required node modules
presets: ['env']
});
if (shouldWatch) {
browserifyInstance = watch(browserifyInstance, config);
}
// Build the web app.
child_process.execSync('yarn do src/www/build', {stdio: 'inherit'});
return merge_stream(
bundleJs(browserifyInstance).pipe(gulp.dest(SRC_DIR))
).on('finish', () => {
// cordova-custom-config isn't be invoked as part of "cordova prepare"
// unless, beforehand, the platform has been added.
runCommand(`test -d platforms/${platform} || cordova platform add ${platform}`, {}, function () {
// cordova build == cordova prepare + cordova compile
runCommand(`cordova prepare ${platform}`, {}, function () {
copyBabelPolyfill(config);
writeEnvJson(platform, config, envVars.RELEASE);
transpileBowerComponents(config).on('finish', function() {
transpileUiComponents(config).on('finish', function() {
generateRtlCss(
`${config.targetDir}/ui_components/*.html`, `${config.targetDir}/ui_components`)
.on('finish', function() {
const compileArgs = config.compileArgs || '';
const releaseArgs = envVars.RELEASE ? getReleaseCompileArgs(platform, envVars) : '';
const platformArgs = config.platformArgs || '';
// Do this now, otherwise "cordova compile" fails.
// TODO: use some gulp plugin
// -c means "use file's checksum, not last modified time"
const syncXcode = platform === 'ios' || platform === 'osx' ?
`rsync -avzc apple/xcode/${platform}/ platforms/${platform}/` :
':';
runCommand(syncXcode, {}, () => {
runCommand(
`cordova compile ${platform} ${compileArgs} ${releaseArgs} -- ${platformArgs}`,
{}, function() {
if (shouldWatch) {
gutil.log('Running...');
runCommand(`cordova run ${platform} --noprepare --nobuild`);
} else {
gutil.log('Done');
}
});
});
});
});
});
});
});
});
}