Created
August 7, 2013 23:15
-
-
Save apla/6179863 to your computer and use it in GitHub Desktop.
cordova hook script to copy icons and splash screens to platform directories
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
#!/usr/bin/env node | |
var cordova_util = require('cordova/src/util'); | |
var projectRoot = cordova_util.isCordova(process.cwd()); | |
var projectXml = cordova_util.projectConfig(projectRoot); | |
var projectConfig = new cordova_util.config_parser(projectXml); | |
projectConfig.name(); | |
var fs = require ('fs'); | |
var platformDir = { | |
ios: { | |
icon: "{$projectName}/Resources/icons", | |
splash: "{$projectName}/Resources/splash", | |
nameMap: { | |
"icon-57.png": "icon.png", | |
"icon-57-2x.png": "icon@2x.png", | |
"icon-72-2x.png": "icon-72@2x.png", | |
"screen-iphone-portrait.png": "Default~iphone.png", | |
"screen-iphone-portrait-2x.png": "Default@2x~iphone.png", | |
"screen-iphone-portrait-568h-2x.png": "Default-568h@2x~iphone.png" | |
} | |
}, | |
android: { | |
icon:"res/drawable-{$density}", | |
splash:"res/drawable-{$density}", | |
nameMap: { | |
"icon-36-ldpi.png": "icon.png", | |
"icon-48-mdpi.png": "icon.png", | |
"icon-72-hdpi.png": "icon.png", | |
"icon-96-xhdpi.png": "icon.png", | |
"screen-ldpi-portrait.png": "ic_launcher.png", | |
"screen-mdpi-portrait.png": "ic_launcher.png", | |
"screen-hdpi-portrait.png": "ic_launcher.png", | |
"screen-xhdpi-portrait.png": "ic_launcher.png" | |
} | |
}, | |
blackberry10: {}, | |
wp7: {}, | |
wp8: {} | |
} | |
function copyAsset (scope, node) { | |
var platform = node.attrib['gap:platform']; | |
var density = node.attrib['gap:density']; | |
var assetDirTmpl = platformDir[platform] && platformDir[platform][scope]; | |
if (!assetDirTmpl) | |
return; | |
var dict = { | |
projectName: projectConfig.name(), | |
density: density | |
}; | |
var assetDir = assetDirTmpl.replace (/{\$([^}]+)}/, function (match, p1) { | |
return dict[p1]; | |
}); | |
var srcPath = 'www/'+node.attrib.src; | |
var fileName = srcPath.match(/[^\/]+$/)[0]; | |
if (platformDir[platform] && platformDir[platform].nameMap && platformDir[platform].nameMap[fileName]) { | |
fileName = platformDir[platform].nameMap[fileName]; | |
} | |
var dstPath = 'platforms/'+platform+'/'+assetDir+'/'+fileName; | |
console.log ('copying from '+srcPath+' to the '+dstPath); | |
// so, here we start to copy asset | |
fs.stat (srcPath, function (err, stats) { | |
if (err) { | |
return; | |
} | |
var r = fs.createReadStream(srcPath); | |
r.on ('open', function () { | |
r.pause(); | |
var w = fs.createWriteStream(dstPath); | |
w.on ('open', function () { | |
r.pipe(w); | |
r.resume(); | |
}); | |
w.on ('error', function() { | |
console.log('Cannot write file'); | |
}) | |
}); | |
r.on ('error', function() { | |
console.log('Cannot read file'); | |
}) | |
}) | |
} | |
projectConfig.doc.findall ('icon').map (function (node) { | |
copyAsset ('icon', node); | |
}); | |
projectConfig.doc.findall ('*').filter (function (node) {if (node.tag == 'gap:splash') return true;}).map (function (node) { | |
copyAsset ('splash', node); | |
}); | |
// echo "=======================================================================================================" | |
// set | |
// echo $1 | |
// platforms/ios/G20Summit2013/Resources/icons/ |
hooks/README.md says:
Any scripts you add to these directories will be executed before
and after the commands corresponding to the directory name.
Hook Directories
The following subdirectories will be used for hooks:
after_build/
after_compile/
after_docs/
after_emulate/
after_platform_add/
after_platform_rm/
after_platform_ls/
after_plugin_add/
after_plugin_ls/
after_plugin_rm/
after_plugin_search/
after_prepare/
after_run/
after_serve/
before_build/
before_compile/
before_docs/
before_emulate/
before_platform_add/
before_platform_rm/
before_platform_ls/
before_plugin_add/
before_plugin_ls/
before_plugin_rm/
before_plugin_search/
before_prepare/
before_run/
before_serve/
So which one should this script go into? I'm not seeing anything happen with it in the root hooks directory. I'm guessing it should go in after_build
where I'm seeing it run.
This doesn't seem to work anymore. I also get the error Error: Cannot find module 'cordova/src/util'. Does anyone know how to fix this problem? I'm using Cordova 3.6.3-0.2.13
I have fixed the error for cordova 3.6.3. You have to:
npm install cordova-lib
And replace lines from 3 to 6 with:
var cordova_util = require('cordova-lib/src/cordova/util');
var projectRoot = cordova_util.isCordova(process.cwd());
var projectXml = cordova_util.projectConfig(projectRoot);
var ConfigParser = require('cordova-lib/src/ConfigParser/ConfigParser');
var projectConfig = new ConfigParser(projectXml);
or simply use my fixed version: https://gist.github.com/brugnara/a6cbd8ba90a3a73aa91a
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've updated the Gist to support Cordova 3.4
https://gist.github.com/Cyril-sf/11409317/revisions