Skip to content

Instantly share code, notes, and snippets.

@joaoneto
Created November 1, 2014 18:45
Show Gist options
  • Save joaoneto/7d095ca2a006a79103b3 to your computer and use it in GitHub Desktop.
Save joaoneto/7d095ca2a006a79103b3 to your computer and use it in GitHub Desktop.
Gulp tasks to download and update a existent cordova project with crosswallk
var gulp = require('gulp');
var gutil = require('gulp-util');
var shell = require('gulp-shell');
var download = require('gulp-download');
var unzip = require('gulp-unzip');
var clean = require('gulp-clean');
var runSequence = require('gulp-run-sequence');
var fs = require('fs');
var path = require('path');
var et = require('elementtree');
var config = module.exports = {
CACHE_DIR: 'cache',
CORDOVA_LIB_DIR: path.join('platforms', 'android', 'CordovaLib'),
CROSSWALK_ARCH: 'arm',
CROSSWALK_VERSION: '8.37.189.12',
CROSSWALK_CDN_BASEURL: 'https://download.01.org/crosswalk/releases/crosswalk/android/stable',
get CROSSWALK_CORDOVA_PKG() {
return ['crosswalk-cordova-', this.CROSSWALK_VERSION, '-', this.CROSSWALK_ARCH].join('');
},
get CROSSWALK_CORDOVA_FILENAME() {
return [this.CROSSWALK_CORDOVA_PKG, '.zip'].join('');
},
get CROSSWALK_CORDOVA_DOWNLOAD_URL() {
return [
this.CROSSWALK_CDN_BASEURL,
this.CROSSWALK_VERSION,
this.CROSSWALK_ARCH,
this.CROSSWALK_CORDOVA_FILENAME
].join('/')
}
};
/**
* Update AndroidManifest.xml
*/
gulp.task('crosswalk:update:androidmanifest', function (done) {
var androidManifestXml;
var crosswalkAditionalPermissions = [
'android.permission.ACCESS_WIFI_STATE',
'android.permission.ACCESS_NETWORK_STATE'
];
var androidManifestXmlFile = path.join('platforms', 'android', 'AndroidManifest.xml');
fs.exists(androidManifestXmlFile, function (exists) {
if (!exists) return done(new Error('AndroidManifest.xml not found.'));
androidManifestXml = et.parse(fs.readFileSync(androidManifestXmlFile, 'utf-8'));
crosswalkAditionalPermissions.forEach(function (permission) {
var element;
if (!androidManifestXml.find('./uses-permission/[@android:name="' + permission + '"]') ) {
element = new et.ElementTree(et.XML('<uses-permission android:name="' + permission + '" />'));
androidManifestXml.getroot().append(element.getroot());
}
});
fs.writeFile(androidManifestXmlFile, androidManifestXml.write({'indent': 4}), 'utf-8', done);
});
});
/**
* Clean
*/
gulp.task('crosswalk:clean', function (done) {
runSequence([
'crosswalk:clean:version',
'crosswalk:clean:cordovalib'
], done);
});
gulp.task('crosswalk:clean:version', function () {
gulp.src(config.CORDOVA_LIB_DIR).pipe(clean());
});
gulp.task('crosswalk:clean:cordovalib', function () {
gulp.src(config.CORDOVA_LIB_DIR).pipe(clean({force: true}));
});
/**
* Copy
*/
gulp.task('crosswalk:copy', function (done) {
runSequence([
'crosswalk:copy:versionfile',
'crosswalk:copy:cordovalib'
], done);
});
gulp.task('crosswalk:copy:versionfile', function () {
var cache_dir = path.join(config.CACHE_DIR, 'crosswalk');
var cache_pkg_file = path.join(cache_dir, config.CROSSWALK_CORDOVA_PKG, 'VERSION');
var dest = path.join('platforms', 'android');
gulp.src(cache_pkg_file).pipe(gulp.dest(dest));
});
gulp.task('crosswalk:copy:cordovalib', function () {
var cache_dir = path.join(config.CACHE_DIR, 'crosswalk');
var cache_pkg_dir = path.join(cache_dir, config.CROSSWALK_CORDOVA_PKG, 'framework');
gulp.src(path.join(cache_pkg_dir, '**', '*')).pipe(gulp.dest(config.CORDOVA_LIB_DIR))
});
/**
* Install
*/
gulp.task('crosswalk:install', function (done) {
runSequence(
'crosswalk:clean',
'crosswalk:compile',
'crosswalk:copy',
'crosswalk:update:androidmanifest',
done);
});
/**
* Compile
*/
gulp.task('crosswalk:compile', function (done) {
runSequence(
'crosswalk:compile:cordova',
'crosswalk:compile:webview',
done);
});
gulp.task('crosswalk:compile:cordova', ['crosswalk:download', 'crosswalk:compile:webview'], function () {
var cache_dir = path.join(config.CACHE_DIR, 'crosswalk');
var cache_pkg_dir = path.join(cache_dir, config.CROSSWALK_CORDOVA_PKG, 'framework');
// @todo make target and ant debug or ant release parametrizable
return shell.task([
'android update project --subprojects --path . --target "android-19"',
'ant release'
], { cwd: cache_pkg_dir }).call();
});
gulp.task('crosswalk:compile:webview', ['crosswalk:download'], function () {
var cache_dir = path.join(config.CACHE_DIR, 'crosswalk');
var cache_pkg_dir = path.join(cache_dir, config.CROSSWALK_CORDOVA_PKG, 'framework', 'xwalk_core_library');
// @todo make target and ant debug or ant release parametrizable
return shell.task([
'android update project --subprojects --path . --target "android-19"',
'ant release'
], { cwd: cache_pkg_dir }).call();
});
/**
* Download
*/
gulp.task('crosswalk:download', function (done) {
var cache_dir = path.join(config.CACHE_DIR, 'crosswalk');
var cache_pkg_dir = path.join(cache_dir, config.CROSSWALK_CORDOVA_PKG);
var cache_pkg_file = path.join(cache_dir, config.CROSSWALK_CORDOVA_FILENAME);
var url = config.CROSSWALK_CORDOVA_DOWNLOAD_URL;
fs.exists(cache_pkg_dir, function (exists) {
if (exists) return done();
download(url)
.pipe(gulp.dest(cache_dir))
.on('error', done)
.on('end', function () {
gulp.src(cache_pkg_file)
.pipe(unzip())
.pipe(gulp.dest(cache_dir))
.on('error', done)
.on('end', function () {
gulp.src(cache_pkg_file)
.pipe(clean())
.on('data', function() {})
.on('error', done)
.on('end', done);
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment