authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-06-05 16:54:51 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-06-05 16:54:51 -07:00
log7a3c5144855c3dd5d6bdb3025357d576dfb589c4
tree000ac4e2d2cee3c2103ccab175b8f996d64876a7
parent66cfa409347fd6bf982dcc6831b788b969bfcaa5

add `local` dependency type, fixes #1


5 files changed, 30 insertions(+), 2 deletions(-)

docs/zig.mod.md+3
......@@ -50,6 +50,7 @@ This is the object used in the top-level `dependencies` attribute and used to ad
5050This is the base attribute used to reference external code for use in your project. `type` is an enum and only allows certain values. `path` is the URL or other identifier used to locate the contents of this package based on the `type`.
5151
5252The available `type`s are:
53- `local`
5354- `system_lib`
5455- `git`
5556- `hg`
......@@ -57,6 +58,8 @@ The available `type`s are:
5758
5859For the full details on `Dep` types, you can check out the source where the enum is defined: https://github.com/nektro/zigmod/blob/master/src/util/dep_type.zig.
5960
61> Note: the `local` type modifies the input behavior to be shorthand for `<name> <main>` rather than `path version` since the latter fields don't make sense for local files.
62
6063#### Dep `version`
6164- Type: `string-string`
6265- Example: `commit-2c21764`
src/common.zig+7
......@@ -82,6 +82,9 @@ fn get_moddir(basedir: []const u8, d: u.Dep, parent_name: []const u8, options: C
8282 u.print("fetch: {s}: {s}: {s}", .{ parent_name, @tagName(d.type), d.path });
8383 }
8484 switch (d.type) {
85 .local => {
86 return d.path;
87 },
8588 .system_lib => {
8689 // no op
8790 return "";
......@@ -186,6 +189,10 @@ fn get_module_from_dep(list: *std.ArrayList(u.Module), d: u.Dep, dir: []const u8
186189 });
187190 },
188191 else => {
192 if (d.type == .local) {
193 try list.append(try u.Module.from(d));
194 return;
195 }
189196 var dd = try collect_deps(dir, try u.concat(&.{ moddir, "/zig.mod" }), options) catch |e| switch (e) {
190197 error.FileNotFound => {
191198 if (d.main.len > 0 or d.c_include_dirs.len > 0 or d.c_source_files.len > 0) {
src/util/dep.zig+3
......@@ -26,6 +26,9 @@ pub const Dep = struct {
2626 yaml: ?yaml.Mapping,
2727
2828 pub fn clean_path(self: Dep) ![]const u8 {
29 if (self.type == .local) {
30 return if (self.path.len == 0) "../.." else self.path;
31 }
2932 var p = self.path;
3033 p = u.trim_prefix(p, "http://");
3134 p = u.trim_prefix(p, "https://");
src/util/dep_type.zig+3
......@@ -8,6 +8,7 @@ const u = @import("./index.zig");
88
99// zig fmt: off
1010pub const DepType = enum {
11 local, // A 'package' derived from files in the same repository.
1112 system_lib, // std.build.LibExeObjStep.linkSystemLibrary
1213 git, // https://git-scm.com/
1314 hg, // https://www.mercurial-scm.org/
......@@ -32,6 +33,7 @@ pub const DepType = enum {
3233
3334 pub fn pull(self: DepType, rpath: []const u8, dpath: []const u8) !void {
3435 switch (self) {
36 .local => {},
3537 .system_lib => {},
3638 .git => {
3739 u.assert((try u.run_cmd(null, &.{ "git", "clone", "--recurse-submodules", rpath, dpath })) == 0, "git clone {s} failed", .{rpath});
......@@ -55,6 +57,7 @@ pub const DepType = enum {
5557
5658 pub fn update(self: DepType, dpath: []const u8, rpath: []const u8) !void {
5759 switch (self) {
60 .local => {},
5861 .system_lib => {},
5962 .git => {
6063 u.assert((try u.run_cmd(dpath, &.{ "git", "fetch" })) == 0, "git fetch failed", .{});
src/util/modfile.zig+14-2
......@@ -70,6 +70,8 @@ pub const ModFile = struct {
7070 var dtype: []const u8 = undefined;
7171 var path: []const u8 = undefined;
7272 var version: ?[]const u8 = null;
73 var name = item.mapping.get_string("name");
74 var main = item.mapping.get_string("main");
7375 if (item.mapping.get("src")) |val| {
7476 var src_iter = std.mem.tokenize(val.string, " ");
7577 dtype = src_iter.next().?;
......@@ -88,13 +90,23 @@ pub const ModFile = struct {
8890 version = "";
8991 }
9092 const dep_type = std.meta.stringToEnum(u.DepType, dtype).?;
93 if (dep_type == .local) {
94 if (path.len > 0) {
95 name = path;
96 path = "";
97 }
98 if (version.?.len > 0) {
99 main = version.?;
100 version = "";
101 }
102 }
91103
92104 try dep_list.append(u.Dep{
93105 .type = dep_type,
94106 .path = path,
95107 .id = item.mapping.get_string("id"),
96 .name = item.mapping.get_string("name"),
97 .main = item.mapping.get_string("main"),
108 .name = name,
109 .main = main,
98110 .version = version.?,
99111 .c_include_dirs = try item.mapping.get_string_array(alloc, "c_include_dirs"),
100112 .c_source_flags = try item.mapping.get_string_array(alloc, "c_source_flags"),