diff options
Diffstat (limited to 'src/Installation.zig')
| -rw-r--r-- | src/Installation.zig | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/src/Installation.zig b/src/Installation.zig index 54b870c..8218f23 100644 --- a/src/Installation.zig +++ b/src/Installation.zig | |||
| @@ -1,4 +1,3 @@ | |||
| 1 | const curl = @import("curl"); | ||
| 2 | const std = @import("std"); | 1 | const std = @import("std"); |
| 3 | const xdg = @import("xdg"); | 2 | const xdg = @import("xdg"); |
| 4 | const zup = @import("root"); | 3 | const zup = @import("root"); |
| @@ -7,9 +6,11 @@ const Allocator = std.mem.Allocator; | |||
| 7 | const ArenaAllocator = std.heap.ArenaAllocator; | 6 | const ArenaAllocator = std.heap.ArenaAllocator; |
| 8 | const Config = zup.Config; | 7 | const Config = zup.Config; |
| 9 | const ChildProcess = std.ChildProcess; | 8 | const ChildProcess = std.ChildProcess; |
| 9 | const EasyHttp = zup.EasyHttp; | ||
| 10 | const JsonValue = std.json.Value; | 10 | const JsonValue = std.json.Value; |
| 11 | const SemanticVersion = std.SemanticVersion; | 11 | const SemanticVersion = std.SemanticVersion; |
| 12 | const StringHashMap = std.StringHashMap; | 12 | const StringHashMap = std.StringHashMap; |
| 13 | const Uri = std.Uri; | ||
| 13 | 14 | ||
| 14 | const Installation = @This(); | 15 | const Installation = @This(); |
| 15 | 16 | ||
| @@ -18,11 +19,12 @@ pub const Installations = StringHashMap(Installation); | |||
| 18 | allocator: Allocator, | 19 | allocator: Allocator, |
| 19 | ver_str: []u8, | 20 | ver_str: []u8, |
| 20 | version: SemanticVersion, | 21 | version: SemanticVersion, |
| 21 | url: ?[:0]u8, | 22 | url_str: ?[]u8, |
| 23 | url: ?Uri, | ||
| 22 | 24 | ||
| 23 | pub fn deinit(self: *Installation) void { | 25 | pub fn deinit(self: *Installation) void { |
| 24 | self.allocator.free(self.ver_str); | 26 | self.allocator.free(self.ver_str); |
| 25 | if (self.url) |url| self.allocator.free(url); | 27 | if (self.url_str) |str| self.allocator.free(str); |
| 26 | 28 | ||
| 27 | self.* = undefined; | 29 | self.* = undefined; |
| 28 | } | 30 | } |
| @@ -118,6 +120,7 @@ pub fn getInstalledList(allocator: Allocator) !Installations { | |||
| 118 | .allocator = allocator, | 120 | .allocator = allocator, |
| 119 | .ver_str = res.stdout, | 121 | .ver_str = res.stdout, |
| 120 | .version = version, | 122 | .version = version, |
| 123 | .url_str = null, | ||
| 121 | .url = null, | 124 | .url = null, |
| 122 | }); | 125 | }); |
| 123 | } | 126 | } |
| @@ -133,18 +136,21 @@ pub fn isInstalled(allocator: Allocator, name: []const u8) !bool { | |||
| 133 | return true; | 136 | return true; |
| 134 | } | 137 | } |
| 135 | 138 | ||
| 139 | const index_json_uri = Uri.parse("https://ziglang.org/download/index.json") catch unreachable; | ||
| 140 | |||
| 136 | pub fn getAvailableList(config: Config) !Installations { | 141 | pub fn getAvailableList(config: Config) !Installations { |
| 137 | var arena = ArenaAllocator.init(config.allocator); | 142 | var arena = ArenaAllocator.init(config.allocator); |
| 138 | defer arena.deinit(); | 143 | defer arena.deinit(); |
| 139 | 144 | ||
| 140 | const allocator = arena.allocator(); | 145 | const allocator = arena.allocator(); |
| 141 | 146 | ||
| 142 | const json_str = try curl.easyDownload(allocator, "https://ziglang.org/download/index.json"); | 147 | var data = try EasyHttp.get(allocator, index_json_uri); |
| 148 | defer allocator.free(data); | ||
| 143 | 149 | ||
| 144 | const parsed = try std.json.parseFromSliceLeaky( | 150 | const parsed = try std.json.parseFromSliceLeaky( |
| 145 | JsonValue, | 151 | JsonValue, |
| 146 | allocator, | 152 | allocator, |
| 147 | json_str, | 153 | data, |
| 148 | .{}, | 154 | .{}, |
| 149 | ); | 155 | ); |
| 150 | 156 | ||
| @@ -203,8 +209,9 @@ fn parseInstallation(config: Config, name: []const u8, value: JsonValue) !?Insta | |||
| 203 | if (url_src != .string) { | 209 | if (url_src != .string) { |
| 204 | return error.JsonSchema; | 210 | return error.JsonSchema; |
| 205 | } | 211 | } |
| 206 | var url = try allocator.dupeZ(u8, url_src.string); | 212 | var url_str = try allocator.dupe(u8, url_src.string); |
| 207 | errdefer allocator.free(url); | 213 | errdefer allocator.free(url_str); |
| 214 | const url = try Uri.parse(url_str); | ||
| 208 | 215 | ||
| 209 | const version_src = if (map.get("version")) |ver| blk: { | 216 | const version_src = if (map.get("version")) |ver| blk: { |
| 210 | if (ver != .string) { | 217 | if (ver != .string) { |
| @@ -223,6 +230,7 @@ fn parseInstallation(config: Config, name: []const u8, value: JsonValue) !?Insta | |||
| 223 | .allocator = allocator, | 230 | .allocator = allocator, |
| 224 | .ver_str = ver_str, | 231 | .ver_str = ver_str, |
| 225 | .version = version, | 232 | .version = version, |
| 233 | .url_str = url_str, | ||
| 226 | .url = url, | 234 | .url = url, |
| 227 | }; | 235 | }; |
| 228 | } | 236 | } |