-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvm.zig
301 lines (248 loc) · 9.98 KB
/
vm.zig
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
const std = @import("std");
const os = std.os;
const mem = std.mem;
const fs = std.fs;
const json = std.json;
const http = std.http;
const utils = @import("utils.zig");
const c = @import("colors.zig");
pub fn getInstalledVersions(allocator: mem.Allocator, config_dir: []const u8) !std.ArrayList([]const u8) {
const versions_dir_path = try getVersionsDir(allocator, config_dir);
defer allocator.free(versions_dir_path);
var result = std.ArrayList([]const u8).init(allocator);
var versions_dir = fs.openDirAbsolute(versions_dir_path, .{ .iterate = true }) catch |err| switch (err) {
error.FileNotFound => return result,
else => |e| return e,
};
defer versions_dir.close();
var versions_iter = versions_dir.iterateAssumeFirstIteration();
while (try versions_iter.next()) |entry| {
if (entry.kind != .directory) continue;
const version = try allocator.dupe(u8, entry.name);
errdefer allocator.free(version);
const bin = try getBinPath(allocator, config_dir, version);
defer allocator.free(bin);
if (try utils.file_exists(bin)) {
try result.append(version);
} else {
allocator.free(version);
}
}
return result;
}
/// Reads the different files potentially containing the bun version to use. Walks up the directory tree until it finds one of the files.
pub fn detectProjectVersion(allocator: mem.Allocator, is_debug: bool) !?[]const u8 {
const files = comptime [_]VersionFile{
PackageJsonVersionFile.init(),
BunVersionFile.init(),
// ToolVersionsFile.init(),
};
var current_dir = try fs.cwd().realpathAlloc(allocator, ".");
defer allocator.free(current_dir);
while (true) {
if (is_debug) std.debug.print("Checking dir: {s}\n", .{current_dir});
for (files) |version_file| {
const file_path = try fs.path.join(allocator, &[_][]const u8{ current_dir, version_file.name });
defer allocator.free(file_path);
const file = fs.openFileAbsolute(file_path, .{}) catch |err| switch (err) {
error.FileNotFound => continue,
else => |e| return e,
};
defer file.close();
const file_size = try file.getEndPos();
const buffer = try allocator.alloc(u8, file_size);
defer allocator.free(buffer);
_ = try file.readAll(buffer);
if (version_file.extractBunVersion(allocator, buffer)) |version| {
if (is_debug) std.debug.print("Found v{s} in {s}\n", .{ version, file_path });
return try allocator.dupe(u8, version);
}
}
// Move up to the parent directory
const parent_dir = fs.path.dirname(current_dir);
if (parent_dir == null or mem.eql(u8, parent_dir.?, current_dir)) {
// We've reached the root directory, stop searching
break;
}
const new_dir = try fs.path.join(allocator, &[_][]const u8{parent_dir.?});
allocator.free(current_dir);
current_dir = new_dir;
}
return null;
}
pub fn getLatestLocalVersion(allocator: mem.Allocator, is_debug: bool, config_dir: []const u8) !?[]const u8 {
if (is_debug) std.debug.print("Getting latest local version...\n", .{});
const installed_versions = try getInstalledVersions(allocator, config_dir);
if (is_debug) std.debug.print("{d} versions: {s}\n", .{ installed_versions.items.len, installed_versions.items });
defer {
for (installed_versions.items) |item| {
allocator.free(item);
}
installed_versions.deinit();
}
if (installed_versions.items.len == 0) {
return null;
}
// TODO: installed versions are not sorted, so naively assuming the first item is the latest is wrong.
return try allocator.dupe(u8, installed_versions.items[0]);
}
pub fn getLatestRemoteVersion(allocator: mem.Allocator, is_debug: bool) ![]const u8 {
if (is_debug) std.debug.print("Getting latest remote version...\n", .{});
var client = http.Client{ .allocator = allocator };
defer client.deinit();
const accept_header = http.Header{
.name = "accept",
.value = "application/json",
};
const uri = try std.Uri.parse("https://ungh.cc/repos/oven-sh/bun/releases/latest");
const buf = try allocator.alloc(u8, 1024 * 1024 * 4);
defer allocator.free(buf);
var req = try client.open(
.GET,
uri,
.{ .server_header_buffer = buf, .keep_alive = false, .extra_headers = &[_]http.Header{accept_header} },
);
defer req.deinit();
try req.send();
try req.finish();
try req.wait();
var rdr = req.reader();
const body = try rdr.readAllAlloc(allocator, 1024 * 1024 * 4);
defer allocator.free(body);
if (is_debug) std.debug.print("Latest release: {s}\n", .{body});
const parsed = try json.parseFromSlice(std.json.Value, allocator, body, .{});
defer parsed.deinit();
const release = parsed.value.object.get("release").?.object;
// "bun-v1.1.27"
const gitTag = release.get("tag").?.string;
// strip off "bun-v"
const tag = gitTag[5..];
return allocator.dupe(u8, tag);
}
pub fn ensureVersionDownloaded(allocator: mem.Allocator, config_dir: []const u8, version: []const u8) !void {
const bin = try getBinPath(allocator, config_dir, version);
defer allocator.free(bin);
if (try utils.file_exists(bin)) {
return;
}
try confirmInstallation(version);
std.debug.print("Installing...\n", .{});
const install_script_path = try fs.path.join(allocator, &[_][]const u8{ config_dir, "install.sh" });
defer allocator.free(install_script_path);
// Download install script
const curlProcess = try std.process.Child.run(.{
.allocator = allocator,
.argv = &[_][]const u8{
"curl",
"-fsSL",
"-o",
install_script_path,
"https://bun.sh/install",
},
});
defer allocator.free(curlProcess.stderr);
defer allocator.free(curlProcess.stdout);
// Run install script
const version_path = try fs.path.join(allocator, &[_][]const u8{ config_dir, "versions", version });
defer allocator.free(version_path);
var env = try std.process.getEnvMap(allocator);
defer env.deinit();
try env.put("BUN_INSTALL", version_path);
const version_arg = try std.fmt.allocPrint(
allocator,
"bun-v{s}",
.{version},
);
defer allocator.free(version_arg);
const installProcess = try std.process.Child.run(.{
.allocator = allocator,
.argv = &[_][]const u8{
"sh",
install_script_path,
version_arg,
},
.env_map = &env,
});
defer allocator.free(installProcess.stderr);
defer allocator.free(installProcess.stdout);
std.debug.print("{s}✓{s} Done! {s}Bun v{s}{s} is installed\n", .{ c.green, c.reset, c.cyan, version, c.reset });
}
fn confirmInstallation(version: []const u8) !void {
const stdout = std.io.getStdOut().writer();
const stdin = std.io.getStdIn().reader();
try stdout.print("{s}Bun v{s} is not installed. Do you want to install it? [y/N]{s} ", .{ c.yellow, version, c.reset });
var buffer: [2]u8 = undefined;
const user_input = stdin.readUntilDelimiterOrEof(&buffer, '\n') catch |err| switch (err) {
error.StreamTooLong => "N",
else => return err,
} orelse "N";
if (mem.eql(u8, user_input, "y")) return;
return error.UserAborted;
}
pub fn getVersionsDir(allocator: mem.Allocator, config_dir: []const u8) ![]u8 {
return try fs.path.join(allocator, &[_][]const u8{ config_dir, "versions" });
}
pub fn getVersionDir(allocator: mem.Allocator, config_dir: []const u8, version: []const u8) ![]u8 {
return try fs.path.join(allocator, &[_][]const u8{ config_dir, "versions", version });
}
pub fn getBinPath(allocator: mem.Allocator, config_dir: []const u8, version: []const u8) ![]u8 {
return try fs.path.join(allocator, &[_][]const u8{ config_dir, "versions", version, "bin", "bun" });
}
const VersionFile = struct {
name: []const u8,
extractBunVersion: *const fn (allocator: mem.Allocator, contents: []u8) ?[]const u8,
};
const PackageJsonVersionFile = struct {
fn init() VersionFile {
return .{
.name = "package.json",
.extractBunVersion = &extractBunVersion,
};
}
fn extractBunVersion(allocator: mem.Allocator, contents: []u8) ?[]const u8 {
const parsed = json.parseFromSlice(std.json.Value, allocator, contents, .{}) catch |err| switch (err) {
else => return null,
};
defer parsed.deinit();
// "[email protected]"
if (parsed.value.object.get("packageManager")) |package_manager| {
const str = package_manager.string;
if (mem.eql(u8, str[0..4], "bun@")) {
return allocator.dupe(u8, str[4..]) catch return null;
}
}
return null;
}
};
const BunVersionFile = struct {
fn init() VersionFile {
return .{
.name = ".bun-version",
.extractBunVersion = &extractBunVersion,
};
}
fn extractBunVersion(allocator: mem.Allocator, contents: []u8) ?[]u8 {
return allocator.dupe(u8, mem.trim(u8, contents, &std.ascii.whitespace)) catch |err| switch (err) {
else => return null,
};
}
};
const ToolVersionsFile = struct {
fn init() VersionFile {
return .{
.name = ".tool-versions",
.extractBunVersion = &extractBunVersion,
};
}
fn extractBunVersion(allocator: mem.Allocator, contents: []u8) ?[]u8 {
const regex = std.regex.compile(allocator, "^bun\\s*(.*?)$", .{});
defer std.regex.free(regex);
const match = try std.regex.match(allocator, regex, contents, 0, contents.len);
defer allocator.free(match);
if (match.len == 0) {
return null;
}
const version = match[0].groups[0];
return allocator.dupe(u8, version);
}
};