Skip to content

Commit 2933117

Browse files
Merge pull request #1322 from Icenium/vladimirov/fix-feedback-plugin
Make sure CLI works correctly with plugins
2 parents 845f6fc + b8b3e1c commit 2933117

File tree

2 files changed

+44
-28
lines changed

2 files changed

+44
-28
lines changed

lib/services/cordova-project-plugins-service.ts

+28-20
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,14 @@ export class CordovaProjectPluginsService implements IPluginsService {
128128
if(config) {
129129
corePlugins = this.$project.getProperty(CordovaProjectPluginsService.CORE_PLUGINS_PROPERTY_NAME, config);
130130
} else {
131-
corePlugins = <string[]>_.union(this.$project.getProperty(CordovaProjectPluginsService.CORE_PLUGINS_PROPERTY_NAME, this.$projectConstants.DEBUG_CONFIGURATION_NAME), this.$project.getProperty(CordovaProjectPluginsService.CORE_PLUGINS_PROPERTY_NAME, this.$projectConstants.RELEASE_CONFIGURATION_NAME));
131+
corePlugins = _.union<string>(this.$project.getProperty(CordovaProjectPluginsService.CORE_PLUGINS_PROPERTY_NAME, this.$projectConstants.DEBUG_CONFIGURATION_NAME),
132+
this.$project.getProperty(CordovaProjectPluginsService.CORE_PLUGINS_PROPERTY_NAME, this.$projectConstants.RELEASE_CONFIGURATION_NAME));
132133
}
133134

134135
return _.map(corePlugins, pluginIdentifier => {
135-
let plugin = this.identifierToPlugin[pluginIdentifier];
136+
let [name, version] = pluginIdentifier.split("@");
137+
let plugin = this.getBestMatchingPlugin(name, version);
138+
136139
if (!plugin) {
137140
let failMessage = config ?
138141
`You have enabled an invalid plugin: ${pluginIdentifier} for the ${config} build configuration. Check your .${config}.abproject file in the project root and correct or remove the invalid plugin entry.` :
@@ -144,6 +147,11 @@ export class CordovaProjectPluginsService implements IPluginsService {
144147
});
145148
}
146149

150+
private getBestMatchingPlugin(name: string, version: string): IPlugin {
151+
let plugins = this.getPluginInstancesByName(name, version);
152+
return _.find(plugins, pl => pl.type === PluginType.MarketplacePlugin) || plugins[0];
153+
}
154+
147155
public getAvailablePlugins(pluginsCount?: number): IPlugin[] {
148156
let plugins = _.values(this.identifierToPlugin);
149157
if(this.$project.projectData) {
@@ -183,8 +191,13 @@ export class CordovaProjectPluginsService implements IPluginsService {
183191
if(installedPluginInstances && installedPluginInstances.length > 0) {
184192
let installedPluginsType = _.chain(installedPluginInstances).groupBy((pl: IPlugin) => pl.type).keys().value();
185193
if(installedPluginsType.length > 1) {
186-
// We should NEVER get here. CorePlugins and Marketplace plugins cannot have duplicate identifiers.
187-
this.$errors.failWithoutHelp("There are several plugins with name '%s' and they have different types: '%s'", pluginName, installedPluginsType.join(", "));
194+
// In case integrated and Marketplace plugins have duplicate identifiers, try using MarketplacePlugin
195+
let mpPlugin = _.find(installedPluginInstances, pl => pl.type === PluginType.MarketplacePlugin);
196+
if(mpPlugin) {
197+
return this.modifyInstalledMarketplacePlugin(mpPlugin.data.Identifier, version).wait();
198+
} else {
199+
this.$errors.failWithoutHelp("There are several plugins with name '%s' and they have different types: '%s'", pluginName, installedPluginsType.join(", "));
200+
}
188201
} else if(installedPluginsType.length === 1) {
189202
if(installedPluginsType[0].toString() === PluginType.MarketplacePlugin.toString()) {
190203
return this.modifyInstalledMarketplacePlugin(installedPluginInstances[0].data.Identifier, version).wait();
@@ -199,11 +212,11 @@ export class CordovaProjectPluginsService implements IPluginsService {
199212
}
200213
}
201214

202-
let pluginToAdd = this.getPluginByName(pluginName);
215+
let pluginToAdd = this.getBestMatchingPlugin(pluginName, version);
203216
if(pluginToAdd.type === PluginType.MarketplacePlugin) {
204217
version = this.selectPluginVersion(version, pluginToAdd).wait();
205218
if(!this.isPluginSupported(pluginToAdd, this.$project.projectData.FrameworkVersion, version)) {
206-
this.$errors.failWithoutHelp(`Plugin ${pluginName} is not avaialble for framework version '${this.$project.projectData.FrameworkVersion}'.`);
219+
this.$errors.failWithoutHelp(`Plugin ${pluginName} is not available for framework version '${this.$project.projectData.FrameworkVersion}'.`);
207220
}
208221
}
209222

@@ -327,7 +340,7 @@ export class CordovaProjectPluginsService implements IPluginsService {
327340

328341
private configurePluginCore(pluginName: string, configuration?: string, version?: string): IFuture<void> {
329342
return (() => {
330-
let plugin = this.getPluginByName(pluginName);
343+
let plugin = this.getBestMatchingPlugin(pluginName, version);
331344
let pluginData = <IMarketplacePluginData>plugin.data;
332345
let cordovaPluginVariables = this.$project.getProperty(CordovaProjectPluginsService.CORDOVA_PLUGIN_VARIABLES_PROPERTY_NAME, configuration) || {};
333346

@@ -346,10 +359,7 @@ export class CordovaProjectPluginsService implements IPluginsService {
346359

347360
let newCorePlugins = this.$project.getProperty(CordovaProjectPluginsService.CORE_PLUGINS_PROPERTY_NAME, configuration) || [];
348361
// remove all instances of the plugin from current configuration
349-
let lowerCasePluginIdentifier = plugin.data.Identifier.toLowerCase();
350-
let installedPlugin = this.getInstalledPluginsForConfiguration(configuration).filter((pl: IPlugin) => pl.data.Identifier.toLowerCase() === lowerCasePluginIdentifier);
351-
352-
_.each(installedPlugin, pl => newCorePlugins = _.without(newCorePlugins, pl.toProjectDataRecord(pl.data.Version)));
362+
newCorePlugins = _.without(newCorePlugins, ...this.getPluginInstancesByName(plugin.data.Identifier).map(plug => plug.toProjectDataRecord()));
353363

354364
newCorePlugins.push(plugin.toProjectDataRecord(version));
355365
this.$project.setProperty(CordovaProjectPluginsService.CORE_PLUGINS_PROPERTY_NAME, newCorePlugins, configuration);
@@ -408,12 +418,11 @@ export class CordovaProjectPluginsService implements IPluginsService {
408418
_.each(configurations, (configData: IDictionary<any>, configuration:string) => {
409419
if (configData) {
410420
let corePlugins = configData[CordovaProjectPluginsService.CORE_PLUGINS_PROPERTY_NAME];
411-
if (corePlugins && _.contains(corePlugins, projectDataRecord)) {
421+
if (corePlugins && (_.contains(corePlugins, projectDataRecord) || _.contains(corePlugins, pluginData.data.Identifier))) {
412422
pluginData.configurations.push(configuration);
413423
}
414424
}
415425
});
416-
417426
this._identifierToPlugin[projectDataRecord] = pluginData;
418427
} else {
419428
this.$logger.warn("Unable to fetch data for plugin %s.", plugin.Identifier);
@@ -445,11 +454,10 @@ export class CordovaProjectPluginsService implements IPluginsService {
445454
}).future<any>()();
446455
}
447456

448-
private getPluginByName(pluginName: string, version?: string): IPlugin {
457+
private getPluginInstancesByName(pluginName: string, version?: string): IPlugin[] {
449458
let plugins = this.getAvailablePlugins();
450459
let toLowerCasePluginName = pluginName.toLowerCase();
451-
452-
let plugin = _.find(plugins, (_plugin: IPlugin) => {
460+
let matchingPlugins = _.filter(plugins, (_plugin: IPlugin) => {
453461
let condition = _plugin.data.Name.toLowerCase() === toLowerCasePluginName || _plugin.data.Identifier.toLowerCase() === toLowerCasePluginName;
454462
if(version) {
455463
condition = condition && _plugin.data.Version === version;
@@ -458,11 +466,11 @@ export class CordovaProjectPluginsService implements IPluginsService {
458466
return condition;
459467
});
460468

461-
if(!plugin) {
469+
if(!matchingPlugins || !matchingPlugins.length) {
462470
this.$errors.fail("Invalid plugin name: %s", pluginName);
463471
}
464472

465-
return plugin;
473+
return matchingPlugins;
466474
}
467475

468476
private promptForVersion(pluginName: string, versions: any[]): IFuture<string> {
@@ -505,10 +513,10 @@ export class CordovaProjectPluginsService implements IPluginsService {
505513
pluginName = pluginName.toLowerCase();
506514
let isConsoleInteractive = helpers.isInteractive();
507515
let allInstalledPlugins = this.getInstalledPluginsForConfiguration();
508-
let installedPluginInstances = _.filter(allInstalledPlugins,(plugin: IPlugin) => plugin.data.Name.toLowerCase() === pluginName || plugin.data.Identifier.toLowerCase() === pluginName);
516+
let installedPluginInstances = _.filter(allInstalledPlugins, (plugin: IPlugin) => plugin.data.Name.toLowerCase() === pluginName || plugin.data.Identifier.toLowerCase() === pluginName);
509517
let selectedVersion: string;
510518
if(installedPluginInstances.length > 1) {
511-
this.$logger.warn(`Plugin '${pluginName}' is enabled with different versions in your project configurations. You must use the same version in all configurations.'`);
519+
this.$logger.warn(`Plugin '${pluginName}' is enabled with different versions in your project configurations. You must use the same version in all configurations.`);
512520
}
513521

514522
_.each(installedPluginInstances, (pl: IPlugin) => {

test/plugins-service.ts

+16-8
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,8 @@ describe("plugins-service", () => {
303303
Versions: [{
304304
Identifier: "com.telerik.stripe",
305305
Name: "Stripe",
306-
Version: "1.0.4"
306+
Version: "1.0.4",
307+
SupportedVersion: ">=3.5.0"
307308
}]
308309
}];
309310

@@ -471,12 +472,14 @@ describe("plugins-service", () => {
471472
let installedMarketplacePlugins = [{
472473
Identifier: "com.telerik.stripe",
473474
Name: "Stripe",
474-
Version: "1.0.4"
475+
Version: "1.0.4",
476+
SupportedVersion: ">=3.5.0"
475477
},
476478
{
477479
Identifier: "nl.x-services.plugins.toast",
478480
Name: "Toast",
479-
Version: "2.0.1"
481+
Version: "2.0.1",
482+
SupportedVersion: ">=3.5.0"
480483
}
481484
];
482485
let availableMarketplacePlugins = [
@@ -486,7 +489,8 @@ describe("plugins-service", () => {
486489
Versions: [{
487490
Identifier: "com.telerik.stripe",
488491
Name: "Stripe",
489-
Version: "1.0.4"
492+
Version: "1.0.4",
493+
SupportedVersion: ">=3.5.0"
490494
}]
491495
},
492496
{
@@ -495,7 +499,8 @@ describe("plugins-service", () => {
495499
Versions: [{
496500
Identifier: "nl.x-services.plugins.toast",
497501
Name: "Toast",
498-
Version: "2.0.1"
502+
Version: "2.0.1",
503+
SupportedVersion: ">=3.5.0"
499504
}]
500505
}
501506
];
@@ -545,7 +550,8 @@ describe("plugins-service", () => {
545550
Versions: [{
546551
Identifier: "nl.x-services.plugins.toast",
547552
Name: "Toast",
548-
Version: "2.0.1"
553+
Version: "2.0.1",
554+
SupportedVersion: ">=3.5.0"
549555
}]
550556
},
551557
{
@@ -554,7 +560,8 @@ describe("plugins-service", () => {
554560
Versions: [{
555561
Identifier: "com.telerik.stripe",
556562
Name: "Stripe",
557-
Version: "1.0.4"
563+
Version: "1.0.4",
564+
SupportedVersion: ">=3.5.0"
558565
}]
559566
}
560567
];
@@ -587,7 +594,8 @@ describe("plugins-service", () => {
587594
Versions: [{
588595
Identifier: "com.telerik.stripe",
589596
Name: "Stripe",
590-
Version: "1.0.4"
597+
Version: "1.0.4",
598+
SupportedVersion: ">=3.5.0"
591599
}]
592600
}
593601
];

0 commit comments

Comments
 (0)