diff options
Diffstat (limited to 'src/Installation.zig')
| -rw-r--r-- | src/Installation.zig | 48 |
1 files changed, 26 insertions, 22 deletions
diff --git a/src/Installation.zig b/src/Installation.zig index 3ff65ea..54b870c 100644 --- a/src/Installation.zig +++ b/src/Installation.zig | |||
| @@ -4,6 +4,7 @@ const xdg = @import("xdg"); | |||
| 4 | const zup = @import("root"); | 4 | const zup = @import("root"); |
| 5 | 5 | ||
| 6 | const Allocator = std.mem.Allocator; | 6 | const Allocator = std.mem.Allocator; |
| 7 | const ArenaAllocator = std.heap.ArenaAllocator; | ||
| 7 | const Config = zup.Config; | 8 | const Config = zup.Config; |
| 8 | const ChildProcess = std.ChildProcess; | 9 | const ChildProcess = std.ChildProcess; |
| 9 | const JsonValue = std.json.Value; | 10 | const JsonValue = std.json.Value; |
| @@ -84,7 +85,7 @@ pub fn getInstalledList(allocator: Allocator) !Installations { | |||
| 84 | 85 | ||
| 85 | var it = zup_data_iterable.iterate(); | 86 | var it = zup_data_iterable.iterate(); |
| 86 | while (try it.next()) |item| { | 87 | while (try it.next()) |item| { |
| 87 | if (item.kind != .Directory) { | 88 | if (item.kind != .directory) { |
| 88 | continue; | 89 | continue; |
| 89 | } | 90 | } |
| 90 | 91 | ||
| @@ -133,32 +134,35 @@ pub fn isInstalled(allocator: Allocator, name: []const u8) !bool { | |||
| 133 | } | 134 | } |
| 134 | 135 | ||
| 135 | pub fn getAvailableList(config: Config) !Installations { | 136 | pub fn getAvailableList(config: Config) !Installations { |
| 136 | const allocator = config.allocator; | 137 | var arena = ArenaAllocator.init(config.allocator); |
| 138 | defer arena.deinit(); | ||
| 137 | 139 | ||
| 138 | var json_str = try curl.easyDownload(allocator, "https://ziglang.org/download/index.json"); | 140 | const allocator = arena.allocator(); |
| 139 | defer allocator.free(json_str); | ||
| 140 | 141 | ||
| 141 | var parser = std.json.Parser.init(allocator, false); | 142 | const json_str = try curl.easyDownload(allocator, "https://ziglang.org/download/index.json"); |
| 142 | defer parser.deinit(); | ||
| 143 | 143 | ||
| 144 | var vt = try parser.parse(json_str); | 144 | const parsed = try std.json.parseFromSliceLeaky( |
| 145 | defer vt.deinit(); | 145 | JsonValue, |
| 146 | allocator, | ||
| 147 | json_str, | ||
| 148 | .{}, | ||
| 149 | ); | ||
| 146 | 150 | ||
| 147 | if (vt.root != .Object) { | 151 | if (parsed != .object) { |
| 148 | return error.JsonSchema; | 152 | return error.JsonSchema; |
| 149 | } | 153 | } |
| 150 | 154 | ||
| 151 | var installations = Installations.init(allocator); | 155 | var installations = Installations.init(config.allocator); |
| 152 | errdefer Installation.deinitMap(allocator, &installations); | 156 | errdefer Installation.deinitMap(config.allocator, &installations); |
| 153 | 157 | ||
| 154 | var it = vt.root.Object.iterator(); | 158 | var it = parsed.object.iterator(); |
| 155 | while (it.next()) |kv| { | 159 | while (it.next()) |kv| { |
| 156 | var installation_opt = try parseInstallation(config, kv.key_ptr.*, kv.value_ptr.*); | 160 | var installation_opt = try parseInstallation(config, kv.key_ptr.*, kv.value_ptr.*); |
| 157 | if (installation_opt) |*installation| { | 161 | if (installation_opt) |*installation| { |
| 158 | errdefer installation.deinit(); | 162 | errdefer installation.deinit(); |
| 159 | 163 | ||
| 160 | var name = try allocator.dupe(u8, kv.key_ptr.*); | 164 | var name = try config.allocator.dupe(u8, kv.key_ptr.*); |
| 161 | errdefer allocator.free(name); | 165 | errdefer config.allocator.free(name); |
| 162 | 166 | ||
| 163 | try installations.putNoClobber(name, installation.*); | 167 | try installations.putNoClobber(name, installation.*); |
| 164 | } | 168 | } |
| @@ -170,10 +174,10 @@ pub fn getAvailableList(config: Config) !Installations { | |||
| 170 | fn parseInstallation(config: Config, name: []const u8, value: JsonValue) !?Installation { | 174 | fn parseInstallation(config: Config, name: []const u8, value: JsonValue) !?Installation { |
| 171 | const allocator = config.allocator; | 175 | const allocator = config.allocator; |
| 172 | 176 | ||
| 173 | if (value != .Object) { | 177 | if (value != .object) { |
| 174 | return error.JsonSchema; | 178 | return error.JsonSchema; |
| 175 | } | 179 | } |
| 176 | const map = value.Object; | 180 | const map = value.object; |
| 177 | 181 | ||
| 178 | const url_root = url_root: { | 182 | const url_root = url_root: { |
| 179 | for (config.supported_targets.items) |target| { | 183 | for (config.supported_targets.items) |target| { |
| @@ -190,23 +194,23 @@ fn parseInstallation(config: Config, name: []const u8, value: JsonValue) !?Insta | |||
| 190 | 194 | ||
| 191 | return null; | 195 | return null; |
| 192 | }; | 196 | }; |
| 193 | if (url_root != .Object) { | 197 | if (url_root != .object) { |
| 194 | return error.JsonSchema; | 198 | return error.JsonSchema; |
| 195 | } | 199 | } |
| 196 | const url_src = url_root.Object.get("tarball") orelse { | 200 | const url_src = url_root.object.get("tarball") orelse { |
| 197 | return error.JsonSchema; | 201 | return error.JsonSchema; |
| 198 | }; | 202 | }; |
| 199 | if (url_src != .String) { | 203 | if (url_src != .string) { |
| 200 | return error.JsonSchema; | 204 | return error.JsonSchema; |
| 201 | } | 205 | } |
| 202 | var url = try allocator.dupeZ(u8, url_src.String); | 206 | var url = try allocator.dupeZ(u8, url_src.string); |
| 203 | errdefer allocator.free(url); | 207 | errdefer allocator.free(url); |
| 204 | 208 | ||
| 205 | const version_src = if (map.get("version")) |ver| blk: { | 209 | const version_src = if (map.get("version")) |ver| blk: { |
| 206 | if (ver != .String) { | 210 | if (ver != .string) { |
| 207 | return error.JsonSchema; | 211 | return error.JsonSchema; |
| 208 | } else { | 212 | } else { |
| 209 | break :blk ver.String; | 213 | break :blk ver.string; |
| 210 | } | 214 | } |
| 211 | } else blk: { | 215 | } else blk: { |
| 212 | break :blk name; | 216 | break :blk name; |