diff options
Diffstat (limited to 'src/Installation.zig')
| -rw-r--r-- | src/Installation.zig | 27 |
1 files changed, 21 insertions, 6 deletions
diff --git a/src/Installation.zig b/src/Installation.zig index fb06824..031b59a 100644 --- a/src/Installation.zig +++ b/src/Installation.zig | |||
| @@ -1,9 +1,10 @@ | |||
| 1 | const builtin = @import("builtin"); | ||
| 2 | const curl = @import("curl"); | 1 | const curl = @import("curl"); |
| 3 | const std = @import("std"); | 2 | const std = @import("std"); |
| 4 | const xdg = @import("xdg"); | 3 | const xdg = @import("xdg"); |
| 4 | const zup = @import("zup"); | ||
| 5 | 5 | ||
| 6 | const Allocator = std.mem.Allocator; | 6 | const Allocator = std.mem.Allocator; |
| 7 | const Config = zup.Config; | ||
| 7 | const ChildProcess = std.ChildProcess; | 8 | const ChildProcess = std.ChildProcess; |
| 8 | const JsonValue = std.json.Value; | 9 | const JsonValue = std.json.Value; |
| 9 | const SemanticVersion = std.SemanticVersion; | 10 | const SemanticVersion = std.SemanticVersion; |
| @@ -128,7 +129,9 @@ pub fn isInstalled(allocator: Allocator, name: []const u8) !bool { | |||
| 128 | return true; | 129 | return true; |
| 129 | } | 130 | } |
| 130 | 131 | ||
| 131 | pub fn getAvailableList(allocator: Allocator) !Installations { | 132 | pub fn getAvailableList(config: Config) !Installations { |
| 133 | const allocator = config.allocator; | ||
| 134 | |||
| 132 | var json_str = try curl.easyDownload(allocator, "https://ziglang.org/download/index.json"); | 135 | var json_str = try curl.easyDownload(allocator, "https://ziglang.org/download/index.json"); |
| 133 | defer allocator.free(json_str); | 136 | defer allocator.free(json_str); |
| 134 | 137 | ||
| @@ -147,7 +150,7 @@ pub fn getAvailableList(allocator: Allocator) !Installations { | |||
| 147 | 150 | ||
| 148 | var it = vt.root.Object.iterator(); | 151 | var it = vt.root.Object.iterator(); |
| 149 | while (it.next()) |kv| { | 152 | while (it.next()) |kv| { |
| 150 | if (try parseInstallation(allocator, kv.key_ptr.*, kv.value_ptr.*)) |*installation| { | 153 | if (try parseInstallation(config, kv.key_ptr.*, kv.value_ptr.*)) |*installation| { |
| 151 | errdefer installation.deinit(); | 154 | errdefer installation.deinit(); |
| 152 | 155 | ||
| 153 | var name = try allocator.dupe(u8, kv.key_ptr.*); | 156 | var name = try allocator.dupe(u8, kv.key_ptr.*); |
| @@ -160,15 +163,27 @@ pub fn getAvailableList(allocator: Allocator) !Installations { | |||
| 160 | return installations; | 163 | return installations; |
| 161 | } | 164 | } |
| 162 | 165 | ||
| 163 | fn parseInstallation(allocator: Allocator, name: []const u8, value: JsonValue) !?Installation { | 166 | fn parseInstallation(config: Config, name: []const u8, value: JsonValue) !?Installation { |
| 167 | const allocator = config.allocator; | ||
| 168 | |||
| 164 | if (value != .Object) { | 169 | if (value != .Object) { |
| 165 | return error.JsonSchema; | 170 | return error.JsonSchema; |
| 166 | } | 171 | } |
| 167 | const map = value.Object; | 172 | const map = value.Object; |
| 168 | 173 | ||
| 169 | const triple = @tagName(builtin.target.cpu.arch) ++ "-" ++ @tagName(builtin.target.os.tag); | 174 | const url_root = url_root: { |
| 175 | for (config.supported_targets.items) |target| { | ||
| 176 | const triple = try std.fmt.allocPrint(allocator, "{s}-{s}", .{ | ||
| 177 | @tagName(target.cpu.arch), | ||
| 178 | @tagName(target.os.tag), | ||
| 179 | }); | ||
| 180 | defer allocator.free(triple); | ||
| 181 | |||
| 182 | if (map.get(triple)) |url_root| { | ||
| 183 | break :url_root url_root; | ||
| 184 | } | ||
| 185 | } | ||
| 170 | 186 | ||
| 171 | const url_root = map.get(triple) orelse { | ||
| 172 | return null; | 187 | return null; |
| 173 | }; | 188 | }; |
| 174 | if (url_root != .Object) { | 189 | if (url_root != .Object) { |