Skip to content

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

Merged
merged 1 commit into from
Oct 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ $injector.require("broccoliBuilder", "./tools/broccoli/builder");
$injector.require("nodeModulesTree", "./tools/broccoli/trees/node-modules-tree");
$injector.require("broccoliPluginWrapper", "./tools/broccoli/broccoli-plugin-wrapper");

$injector.require("pluginVariablesService", "./services/plugin-variables-service");
$injector.require("pluginsService", "./services/plugins-service");
$injector.requireCommand("plugin|add", "./commands/plugin/add-plugin");
$injector.requireCommand("plugin|remove", "./commands/plugin/remove-plugin");
Expand Down
2 changes: 1 addition & 1 deletion lib/common
1 change: 1 addition & 0 deletions lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export let APP_RESOURCES_FOLDER_NAME = "App_Resources";
export let PROJECT_FRAMEWORK_FOLDER_NAME = "framework";
export let NATIVESCRIPT_KEY_NAME = "nativescript";
export let NODE_MODULES_FOLDER_NAME = "node_modules";
export let TNS_MODULES_FOLDER_NAME = "tns_modules";
export let TNS_CORE_MODULES_NAME = "tns-core-modules";
export let PACKAGE_JSON_FILE_NAME = "package.json";
export let NODE_MODULE_CACHE_PATH_KEY_NAME = "node-modules-cache-path";
Expand Down
38 changes: 37 additions & 1 deletion lib/definitions/plugins.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
Copy link
Contributor

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?

pluginPlatformsFolderPath(platform: string): string;
}

interface INodeModuleData {
Expand All @@ -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>;
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
}
1 change: 1 addition & 0 deletions lib/definitions/project.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface IProjectDataService {
getValue(propertyName: string): IFuture<any>;
setValue(key: string, value: any): IFuture<void>;
removeProperty(propertyName: string): IFuture<void>;
removeDependency(dependencyName: string): IFuture<void>;
}

interface IProjectTemplatesService {
Expand Down
2 changes: 1 addition & 1 deletion lib/services/platform-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export class PlatformService implements IPlatformService {
try {
this.$pluginsService.ensureAllDependenciesAreInstalled().wait();
let tnsModulesDestinationPath = path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME, PlatformService.TNS_MODULES_FOLDER_NAME);
this.$broccoliBuilder.prepareNodeModules(tnsModulesDestinationPath, this.$projectData.projectDir, platform, lastModifiedTime).wait();
this.$broccoliBuilder.prepareNodeModules(tnsModulesDestinationPath, platform, lastModifiedTime).wait();
} catch(error) {
this.$logger.debug(error);
shell.rm("-rf", appResourcesDirectoryPath);
Expand Down
103 changes: 103 additions & 0 deletions lib/services/plugin-variables-service.ts
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this code really needed to be executed for every plugin variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have caching inside projectDataService so the logic will be executed only the first time.

Copy link
Contributor

Choose a reason for hiding this comment

The 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);

Loading