suppor env for created terminal and subprocess and fix issue #324 and… (#349)

* suppor env for created terminal and subprocess and fix issue #324 and turn NSAT survey to 100%

* remove exec ref
This commit is contained in:
Wei Shen 2019-01-21 14:06:18 +08:00 коммит произвёл GitHub
Родитель 16e0fd6e1a
Коммит 0a563be113
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
13 изменённых файлов: 93 добавлений и 41 удалений

Просмотреть файл

@ -281,6 +281,10 @@
"type": "object",
"default": {},
"description": "Templates for third party modules"
},
"azure-iot-edge.executor.env": {
"type": "object",
"description": "Environment variables for the command executor and Azure IoT Edge terminals"
}
}
},

Просмотреть файл

@ -0,0 +1,20 @@
"use strict";
import * as vscode from "vscode";
export class Configuration {
public static getConfiguration(): vscode.WorkspaceConfiguration {
return vscode.workspace.getConfiguration("azure-iot-edge");
}
public static getConfigurationProperty(id: string): any {
return Configuration.getConfiguration().get(id);
}
public static async setGlobalConfigurationProperty(id: string, value: any): Promise<void> {
await Configuration.getConfiguration().update(id, value, true);
}
public static async setWorkspaceConfigurationProperty(id: string, value: any): Promise<void> {
await Configuration.getConfiguration().update(id, value, false);
}
}

Просмотреть файл

