Skip to content

Commit

Permalink
Support shared variables across all envs #128 #133
Browse files Browse the repository at this point in the history
  • Loading branch information
Huachao committed Nov 1, 2017
1 parent be8dda6 commit 11f38e9
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 16 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
],
"keywords": [
"REST",
"Http"
"Http",
"multi-root ready"
],
"activationEvents": [
"onCommand:rest-client.request",
Expand Down Expand Up @@ -330,7 +331,7 @@
},
"rest-client.environmentVariables": {
"type": "object",
"default": {},
"default": {"$shared": {}},
"scope": "resource",
"description": "Sets the environments and custom variables belongs to it (e.g., {\"production\": {\"host\": \"api.example.com\"}, \"sandbox\":{\"host\":\"sandbox.api.example.com\"}})",
"additionalProperties": {
Expand Down
26 changes: 18 additions & 8 deletions src/controllers/environmentController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import * as Constants from '../constants';
import { trace } from "../decorator";

export class EnvironmentController {
public static noEnvironmentPickItem: EnvironmentPickItem = new EnvironmentPickItem(
'No Environment', Constants.NoEnvironmentSelectedName, 'DO NOT Use Any Environment');
private static readonly noEnvironmentPickItem: EnvironmentPickItem = new EnvironmentPickItem(
'No Environment', Constants.NoEnvironmentSelectedName, 'You Can Still Use Variables Defined In $shared Environment');

private static readonly sharedEnvironmentName: string = '$shared';

private _environmentStatusBarItem: StatusBarItem;
private _restClientSettings: RestClientSettings;
Expand All @@ -29,6 +31,9 @@ export class EnvironmentController {
let itemPickList: EnvironmentPickItem[] = [];
itemPickList.push(EnvironmentController.noEnvironmentPickItem);
for (let name in this._restClientSettings.environmentVariables) {
if (name === EnvironmentController.sharedEnvironmentName) {
continue;
}
let item = new EnvironmentPickItem(name, name);
if (item.name === currentEnvironment.name) {
item.description = '$(check)';
Expand Down Expand Up @@ -61,13 +66,18 @@ export class EnvironmentController {
}

let settings = new RestClientSettings();
for (let environmentName in settings.environmentVariables) {
if (environmentName === environment.name) {
return settings.environmentVariables[environmentName];
}
}
let environments = settings.environmentVariables;
let variables = {};
Object.assign(
variables,
environments[EnvironmentController.sharedEnvironmentName] || {},
environments[environment.name] || {});

return new Map<string, string>();
const map = new Map<string, string>();
Object.keys(variables).forEach(key => {
map.set(key, variables[key]);
});
return map;
}

public dispose() {
Expand Down
4 changes: 2 additions & 2 deletions src/customVariableHoverProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ export class CustomVariableHoverProvider implements HoverProvider {
}

let environmentCustomVariables = await EnvironmentController.getCustomVariables();
for (let variableName in environmentCustomVariables) {
for (let [variableName, variableValue] of environmentCustomVariables) {
if (variableName === selectedVariableName) {
let contents: MarkedString[] = [environmentCustomVariables[variableName], { language: 'http', value: `Environment Variable ${variableName}` }];
let contents: MarkedString[] = [variableValue, { language: 'http', value: `Environment Variable ${variableName}` }];
return new Hover(contents, wordRange);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/httpElementFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ export class HttpElementFactory {

// add environment custom variables
let customVariables = await EnvironmentController.getCustomVariables();
for (let variableName in customVariables) {
originalElements.push(new HttpElement(variableName, ElementType.EnvironmentCustomVariable, null, `Value: ${customVariables[variableName]}`, new SnippetString(`{{${variableName}}}`)));
for (let [variableName, variableValue] of customVariables) {
originalElements.push(new HttpElement(variableName, ElementType.EnvironmentCustomVariable, null, `Value: ${variableValue}`, new SnippetString(`{{${variableName}}}`)));
}

// add file custom variables
Expand Down
4 changes: 2 additions & 2 deletions src/variableProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ export class VariableProcessor {
}

let environmentVariables = await EnvironmentController.getCustomVariables();
for (let variableName in environmentVariables) {
for (let [variableName, variableValue] of environmentVariables) {
let regex = new RegExp(`\\{\\{\\s*${variableName}\\s*\\}\\}`, 'g');
if (regex.test(request)) {
request = request.replace(regex, environmentVariables[variableName]);
request = request.replace(regex, variableValue);
}
}

Expand Down

0 comments on commit 11f38e9

Please sign in to comment.