Skip to content
This repository was archived by the owner on Feb 2, 2021. It is now read-only.

Commit 95ae6b3

Browse files
Merge pull request #477 from telerik/vladimirov/plugin-vars
Plugin variables
2 parents aa3b05f + 39f7bbc commit 95ae6b3

File tree

5 files changed

+103
-1
lines changed

5 files changed

+103
-1
lines changed

bootstrap.ts

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ $injector.require("httpClient", "./http-client");
2727
$injector.require("childProcess", "./child-process");
2828
$injector.require("prompter", "./prompter");
2929
$injector.require("projectHelper", "./project-helper");
30+
$injector.require("pluginVariablesHelper", "./plugin-variables-helper");
3031
$injector.require("propertiesParser", "./properties-parser");
3132

3233
$injector.requireCommand(["help", "/?"], "./commands/help");

declarations.d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ interface ICommonOptions {
386386
force: boolean;
387387
companion: boolean;
388388
emulator: boolean;
389+
var: Object;
389390
}
390391

391392
interface IYargArgv extends IDictionary<any> {
@@ -507,3 +508,8 @@ interface IResourceLoader {
507508
*/
508509
getPathToAppResources(framework: string): string;
509510
}
511+
512+
interface IPluginVariablesHelper {
513+
getPluginVariableFromVarOption(variableName: string, configuration?: string): any;
514+
simplifyYargsObject(obj: any, configuration?: string): any;
515+
}

definitions/node-fibers.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ declare module "fibers/future" {
7676
static settle(...future_list: IFuture<any>[]): void;
7777

7878
static fromResult<T>(value: T): IFuture<T>;
79+
static fromResult<T>(value: any): IFuture<T>;
7980
static fromResult(): IFuture<void>;
8081

8182
static assertNoFutureLeftBehind(): void;

options.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ export class OptionsBase {
6666
"file": { type: OptionType.String },
6767
"force": { type: OptionType.Boolean, alias: "f" },
6868
"companion": { type: OptionType.Boolean },
69-
"emulator": { type: OptionType.Boolean }
69+
"emulator": { type: OptionType.Boolean },
70+
var: {type: OptionType.Object},
7071
};
7172
}
7273

plugin-variables-helper.ts

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
///<reference path=".d.ts"/>
2+
"use strict";
3+
4+
export class PluginVariablesHelper implements IPluginVariablesHelper {
5+
constructor(private $options: ICommonOptions) { }
6+
7+
/**
8+
* Checks if the specified pluginVariable exists in the --var option specified by user.
9+
* The variable can be added to --var option for configuration or globally, for ex.:
10+
* `--var.APP_ID myAppIdentifier` or `--var.debug.APP_ID myAppIdentifier`.
11+
* NOTE: If the variable is added for specific configuration and globally,
12+
* the value for the specified configuration will be used as it has higher priority. For ex.:
13+
* `--var.APP_ID myAppIdentifier1 --var.debug.APP_ID myAppIdentifier2` will return myAppIdentifier2 for debug configuration
14+
* and myAppIdentifier for release configuration.
15+
* @param {string} variableName The name of the plugin variable.
16+
* @param {string} configuration The configuration for which the variable will be used.
17+
* @returns {any} The value of the plugin variable specified in --var or undefined.
18+
*/
19+
public getPluginVariableFromVarOption(variableName: string, configuration?: string): any {
20+
let varOption = this.$options.var;
21+
configuration = configuration ? configuration.toLowerCase() : undefined;
22+
let lowerCasedVariableName = variableName.toLowerCase();
23+
if(varOption) {
24+
let configVariableValue: string;
25+
let generalVariableValue: string;
26+
if(variableName.indexOf(".") !== -1) {
27+
varOption = this.simplifyYargsObject(varOption, configuration);
28+
}
29+
_.each(varOption, (propValue: any, propKey: string) => {
30+
if(propKey.toLowerCase() === configuration) {
31+
_.each(propValue, (configPropValue: string, configPropKey: string) => {
32+
if(configPropKey.toLowerCase() === lowerCasedVariableName) {
33+
configVariableValue = configPropValue;
34+
return false;
35+
}
36+
});
37+
} else if(propKey.toLowerCase() === lowerCasedVariableName) {
38+
generalVariableValue = propValue;
39+
}
40+
});
41+
42+
let value = configVariableValue || generalVariableValue;
43+
if(value) {
44+
let obj = Object.create(null);
45+
obj[variableName] = value.toString();
46+
return obj;
47+
}
48+
}
49+
50+
return undefined;
51+
}
52+
53+
/**
54+
* Converts complicated yargs object with many subobjects, to simplified one.
55+
* Use it when the plugin variable contains dots ("."). In this case yargs treats them as inner object instead of propery name.
56+
* For ex. '--var.debug.DATA.APP.ID testId' will be converted to {debug: {DATA: {APP: {ID: testId}}}}, while we need {debug: {DATA.APP.ID: testId}}
57+
* '--var.DATA.APP.ID testId' will be converted to DATA: {APP: {ID: testId}}}, while we need {DATA.APP.ID: testId}
58+
* @param {any} obj varObject created by yargs
59+
* @param {string} configuration The configuration for which the plugin variable will be used.
60+
* @return {any} Converted object if the obj paramater is of type object, otherwise - the object itself.
61+
*/
62+
public simplifyYargsObject(obj: any, configuration?: string): any {
63+
if(obj && typeof(obj) === "object") {
64+
let convertedObject:any = Object.create({});
65+
66+
_.each(obj, (propValue: any, propKey: string) => {
67+
if(typeof(propValue) !== "object") {
68+
convertedObject[propKey] = propValue;
69+
return false;
70+
}
71+
72+
configuration = configuration ? configuration.toLowerCase() : undefined;
73+
let innerObj = this.simplifyYargsObject(propValue, configuration);
74+
75+
if(propKey.toLowerCase() === configuration) {
76+
// for --var.debug.DATA.APP.ID testId
77+
convertedObject[propKey] = innerObj;
78+
} else {
79+
// for --var.DATA.APP.ID testId
80+
_.each(innerObj, (innerPropValue: any, innerPropKey: string) => {
81+
convertedObject[`${propKey}.${innerPropKey}`] = innerPropValue;
82+
});
83+
}
84+
85+
});
86+
87+
return convertedObject;
88+
}
89+
90+
return obj;
91+
}
92+
}
93+
$injector.register("pluginVariablesHelper", PluginVariablesHelper);

0 commit comments

Comments
 (0)