-
-
Notifications
You must be signed in to change notification settings - Fork 197
Plugin variables #981
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Plugin variables #981
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
+1 −0 | bootstrap.ts | |
+6 −0 | declarations.d.ts | |
+1 −0 | definitions/node-fibers.d.ts | |
+3 −1 | errors.ts | |
+2 −1 | options.ts | |
+93 −0 | plugin-variables-helper.ts |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,9 @@ interface IPluginsService { | |
|
||
interface IPluginData extends INodeModuleData { | ||
platformsData: IPluginPlatformsData; | ||
pluginPlatformsFolderPath(platform: string): string; | ||
/* Gets all plugin variables from plugin */ | ||
pluginVariables: IDictionary<IPluginVariableData>; | ||
pluginPlatformsFolderPath(platform: string): string; | ||
} | ||
|
||
interface INodeModuleData { | ||
|
@@ -23,4 +25,38 @@ interface INodeModuleData { | |
interface IPluginPlatformsData { | ||
ios: string; | ||
android: string; | ||
} | ||
|
||
interface IPluginVariablesService { | ||
/** | ||
* Saves plugin variables in project package.json file. | ||
* @param {IPluginData} pluginData for the plugin. | ||
* @return {IFuture<void>} | ||
*/ | ||
savePluginVariablesInProjectFile(pluginData: IPluginData): IFuture<void>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto, consider documenting this interface |
||
/** | ||
* Removes plugin variables from project package.json file. | ||
* @param {IPluginData} pluginData for the plugin. | ||
* @return {IFuture<void>} | ||
*/ | ||
removePluginVariablesFromProjectFile(pluginData: IPluginData): IFuture<void>; | ||
/** | ||
* Replaces all plugin variables with their corresponding values. | ||
* @param {IPluginData} pluginData for the plugin. | ||
* @param {pluginConfigurationFileContent} pluginConfigurationFileContent for the plugin. | ||
* @return {IFuture<string>} returns the changed plugin configuration file content. | ||
*/ | ||
interpolatePluginVariables(pluginData: IPluginData, pluginConfigurationFileContent: string): IFuture<string>; | ||
/** | ||
* Returns the | ||
* @param {IPluginData} pluginData for the plugin. | ||
* @return {IFuture<string>} returns the changed plugin configuration file content. | ||
*/ | ||
getPluginVariablePropertyName(pluginData: IPluginData): string; | ||
} | ||
|
||
interface IPluginVariableData { | ||
defaultValue?: string; | ||
name?: string; | ||
value?: string; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
///<reference path="../.d.ts"/> | ||
"use strict"; | ||
|
||
import * as helpers from "./../common/helpers"; | ||
|
||
export class PluginVariablesService implements IPluginVariablesService { | ||
private static PLUGIN_VARIABLES_KEY = "variables"; | ||
|
||
constructor(private $errors: IErrors, | ||
private $pluginVariablesHelper: IPluginVariablesHelper, | ||
private $projectData: IProjectData, | ||
private $projectDataService: IProjectDataService, | ||
private $prompter: IPrompter) { } | ||
|
||
public getPluginVariablePropertyName(pluginData: IPluginData): string { | ||
return `${pluginData.name}-${PluginVariablesService.PLUGIN_VARIABLES_KEY}`; | ||
} | ||
|
||
public savePluginVariablesInProjectFile(pluginData: IPluginData): IFuture<void> { | ||
return (() => { | ||
let values = Object.create(null); | ||
this.executeForAllPluginVariables(pluginData, (pluginVariableData: IPluginVariableData) => | ||
(() => { | ||
let pluginVariableValue = this.getPluginVariableValue(pluginVariableData).wait(); | ||
this.ensurePluginVariableValue(pluginVariableValue, `Unable to find value for ${pluginVariableData.name} plugin variable from ${pluginData.name} plugin. Ensure the --var option is specified or the plugin variable has default value.`); | ||
values[pluginVariableData.name] = pluginVariableValue; | ||
}).future<void>()()).wait(); | ||
|
||
this.$projectDataService.initialize(this.$projectData.projectDir); | ||
this.$projectDataService.setValue(this.getPluginVariablePropertyName(pluginData), values).wait(); | ||
}).future<void>()(); | ||
} | ||
|
||
public removePluginVariablesFromProjectFile(pluginData: IPluginData): IFuture<void> { | ||
this.$projectDataService.initialize(this.$projectData.projectDir); | ||
return this.$projectDataService.removeProperty(this.getPluginVariablePropertyName(pluginData)); | ||
} | ||
|
||
public interpolatePluginVariables(pluginData: IPluginData, pluginConfigurationFileContent: string): IFuture<string> { | ||
return (() => { | ||
this.executeForAllPluginVariables(pluginData, (pluginVariableData: IPluginVariableData) => | ||
(() => { | ||
this.ensurePluginVariableValue(pluginVariableData.value, `Unable to find the value for ${pluginVariableData.name} plugin variable into project package.json file. Verify that your package.json file is correct and try again.`); | ||
pluginConfigurationFileContent = pluginConfigurationFileContent.replace(new RegExp(`{${pluginVariableData.name}}`, "gi"), pluginVariableData.value); | ||
}).future<void>()()).wait(); | ||
return pluginConfigurationFileContent; | ||
}).future<string>()(); | ||
} | ||
|
||
private ensurePluginVariableValue(pluginVariableValue: string, errorMessage: string): void { | ||
if(!pluginVariableValue) { | ||
this.$errors.failWithoutHelp(errorMessage); | ||
} | ||
} | ||
|
||
private getPluginVariableValue(pluginVariableData: IPluginVariableData): IFuture<string> { | ||
return (() => { | ||
let pluginVariableName = pluginVariableData.name; | ||
let value = this.$pluginVariablesHelper.getPluginVariableFromVarOption(pluginVariableName); | ||
if(value) { | ||
value = value[pluginVariableName]; | ||
} else { | ||
value = pluginVariableData.defaultValue; | ||
if(!value && helpers.isInteractive() ) { | ||
let promptSchema = { | ||
name: pluginVariableName, | ||
type: "input", | ||
message: `Enter value for ${pluginVariableName} variable:`, | ||
validate: (val: string) => !!val ? true : 'Please enter a value!' | ||
}; | ||
let promptData = this.$prompter.get([promptSchema]).wait(); | ||
value = promptData[pluginVariableName]; | ||
} | ||
} | ||
|
||
return value; | ||
}).future<string>()(); | ||
} | ||
|
||
private executeForAllPluginVariables(pluginData: IPluginData, action: (pluginVariableData: IPluginVariableData) => IFuture<void>): IFuture<void> { | ||
return (() => { | ||
let pluginVariables = pluginData.pluginVariables; | ||
let pluginVariablesNames = _.keys(pluginVariables); | ||
_.each(pluginVariablesNames, pluginVariableName => action(this.createPluginVariableData(pluginData, pluginVariableName).wait()).wait()); | ||
}).future<void>()(); | ||
} | ||
|
||
private createPluginVariableData(pluginData: IPluginData, pluginVariableName: string): IFuture<IPluginVariableData> { | ||
return (() => { | ||
let variableData = pluginData.pluginVariables[pluginVariableName]; | ||
|
||
variableData.name = pluginVariableName; | ||
|
||
this.$projectDataService.initialize(this.$projectData.projectDir); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this code really needed to be executed for every plugin variable? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have caching inside There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is implementation detail. The code simply does not belong to the callback. In case we decide to refactor projectDataService we need to remember to change this code as well. |
||
let pluginVariableValues = this.$projectDataService.getValue(this.getPluginVariablePropertyName(pluginData)).wait(); | ||
variableData.value = pluginVariableValues ? pluginVariableValues[pluginVariableName] : undefined; | ||
|
||
return variableData; | ||
}).future<IPluginVariableData>()(); | ||
} | ||
} | ||
$injector.register("pluginVariablesService", PluginVariablesService); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we document these members with jsdoc?