Github user omefire commented on a diff in the pull request: https://github.com/apache/cordova-lib/pull/235#discussion_r32236650 --- Diff: cordova-lib/src/platforms/PlatformApi.js --- @@ -0,0 +1,414 @@ +/** + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +*/ + +var Q = require('q'), + fs = require('fs'), + path = require('path'), + shell = require('shelljs'), + events = require('../events'), + util = require('../cordova/util'), + superspawn = require('../cordova/superspawn'), + platforms = require('./platformsConfig.json'), + ConfigParser = require('../configparser/ConfigParser'); + +var BasePluginHandler = require('../plugman/platforms/PluginHandler'); +var ParserHelper = require('../cordova/metadata/parserhelper/ParserHelper'); + +// Avoid loading the same platform projects more than once (identified by path) +var cachedProjects = {}; + +/** + * A single class that exposes platform-dependent functionality for cordova and + * plugman. This class works as a base, and provides some default implementation + * for that functionality. It should not be instantiated directly, only through + * 'getPlatformApi' module method. + * @param {String} platform Name of platform. Should be one of names, declared + * in platformsConfig.json + * @param {String} platformRootDir Optional. Path to platform directory. If not provided + * default value is set to<cordova_project>/platforms/<platform> + */ +function BasePlatformApi(platform, platformRootDir) { + this.root = platformRootDir; + this.platform = platform; + + // Parser property of Platform API left for backward compatibility + // and smooth transition to ne API. It also does the job of requiring + // and constructing legacy Parser instance, which required for platforms + // that still stores their code in cordova-lib. + var parser; + Object.defineProperty(this, 'parser', { + get: function () { + if (parser)return parser; + + var ParserConstructor; + try { + ParserConstructor = require(platforms[this.platform].parser_file); + } catch (e) { } + + // parser === null is the special case which means that we've tried + // to get embedded platform parser and failed. In this case instead of + // parser's methods will be called PlatformApi default implementations. + parser = ParserConstructor ? new ParserConstructor(this.root) : null; + return parser; + }, + configurable: true + }); + + // Extend with a ParserHelper instance + Object.defineProperty(this, 'helper', { + value: new ParserHelper(this.platform), + enumerable: true, + configurable: false, + writable: false + }); +} + +/** + * Gets a plugin handler (former 'handler') for this project's platform. + * Platform can provide it's own implementation for PluginHandler by + * exposing PlatformApi.PluginHandler constructor. If platform doesn't + * provides its own implementation, then embedded PluginHandler will be used. + * (Taken from platformConfig.json/<platform>/handler_file field) + * + * @return {PluginHandler} Instance of PluginHandler class that exposes + * platform-related functionality for cordova. + */ +BasePlatformApi.prototype.getPluginHandler = function() { + if (!this._pluginHandler) { + var PluginHandler = BasePluginHandler; + var PluginHandlerImpl; + + // Try find whether platform exposes its' API via js module + var platformApiModule = path.join(this.root, 'cordova', 'Api.js'); + if (fs.existsSync(platformApiModule)) { + PluginHandlerImpl = require(platformApiModule).PluginHandler; + } + + if (!PluginHandlerImpl) { + // If platform doesn't provide PluginHandler, use embedded one for current platform + // The platform implementation, shipped with cordova-lib, isn't constructable so + // we need to create a dummy class and copy implementation to its prototype. + var LegacyPluginHandler = function LegacyPluginHandler () {}; + LegacyPluginHandler.prototype = require(platforms[this.platform].handler_file); + PluginHandlerImpl = LegacyPluginHandler; + } + + // Extend BasePlatformApi with platform implementation. + PluginHandler = inherit(PluginHandlerImpl, BasePluginHandler); + this._pluginHandler = new PluginHandler(); + } + + return this._pluginHandler; +}; + +/** + * Gets an installer function, that handles installation for specific type of plugin's files + * (such as <src-file>, <lib-file>, etc.). Used in plugman only. + * @param {String} type Plugin file type (<src-file>, <lib-file>, etc.) + * @return {Function} Handler function for file, that accepts the following set of arguments: + * item: corresponding item from plugin.xml file. + * Could be get by PluginInfo.get* methods. + * plugin_dir: source directory for plugin + * plugin_id: plugin id, such as 'org.apache.cordova.camera' + * options: complex object, passed by plugman. + * See plugman/install module for reference. + * project: object that represents underlying IDE project for plaform. + * For Android it's an instance of plugman/util/android-project, + * for Windows - util/windows/jsprojmanager, etc. + */ +BasePlatformApi.prototype.getInstaller = function(type) { + var self = this; + function installWrapper(item, plugin_dir, plugin_id, options, project) { + self.getPluginHandler()[type].install(item, plugin_dir, self.root, plugin_id, options, project); + } + return installWrapper; +}; + +/** + * Gets an uninstaller function, that handles uninstallation for specific type of plugin's files + * (such as <src-file>, <lib-file>, etc.). Used in plugman only. + * @param {String} type Plugin file type (<src-file>, <lib-file>, etc.) + * @return {Function} Handler function for file, that accepts the following set of arguments: + * item: corresponding item from plugin.xml file. + * Could be get by PluginInfo.get* methods. + * plugin_id: plugin id, such as 'org.apache.cordova.camera' + * options: complex object, passed by plugman. + * See plugman/install module for reference. + * project: object that represents underlying IDE project for plaform. + * For Android it's an instance of plugman/util/android-project, + * for Windows - util/windows/jsprojmanager, etc. + */ +BasePlatformApi.prototype.getUninstaller = function(type) { + var self = this; + function uninstallWrapper(item, plugin_id, options, project) { + self.getPluginHandler()[type].uninstall(item, self.root, plugin_id, options, project); + } + return uninstallWrapper; +}; + +/** + * Base implementation for getConfigXml. Assumes that config.xml + * is placed at the root of project. + * @return {String} Path to platform's config.xml file + */ +BasePlatformApi.prototype.getConfigXml = function() { + if (this.parser && this.parser.config_xml) { + return this.parser.config_xml(); + } + + return path.join(this.root, 'config.xml'); +}; + +/** + * Base implementation for updateConfig. Reset platform's config to default. + * @return {String} Path to platform's config.xml file + */ +BasePlatformApi.prototype.updateConfig = function(optSource) { + var defaultConfig = path.join(this.root, 'cordova', 'defaults.xml'); + var ownConfig = this.getConfigXml(); + // If defaults.xml is present, overwrite platform config.xml with it + if (fs.existsSync(defaultConfig)) { --- End diff -- The old version of the code also handled the case when both defaults.xml and project_dir/config.xml were not present. In that case, it would grab 'www/config.xml' and copy it to project_dir/config.xml. Any reason that case isn't handled anymore ?
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@cordova.apache.org For additional commands, e-mail: dev-h...@cordova.apache.org