Github user vladimir-kotikov commented on a diff in the pull request: https://github.com/apache/cordova-lib/pull/235#discussion_r31924092 --- Diff: cordova-lib/src/platforms/PlatformApi.js --- @@ -0,0 +1,365 @@ +/** + 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 functionality from platform specific files from +// both places cordova/metadata and plugman/platforms. Hopefully, to be soon +// replaced by real unified platform specific classes. +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; +}; + +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; +}; + +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)) { + shell.cp('-f', defaultConfig, ownConfig); + events.emit('verbose', 'Generating config.xml from defaults for platform "' + this.platform + '"'); + } else { + shell.cp('-f', ownConfig, defaultConfig); + } + + // Update platform config.xml based on top level config.xml + var baseConfig = optSource || util.projectConfig(util.isCordova(this.root)); + + var baseCfg = new ConfigParser(baseConfig); + var ownCfg = new ConfigParser(ownConfig); + util.mergeXml(baseCfg.doc.getroot(), ownCfg.doc.getroot(), this.platform, true); + + // CB-6976 Windows Universal Apps. For smooth transition and to prevent mass api failures + // we allow using windows8 tag for new windows platform + // TODO: this section should be moved to windows platform's implementation of PlatformApi + if (this.platform == 'windows') { + util.mergeXml(baseCfg.doc.getroot(), ownCfg.doc.getroot(), 'windows8', true); + } + + return Q.when(ownCfg.write()); +}; + +/** + * Base implementation for getWwwDir. Assumes that + * www directory is placed at the root of project. + * @return {String} Path to platform's www directory. + */ +BasePlatformApi.prototype.getWwwDir = function() { + if (this.parser && this.parser.www_dir) { + return this.parser.www_dir(); + } + + return path.join(this.root, 'www'); +}; + +/** + * Base implementation for getCordovaJsSrc. Assumes that cordova.js + * source is placed at the root of platform's source dir. + * @return {String} Path to platform's 'cordova-js-src' folder. + */ +BasePlatformApi.prototype.getCordovaJsSrc = function(platformSource) { + if (this.parser && this.parser.cordovajs_src_path) { + return this.parser.cordovajs_src_path(platformSource); + } + + return path.resolve(platformSource, 'cordova-js-src'); +}; + +/** + * Base implementation for updateWww. + * @param {string} [wwwSource] Source dir for www files. If not provided, method + * will try to find www directory from cordova project + */ +BasePlatformApi.prototype.updateWww = function(wwwSource) { + if (this.parser && this.parser.update_www) { + return Q.when(this.parser.update_www()); + } + + if (!wwwSource) { + var projectRoot = util.isCordova(this.root); + wwwSource = util.projectWww(projectRoot); + } + var platformWww = path.join(this.root, 'platform_www'); + + // Clear the www dir + shell.rm('-rf', this.getWwwDir()); + shell.mkdir(this.getWwwDir()); + // Copy over all app www assets + shell.cp('-rf', path.join(wwwSource, '*'), this.getWwwDir()); + // Copy over stock platform www assets (cordova.js) + runShellSilent(function () { + shell.cp('-rf', path.join(platformWww, '*'), this.getWwwDir()); + }.bind(this)); + + return Q(); +}; + +/** + * Base implementation for updateProject. Does nothing + * since implementation is heavily depends on platform specifics. + * Always should be overridden by platform. + */ +BasePlatformApi.prototype.updateProject = function(configSource) { + if (this.parser && this.parser.update_project) { + var configParser = new ConfigParser(configSource || this.getConfigXml()); + return Q.when(this.parser.update_project(configParser)); + } + return Q(); --- End diff -- Well, in some cases platform may want to do nothing in `updateProject`. Example is a browser platform, which needs to update config.xml and www content only during prepare.
--- 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