Return-Path: X-Original-To: apmail-cordova-dev-archive@www.apache.org Delivered-To: apmail-cordova-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 1528518434 for ; Wed, 10 Jun 2015 18:19:46 +0000 (UTC) Received: (qmail 72380 invoked by uid 500); 10 Jun 2015 18:19:45 -0000 Delivered-To: apmail-cordova-dev-archive@cordova.apache.org Received: (qmail 72340 invoked by uid 500); 10 Jun 2015 18:19:45 -0000 Mailing-List: contact dev-help@cordova.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cordova.apache.org Delivered-To: mailing list dev@cordova.apache.org Received: (qmail 72329 invoked by uid 99); 10 Jun 2015 18:19:45 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 10 Jun 2015 18:19:45 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 6611AE0385; Wed, 10 Jun 2015 18:19:45 +0000 (UTC) From: omefire To: dev@cordova.apache.org Reply-To: dev@cordova.apache.org References: In-Reply-To: Subject: [GitHub] cordova-lib pull request: Implementation for Unified platform API Content-Type: text/plain Message-Id: <20150610181945.6611AE0385@git1-us-west.apache.org> Date: Wed, 10 Jun 2015 18:19:45 +0000 (UTC) Github user omefire commented on a diff in the pull request: https://github.com/apache/cordova-lib/pull/235#discussion_r32148857 --- 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//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(); +}; + +// PLATFORM ACTIONS + +/** + * Default implementation for platform build. Uses executable script shipped with platform to build project. + * Could be overridden using PlatformApi implementation if it is provided by platform. + * @param {Object} options Complex object that provides cordova API to method + * @return {Promise} Promise, either resolve or rejected with error code. + */ +BasePlatformApi.prototype.build = function(options) { + var cmd = path.join(this.root, 'cordova', 'build'); + return superspawn.spawn(cmd, options.options, { printCommand: options.verbose, stdio: 'inherit' }); +}; + +/** + * Default implementation for platform run. Uses executable script shipped with platform + * to deploy and run project. Could be overridden using PlatformApi implementation if it + * is provided by platform. + * @param {Object} options Complex object that provides cordova API to method + * @return {Promise} Promise, either resolved or rejected with error code. + */ +BasePlatformApi.prototype.run = function(options) { + var cmd = path.join(this.root, 'cordova', 'run'); + return superspawn.spawn(cmd, options.options, { printCommand: options.verbose, stdio: 'inherit' }); +}; + +/** + * Default implementation for platform requirements. Uses module shipped with platform. + * Could be overridden using PlatformApi implementation if it is provided by platform. + * @return {Promise} Promise, either resolved with array of requirements + * or rejected with error. + */ +BasePlatformApi.prototype.requirements = function(options) { + var modulePath = path.join(this.root, 'cordova', 'lib', 'check_reqs'); + return require(modulePath).check_all(); +}; + +// getPlatformApi() should be the only method of instantiating the +// PlatformProject classes for now. --- End diff -- :nit PlatformProject -> PlatformApi ? --- 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 infrastructure@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org For additional commands, e-mail: dev-help@cordova.apache.org