Skip to content

Commit 2bced8e

Browse files
Merge pull request #1104 from Icenium/vladimirov/validate-plugin-framework-version
Filter plugins not supported for projects Cordova version
2 parents 04986d3 + fb21951 commit 2bced8e

6 files changed

+302
-46
lines changed

lib/declarations.d.ts

-8
Original file line numberDiff line numberDiff line change
@@ -519,14 +519,6 @@ interface IPluginsService {
519519
* @return {IBasicPluginInformation} Basic information about the plugin
520520
*/
521521
getPluginBasicInformation(pluginName: string): IBasicPluginInformation;
522-
/**
523-
* Checks wether a plugin is supported for a specific framework version
524-
* @param {string} plugin The name of the plugin
525-
* @param {string} version The plugin's version
526-
* @param {string} frameworkVersion The framework's version
527-
* @return {boolean} true if the plugin is supported, false otherwise
528-
*/
529-
isPluginSupported(plugin: string, version: string, frameworkVersion: string): boolean;
530522
}
531523

532524
interface IPlugin {

lib/services/cordova-migration-service.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -215,15 +215,15 @@ export class CordovaMigrationService implements ICordovaMigrationService {
215215

216216
this.$logger.info("Migrating to Cordova version %s", versionDisplayName);
217217
let oldVersion = this.$project.projectData.FrameworkVersion;
218-
218+
let availablePlugins = this.$pluginsService.getAvailablePlugins();
219219
this.invalidMarketplacePlugins = _(this.$project.configurations)
220220
.map(configuration => <string[]>this.$project.getProperty("CorePlugins", configuration))
221221
.union()
222222
.flatten<string>()
223223
.unique()
224224
.filter((plugin: string) => {
225225
let pluginBasicInformation = this.$pluginsService.getPluginBasicInformation(plugin);
226-
return _.contains(plugin, '@') && !this.$pluginsService.isPluginSupported(pluginBasicInformation.name, pluginBasicInformation.version, newVersion)
226+
return _.contains(plugin, '@') && !_.any(availablePlugins, pl => pl.data.Identifier.toLowerCase() === pluginBasicInformation.name.toLowerCase() && pl.data.Version === pluginBasicInformation.version)
227227
})
228228
.value();
229229

lib/services/plugins-service.ts

+30-25
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,13 @@ export class PluginsService implements IPluginsService {
8585
}
8686

8787
public getAvailablePlugins(): IPlugin[] {
88-
return _.values(this.identifierToPlugin);
88+
let plugins = _.values(this.identifierToPlugin);
89+
if(this.$project.projectData) {
90+
plugins = _.filter(plugins, pl => this.isPluginSupported(pl, this.$project.projectData.FrameworkVersion));
91+
}
92+
return plugins;
8993
}
94+
9095
private get configurations(): string[]{
9196
return [this.$projectConstants.DEBUG_CONFIGURATION_NAME, this.$projectConstants.RELEASE_CONFIGURATION_NAME];
9297
}
@@ -136,6 +141,9 @@ export class PluginsService implements IPluginsService {
136141
let pluginToAdd = this.getPluginByName(pluginName);
137142
if(pluginToAdd.type === pluginsDataLib.PluginType.MarketplacePlugin) {
138143
version = this.selectPluginVersion(version, pluginToAdd).wait();
144+
if(!this.isPluginSupported(pluginToAdd, this.$project.projectData.FrameworkVersion, version)) {
145+
this.$errors.failWithoutHelp(`Plugin ${pluginName} is not avaialble for framework version '${this.$project.projectData.FrameworkVersion}'.`);
146+
}
139147
}
140148

141149
let configurations = this.$project.configurations;
@@ -220,12 +228,18 @@ export class PluginsService implements IPluginsService {
220228
return { name, version };
221229
}
222230

223-
public isPluginSupported(plugin: string, version: string, frameworkVersion: string): boolean {
224-
let pluginCordovaVersionRange = _.find(this.getPluginVersions(plugin), v => v.value === version).cordovaVersionRange;
225-
return semver.satisfies(frameworkVersion, pluginCordovaVersionRange);
231+
private isPluginSupported(plugin: IPlugin, frameworkVersion: string, pluginVersion?: string): boolean {
232+
if(!this.isMarketplacePlugin(plugin)) {
233+
return true;
234+
}
235+
236+
pluginVersion = pluginVersion || plugin.data.Version;
237+
let pluginVersions = this.getPluginVersions(plugin);
238+
let version = _.find(pluginVersions, v => v.value === pluginVersion);
239+
return version && semver.satisfies(frameworkVersion, version.cordovaVersionRange);
226240
}
227241

228-
public getInstalledPluginByName(pluginName: string): IPlugin[] {
242+
private getInstalledPluginByName(pluginName: string): IPlugin[] {
229243
pluginName = pluginName.toLowerCase();
230244
let installedPlugins = this.getInstalledPlugins();
231245
let installedPluginInstances = _.filter(installedPlugins,(plugin: IPlugin) => plugin.data.Name.toLowerCase() === pluginName || plugin.data.Identifier.toLowerCase() === pluginName);
@@ -378,28 +392,19 @@ export class PluginsService implements IPluginsService {
378392
return plugin;
379393
}
380394

381-
public getPluginVersions(pluginName: string): IPluginVersion[] {
382-
let toLowerCasePluginName = pluginName.toLowerCase();
383-
384-
let versions:any[] = [];
385-
_.each(this.getAvailablePlugins(), (plugin:IPlugin) => {
386-
if (plugin.data.Name.toLowerCase() === toLowerCasePluginName || plugin.data.Identifier.toLowerCase() === toLowerCasePluginName) {
387-
let pluginVersionsData = (<IMarketplacePlugin>plugin).pluginVersionsData;
388-
versions = _.map(pluginVersionsData.Versions, p => {
389-
let pluginVersion: IPluginVersion = {
390-
name: p.Version,
391-
value: p.Version,
392-
cordovaVersionRange: p.SupportedVersion
393-
}
394-
395-
return pluginVersion;
396-
});
397-
return false;
395+
public getPluginVersions(plugin: IPlugin): IPluginVersion[] {
396+
let pluginVersionsData = (<IMarketplacePlugin>plugin).pluginVersionsData;
397+
return _.map(pluginVersionsData.Versions, p => {
398+
let pluginVersion: IPluginVersion = {
399+
name: p.Version,
400+
value: p.Version,
401+
cordovaVersionRange: p.SupportedVersion
398402
}
399-
});
400403

401-
return versions;
404+
return pluginVersion;
405+
});
402406
}
407+
403408
private promptForVersion(pluginName: string, versions: any[]): IFuture<string> {
404409
return (() => {
405410
return versions.length > 1 ? this.promptForVersionCore(pluginName, versions).wait() : versions[0].value;
@@ -500,7 +505,7 @@ export class PluginsService implements IPluginsService {
500505
private selectPluginVersion(version: string, plugin: IPlugin, options?: { excludeCurrentVersion: boolean }): IFuture<string> {
501506
return ((): string => {
502507
let pluginName = plugin.data.Name;
503-
let versions = this.getPluginVersions(pluginName);
508+
let versions = this.getPluginVersions(plugin);
504509
if(version) {
505510
if(!_.any(versions, v => v.value === version)) {
506511
this.$errors.failWithoutHelp("Invalid version %s. The valid versions are: %s.", version, versions.map(v => v.value).join(", "));

test/cordova-migration-service.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ testInjector.register("pluginsService", {
2121
version: '1.0.0'
2222
}
2323
},
24-
getPluginVersions: (pluginName: string) => {
24+
getPluginVersions: (plugin: IPlugin) => {
2525
return [{
2626
name: '1.0.0',
2727
value: '1.0.0',
2828
minCordova: '3.0.0'
2929
}]
3030
},
3131
removePlugin: (pluginName: string) => {return (() => { }).future<void>()() },
32-
isPluginSupported: (plugin: string, version: string, migrationVersion: string) => { return true;}
32+
isPluginSupported: (plugin: IPlugin, version: string, migrationVersion: string) => { return true;}
3333
});
3434
testInjector.register("project", {});
3535
testInjector.register("projectConstants", {});

0 commit comments

Comments
 (0)