Skip to content

Commit

Permalink
feat(generator): Scaffold Cordova hook for copying Icons and Splash S…
Browse files Browse the repository at this point in the history
…creens
  • Loading branch information
diegonetto committed Aug 19, 2014
1 parent 435beaf commit b61120a
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ IonicGenerator.prototype.packageFiles = function packageFiles() {
this.template('common/_gitignore', '.gitignore');
};

IonicGenerator.prototype.cordovaHooks = function cordovaHooks() {
var iconsAndSplash = 'hooks/after_prepare/icons_and_splashscreens.js';
this.template(iconsAndSplash);
fs.chmodSync(iconsAndSplash, '755');
};

IonicGenerator.prototype.testFiles = function testFiles() {
this.template('spec/controllers.js', 'test/spec/controllers.js');
};
4 changes: 4 additions & 0 deletions templates/common/_package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"dependencies": {},
"devDependencies": {
"grunt": "~0.4.1",
"glob": "~4.0.5",
"grunt-autoprefixer": "~0.4.0",
"grunt-wiredep": "^1.7.0",
"grunt-concurrent": "~0.4.1",
Expand Down Expand Up @@ -33,6 +34,9 @@
"time-grunt": "~0.2.1",
"cordova": "~3.5.0-0.2.6",
"lodash": "~2.4.1",
"mkdirp": "~0.5.0",
"ncp": "~0.6.0",
"orchestrator": "~0.3.7",
"ripple-emulator": "~0.9.23"
},
"engines": {
Expand Down
76 changes: 76 additions & 0 deletions templates/hooks/after_prepare/icons_and_splashscreens.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env node
/**
* Algorithm
* [1] Look at all installed platforms
* [2] Copy (non-destructive) icons and splash screens from platform to local RESOURCE_DIR
* [3] Copy (destructive) matching icons and splash screens from RESOURCE_DIR to platform
*
* This ensures that local RESOURCE_DIR will be pre-scaffolded with icons and splash
* screens generated by Cordova as placeholder ONLY for installed platforms and that
* any modifications to local assets are reflected in the CORRECT Cordova platform
* locations, without having to hardcode file paths.
*/
var fs = require('fs');
var path = require('path');
var _ = require('lodash');
var ncp = require('ncp');
var mkdirp = require('mkdirp');
var glob = require('glob');
var Orchestrator = require('orchestrator');

var BASES = {
android: 'res',
ios: '<%= appName %>/Resources'
};
var RESOURCE_DIR = 'resources';

// Helper function for file copying that ensures directory existence.
function copyFile (src, dest, ncpOpts, callback) {
var orchestrator = new Orchestrator();
var parts = dest.split(path.sep);
var fileName = parts.pop();
var destDir = parts.join(path.sep);
var destFile = path.resolve(destDir, fileName);
orchestrator.add('ensureDir', function (done) {
mkdirp(destDir, function (err) {
done(err);
});
});
orchestrator.add('copyFile', ['ensureDir'], function (done) {
ncp(src, destFile, ncpOpts, function (err) {
done(err);
});
});
orchestrator.start('copyFile', function (err) {
callback(err);
});
}

// Main
var platforms = fs.readdirSync('platforms');
_.each(platforms, function (platform) {
var base = path.resolve('platforms', platform, BASES[platform]);
glob(base + '/**/*.png', function (err, files) {
_.each(files, function (cordovaFile) {
var orchestrator = new Orchestrator();
var parts = cordovaFile.split(path.sep);
var fileName = parts.pop();
var localDir = path.resolve(RESOURCE_DIR, platform, _.last(parts));
var localFile = path.resolve(localDir, fileName);

orchestrator.add('copyFromCordova', function (done) {
copyFile(cordovaFile, localFile, { clobber: false }, function (err) {
done(err);
});
});
orchestrator.add('copyToCordova', ['copyFromCordova'], function (done) {
copyFile(localFile, cordovaFile, { clobber: true }, function (err) {
done(err);
});
});
orchestrator.start('copyToCordova', function (err) {
if (err) { console.error(err); }
});
});
});
});

0 comments on commit b61120a

Please sign in to comment.