UNPKG

teamsfx-extension

Version:

Create, debug, and deploy Teams apps with Teams Toolkit

119 lines 5.92 kB
"use strict"; // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createTmpBackendProjectDir = exports.withDotnet = exports.cleanup = exports.hasAnyDotnetVersions = exports.hasDotnetVersion = exports.getDotnetExecPathFromConfig = exports.dotnetSupportedVersions = exports.dotnetInstallVersion = exports.dotnetOldVersion = exports.dotnetCommand = exports.dotnetPrivateInstallPath = exports.dotnetConfigPath = void 0; const fs = require("fs-extra"); const path = require("path"); const os = require("os"); const tmp = require("tmp"); const teamsfx_api_1 = require("@microsoft/teamsfx-api"); const cpUtils_1 = require("../../../../src/debug/depsChecker/cpUtils"); const common_1 = require("../../../../src/debug/depsChecker/common"); const testLogger_1 = require("../adapters/testLogger"); const common_2 = require("./common"); const dotnetChecker_1 = require("../../../../src/debug/depsChecker/dotnetChecker"); const find = require("find-process"); tmp.setGracefulCleanup(); exports.dotnetConfigPath = path.join(os.homedir(), "." + teamsfx_api_1.ConfigFolderName, "dotnet.json"); exports.dotnetPrivateInstallPath = path.join(os.homedir(), "." + teamsfx_api_1.ConfigFolderName, "bin", "dotnet"); exports.dotnetCommand = "dotnet"; exports.dotnetOldVersion = dotnetChecker_1.DotnetVersion.v21; exports.dotnetInstallVersion = dotnetChecker_1.DotnetVersion.v31; exports.dotnetSupportedVersions = [dotnetChecker_1.DotnetVersion.v31, dotnetChecker_1.DotnetVersion.v50]; function getDotnetExecPathFromConfig(dotnetConfigPath) { return __awaiter(this, void 0, void 0, function* () { try { const config = yield fs.readJson(dotnetConfigPath, { encoding: "utf-8" }); if (typeof config.dotnetExecutablePath === "string") { return config.dotnetExecutablePath; } } catch (error) { console.debug(`Failed to getDotnetConfig, error = '${error}'`); } return null; }); } exports.getDotnetExecPathFromConfig = getDotnetExecPathFromConfig; function hasDotnetVersion(dotnetExecPath, versionString) { return __awaiter(this, void 0, void 0, function* () { return yield hasAnyDotnetVersions(dotnetExecPath, [versionString]); }); } exports.hasDotnetVersion = hasDotnetVersion; function hasAnyDotnetVersions(dotnetExecPath, versionStrings) { return __awaiter(this, void 0, void 0, function* () { try { const output = yield cpUtils_1.cpUtils.executeCommand(undefined, testLogger_1.logger, undefined, dotnetExecPath, "--list-sdks"); return output.split(/\r?\n/).some((line) => { return versionStrings.some((versionString) => line.startsWith(versionString)); }); } catch (error) { console.debug(`Failed to run "${dotnetExecPath} --list-sdks", error = '${error}'`); return false; } }); } exports.hasAnyDotnetVersions = hasAnyDotnetVersions; function cleanup() { return __awaiter(this, void 0, void 0, function* () { // fs-extra.remove() does nothing if the file does not exist. yield fs.remove(exports.dotnetConfigPath); const processes = yield find("name", "dotnet", true); processes.forEach((p, index, array) => process.kill(p.pid, "SIGKILL")); yield fs.remove(exports.dotnetPrivateInstallPath); }); } exports.cleanup = cleanup; function withDotnet(dotnetChecker, version, addToPath, callback) { return __awaiter(this, void 0, void 0, function* () { let installDir; let cleanupCallback; try { [installDir, cleanupCallback] = yield common_2.createTmpDir(); } catch (error) { throw new Error(`Failed to create tmpdir for dotnet, error = '${error}'`); } const backupPath = process.env.PATH; try { // use private method as a helper method in test only yield dotnetChecker["runDotnetInstallScript"](version, installDir); const dotnetExecPath = dotnetChecker_1.DotnetChecker["getDotnetExecPathFromDotnetInstallationDir"](installDir); if (addToPath) { process.env.PATH = path.resolve(dotnetExecPath, "..") + (common_1.isWindows() ? ";" : ":") + process.env.PATH; } yield callback(dotnetExecPath); } finally { if (addToPath) { process.env.PATH = backupPath; } cleanupCallback(); } }); } exports.withDotnet = withDotnet; function createTmpBackendProjectDir(csprojFileName) { return __awaiter(this, void 0, void 0, function* () { const [dir, cleanupCallback] = yield common_2.createTmpDir(); const csprojPath = path.resolve(__dirname, "../../../../../../../templates/function-base/ts/default/extensions.csproj"); const targetPath = path.join(dir, csprojFileName); yield fs.copyFile(csprojPath, targetPath, fs.constants.COPYFILE_EXCL); return [dir, cleanupCallback]; }); } exports.createTmpBackendProjectDir = createTmpBackendProjectDir; //# sourceMappingURL=dotnet.js.map