1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.macPathToParallelsWindows = exports.ParallelsVmManager = exports.parseVmList = void 0;
|
4 | const builder_util_1 = require("builder-util");
|
5 | const child_process_1 = require("child_process");
|
6 | const vm_1 = require("./vm");
|
7 |
|
8 | async function parseVmList(debugLogger) {
|
9 |
|
10 | let rawList = await (0, builder_util_1.exec)("prlctl", ["list", "-i", "-s", "name"], undefined, false);
|
11 | debugLogger.add("parallels.list", rawList);
|
12 | rawList = rawList.substring(rawList.indexOf("ID:"));
|
13 |
|
14 | const result = [];
|
15 | for (const info of rawList
|
16 | .split("\n\n")
|
17 | .map(it => it.trim())
|
18 | .filter(it => it.length > 0)) {
|
19 | const vm = {};
|
20 | for (const line of info.split("\n")) {
|
21 | const meta = /^([^:("]+): (.*)$/.exec(line);
|
22 | if (meta == null) {
|
23 | continue;
|
24 | }
|
25 | const key = meta[1].toLowerCase();
|
26 | if (key === "id" || key === "os" || key === "name" || key === "state" || key === "name") {
|
27 | vm[key] = meta[2].trim();
|
28 | }
|
29 | }
|
30 | result.push(vm);
|
31 | }
|
32 | return result;
|
33 | }
|
34 | exports.parseVmList = parseVmList;
|
35 |
|
36 | class ParallelsVmManager extends vm_1.VmManager {
|
37 | constructor(vm) {
|
38 | super();
|
39 | this.vm = vm;
|
40 | this.isExitHookAdded = false;
|
41 | this.startPromise = this.doStartVm();
|
42 | }
|
43 | get pathSep() {
|
44 | return "/";
|
45 | }
|
46 | handleExecuteError(error) {
|
47 | if (error.message.includes("Unable to open new session in this virtual machine")) {
|
48 | throw new Error(`Please ensure that your are logged in "${this.vm.name}" parallels virtual machine. In the future please do not stop VM, but suspend.\n\n${error.message}`);
|
49 | }
|
50 | builder_util_1.log.warn("ensure that 'Share folders' is set to 'All Disks', see https://goo.gl/E6XphP");
|
51 | throw error;
|
52 | }
|
53 | async exec(file, args, options) {
|
54 | await this.ensureThatVmStarted();
|
55 |
|
56 | return await (0, builder_util_1.exec)("prlctl", ["exec", this.vm.id, "--current-user", file.startsWith("/") ? macPathToParallelsWindows(file) : file].concat(args), options).catch(error => this.handleExecuteError(error));
|
57 | }
|
58 | async spawn(file, args, options, extraOptions) {
|
59 | await this.ensureThatVmStarted();
|
60 | return await (0, builder_util_1.spawn)("prlctl", ["exec", this.vm.id, file].concat(args), options, extraOptions).catch(error => this.handleExecuteError(error));
|
61 | }
|
62 | async doStartVm() {
|
63 | const vmId = this.vm.id;
|
64 | const state = this.vm.state;
|
65 | if (state === "running") {
|
66 | return;
|
67 | }
|
68 | if (!this.isExitHookAdded) {
|
69 | this.isExitHookAdded = true;
|
70 |
|
71 | require("async-exit-hook")((callback) => {
|
72 | const stopArgs = ["suspend", vmId];
|
73 | if (callback == null) {
|
74 | (0, child_process_1.execFileSync)("prlctl", stopArgs);
|
75 | }
|
76 | else {
|
77 | (0, builder_util_1.exec)("prlctl", stopArgs).then(callback).catch(callback);
|
78 | }
|
79 | });
|
80 | }
|
81 | await (0, builder_util_1.exec)("prlctl", ["start", vmId]);
|
82 | }
|
83 | ensureThatVmStarted() {
|
84 | let startPromise = this.startPromise;
|
85 | if (startPromise == null) {
|
86 | startPromise = this.doStartVm();
|
87 | this.startPromise = startPromise;
|
88 | }
|
89 | return startPromise;
|
90 | }
|
91 | toVmFile(file) {
|
92 |
|
93 | return macPathToParallelsWindows(file);
|
94 | }
|
95 | }
|
96 | exports.ParallelsVmManager = ParallelsVmManager;
|
97 | function macPathToParallelsWindows(file) {
|
98 | if (file.startsWith("C:\\")) {
|
99 | return file;
|
100 | }
|
101 | return "\\\\Mac\\Host\\" + file.replace(/\//g, "\\");
|
102 | }
|
103 | exports.macPathToParallelsWindows = macPathToParallelsWindows;
|
104 |
|
\ | No newline at end of file |