Skip to content
This repository has been archived by the owner on Jul 8, 2020. It is now read-only.

Commit

Permalink
closes #143: device livereload
Browse files Browse the repository at this point in the history
  • Loading branch information
gruppjo committed Jun 27, 2016
1 parent 35f5d5f commit 6fb80cb
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
40 changes: 38 additions & 2 deletions generators/app/templates/gulp/cordova.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ var options = gulp.options;
var $ = require('gulp-load-plugins')();
// packages
var path = require('path');
var process = require('process');
var del = require('del');
var vinylPaths = require('vinyl-paths');
var bs = require('browser-sync').create('m-ionic:livereload');
var Patcher = require('./utils/Patcher');

var runCordova = function (command, stream) {
// allow to overwrite command from option.cordova with parameter
Expand All @@ -21,16 +24,16 @@ var runCordova = function (command, stream) {
]));
};

// CORDOVA
gulp.task('cordova', runCordova);
gulp.task('cordova-only-resources', ['resources'], runCordova);
gulp.task('cordova-with-build', ['build', 'resources'], runCordova);

// Handle resources
// RESOURCES
gulp.task('clean-res', function () {
return gulp.src('res/*/current/*')
.pipe(vinylPaths(del));
});

gulp.task('resources', ['clean-res'], function () {
var setFolder = options.res || 'default';

Expand All @@ -41,3 +44,36 @@ gulp.task('resources', ['clean-res'], function () {
}))
.pipe(gulp.dest('res'));
});

// LIVERELOAD
gulp.task('livereload', ['serve-livereload'], function () {
// watch for changes in scss
gulp.watch('app/*/styles/**/*.scss', ['styles']);
return runCordova(options.livereload + ' --noprepare');
});
gulp.task('serve-livereload', ['cordova-prepare'], function (done) {
var bsOptions = {
logConnections: true,
open: false,
files: ['app', '.tmp'],
server: {
baseDir: ['app', '.tmp', 'platforms/ios/www/', 'platforms/android/assets/www/'],
// platform www's for cordova.js
}
};

bs.init(bsOptions, function (err, bsInstance) {
if (err) {
console.log(err);
}
var urls = bsInstance.options.getIn(['urls']);
var patcher = new Patcher(process.cwd());
// patch platform's config xml to allow navigation to
// & to set content tag to bs externalUrl
patcher.patchConfigXml(urls.get('external'));
done();
});
});
gulp.task('cordova-prepare', function () {
return runCordova('prepare');
});
52 changes: 52 additions & 0 deletions generators/app/templates/gulp/utils/Patcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';

var path = require('path');
var fs = require('fs');
var et = require('elementtree'); // also included in gen-m/package.json
var chalk = require('chalk');

function Patcher (projectRoot) {
this.projectRoot = projectRoot || '.';
}

Patcher.prototype.parseXml = function (filename) {
return new et.ElementTree(et.XML(fs.readFileSync(filename, 'utf-8')));
};

Patcher.prototype.patchConfigXml = function (externalUrl) {
var platforms = ['android', 'ios'];
platforms.forEach(function (platform) {

var CONFIG_LOCATION = {
android: 'res/xml',
// retrieve project name which is necessary for ios config.xml path
ios: this.getProjectName()
};

// retrieve platform's path to config.xml & parse it
var configXmlPath = path.join(this.projectRoot, 'platforms', platform, CONFIG_LOCATION[platform], 'config.xml');
var configXml = this.parseXml(configXmlPath);
console.log(chalk.green('patching ') + configXmlPath);

// set content src attrib to externalUrl
var contentTag = configXml.find('content[@src]');
contentTag.attrib.src = externalUrl;

// Add allow-navigation element so it's possible to navigate to externalUrl
var allowNavTag = et.SubElement(configXml.find('.'), 'allow-navigation');
allowNavTag.set('href', '*');

fs.writeFileSync(configXmlPath, configXml.write({
indent: 4
}), 'utf-8');
}, this);
};

Patcher.prototype.getProjectName = function () {
var parsedConfigXML = this.parseXml(path.join(this.projectRoot, 'config.xml'));
var nameTag = parsedConfigXML.find('name');
return nameTag.text;
};


module.exports = Patcher;

0 comments on commit 6fb80cb

Please sign in to comment.