summaryrefslogtreecommitdiff
path: root/src/Installation.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/Installation.zig')
-rw-r--r--src/Installation.zig22
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 @@
1const curl = @import("curl");
2const std = @import("std"); 1const std = @import("std");
3const xdg = @import("xdg"); 2const xdg = @import("xdg");
4const zup = @import("root"); 3const zup = @import("root");
@@ -7,9 +6,11 @@ const Allocator = std.mem.Allocator;
7const ArenaAllocator = std.heap.ArenaAllocator; 6const ArenaAllocator = std.heap.ArenaAllocator;
8const Config = zup.Config; 7const Config = zup.Config;
9const ChildProcess = std.ChildProcess; 8const ChildProcess = std.ChildProcess;
9const EasyHttp = zup.EasyHttp;
10const JsonValue = std.json.Value; 10const JsonValue = std.json.Value;
11const SemanticVersion = std.SemanticVersion; 11const SemanticVersion = std.SemanticVersion;
12const StringHashMap = std.StringHashMap; 12const StringHashMap = std.StringHashMap;
13const Uri = std.Uri;
13 14
14const Installation = @This(); 15const Installation = @This();
15 16
@@ -18,11 +19,12 @@ pub const Installations = StringHashMap(Installation);
18allocator: Allocator, 19allocator: Allocator,
19ver_str: []u8, 20ver_str: []u8,
20version: SemanticVersion, 21version: SemanticVersion,
21url: ?[:0]u8, 22url_str: ?[]u8,
23url: ?Uri,
22 24
23pub fn deinit(self: *Installation) void { 25pub 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
139const index_json_uri = Uri.parse("https://ziglang.org/download/index.json") catch unreachable;
140
136pub fn getAvailableList(config: Config) !Installations { 141pub 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}