Last active
August 23, 2017 14:48
-
-
Save dgg/9ae1b698c42043bac0f6 to your computer and use it in GitHub Desktop.
automating-phonegap-builds-with-gulp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gulp.task('build', gulpsync.sync(['unlock', 'compress']), function () { | |
var endpoint = '/apps/' + config.phoneGap.appId; | |
var env = config.ensure.environment(argv.env, argv.debugmode); | |
pgBuild.auth({ token: config.phoneGap.authToken }, function (e, api) { | |
gulp.src('tmp/*.zip').pipe(tap(function (file, t) { | |
var options = { | |
form: { | |
data: { | |
debug: config.phoneGap.debug[env], | |
keys: config.phoneGap.keys[env] | |
}, | |
file: file.path | |
} | |
}; | |
api.put(endpoint, options, buildCallBack); | |
return t; | |
})); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> gulp build --env=stage | |
> gulp deploy --env=stage --platform=ios |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gulp.task('compress', gulpsync.sync(['clean', 'mirror', 'config', 'services']), function () { | |
var env = config.ensure.environment(argv.env, argv.debugmode); | |
var timestamp = moment().format('YYYYMMDDhhmmss'); | |
var archiveName = config.cordova[env].appNamespace + '_' + timestamp + '.zip'; | |
return gulp.src(['tmp/www/**', 'tmp/res/**'], { base: 'tmp' }) | |
.pipe(zip(archiveName)) | |
.pipe(gulp.dest('tmp')); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gulp.task('config', function (done) { | |
var env = config.ensure.environment(argv.env, argv.debugmode); | |
var timestamp = moment().format('YYYYMMDDhhmmss'); | |
gulp.src(['config.xml']) | |
.pipe(replace('%APP_NAMESPACE%', config.cordova[env].appNamespace)) | |
.pipe(replace('%APP_VERSION%', config.appVersion(timestamp))) | |
.pipe(replace('%APP_BUILD%', config.build)) | |
.pipe(gulp.dest('tmp/www')) | |
.on('end', done); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// If you add new require/dependencies to this file, be sure that you have installed the node package etc. on our build server first. | |
// Else will the build server break. | |
var fs = require('fs'); | |
module.exports = function () { | |
var config = { | |
build: 1 | |
}; | |
// read major/minor version of services | |
config.appVersion = function (timestamp) { | |
var info = fs.readFileSync('../WebApplication/Properties/AssemblyInfo.cs').toString(); | |
var majorMinor = /AssemblyVersion\("(\d\.\d)/.exec(info)[1].trim(); | |
var appVersion = majorMinor + "." + timestamp; | |
return appVersion; | |
}; | |
config.apiUrl = { | |
dev: 'http://...', | |
stage: 'https://...', | |
prod: 'https://...' | |
}, | |
// PhoneGap build configuration | |
config.phoneGap = { | |
appId: 'theAppId', | |
authToken: 'theAuthToken', | |
debug: { | |
dev: true, | |
stage: true, | |
prod: false | |
}, | |
keys: { | |
dev: { ios: 1, android: 1 }, | |
stage: { ios: 2, android: 2 }, | |
prod: { ios: 3, android: 3 } | |
}, | |
unlockData: { | |
ios: { form: { data: { password: 'aPassword' } } }, | |
android: { form: { data: { key_pw: 'aPassword', keystore_pw: 'anotherPassword' } } } | |
} | |
}, | |
config.hockeyApp = { | |
token: 'theToken', | |
ids: { | |
dev: { | |
ios: '', | |
android: '' | |
}, | |
stage: { | |
ios: '', | |
android: '' | |
}, | |
prod: { | |
ios: '', | |
android: '' | |
} | |
}, | |
}, | |
config.cordova = { | |
dev: { appNamespace: 'dev.namespace' }, | |
stage: { appNamespace: 'stage.namespace' }, | |
prod: { appNamespace: 'prod.namespace' } | |
}, | |
config.notifications = { | |
dev: { appId: '' }, | |
stage: { appId: '' }, | |
prod: { appId: '' }, | |
}, | |
config.tracking = { | |
dev: { trackerId: '' }, | |
stage: { trackerId: '' }, | |
prod: { trackerId: '' }, | |
}, | |
config.ensure = { | |
environment: function (env, debugmode) { | |
if (debugmode) return 'dev'; | |
if (env !== null && env !== undefined) return env.toLowerCase(); | |
else return 'dev'; | |
}, | |
platform: function (platform) { | |
if (platform !== undefined && platform !== null) { | |
platform = platform.toLowerCase(); | |
} else { | |
throw 'a platform must be supplied with --platform=ios/android'; | |
} | |
return platform; | |
}, | |
services : function(svc, env) { | |
var servicesEnvironment = env; | |
if (svc) { | |
servicesEnvironment = svc.toLowerCase(); | |
} | |
return servicesEnvironment; | |
} | |
} | |
config.extension = function (platform) { | |
var extension = '.zip'; | |
if (platform === 'android') { | |
extension = '.apk'; | |
} | |
else if (platform === 'ios') { | |
extension = '.ipa'; | |
} | |
return extension; | |
} | |
return config; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gulp.task('deploy', function (done) { | |
var platform = config.ensure.platform(argv.platform); | |
var env = config.ensure.environment(argv.env, argv.debugmode); | |
if (env === 'prod' && platform === 'ios') { | |
throw 'ios applications cannot be distributed via HockeyApp'; | |
} | |
var endpoint = '/apps/' + config.phoneGap.appId; | |
pgBuild.auth({ token: config.phoneGap.authToken }, function (e, api) { | |
api.get(endpoint, function (ee, data) { | |
var status = (!ee && data) ? data.status[platform] : null; | |
if (status === 'complete') { | |
var filePath = 'tmp/' + data.package + '.' + data.version + config.extension(platform); | |
var write = api.get(endpoint + '/' + platform).pipe(fs.createWriteStream(filePath)); | |
write.on('finish', function () { | |
var form = { | |
ipa: fs.createReadStream(filePath), | |
status: 2 | |
}; | |
var header = { | |
'X-HockeyAppToken': config.hockeyApp.token | |
}; | |
endpoint = 'https://rink.hockeyapp.net/api/2/apps/' + config.hockeyApp.ids[env][platform] + '/app_versions/upload'; | |
request.post({ url: endpoint, formData: form, headers: header }, function (err, httpResponse, body) { | |
requestCallBack(err, httpResponse, body); | |
done(); | |
}); | |
}); | |
} else { | |
console.log( | |
gutil.colors.red('Cannnot download application'), | |
gutil.colors.white('\n plaform: ' + platform) + | |
gutil.colors.white('\n status: ' + status)); | |
done(); | |
} | |
}); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ls | |
├── bower.json // bower dependencies | |
├── config.xml // cordova configuration | |
├── gulpfile.js // gulp tasks | |
├── hooks // custom cordova hooks to execute on specific commands | |
├── ionic.project // ionic configuration | |
├── package.json // node dependencies | |
├── platforms // iOS/Android specific builds will reside here | |
├── plugins // where your cordova/ionic plugins will be installed | |
├── scss // scss code, which will output to www/css/ | |
└── www // application - JS code and libs, CSS, images, etc. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gulp.task('mirror', function (done) { | |
var files = ['res/**', | |
'www/*.*', | |
'www/css/*.min.css', | |
'www/js/*.js', | |
'www/templates/**', | |
'www/img/**', | |
'www/lib/custom/**', | |
'www/lib/**/*min.js', | |
'www/lib/ionic/**', | |
'!www/lib/ionic/scss/**', | |
'!www/lib/ionic/css/ionic.css', | |
'!www/lib/ionic/js/ionic.bundle.js', | |
'!www/lib/ionic/js/ionic.js', | |
'!www/lib/ionic/js/ionic.min.js', | |
'!www/lib/ionic/js/ionic-angular*' | |
]; | |
gulp.src(files, { base: "." }) | |
.pipe(chmod(666)) | |
.pipe(gulp.dest('tmp')) | |
.on('end', done); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gulp.task('unlock', function () { | |
var env = config.ensure.environment(argv.env, argv.debugmode); | |
pgBuild.auth({ token: config.phoneGap.authToken }, function (e, api) { | |
var key = config.phoneGap.keys[env]; | |
for (var platform in key) { | |
var endpoint = '/keys/' + platform + '/' + key[platform]; | |
api.put(endpoint, config.phoneGap.unlockData[platform], buildCallBack); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the example. Excuse my ignorance but how do you include the configure.js in jour gulp file