Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for git+https sources #24

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions fixtures/basic.zon
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,15 @@
.url = "https://gist.github.com/antlilja/8372900fcc09e38d7b0b6bbaddad3904/archive/6c3321e0969ff2463f8335da5601986cf2108690.tar.gz",
.hash = "1220363c7e27b2d3f39de6ff6e90f9537a0634199860fea237a55ddb1e1717f5d6a5",
},
// git+https test
.ziggy = .{
.url = "git+https://github.com/kristoff-it/ziggy#c66f47bc632c66668d61fa06eda112b41d6e5130",
.hash = "1220115ff095a3c970cc90fce115294ba67d6fbc4927472dc856abc51e2a1a9364d7",
},
// this has deps (github.com/zigimg/zigimg and codeberg.org/atman/zg) with no deps
.vaxis = .{
.url = "git+https://github.com/rockorager/libvaxis#1fd920a7aea1bb040c7c028f4bbf0af2ea58e1d1",
.hash = "1220feaa655e14cbb4baf59fe746f09a17fc6949be46ad64dd5044982f4fc1bb57c7",
},
},
}
1 change: 1 addition & 0 deletions src/Dependency.zig
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
url: []const u8,
rev: []const u8,
nix_hash: []const u8,
done: bool,
36 changes: 25 additions & 11 deletions src/codegen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn write(alloc: Allocator, out: anytype, deps: StringHashMap(Dependency)) !v
try out.writeAll(
\\# generated by zon2nix (https://github.com/nix-community/zon2nix)
\\
\\{ linkFarm, fetchzip }:
\\{ linkFarm, fetchzip, fetchgit }:
\\
\\linkFarm "zig-packages" [
\\
Expand All @@ -28,16 +28,30 @@ pub fn write(alloc: Allocator, out: anytype, deps: StringHashMap(Dependency)) !v
for (entries) |entry| {
const key = entry.key_ptr.*;
const dep = entry.value_ptr.*;
try out.print(
\\ {{
\\ name = "{s}";
\\ path = fetchzip {{
\\ url = "{s}";
\\ hash = "{s}";
\\ }};
\\ }}
\\
, .{ key, dep.url, dep.nix_hash });
if (dep.rev.len != 0) {
try out.print(
\\ {{
\\ name = "{s}";
\\ path = fetchgit {{
\\ url = "{s}";
\\ rev = "{s}";
\\ hash = "{s}";
\\ }};
\\ }}
\\
, .{ key, dep.url, dep.rev, dep.nix_hash });
} else {
try out.print(
\\ {{
\\ name = "{s}";
\\ path = fetchzip {{
\\ url = "{s}";
\\ hash = "{s}";
\\ }};
\\ }}
\\
, .{ key, dep.url, dep.nix_hash });
}
}

try out.writeAll("]\n");
Expand Down
8 changes: 7 additions & 1 deletion src/fetch.zig
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ pub fn fetch(alloc: Allocator, deps: *StringHashMap(Dependency)) !void {
}

var child = try alloc.create(ChildProcess);
const ref = try fmt.allocPrint(alloc, "tarball+{s}", .{dep.url});
const ref = ref: {
if (dep.rev.len == 0) {
break :ref try fmt.allocPrint(alloc, "tarball+{s}", .{dep.url});
} else {
break :ref try fmt.allocPrint(alloc, "git+{s}?rev={s}", .{ dep.url, dep.rev });
}
};
const argv = &[_][]const u8{ nix, "flake", "prefetch", "--json", "--extra-experimental-features", "flakes nix-command", ref };
child.* = ChildProcess.init(argv, alloc);
child.stdin_behavior = .Ignore;
Expand Down
26 changes: 22 additions & 4 deletions src/parse.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub fn parse(alloc: Allocator, deps: *StringHashMap(Dependency), file: File) !vo
for (deps_init.ast.fields) |dep_idx| {
var dep: Dependency = .{
.url = undefined,
.rev = undefined,
.nix_hash = undefined,
.done = false,
};
Expand All @@ -40,14 +41,25 @@ pub fn parse(alloc: Allocator, deps: *StringHashMap(Dependency), file: File) !vo
var has_hash = false;

const dep_init = ast.fullStructInit(&buf, dep_idx) orelse {
return error.parseError;
std.log.warn("failed to get dependencies", .{});
continue;
};

for (dep_init.ast.fields) |dep_field_idx| {
const name = try parseFieldName(alloc, ast, dep_field_idx);

if (mem.eql(u8, name, "url")) {
dep.url = try parseString(alloc, ast, dep_field_idx);
const url = try parseString(alloc, ast, dep_field_idx);
if (std.mem.startsWith(u8, url, "https://")) {
dep.url = url;
} else if (std.mem.startsWith(u8, url, "git+https://")) {
const url_end = std.mem.indexOf(u8, url[0..], "#").?;
const raw_url = url[4..url_end];
const hash_start = url_end + 1; // +1 to skip the '#'
const git_hash = url[hash_start..];
dep.url = raw_url;
dep.rev = git_hash;
}
has_url = true;
} else if (mem.eql(u8, name, "hash")) {
hash = try parseString(alloc, ast, dep_field_idx);
Expand Down Expand Up @@ -84,11 +96,17 @@ test parse {

var deps = StringHashMap(Dependency).init(alloc);
const basic = try fs.cwd().openFile("fixtures/basic.zon", .{});
defer basic.close();
try parse(alloc, &deps, basic);
basic.close();

try testing.expectEqual(deps.count(), 3);
try testing.expectEqual(deps.count(), 5);
try testing.expectEqualStrings(deps.get("122048992ca58a78318b6eba4f65c692564be5af3b30fbef50cd4abeda981b2e7fa5").?.url, "https://github.com/ziglibs/known-folders/archive/fa75e1bc672952efa0cf06160bbd942b47f6d59b.tar.gz");
try testing.expectEqualStrings(deps.get("122089a8247a693cad53beb161bde6c30f71376cd4298798d45b32740c3581405864").?.url, "https://github.com/ziglibs/diffz/archive/90353d401c59e2ca5ed0abe5444c29ad3d7489aa.tar.gz");
try testing.expectEqualStrings(deps.get("1220363c7e27b2d3f39de6ff6e90f9537a0634199860fea237a55ddb1e1717f5d6a5").?.url, "https://gist.github.com/antlilja/8372900fcc09e38d7b0b6bbaddad3904/archive/6c3321e0969ff2463f8335da5601986cf2108690.tar.gz");
const ziggy = deps.get("1220115ff095a3c970cc90fce115294ba67d6fbc4927472dc856abc51e2a1a9364d7").?;
try testing.expectEqualStrings(ziggy.url, "https://github.com/kristoff-it/ziggy");
try testing.expectEqualStrings(ziggy.rev, "c66f47bc632c66668d61fa06eda112b41d6e5130");
const vaxis = deps.get("1220feaa655e14cbb4baf59fe746f09a17fc6949be46ad64dd5044982f4fc1bb57c7").?;
try testing.expectEqualStrings(vaxis.url, "https://github.com/rockorager/libvaxis");
try testing.expectEqualStrings(vaxis.rev, "1fd920a7aea1bb040c7c028f4bbf0af2ea58e1d1");
}