diff --git a/package-lock.json b/package-lock.json index 55cf084..47cae54 100644 --- a/package-lock.json +++ b/package-lock.json @@ -130,6 +130,12 @@ "integrity": "sha512-7NQmHra/JILCd1QqpSzl8+mJRc8ZHz3uDm8YV1Ks9IhK0epEiTw8aIErbvH9PI+6XbqhyIQy3462nEsn7UVzjQ==", "dev": true }, + "@types/tmp": { + "version": "0.0.33", + "resolved": "https://registry.npmjs.org/@types/tmp/-/tmp-0.0.33.tgz", + "integrity": "sha1-EHPEvIJHVK49EM+riKsCN7qWTk0=", + "dev": true + }, "@types/tough-cookie": { "version": "2.3.4", "resolved": "https://registry.npmjs.org/@types/tough-cookie/-/tough-cookie-2.3.4.tgz", @@ -2223,7 +2229,7 @@ }, "event-stream": { "version": "3.3.4", - "resolved": "https://registry.npmjs.org/event-stream/-/event-stream-3.3.4.tgz", + "resolved": "http://registry.npmjs.org/event-stream/-/event-stream-3.3.4.tgz", "integrity": "sha1-SrTJoPWlTbkzi0w02Gv86PSzVXE=", "dev": true, "requires": { @@ -5084,6 +5090,11 @@ "mem": "^4.0.0" } }, + "os-tmpdir": { + "version": "1.0.2", + "resolved": "http://registry.npmjs.org/os-tmpdir/-/os-tmpdir-1.0.2.tgz", + "integrity": "sha1-u+Z0BseaqFxc/sdm/lc0VV36EnQ=" + }, "p-defer": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/p-defer/-/p-defer-1.0.0.tgz", @@ -6731,6 +6742,14 @@ "setimmediate": "^1.0.4" } }, + "tmp": { + "version": "0.0.33", + "resolved": "https://registry.npmjs.org/tmp/-/tmp-0.0.33.tgz", + "integrity": "sha512-jRCJlojKnZ3addtTOjdIqoRuPEKBvNXcGYqzO6zWZX8KfKEpnGY5jfggJQ3EjKuu8D4bJRr0y+cYJFmYbImXGw==", + "requires": { + "os-tmpdir": "~1.0.2" + } + }, "to-absolute-glob": { "version": "0.1.1", "resolved": "https://registry.npmjs.org/to-absolute-glob/-/to-absolute-glob-0.1.1.tgz", diff --git a/package.json b/package.json index a4d2e99..484b3d0 100644 --- a/package.json +++ b/package.json @@ -435,9 +435,10 @@ "@types/mocha": "^2.2.32", "@types/node": "^6.0.40", "@types/request-promise": "^4.1.41", + "@types/semver": "^5.5.0", "@types/sinon": "^5.0.5", "@types/strip-json-comments": "0.0.30", - "@types/semver": "^5.5.0", + "@types/tmp": "0.0.33", "azure-arm-resource": "^3.1.1-preview", "fail-on-errors-webpack-plugin": "^3.0.0", "mocha": "^5.1.1", @@ -460,8 +461,9 @@ "jsonc-parser": "^1.0.1", "request": "^2.88.0", "request-promise": "^4.2.2", - "strip-json-comments": "^2.0.1", "semver": "^5.6.0", + "strip-json-comments": "^2.0.1", + "tmp": "0.0.33", "vscode-extension-telemetry": "0.0.18" }, "extensionDependencies": [ diff --git a/src/edge/edgeManager.ts b/src/edge/edgeManager.ts index 97a1bf3..194f012 100644 --- a/src/edge/edgeManager.ts +++ b/src/edge/edgeManager.ts @@ -7,6 +7,7 @@ import * as fse from "fs-extra"; import * as os from "os"; import * as path from "path"; import * as stripJsonComments from "strip-json-comments"; +import * as tmp from "tmp"; import * as vscode from "vscode"; import { Constants } from "../common/constants"; import { Executor } from "../common/executor"; @@ -397,12 +398,30 @@ export class EdgeManager { await Executor.executeCMD(outputChannel, "dotnet", { cwd: `${parent}`, shell: true }, `new aziotedgefunction -n "${name}" -r ${repositoryName}`); break; case Constants.LANGUAGE_PYTHON: - const gitHubSource = "https://github.com/Azure/cookiecutter-azure-iot-edge-module"; - const branch = Versions.pythonTemplateVersion(); - await Executor.executeCMD(outputChannel, - "cookiecutter", - { cwd: `${parent}`, shell: true }, - `--no-input ${gitHubSource} module_name=${name} image_repository=${repositoryName} --checkout ${branch}`); + await new Promise((resolve, reject) => { + tmp.dir({ unsafeCleanup: true }, (err, tmpDir, cleanupCallback) => { + if (err) { + reject(err); + } else { + const moduleContentDirName = "{{cookiecutter.module_name}}"; + const moduleContentDir = path.join(tmpDir, moduleContentDirName); + download(`github:Azure/cookiecutter-azure-iot-edge-module#${Versions.pythonTemplateVersion()}`, tmpDir, async (downloadErr) => { + if (downloadErr) { + reject(downloadErr); + } else { + try { + await this.updateRepositoryName(tmpDir, moduleContentDirName, repositoryName); + await fse.move(moduleContentDir, path.join(parent, name)); + resolve(); + } catch (error) { + reject(error); + } + } + cleanupCallback(); + }); + } + }); + }); break; case Constants.LANGUAGE_NODE: if (Versions.installNodeTemplate()) { @@ -432,10 +451,7 @@ export class EdgeManager { } }); }); - const moduleFile = path.join(parent, name, Constants.moduleManifest); - const moduleJson = await fse.readJson(moduleFile); - moduleJson.image.repository = repositoryName; - await fse.writeFile(moduleFile, JSON.stringify(moduleJson, null, 2), { encoding: "utf8" }); + await this.updateRepositoryName(parent, name, repositoryName); break; case Constants.LANGUAGE_JAVA: const groupId = extraProps.get(Constants.groupId); @@ -468,6 +484,13 @@ export class EdgeManager { } } + private async updateRepositoryName(parent: string, name: string, repositoryName: string) { + const moduleFile = path.join(parent, name, Constants.moduleManifest); + const moduleJson = await fse.readJson(moduleFile); + moduleJson.image.repository = repositoryName; + await fse.writeFile(moduleFile, JSON.stringify(moduleJson, null, 2), { encoding: "utf8" }); + } + private async validateInputName(name: string, parentPath?: string): Promise { if (!name) { return "The name could not be empty";