|
| 1 | +const std = @import("std"); |
| 2 | +const TTY = @import("./TTY.zig"); |
| 3 | +const Allocator = std.mem.Allocator; |
| 4 | +const AutoHashMap = std.AutoHashMap; |
| 5 | +const Dir = std.fs.Dir; |
| 6 | +const eql = std.mem.eql; |
| 7 | +const extension = std.fs.path.extension; |
| 8 | +const isDigit = std.ascii.isDigit; |
| 9 | + |
| 10 | +/// Limit number of parent directories to scan (inclusive of current directory) |
| 11 | +const max_scan_depth = 10; |
| 12 | + |
| 13 | +pub const Prop = enum { |
| 14 | + bun, |
| 15 | + deno, |
| 16 | + docker, |
| 17 | + node, |
| 18 | + rust, |
| 19 | + zig, |
| 20 | + |
| 21 | + /// Nerd font unicode characters |
| 22 | + pub fn symbol(prop: Prop) []const u8 { |
| 23 | + return switch (prop) { |
| 24 | + .bun => "", |
| 25 | + .deno => "", |
| 26 | + .docker => "", |
| 27 | + .node => "", |
| 28 | + .rust => "", |
| 29 | + .zig => "", |
| 30 | + }; |
| 31 | + } |
| 32 | + |
| 33 | + /// Spawn child process to get the version string. |
| 34 | + /// If successful, caller owns result. |
| 35 | + pub fn version(prop: Prop, allocator: Allocator) !?[]const u8 { |
| 36 | + if (prop.versionArgv()) |argv| { |
| 37 | + const result = std.process.Child.run(.{ |
| 38 | + .allocator = allocator, |
| 39 | + .argv = argv, |
| 40 | + }) catch return null; |
| 41 | + if (result.term == .Exited) { |
| 42 | + allocator.free(result.stderr); |
| 43 | + return result.stdout; |
| 44 | + } |
| 45 | + allocator.free(result.stderr); |
| 46 | + allocator.free(result.stdout); |
| 47 | + } |
| 48 | + return null; |
| 49 | + } |
| 50 | + |
| 51 | + /// Get the version command arguments |
| 52 | + pub fn versionArgv(prop: Prop) ?[]const []const u8 { |
| 53 | + return switch (prop) { |
| 54 | + .bun => &.{ "bun", "-v" }, |
| 55 | + .deno => &.{ "deno", "-v" }, |
| 56 | + .docker => &.{ "docker", "-v" }, |
| 57 | + .node => &.{ "node", "-v" }, |
| 58 | + .rust => &.{ "rustc", "--version" }, |
| 59 | + .zig => &.{ "zig", "version" }, |
| 60 | + }; |
| 61 | + } |
| 62 | + |
| 63 | + /// Returns a version slice, e.g. "1.0.0", trimming additional text |
| 64 | + pub fn versionFormat(prop: Prop, string: []const u8) []const u8 { |
| 65 | + _ = prop; |
| 66 | + var start: usize = 0; |
| 67 | + var end: usize = 0; |
| 68 | + for (0..string.len) |i| if (isDigit(string[i])) { |
| 69 | + start = i; |
| 70 | + break; |
| 71 | + }; |
| 72 | + for (start..string.len) |i| if (string[i] != '.' and !isDigit(string[i])) { |
| 73 | + end = i; |
| 74 | + break; |
| 75 | + }; |
| 76 | + return if (start < end) string[start..end] else ""; |
| 77 | + } |
| 78 | +}; |
| 79 | + |
| 80 | +const Context = @This(); |
| 81 | + |
| 82 | +allocator: Allocator, |
| 83 | +cwd: Dir, |
| 84 | +props: AutoHashMap(Prop, void), |
| 85 | + |
| 86 | +pub fn init(allocator: Allocator, cwd: std.fs.Dir) Context { |
| 87 | + return Context{ |
| 88 | + .allocator = allocator, |
| 89 | + .cwd = cwd, |
| 90 | + .props = AutoHashMap(Prop, void).init(allocator), |
| 91 | + }; |
| 92 | +} |
| 93 | + |
| 94 | +pub fn deinit(self: *Context) void { |
| 95 | + self.props.deinit(); |
| 96 | +} |
| 97 | + |
| 98 | +pub fn is(self: Context, prop: Prop) bool { |
| 99 | + return self.props.contains(prop); |
| 100 | +} |
| 101 | + |
| 102 | +pub fn print(self: Context, tty: TTY) !void { |
| 103 | + inline for (std.meta.fields(Prop)) |field| { |
| 104 | + const prop: Prop = @enumFromInt(field.value); |
| 105 | + if (self.props.contains(prop)) { |
| 106 | + try tty.setColor(.white); |
| 107 | + try tty.write(" via "); |
| 108 | + try tty.setColor(.yellow); |
| 109 | + try tty.write(prop.symbol()); |
| 110 | + const version = try prop.version(self.allocator); |
| 111 | + if (version) |string| { |
| 112 | + defer self.allocator.free(string); |
| 113 | + try tty.write(" "); |
| 114 | + try tty.write(prop.versionFormat(string)); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + try tty.setColor(.reset); |
| 119 | +} |
| 120 | + |
| 121 | +pub fn scanAll(self: *Context) !void { |
| 122 | + var dir = try self.cwd.openDir("./", .{}); |
| 123 | + defer dir.close(); |
| 124 | + var depth: usize = 0; |
| 125 | + while (true) : (depth += 1) { |
| 126 | + if (depth == max_scan_depth) break; |
| 127 | + self.scanDirectory(&dir); |
| 128 | + const parent = try dir.openDir("../", .{}); |
| 129 | + dir.close(); |
| 130 | + dir = parent; |
| 131 | + // Exit once root is reached |
| 132 | + const path = try dir.realpathAlloc(self.allocator, "."); |
| 133 | + defer self.allocator.free(path); |
| 134 | + if (eql(u8, path, "/")) break; |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +/// Check all entries inside the open directory |
| 139 | +pub fn scanDirectory(self: *Context, open_dir: *Dir) void { |
| 140 | + var iter = open_dir.iterate(); |
| 141 | + while (iter.next()) |next| { |
| 142 | + if (next) |entry| self.scanEntry(entry) else break; |
| 143 | + } else |_| return; |
| 144 | +} |
| 145 | + |
| 146 | +/// Check an individual directory entry |
| 147 | +pub fn scanEntry(self: *Context, entry: Dir.Entry) void { |
| 148 | + const result: ?Prop = switch (entry.kind) { |
| 149 | + .directory => result: { |
| 150 | + if (eql(u8, entry.name, "node_modules")) { |
| 151 | + break :result .node; |
| 152 | + } else if (eql(u8, entry.name, "zig-out")) { |
| 153 | + break :result .zig; |
| 154 | + } |
| 155 | + break :result null; |
| 156 | + }, |
| 157 | + .file, .sym_link => result: { |
| 158 | + const ext = extension(entry.name); |
| 159 | + if (eql(u8, entry.name, "bun.lock")) { |
| 160 | + break :result .bun; |
| 161 | + } else if (eql(u8, entry.name, "bun.lockb")) { |
| 162 | + break :result .bun; |
| 163 | + } else if (eql(u8, entry.name, "bunfig.toml")) { |
| 164 | + break :result .bun; |
| 165 | + } else if (eql(u8, entry.name, "Cargo.lock")) { |
| 166 | + break :result .rust; |
| 167 | + } else if (eql(u8, entry.name, "Cargo.toml")) { |
| 168 | + break :result .rust; |
| 169 | + } else if (eql(u8, entry.name, "deno.json")) { |
| 170 | + break :result .deno; |
| 171 | + } else if (eql(u8, entry.name, "deno.jsonc")) { |
| 172 | + break :result .deno; |
| 173 | + } else if (eql(u8, entry.name, "deno.lock")) { |
| 174 | + break :result .deno; |
| 175 | + } else if (eql(u8, entry.name, "docker-compose.yml")) { |
| 176 | + break :result .docker; |
| 177 | + } else if (eql(u8, entry.name, "package.json")) { |
| 178 | + break :result .node; |
| 179 | + } else if (eql(u8, ext, ".rs")) { |
| 180 | + break :result .rust; |
| 181 | + } else if (eql(u8, ext, ".zig")) { |
| 182 | + break :result .zig; |
| 183 | + } |
| 184 | + break :result null; |
| 185 | + }, |
| 186 | + else => null, |
| 187 | + }; |
| 188 | + if (result) |prop| { |
| 189 | + self.props.put(prop, {}) catch unreachable; |
| 190 | + } |
| 191 | +} |
0 commit comments