summaryrefslogtreecommitdiff
path: root/src/Installation.zig
diff options
context:
space:
mode:
authorGravatar Uko Kokņevičs2022-04-28 15:27:11 +0300
committerGravatar Uko Kokņevičs2022-04-28 15:29:33 +0300
commit53af0877444ea6c14b1ad5baec94afbeebc5e5e0 (patch)
tree837fba9d78c895243f598c95916bff4a4c56f609 /src/Installation.zig
parentAdd Installation.isActive, use it (diff)
downloadzup-53af0877444ea6c14b1ad5baec94afbeebc5e5e0.tar.gz
zup-53af0877444ea6c14b1ad5baec94afbeebc5e5e0.tar.xz
zup-53af0877444ea6c14b1ad5baec94afbeebc5e5e0.zip
Added support for config files
Diffstat (limited to 'src/Installation.zig')
-rw-r--r--src/Installation.zig27
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 @@
1const builtin = @import("builtin");
2const curl = @import("curl"); 1const curl = @import("curl");
3const std = @import("std"); 2const std = @import("std");
4const xdg = @import("xdg"); 3const xdg = @import("xdg");
4const zup = @import("zup");
5 5
6const Allocator = std.mem.Allocator; 6const Allocator = std.mem.Allocator;
7const Config = zup.Config;
7const ChildProcess = std.ChildProcess; 8const ChildProcess = std.ChildProcess;
8const JsonValue = std.json.Value; 9const JsonValue = std.json.Value;
9const SemanticVersion = std.SemanticVersion; 10const 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
131pub fn getAvailableList(allocator: Allocator) !Installations { 132pub 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
163fn parseInstallation(allocator: Allocator, name: []const u8, value: JsonValue) !?Installation { 166fn 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) {