You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by an...@apache.org on 2015/10/20 11:10:27 UTC

cordova-lib git commit: CB-9782 Update create/update signatures for PlatformApi polyfill

Repository: cordova-lib
Updated Branches:
  refs/heads/master acff72319 -> e21689182


CB-9782 Update create/update signatures for PlatformApi polyfill

* Update create/updatePlatform Api according to android changes at @msopentech/cordova-android#platformApi
* Update comments and JSDoc for create/update
* Prevent adding platform failures due to wrong platform's package.json

This closes #320


Project: http://git-wip-us.apache.org/repos/asf/cordova-lib/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-lib/commit/e2168918
Tree: http://git-wip-us.apache.org/repos/asf/cordova-lib/tree/e2168918
Diff: http://git-wip-us.apache.org/repos/asf/cordova-lib/diff/e2168918

Branch: refs/heads/master
Commit: e21689182f3e6af68930c573c6896fd4ec0b2a8f
Parents: acff723
Author: Vladimir Kotikov <v-...@microsoft.com>
Authored: Thu Oct 15 11:29:29 2015 +0300
Committer: Vladimir Kotikov <v-...@microsoft.com>
Committed: Tue Oct 20 12:10:59 2015 +0300

----------------------------------------------------------------------
 .../platforms/PlatformApiPoly.spec.js           | 21 ++++---
 cordova-lib/src/cordova/platform.js             | 38 ++++++------
 cordova-lib/src/platforms/PlatformApiPoly.js    | 64 +++++++++-----------
 3 files changed, 61 insertions(+), 62 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/e2168918/cordova-lib/spec-cordova/platforms/PlatformApiPoly.spec.js
