-
-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathCompiler.js
107 lines (93 loc) · 2.68 KB
/
Compiler.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import Gio from "gi://Gio";
import GLib from "gi://GLib";
import dbus_previewer from "../../Previewer/DBusPreviewer.js";
import { decode, encode } from "../../util.js";
export default function Compiler({ session }) {
const { file } = session;
const cacheDir = GLib.get_user_cache_dir();
const targetPath = `${cacheDir}/rust_build_cache`;
const rustcVersionFile = Gio.File.new_for_path(
`${targetPath}/rustc_version.txt`,
);
let rustcVersion;
let savedRustcVersion;
async function compile() {
rustcVersion ||= await getRustcVersion();
savedRustcVersion ||= await getSavedRustcVersion({ rustcVersionFile });
if (rustcVersion !== savedRustcVersion) {
await cargoClean({ file, targetPath });
await saveRustcVersion({ targetPath, rustcVersion, rustcVersionFile });
}
const cargo_launcher = new Gio.SubprocessLauncher();
cargo_launcher.set_cwd(file.get_path());
const cargo = cargo_launcher.spawnv([
"cargo",
"build",
"--locked",
"--target-dir",
targetPath,
]);
await cargo.wait_async(null);
const result = cargo.get_successful();
cargo_launcher.close();
return result;
}
async function run() {
try {
const proxy = await dbus_previewer.getProxy("vala"); // rust uses the Vala previewer.
await proxy.RunAsync(
`${targetPath}/debug/libdemo.so`,
session.file.get_uri(),
);
} catch (err) {
console.error(err);
return false;
}
return true;
}
return { compile, run };
}
async function getRustcVersion() {
const cargo_launcher = Gio.SubprocessLauncher.new(
Gio.SubprocessFlags.STDOUT_PIPE,
);
const rustcVersionProcess = cargo_launcher.spawnv(["rustc", "--version"]);
const stdout = rustcVersionProcess.communicate_utf8(null, null)[1];
return stdout;
}
async function saveRustcVersion({
targetPath,
rustcVersionFile,
rustcVersion,
}) {
GLib.mkdir_with_parents(targetPath, 0o755);
await rustcVersionFile.replace_contents_async(
encode(rustcVersion),
null,
false,
Gio.FileCreateFlags.NONE,
null,
);
}
async function getSavedRustcVersion({ rustcVersionFile }) {
try {
const [contents] = await rustcVersionFile.load_contents_async(null);
return decode(contents);
} catch (err) {
if (!err.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.NOT_FOUND)) {
throw err;
}
return null;
}
}
async function cargoClean({ file, targetPath }) {
const cargo_launcher = new Gio.SubprocessLauncher();
cargo_launcher.set_cwd(file.get_path());
const cargoCleanProcess = cargo_launcher.spawnv([
"cargo",
"clean",
"--target-dir",
targetPath,
]);
await cargoCleanProcess.wait_async(null);
}