cordova-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From riknoll <...@git.apache.org>
Subject [GitHub] cordova-lib pull request: New plugin version selection implementat...
Date Thu, 03 Mar 2016 00:59:41 GMT
Github user riknoll commented on a diff in the pull request:

    https://github.com/apache/cordova-lib/pull/363#discussion_r54820983
  
    --- Diff: cordova-lib/src/cordova/plugin.js ---
    @@ -512,3 +543,219 @@ function versionString(version) {
     
         return null;
     }
    +
    +/**
    + * Gets the version of a plugin that should be fetched for a given project based
    + * on the plugin's engine information from NPM and the platforms/plugins installed
    + * in the project. The cordovaDependencies object in the package.json's engines
    + * entry takes the form of an object that maps plugin versions to a series of
    + * constraints and semver ranges. For example:
    + *
    + *     { plugin-version: { constraint: semver-range, ...}, ...}
    + *
    + * Constraint can be a plugin, platform, or cordova version. Plugin-version
    + * can be either a single version (e.g. 3.0.0) or an upper bound (e.g. <3.0.0)
    + *
    + * @param {string}  projectRoot     The path to the root directory of the project
    + * @param {object}  pluginInfo      The NPM info of the plugin be fetched (e.g. the
    + *                                  result of calling `registry.info()`)
    + * @param {string}  cordovaVersion  The semver version of cordova-lib
    + *
    + * @return {Promise}                A promise that will resolve to either a string
    + *                                  if there is a version of the plugin that this
    + *                                  project satisfies or null if there is not
    + */
    +function getFetchVersion(projectRoot, pluginInfo, cordovaVersion) {
    +    // Figure out the project requirements
    +    if (pluginInfo.engines && pluginInfo.engines.cordovaDependencies) {
    +        var pluginList = getInstalledPlugins(projectRoot);
    +        var pluginMap = {};
    +
    +        pluginList.forEach(function(plugin) {
    +            pluginMap[plugin.id] = plugin.version;
    +        });
    +
    +        return cordova_util.getInstalledPlatformsWithVersions(projectRoot)
    +        .then(function(platformVersions) {
    +            return determinePluginVersionToFetch(
    +                pluginInfo.versions,
    +                pluginInfo.engines.cordovaDependencies,
    +                pluginMap,
    +                platformVersions,
    +                cordovaVersion);
    +        });
    +    } else {
    +        // If we have no engine, we want to fall back to the default behavior
    +        events.emit('verbose', 'No plugin engine info found or not using registry, falling
back to latest or pinned version');
    +        return Q(null);
    +    }
    +}
    +
    +function findVersion(versions, version) {
    +    var cleanedVersion = semver.clean(version);
    +    for(var i = 0; i < versions.length; i++) {
    +        if(semver.clean(versions[i]) === cleanedVersion) {
    +            return versions[i];
    +        }
    +    }
    +    return null;
    +}
    +
    +/*
    + * The engine entry maps plugin versions to constraints like so:
    + *  {
    + *      '1.0.0' : { 'cordova': '<5.0.0' },
    + *      '<2.0.0': {
    + *          'cordova': '>=5.0.0',
    + *          'cordova-ios': '~5.0.0',
    + *          'cordova-plugin-camera': '~5.0.0'
    + *      },
    + *      '3.0.0' : { 'cordova-ios': '>5.0.0' }
    + *  }
    + *
    + * See cordova-spec/plugin_fetch.spec.js for test cases and examples
    + */
    +function determinePluginVersionToFetch(allVersions, engine, pluginMap, platformMap, cordovaVersion)
{
    +    // Filters out pre-release versions
    +    var latest = semver.maxSatisfying(allVersions, '>=0.0.0');
    +
    +    var versions = [];
    +    var upperBound = null;
    +    var upperBoundRange = null;
    +
    +    for(var version in engine) {
    +        if(semver.valid(semver.clean(version)) && !semver.gt(version, latest))
{
    +            versions.push(version);
    +        } else {
    +            // Check if this is an upperbound; validRange() handles whitespace
    +            var cleanedRange = semver.validRange(version);
    +            if(cleanedRange && UPPER_BOUND_REGEX.exec(cleanedRange)) {
    +                // We only care about the highest upper bound that our project does not
support
    +                if(getFailedRequirements(engine[version], pluginMap, platformMap, cordovaVersion).length
!== 0) {
    +                    var maxMatchingUpperBound = cleanedRange.substring(1);
    +                    if (maxMatchingUpperBound && (!upperBound || semver.gt(maxMatchingUpperBound,
upperBound))) {
    +                        upperBound = maxMatchingUpperBound;
    +                        upperBoundRange = version;
    +                    }
    +                }
    +            }
    +        }
    +    }
    +
    --- End diff --
    
    Right, they are essentially equivalent and will fetch the same version in both scenarios
(except for pinned plugins, but that is a different matter). The only difference will be in
that verbose message, which I am okay with because I think that there is a distinction between
giving an empty `cordovaDependencies` and ignoring the feature. One says "I have no dependencies"
and the other doesn't really say anything. I think it is unnecessary to return the special
case of `null`, because returning latest better matches the expected behavior of this function.
I can still make it return here if that helps clarify the code. I just think it should return
latest rather than `null`.


---
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


Mime
View raw message