----------------------------------------------------------------------
diff --git a/cordova-lib/spec-cordova/platforms/PlatformApiPoly.spec.js b/cordova-lib/spec-cordova/platforms/PlatformApiPoly.spec.js
index 9565c80..4e29064 100644
--- a/cordova-lib/spec-cordova/platforms/PlatformApiPoly.spec.js
+++ b/cordova-lib/spec-cordova/platforms/PlatformApiPoly.spec.js
@@ -114,7 +114,7 @@ describe('PlatformApi polyfill', function () {
 
     describe('methods:', function () {
 
-        var FAKE_PROJECT, OPTIONS, getPlatformApi, fail, success;
+        var FAKE_PROJECT, FAKE_CONFIG, OPTIONS, getPlatformApi, fail, success;
 
         beforeEach(function () {
             getPlatformApi = spyOn(knownPlatforms, 'getPlatformApi').andReturn(platformApi);
@@ -127,7 +127,8 @@ describe('PlatformApi polyfill', function () {
             fail = jasmine.createSpy('fail');
             success = jasmine.createSpy('success');
 
-            FAKE_PROJECT = {locations: {platforms: path.dirname(PLATFORM_ROOT), www: path.join(CORDOVA_ROOT, 'www')}, projectConfig: new ConfigParser('/fake/config.xml')};
+            FAKE_CONFIG = new ConfigParser('/fake/config.xml');
+            FAKE_PROJECT = {locations: {platforms: path.dirname(PLATFORM_ROOT), www: path.join(CORDOVA_ROOT, 'www')}, projectConfig: FAKE_CONFIG};
             OPTIONS = {platformDetails: {libDir: PLATFORM_LIB, platform: PLATFORM, version: PLATFORM_VERSION}};
         });
 
@@ -139,8 +140,8 @@ describe('PlatformApi polyfill', function () {
             });
 
             it('should create/update platform through running platforms\' scripts', function (done) {
-                Q.all([PlatformApiPoly.createPlatform(FAKE_PROJECT, OPTIONS),
-                       PlatformApiPoly.updatePlatform(FAKE_PROJECT, OPTIONS)])
+                Q.all([PlatformApiPoly.createPlatform(PLATFORM_ROOT, FAKE_CONFIG, OPTIONS),
+                       PlatformApiPoly.updatePlatform(PLATFORM_ROOT, OPTIONS)])
                 .then(function () {
                     expect(spawn).toHaveBeenCalled();
                     expect(spawn.calls.length).toBe(2);
@@ -150,8 +151,8 @@ describe('PlatformApi polyfill', function () {
             });
 
             it('should pass down arguments to platforms\' scripts', function (done) {
-                Q.all([PlatformApiPoly.createPlatform(FAKE_PROJECT, OPTIONS),
-                       PlatformApiPoly.updatePlatform(FAKE_PROJECT, OPTIONS)])
+                Q.all([PlatformApiPoly.createPlatform(PLATFORM_ROOT, FAKE_CONFIG, OPTIONS),
+                       PlatformApiPoly.updatePlatform(PLATFORM_ROOT, OPTIONS)])
                 .then(function () {
                     expect(spawn).toHaveBeenCalled();
                     expect(spawn.calls.length).toBe(2);
@@ -165,8 +166,8 @@ describe('PlatformApi polyfill', function () {
             });
 
             it('should copy cordova JS sources into created platform', function (done) {
-                Q.all([PlatformApiPoly.createPlatform(FAKE_PROJECT, OPTIONS),
-                       PlatformApiPoly.updatePlatform(FAKE_PROJECT, OPTIONS)])
+                Q.all([PlatformApiPoly.createPlatform(PLATFORM_ROOT, FAKE_CONFIG, OPTIONS),
+                       PlatformApiPoly.updatePlatform(PLATFORM_ROOT, OPTIONS)])
                 .then(function () {
                     expect(shell.cp).toHaveBeenCalled();
                     expect(shell.cp.calls.length).toBe(2);
@@ -178,8 +179,8 @@ describe('PlatformApi polyfill', function () {
             });
 
             it('should fail immediately if options.platformInfo is not specified', function (done) {
-                Q.all([PlatformApiPoly.createPlatform(FAKE_PROJECT),
-                       PlatformApiPoly.updatePlatform(FAKE_PROJECT)])
+                Q.all([PlatformApiPoly.createPlatform(PLATFORM_ROOT, FAKE_CONFIG),
+                       PlatformApiPoly.updatePlatform(PLATFORM_ROOT, FAKE_CONFIG)])
                 .then(success)
                 .fail(fail)
                 .fin(function function_name (argument) {

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/e2168918/cordova-lib/src/cordova/platform.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/cordova/platform.js b/cordova-lib/src/cordova/platform.js
index ed1862e..2f8875e 100644
--- a/cordova-lib/src/cordova/platform.js
+++ b/cordova-lib/src/cordova/platform.js
@@ -147,16 +147,6 @@ function addHelper(cmd, hooksRunner, projectRoot, targets, opts) {
                     }
                 }
 
-                var cordovaProject = {
-                    root: projectRoot,
-                    projectConfig: cfg,
-                    locations: {
-                        www: path.join(projectRoot, 'www'),
-                        platforms: path.join(projectRoot, 'platforms'),
-                        configXml: path.join(projectRoot, 'config.xml')
-                    }
-                };
-
                 var options = {
                     // We need to pass a platformDetails into update/create
                     // since PlatformApiPoly needs to know something about
@@ -175,17 +165,31 @@ function addHelper(cmd, hooksRunner, projectRoot, targets, opts) {
                 var PlatformApi;
                 try {
                     // Try to get PlatformApi class from platform
-                    PlatformApi = require(path.resolve(platDetails.libDir, 'bin/PlatformApi'));
-                } catch (err) {
-                    PlatformApi = require('../platforms/PlatformApiPoly');
+                    // Get an entry point for platform package
+                    var apiEntryPoint = require.resolve(platDetails.libDir);
+                    // Validate entry point filename. This is required since most of platforms
+                    // defines 'main' entry in package.json pointing to bin/create which is
+                    // basically a valid NodeJS script but intended to be used as a regular
+                    // executable script.
+                    if (path.basename(apiEntryPoint) === 'Api.js') {
+                        PlatformApi = require(apiEntryPoint);
+                        events.emit('verbose', 'PlatformApi successfully found for platform ' + platform);
+                    }
+                } catch (e) {
+                } finally {
+                    if (!PlatformApi) {
+                        events.emit('verbose', 'Failed to require PlatformApi instance for platform "' + platform +
+                            '". Using polyfill instead.');
+                        PlatformApi = require('../platforms/PlatformApiPoly');
+                    }
                 }
 
+                var destination = path.resolve(projectRoot, 'platforms', platform);
                 var promise = cmd === 'add' ?
-                    PlatformApi.createPlatform :
-                    PlatformApi.updatePlatform;
+                    PlatformApi.createPlatform.bind(null, destination, cfg, options, events) :
+                    PlatformApi.updatePlatform.bind(null, destination, options, events);
 
-                return promise(cordovaProject, options)
-                .then(function () {
+                return promise().then(function () {
                     // Call prepare for the current platform.
                     var prepOpts = {
                         platforms :[platform],

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/e2168918/cordova-lib/src/platforms/PlatformApiPoly.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/platforms/PlatformApiPoly.js b/cordova-lib/src/platforms/PlatformApiPoly.js
index 960f4de..5563028 100644
--- a/cordova-lib/src/platforms/PlatformApiPoly.js
+++ b/cordova-lib/src/platforms/PlatformApiPoly.js
@@ -71,35 +71,35 @@ function PlatformApiPoly(platform, platformRootDir, events) {
 /**
  * Installs platform to specified directory and creates a platform project.
  *
- * @param  {CordovaProject} cordovaProject A CordovaProject instance, that defines a
- *   project structure and configuration, that should be applied to new platform
- *   (contains platform's target location and ConfigParser instance for
- *   project's config). This argument is optional and if not defined, this means
- *   that platform is used as standalone project and is not a part of cordova
- *   project.
- * @param  {Object}  options  An options object. The most common options are:
- * @param  {String}  options.customTemplate  A path to custom template, that
+ * @param  {String}  destinationDir  A directory, where platform should be
+ *   created/installed.
+ * @param  {ConfigParser} [projectConfig] A ConfigParser instance, used to get
+ *   some application properties for new platform like application name, package
+ *   id, etc. If not defined, this means that platform is used as standalone
+ *   project and is not a part of cordova project, so platform will use some
+ *   default values.
+ * @param  {Object}   [options]  An options object. The most common options are:
+ * @param  {String}   [options.customTemplate]  A path to custom template, that
  *   should override the default one from platform.
- * @param  {Boolean}  options.link  Flag that indicates that platform's sources
- *   will be linked to installed platform instead of copying.
+ * @param  {Boolean}  [options.link=false]  Flag that indicates that platform's
+ *   sources will be linked to installed platform instead of copying.
  *
  * @return {Promise<PlatformApi>} Promise either fulfilled with PlatformApi
  *   instance or rejected with CordovaError.
  */
-PlatformApiPoly.createPlatform = function (cordovaProject, options) {
+PlatformApiPoly.createPlatform = function (destinationDir, projectConfig, options) {
     if (!options || !options.platformDetails)
-        return Q.reject(CordovaError('Failed to find platform\'s \'create\' script. ' +
+        return Q.reject(new CordovaError('Failed to find platform\'s \'create\' script. ' +
             'Either \'options\' parameter or \'platformDetails\' option is missing'));
 
     var command = path.join(options.platformDetails.libDir, 'bin', 'create');
-    var commandArguments = getCreateArgs(cordovaProject, options);
+    var commandArguments = getCreateArgs(destinationDir, projectConfig, options);
 
     return superspawn.spawn(command, commandArguments,
         { printCommand: true, stdio: 'inherit', chmod: true })
     .then(function () {
-        var destination = path.join(cordovaProject.locations.platforms, options.platformDetails.platform);
         var platformApi = knownPlatforms
-            .getPlatformApi(options.platformDetails.platform, destination);
+            .getPlatformApi(options.platformDetails.platform, destinationDir);
         copyCordovaSrc(options.platformDetails.libDir, platformApi.getPlatformInfo());
         return platformApi;
     });
@@ -108,34 +108,28 @@ PlatformApiPoly.createPlatform = function (cordovaProject, options) {
 /**
  * Updates already installed platform.
  *
- * @param   {CordovaProject}  cordovaProject  A CordovaProject instance, that
- *   defines a project structure and configuration, that should be applied to
- *   new platform (contains platform's target location and ConfigParser instance
- *   for project's config). This argument is optional and if not defined, this
- *   means that platform is used as standalone project and is not a part of
- *   cordova project.
- * @param  {Object}  options  An options object. The most common options are:
- * @param  {String}  options.customTemplate  A path to custom template, that
+ * @param  {String}  destinationDir  A directory, where existing platform
+ *   installed, that should be updated.
+ * @param  {Object}  [options]  An options object. The most common options are:
+ * @param  {String}  [options.customTemplate]  A path to custom template, that
  *   should override the default one from platform.
- * @param  {Boolean}  options.link  Flag that indicates that platform's sources
+ * @param  {Boolean}  [options.link=false]  Flag that indicates that platform's sources
  *   will be linked to installed platform instead of copying.
  *
  * @return {Promise<PlatformApi>} Promise either fulfilled with PlatformApi
  *   instance or rejected with CordovaError.
  */
-PlatformApiPoly.updatePlatform = function (cordovaProject, options) {
+PlatformApiPoly.updatePlatform = function (destinationDir, options) {
     if (!options || !options.platformDetails)
-        return Q.reject(CordovaError('Failed to find platform\'s \'create\' script. ' +
+        return Q.reject(new CordovaError('Failed to find platform\'s \'create\' script. ' +
             'Either \'options\' parameter or \'platformDetails\' option is missing'));
 
     var command = path.join(options.platformDetails.libDir, 'bin', 'update');
-    var destination = path.join(cordovaProject.locations.platforms, options.platformDetails.platform);
-
-    return superspawn.spawn(command, [destination],
+    return superspawn.spawn(command, [destinationDir],
         { printCommand: true, stdio: 'inherit', chmod: true })
     .then(function () {
         var platformApi = knownPlatforms
-            .getPlatformApi(options.platformDetails.platform, destination);
+            .getPlatformApi(options.platformDetails.platform, destinationDir);
         copyCordovaSrc(options.platformDetails.libDir, platformApi.getPlatformInfo());
         return platformApi;
     });
@@ -468,17 +462,17 @@ module.exports = PlatformApiPoly;
  * @return  {String[]}     An array or arguments which can be passed to
  *   'bin/create'.
  */
-function getCreateArgs(project, options) {
+function getCreateArgs(destinationDir, projectConfig, options) {
     var platformName = options.platformDetails.platform;
     var platformVersion = options.platformDetails.version;
 
     var args = [];
-    args.push(path.join(project.locations.platforms, platformName)); // output
-    args.push(project.projectConfig.packageName().replace(/[^\w.]/g,'_'));
+    args.push(destinationDir); // output
+    args.push(projectConfig.packageName().replace(/[^\w.]/g,'_'));
     // CB-6992 it is necessary to normalize characters
     // because node and shell scripts handles unicode symbols differently
     // We need to normalize the name to NFD form since iOS uses NFD unicode form
-    args.push(platformName == 'ios' ? unorm.nfd(project.projectConfig.name()) : project.projectConfig.name());
+    args.push(platformName == 'ios' ? unorm.nfd(projectConfig.name()) : projectConfig.name());
 
     if (options.customTemplate) {
         args.push(options.customTemplate);
@@ -490,7 +484,7 @@ function getCreateArgs(project, options) {
     if (options.link) args.push('--link');
 
     if (platformName === 'android' && semver.gte(platformVersion, '4.0.0-dev')) {
-        var activityName = project.projectConfig.android_activityName();
+        var activityName = projectConfig.android_activityName();
         if (activityName) {
             args.push('--activity-name', activityName.replace(/\W/g, ''));
         }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org