forked from nommiin/builder
-
Notifications
You must be signed in to change notification settings - Fork 6
/
BuilderDrives.js
75 lines (66 loc) · 1.94 KB
/
BuilderDrives.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
class BuilderDrives {
static file = new $gmedit["electron.ConfigFile"]("session", "builder-drives");
static add(path) {
let raw = Builder.Command.execSync("wmic logicaldisk get caption").toString();
let lines = raw.replace(/\r/g, "").split("\n");
let takenLetters = {};
for (let line of lines) {
let mt = /([A-Z]):/.exec(line);
if (mt) takenLetters[mt[1]] = true;
}
let freeLetters = [];
for (let i = "A".charCodeAt(); i <= "Z".charCodeAt(); i++) {
let c = String.fromCharCode(i);
if (!takenLetters[c]) freeLetters.push(c);
}
//console.log("Candidate letters:", freeLetters);
if (freeLetters.length == 0) return null;
let drive = freeLetters[0 | (Math.random() * freeLetters.length)];
try {
Builder.Command.execSync(`subst ${drive}: "${path}"`);
} catch (x) {
BuilderOutput.main.write(`Failed to subst ${drive}: `, x);
return null;
}
BuilderOutput.main.write(`Using Virtual Drive: ${drive}`);
let conf = this.file;
if (conf.sync()) conf.data = [];
conf.data.push(drive);
conf.flush();
return drive;
}
static remove(drive) {
drive ??= Builder.Drive;
if (drive == null) return;
BuilderOutput.main.write(`Removing Virtual Drive: ${drive}`);
Builder.Command.execSync(`subst /d ${drive}:`);
let conf = this.file;
if (conf.sync()) conf.data = [];
let ind = conf.data.indexOf(drive);
if (ind >= 0) {
conf.data.splice(ind, 1);
conf.flush();
}
}
static removeCurrent() {
for (let drive of Builder.Drives) {
this.remove(drive);
}
Builder.Drives.length = 0;
Builder.Drive = "";
}
static clean() {
let conf = this.file;
if (conf.sync()) conf.data = [];
let done = [];
for (let c of conf.data) {
try {
Builder.Command.execSync(`subst /d ${c}:`);
done.push(c);
} catch(e) {};
}
conf.data = [];
conf.flush();
Electron_Dialog.showMessageBox({type: "info", title: "Builder", message: `Finished cleaning virtual drives (${done.join(", ")}).`});
}
}