@ -2,25 +2,27 @@
// Licensed under the MIT license.
"use strict";
import { ChildProcess, exec, execSync, spawn, SpawnOptions } from "child_process";
import { ChildProcess, execSync, ExecSyncOptions, spawn, SpawnOptions } from "child_process";
import * as vscode from "vscode";
import { Configuration } from "./configuration";
import { Constants } from "./constants";
export class Executor {
public static runInTerminal(command: string, terminal: string = Constants.edgeDisplayName): void {
if (this.terminals[terminal] === undefined) {
this.terminals[terminal] = vscode.window.createTerminal(terminal);
this.terminals[terminal] = Executor.createTerminal(terminal);
}
this.terminals[terminal].show();
this.terminals[terminal].sendText(command);
}
public static exec(command: string) {
return exec(command);
}
public static execSync(command: string) {
return execSync(command, { encoding: "utf8" });
const envVars = Executor.getEnvFromConfig();
const options: ExecSyncOptions = { encoding: "utf8" };
if (envVars) {
options.env = Executor.mergeEnvs(envVars, process.env);
}
return execSync(command, options);
}
public static onDidCloseTerminal(closedTerminal: vscode.Terminal): void {
@ -35,6 +37,13 @@ export class Executor {
let stderr: string = "";
let stdOutput: string = "";
const envVars = Executor.getEnvFromConfig();
if (envVars) {
options = options || {};
const processEnvs = Executor.mergeEnvs(envVars, process.env);
options.env = Executor.mergeEnvs(options.env, processEnvs);
}
const p: ChildProcess = spawn(command, args, options);
p.stdout.on("data", (data: string | Buffer): void => {
const dataStr = data.toString();
@ -78,4 +87,34 @@ export class Executor {
outputPane.appendLine(value);
}
}
private static createTerminal(terminal: string): vscode.Terminal {
const envVars = Executor.getEnvFromConfig();
const options: vscode.TerminalOptions = { name: terminal };
if (envVars) {
options.env = envVars;
}
return vscode.window.createTerminal(options);
}
private static mergeEnvs(overrideEnv, envs) {
if (!overrideEnv) {
return envs;
}
envs = envs || {};
for (const key of Object.keys(overrideEnv)) {
envs[key] = overrideEnv[key];
}
return envs;
}
private static getEnvFromConfig() {
const envVars = Configuration.getConfigurationProperty("executor.env");
if (envVars && Object.keys(envVars).length > 0) {
return envVars;
} else {
return undefined;
}
}
}

Просмотреть файл

@ -8,7 +8,7 @@ import { Constants } from "./constants";
import { TelemetryClient } from "./telemetryClient";
const NSAT_SURVEY_URL = "https://aka.ms/vscode-iot-edge-survey";
const PROBABILITY = 0.5;
const PROBABILITY = 1;
const SESSION_COUNT_THRESHOLD = 2;
const SESSION_COUNT_KEY = "nsat/sessionCount";
const LAST_SESSION_DATE_KEY = "nsat/lastSessionDate";

Просмотреть файл

@ -1,9 +1,9 @@
import { Configuration } from "./configuration";
import { Constants } from "./constants";
import { Utility } from "./utility";
export class Platform {
public static getDefaultPlatform(): Platform {
const defaultPlatform = Utility.getConfigurationProperty(Constants.defPlatformConfig);
const defaultPlatform = Configuration.getConfigurationProperty(Constants.defPlatformConfig);
if (!defaultPlatform) {
return new Platform("amd64", null);
}
@ -16,7 +16,7 @@ export class Platform {
}
public static getPlatformsSetting(): Platform[] {
const platformObjs = Utility.getConfiguration().get<any>(Constants.platformsConfig);
const platformObjs = Configuration.getConfiguration().get<any>(Constants.platformsConfig);
const platforms: Platform[] = [];
for (const key in platformObjs) {
if (platformObjs.hasOwnProperty(key)) {

Просмотреть файл

@ -11,6 +11,7 @@ import * as vscode from "vscode";
import { AzureAccount, AzureSession } from "../typings/azure-account.api";
import { IDeviceItem } from "../typings/IDeviceItem";
import { BuildSettings } from "./buildSettings";
import { Configuration } from "./configuration";
import { Constants, ContainerState } from "./constants";
import { Executor } from "./executor";
import { Platform } from "./platform";
@ -18,10 +19,6 @@ import { TelemetryClient } from "./telemetryClient";
import { UserCancelledError } from "./UserCancelledError";
export class Utility {
public static getConfiguration(): vscode.WorkspaceConfiguration {
return vscode.workspace.getConfiguration("azure-iot-edge");
}
public static checkWorkspace(): boolean {
if (!vscode.workspace.workspaceFolders) {
vscode.window.showErrorMessage("This extension only works when folders are opened.");
@ -35,22 +32,10 @@ export class Utility {
return (os.platform() === "linux" || os.platform() === "darwin") ? `sudo ${command}` : command;
}
public static getConfigurationProperty(id: string): any {
return Utility.getConfiguration().get(id);
}
public static async setGlobalConfigurationProperty(id: string, value: any): Promise<void> {
await Utility.getConfiguration().update(id, value, true);
}
public static async setWorkspaceConfigurationProperty(id: string, value: any): Promise<void> {
await Utility.getConfiguration().update(id, value, false);
}
public static adjustFilePath(filePath: string): string {
if (os.platform() === "win32") {
const windowsShell = vscode.workspace.getConfiguration("terminal").get<string>("integrated.shell.windows");
const terminalRoot = Utility.getConfiguration().get<string>("terminalRoot");
const terminalRoot = Configuration.getConfiguration().get<string>("terminalRoot");
if (windowsShell && terminalRoot) {
filePath = filePath
.replace(/^([A-Za-z]):/, (match, p1) => `${terminalRoot}${p1.toLowerCase()}`)

Просмотреть файл

@ -1,5 +1,5 @@
import { Configuration } from "./configuration";
import { Constants } from "./constants";
import { Utility } from "./utility";
export class Versions {
public static getRunTimeVersionMap(): Map<string, string> {
@ -59,7 +59,7 @@ export class Versions {
}
private static getValue(key: string, defaultVal: string|boolean = null): string | boolean {
const value = Utility.getConfigurationProperty(key);
const value = Configuration.getConfigurationProperty(key);
if (value === undefined || value === null) {
return defaultVal;
}

Просмотреть файл

@ -9,6 +9,7 @@ import * as path from "path";
import * as stripJsonComments from "strip-json-comments";
import * as tmp from "tmp";
import * as vscode from "vscode";
import { Configuration } from "../common/configuration";
import { Constants } from "../common/constants";
import { Executor } from "../common/executor";
import { ModuleInfo } from "../common/moduleInfo";
@ -171,7 +172,7 @@ export class EdgeManager {
const platformNames: string[] = (keyWithAlias.sort()).concat(defaultKeys.sort());
const defaultPlatform = await vscode.window.showQuickPick(platformNames, { placeHolder: Constants.selectDefaultPlatform, ignoreFocusOut: true });
if (defaultPlatform) {
await Utility.setWorkspaceConfigurationProperty(Constants.defPlatformConfig, platformMap.get(defaultPlatform));
await Configuration.setWorkspaceConfigurationProperty(Constants.defPlatformConfig, platformMap.get(defaultPlatform));
outputChannel.appendLine(`Default platform is ${defaultPlatform} now.`);
}
}
@ -790,7 +791,7 @@ export class EdgeManager {
}
private get3rdPartyModuleTemplates() {
const templatesConfig = Utility.getConfiguration().get<any>(Constants.thirdPartyModuleTemplatesConfig);
const templatesConfig = Configuration.getConfiguration().get<any>(Constants.thirdPartyModuleTemplatesConfig);
return templatesConfig ? templatesConfig.templates as any[] : undefined;
}

Просмотреть файл

@ -9,6 +9,7 @@ import * as path from "path";
import * as request from "request-promise";
import * as semver from "semver";
import * as vscode from "vscode";
import { Configuration } from "../common/configuration";
import { Constants } from "../common/constants";
import { Executor } from "../common/executor";
import { LearnMoreError } from "../common/LearnMoreError";
@ -184,8 +185,8 @@ export class Simulator {
await Executor.executeCMD(outputChannel, "iotedgehubdev", { shell: true }, `modulecred -l -o "${outputFile}"`);
const moduleConfig = dotenv.parse(await fse.readFile(outputFile));
await Utility.setGlobalConfigurationProperty("EdgeHubConnectionString", moduleConfig.EdgeHubConnectionString);
await Utility.setGlobalConfigurationProperty("EdgeModuleCACertificateFile", moduleConfig.EdgeModuleCACertificateFile);
await Configuration.setGlobalConfigurationProperty("EdgeHubConnectionString", moduleConfig.EdgeHubConnectionString);
await Configuration.setGlobalConfigurationProperty("EdgeModuleCACertificateFile", moduleConfig.EdgeModuleCACertificateFile);
});
}

Просмотреть файл

@ -160,7 +160,8 @@ export function activate(context: vscode.ExtensionContext) {
"azure-iot-edge.setDefaultPlatform",
async (): Promise<void> => {
await edgeManager.selectDefaultPlatform(outputChannel);
return configDiagnosticProvider.updateDiagnostics(vscode.window.activeTextEditor.document, diagCollection);
const document = vscode.window && vscode.window.activeTextEditor ? vscode.window.activeTextEditor.document : null;
return configDiagnosticProvider.updateDiagnostics(document, diagCollection);
});
context.subscriptions.push(vscode.window.onDidCloseTerminal((closedTerminal: vscode.Terminal) => {

Просмотреть файл

@ -12,9 +12,10 @@ import { IntelliSenseUtility } from "./intelliSenseUtility";
export class ConfigDiagnosticProvider {
public async updateDiagnostics(document: vscode.TextDocument, diagCollection: vscode.DiagnosticCollection) {
if (!document && path.basename(document.uri.fsPath) !== Constants.deploymentTemplate
if (!document ||
(path.basename(document.uri.fsPath) !== Constants.deploymentTemplate
&& path.basename(document.uri.fsPath) !== Constants.deploymentDebugTemplate
&& path.basename(document.uri.fsPath) !== Constants.moduleManifest) {
&& path.basename(document.uri.fsPath) !== Constants.moduleManifest)) {
return;
}

Просмотреть файл

@ -1,8 +1,7 @@
import * as assert from "assert";
import * as vscode from "vscode";
import { Configuration } from "../src/common/configuration";
import { Constants } from "../src/common/constants";
import { Utility } from "../src/common/utility";
import * as myExtension from "../src/extension";
suite("Extension Tests", () => {
@ -11,7 +10,7 @@ suite("Extension Tests", () => {
});
test("should be able to get config", () => {
const terminalRoot = Utility.getConfiguration().get<string>("terminalRoot");
const terminalRoot = Configuration.getConfiguration().get<string>("terminalRoot");
assert.equal(terminalRoot, "");
});
});

Просмотреть файл

@ -3,6 +3,7 @@ import * as fse from "fs-extra";
import * as path from "path";
import * as sinon from "sinon";
import { BuildSettings } from "../src/common/buildSettings";
import { Configuration } from "../src/common/configuration";
import { Constants } from "../src/common/constants";
import { Platform } from "../src/common/platform";
import { Utility } from "../src/common/utility";
@ -152,7 +153,7 @@ suite("utility tests", () => {
});
test("getPlatformsSetting", () => {
sinon.stub(Utility, "getConfiguration").callsFake(() => {
sinon.stub(Configuration, "getConfiguration").callsFake(() => {
const stubMap = new Map();
stubMap.set(Constants.platformsConfig, {
arm32v7: [],