-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
154 lines (127 loc) · 5.38 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
'use strict';
/******************************************************************************/
var gulp = require('gulp');
var path = require('path');
var del = require('del');
var fs = require('fs');
var runSeq = require('run-sequence');
var pp = require('cordova-platform-project');
/******************************************************************************/
var platform = 'android'
// One of many options to work with several platforms
var nopt = require('nopt');
var args = nopt({'ios': Boolean, 'android': Boolean});
if (args.ios)
platform = 'ios'
if (args.android)
platform = 'android'
// Collect all the info for the cordova build of this app
var prjInfo = {};
prjInfo.platform = platform;
prjInfo.paths = {};
// Where all your html/css/js files live.
prjInfo.paths.www = path.resolve('www');
// Where cordova_android project will be created
prjInfo.paths.root = path.resolve('build', platform);
// where the cordova-android package lives. In most cases it will be
// downloaded to node_modules by npm But this way it's easy to use your own
// versions of platform templates
prjInfo.paths.template = path.resolve('node_modules', 'cordova-' + platform);
// A list of directories that are plugins or contain plugins.
prjInfo.paths.plugins = [path.resolve('node_modules')];
// Path relative to which icons and splashscreens are specified in config.xml
// See https://cordova.apache.org/docs/en/4.0.0/config_ref_images.md.html
// prjInfo.paths.icons = path.resolve('icons');
// Cordova's ConfigParser object represents the config.xml file The file is
// never read again, so you your manipulations of ConfigParser are carried on
// and saved inside the platform project dir under build.
prjInfo.cfg = new pp.cdv.ConfigParser('config.xml');
/******************************************************************************/
gulp.task('clean', function(cb) {
del(['build'], cb);
});
/******************************************************************************/
// Create a platform project as a build artifact under ./build/<platform>/
// This is the short version, see the full one below
gulp.task('create', ['clean'], function() {
fs.mkdirSync('build');
var proj = new pp.PlatformProject();
return proj.create(prjInfo);
});
/******************************************************************************/
// Create a platform project as a build artifact under ./build/<platform>/
// Full version. Only needed if you went to do some hackish things between the
// stages. Can't handle icons for now.
gulp.task('longcreate', ['clean'], function() {
// TODO: only remove current platform build dir instead of full clean.
fs.mkdirSync('build');
var proj = new pp.PlatformProject();
// This is a list of PluginInfo objects
var plugins = proj.loadPlugins(path.resolve('./node_modules'));
//// Here is how to add a single plugin (with variables) from some other place
// var myPlugin = new pp.cdv.PluginInfo(path.resolve('./my_dev_plugin'))
// myPlugin.vars = {SOME_VAR: 'some_value'};
// plugins.push(myPlugin);
return proj.init(prjInfo)
.then(function(){
// addPlugins() can be called more than once.
return proj.addPlugins(plugins);
}).then(function(){
// This updates config files based on config.xml etc.
return proj.updateConfig(prjInfo.cfg);
}).then(function(){
// Copy www assets. This nneds to be called at least once to take care
// of cordova.js and some plugin assets.
return proj.copyWww(prjInfo.paths.www);
});
// At this stage you can run the platform build script as
// ./build/<platform>/cordova/build
});
/******************************************************************************/
// This will update the www assets and build the app
gulp.task('build', [], function() {
var proj = new pp.PlatformProject();
return proj.open(platform, prjInfo.paths.root)
.then(function() {
// Can be omitted or called after copyWww().
return proj.updateConfig(prjInfo.cfg);
})
.then(function() {
return proj.copyWww(prjInfo.paths.www);
// Alternatively you can copy the www files yourself to proj.wwwDir
// calling copyWww() at least once during create is still required
// to take care of cordova.js and plugin js scripts
})
.then(function() {
return proj.build();
});
});
/******************************************************************************/
// Run the app on device
gulp.task('run', function() {
var proj = new pp.PlatformProject();
return proj.open(platform, prjInfo.paths.root)
.then(function() {
var opts = {args: ['--device', '--nobuild']}
return proj.run(opts);
});
});
/******************************************************************************/
// Run in emulator
gulp.task('emulate', function() {
var proj = new pp.PlatformProject();
return proj.open(platform, prjInfo.paths.root)
.then(function() {
var opts = {args: ['--emulator', '--nobuild']}
return proj.run(opts);
});
});
/******************************************************************************/
// The default task for convenience
gulp.task('default', function() {
// I happen to have an Android device connected right now but not iOS.
if (platform =='android')
runSeq('build', 'run');
else
runSeq('build', 'emulate');
});