From 498f5d32dd72d0cd480bcfe3e7df98b6eb3441be Mon Sep 17 00:00:00 2001 From: Cyrille Colin Date: Thu, 26 Apr 2018 10:26:18 +0200 Subject: [PATCH] [cordova] add resource-file to mobile-config (#9748) * add resource-file to mobile-config * Fix typo in addResourceFile documentation header * get filename with path.parse instead of split * Code formatting adjustments --- tools/cordova/builder.js | 55 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/tools/cordova/builder.js b/tools/cordova/builder.js index 74628ed8a1..a4d20c06c6 100644 --- a/tools/cordova/builder.js +++ b/tools/cordova/builder.js @@ -1,5 +1,6 @@ import _ from 'underscore'; import util from 'util'; +import path from 'path'; import { Console } from '../console/console.js'; import buildmessage from '../utils/buildmessage.js'; import files from '../fs/files.js'; @@ -52,7 +53,7 @@ const launchIosSizes = { 'iphone6p_portrait': '1242x2208', 'iphone6p_landscape': '2208x1242', 'iphoneX_portrait': '1125x2436', - 'iphoneX_landscape': '2436x1125', + 'iphoneX_landscape': '2436x1125', 'ipad_portrait_2x': '1536x2048', 'ipad_landscape_2x': '2048x1536', // Legacy @@ -125,6 +126,9 @@ export class CordovaBuilder { // Custom elements that will be appended into config.xml's widgets this.custom = []; + // Resource files that will be appended to platform bundle and config.xml + this.resourceFiles = []; + const packageMap = this.projectContext.packageMap; if (packageMap && packageMap.getInfo('launch-screen')) { @@ -320,6 +324,12 @@ export class CordovaBuilder { this.configureAndCopyImages(launchAndroidSizes, platformElement.android, 'splash'); } + this.configureAndCopyResourceFiles( + this.resourceFiles, + platformElement.ios, + platformElement.android + ); + Console.debug('Writing new config.xml'); const configXmlPath = files.pathJoin(this.projectRoot, 'config.xml'); @@ -375,6 +385,31 @@ export class CordovaBuilder { }); } + configureAndCopyResourceFiles(resourceFiles, iosElement, androidElement) { + _.each(resourceFiles, resourceFile => { + // Copy file in cordova project root directory + var filename = path.parse(resourceFile.src).base; + files.copyFile( + files.pathResolve(this.projectContext.projectDir, resourceFile.src), + files.pathJoin(this.projectRoot, filename)); + // And entry in config.xml + if (!resourceFile.platform || + (resourceFile.platform && resourceFile.platform === "android")) { + androidElement.element('resource-file', { + src: resourceFile.src, + target: resourceFile.target + }); + } + if (!resourceFile.platform || + (resourceFile.platform && resourceFile.platform === "ios")) { + iosElement.element('resource-file', { + src: resourceFile.src, + target: resourceFile.target + }); + } + }); + } + copyWWW(bundlePath) { const wwwPath = files.pathJoin(this.projectRoot, 'www'); @@ -680,11 +715,27 @@ configuration. The key may be deprecated.`); * * `App.appendToConfig('');` * - * @param {String} element The XML you want to include + * @param {String} element The XML you want to include * @memberOf App */ appendToConfig: function (xml) { builder.custom.push(xml); }, + + /** + * @summary Add a resource file for your build as described in the + * [Cordova documentation](http://cordova.apache.org/docs/en/7.x/config_ref/index.html#resource-file). + * @param {String} src The project resource path. + * @param {String} target Resource destination in build. + * @param {String} [platform] Optional. A platform name (either `ios` or `android`, both if ommited) to add a resource-file entry. + * @memberOf App + */ + addResourceFile: function (src, target, platform) { + builder.resourceFiles.push({ + src: src, + target: target, + platform: platform + }); + } }; }