From fb97f94ee81a6fca7950c481004e0f21d1682b5a Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Thu, 2 Dec 2021 02:57:52 -0800 Subject: [PATCH] zig.mod- retire `dev_dependencies` in favor of `root_dependencies` and `build_dependencies` --- deps.zig | 1 - docs/zig.mod.md | 6 +++++- src/cmd/aquila/add.zig | 7 ++++++- src/cmd/aquila/install.zig | 8 +++++++- src/cmd/fetch.zig | 6 ++++++ src/cmd/zpm/add.zig | 9 +++++++-- src/common.zig | 9 ++++++++- src/util/dep.zig | 1 + src/util/modfile.zig | 13 ++++++++----- src/util/module.zig | 2 ++ zig.mod | 2 +- 11 files changed, 51 insertions(+), 13 deletions(-) diff --git a/deps.zig b/deps.zig index 9bc700a..d7d867a 100644 --- a/deps.zig +++ b/deps.zig @@ -164,5 +164,4 @@ pub const pkgs = struct { }; pub const imports = struct { - pub const zigmod = @import(".zigmod/deps/../../src/lib.zig"); }; diff --git a/docs/zig.mod.md b/docs/zig.mod.md index b29e2ee..3525880 100644 --- a/docs/zig.mod.md +++ b/docs/zig.mod.md @@ -42,10 +42,14 @@ This accepts a list of local directories to embed static assets. These files wil - Type: `[]Dep` This is a list of `Dep` objects. `Dep` objects are how you include the other people's code in your project. See the `Dep` documentation below to learn more about the attributes available here. -### `dev_dependencies` +### `root_dependencies` - Type: `[]Dep` Similar to `dependencies` but will only get added to the project if the current `zig.mod` is the root module. +### `build_dependencies` +- Type: `[]Dep` +Similar to `dependencies` but will only get added to the project if the current `zig.mod` is the root module. Exposed in `deps.zig` through the `deps.imports` decl. + ---- ### Dep Object diff --git a/src/cmd/aquila/add.zig b/src/cmd/aquila/add.zig index 9bd1b9b..6ee8810 100644 --- a/src/cmd/aquila/add.zig +++ b/src/cmd/aquila/add.zig @@ -25,7 +25,12 @@ pub fn do(dir: std.fs.Dir, pkg_id: string) !string { }); const m = try zigmod.ModFile.from_dir(gpa, dir); - for (m.devdeps) |d| { + for (m.rootdeps) |d| { + if (std.mem.eql(u8, d.path, pkg_url)) { + return pkg_url; + } + } + for (m.builddeps) |d| { if (std.mem.eql(u8, d.path, pkg_url)) { return pkg_url; } diff --git a/src/cmd/aquila/install.zig b/src/cmd/aquila/install.zig index d9da552..e425c12 100644 --- a/src/cmd/aquila/install.zig +++ b/src/cmd/aquila/install.zig @@ -31,7 +31,13 @@ pub fn execute(args: [][]u8) !void { // get modfile and dep const m = try zigmod.ModFile.from_dir(gpa, homedir); var dep: zigmod.Dep = undefined; - for (m.devdeps) |d| { + for (m.rootdeps) |d| { + if (std.mem.eql(u8, d.path, pkgurl)) { + dep = d; + break; + } + } + for (m.builddeps) |d| { if (std.mem.eql(u8, d.path, pkgurl)) { dep = d; break; diff --git a/src/cmd/fetch.zig b/src/cmd/fetch.zig index 5cc8003..49b29dd 100644 --- a/src/cmd/fetch.zig +++ b/src/cmd/fetch.zig @@ -351,6 +351,9 @@ fn print_pkgs(w: std.fs.File.Writer, m: zigmod.Module) !void { if (d.main.len == 0) { continue; } + if (d.for_build) { + continue; + } const ident = try zig_name_from_pkg_name(d.name); try w.print(" pub const {s} = package_data._{s};\n", .{ ident, d.id[0..12] }); } @@ -362,6 +365,9 @@ fn print_imports(w: std.fs.File.Writer, m: zigmod.Module, path: string) !void { if (d.main.len == 0) { continue; } + if (!d.for_build) { + continue; + } const ident = try zig_name_from_pkg_name(d.name); try w.print(" pub const {s} = @import(\"{}/{}/{s}\");\n", .{ ident, std.zig.fmtEscapes(path), std.zig.fmtEscapes(d.clean_path), d.main }); } diff --git a/src/cmd/zpm/add.zig b/src/cmd/zpm/add.zig index 9f25b03..931056e 100644 --- a/src/cmd/zpm/add.zig +++ b/src/cmd/zpm/add.zig @@ -40,9 +40,14 @@ pub fn execute(args: [][]u8) !void { std.log.warn("dependency with name '{s}' already exists in your dependencies", .{found.name}); } } - for (self_module.devdeps) |dep| { + for (self_module.rootdeps) |dep| { if (std.mem.eql(u8, dep.name, found.name)) { - std.log.warn("dependency with name '{s}' already exists in your dev_dependencies", .{found.name}); + std.log.warn("dependency with name '{s}' already exists in your root_dependencies", .{found.name}); + } + } + for (self_module.builddeps) |dep| { + if (std.mem.eql(u8, dep.name, found.name)) { + std.log.warn("dependency with name '{s}' already exists in your build_dependencies", .{found.name}); } } diff --git a/src/common.zig b/src/common.zig index 9e67092..1d887b4 100644 --- a/src/common.zig +++ b/src/common.zig @@ -37,7 +37,12 @@ pub fn collect_deps_deep(cachepath: string, mdir: std.fs.Dir, options: *CollectO try moduledeps.append(try add_files_package(options.alloc, cachepath, "root", mdir, m.root_files)); } try moduledeps.append(try collect_deps(cachepath, mdir, options)); - for (m.devdeps) |*d| { + for (m.rootdeps) |*d| { + if (try get_module_from_dep(d, cachepath, options)) |founddep| { + try moduledeps.append(founddep); + } + } + for (m.builddeps) |*d| { if (try get_module_from_dep(d, cachepath, options)) |founddep| { try moduledeps.append(founddep); } @@ -229,6 +234,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO .clean_path = d.path, .yaml = null, .dep = d.*, + .for_build = d.for_build, }; }, else => { @@ -259,6 +265,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO else => e, }; dd.dep = d.*; + dd.for_build = d.for_build; const save = dd; if (d.type != .local) dd.clean_path = u.trim_prefix(modpath, cachepath)[1..]; if (dd.id.len == 0) dd.id = try u.random_string(options.alloc, 48); diff --git a/src/util/dep.zig b/src/util/dep.zig index 74c42d8..ead8498 100644 --- a/src/util/dep.zig +++ b/src/util/dep.zig @@ -28,6 +28,7 @@ pub const Dep = struct { deps: []zigmod.Dep, keep: bool = false, vcpkg: bool = false, + for_build: bool = false, pub fn clean_path(self: Dep) !string { if (self.type == .local) { diff --git a/src/util/modfile.zig b/src/util/modfile.zig index 61dc8b0..598d5a2 100644 --- a/src/util/modfile.zig +++ b/src/util/modfile.zig @@ -23,9 +23,10 @@ pub const ModFile = struct { c_source_files: []const string, deps: []zigmod.Dep, yaml: yaml.Mapping, - devdeps: []zigmod.Dep, root_files: []const string, files: []const string, + rootdeps: []zigmod.Dep, + builddeps: []zigmod.Dep, pub fn init(alloc: *std.mem.Allocator, mpath: string) !Self { const file = try std.fs.cwd().openFile(mpath, .{}); @@ -59,15 +60,16 @@ pub const ModFile = struct { .c_include_dirs = try mapping.get_string_array(alloc, "c_include_dirs"), .c_source_flags = try mapping.get_string_array(alloc, "c_source_flags"), .c_source_files = try mapping.get_string_array(alloc, "c_source_files"), - .deps = try dep_list_by_name(alloc, mapping, &.{"dependencies"}), + .deps = try dep_list_by_name(alloc, mapping, &.{"dependencies"}, false), .yaml = mapping, - .devdeps = try dep_list_by_name(alloc, mapping, &.{"dev_dependencies"}), .root_files = try mapping.get_string_array(alloc, "root_files"), .files = try mapping.get_string_array(alloc, "files"), + .rootdeps = try dep_list_by_name(alloc, mapping, &.{ "dev_dependencies", "root_dependencies" }, false), + .builddeps = try dep_list_by_name(alloc, mapping, &.{ "dev_dependencies", "build_dependencies" }, true), }; } - fn dep_list_by_name(alloc: *std.mem.Allocator, mapping: yaml.Mapping, props: []const string) anyerror![]zigmod.Dep { + fn dep_list_by_name(alloc: *std.mem.Allocator, mapping: yaml.Mapping, props: []const string, for_build: bool) anyerror![]zigmod.Dep { var dep_list = std.ArrayList(zigmod.Dep).init(alloc); defer dep_list.deinit(); @@ -124,9 +126,10 @@ pub const ModFile = struct { .only_os = try u.list_remove(alloc, try u.split(alloc, item.mapping.get_string("only_os"), ","), ""), .except_os = try u.list_remove(alloc, try u.split(alloc, item.mapping.get_string("except_os"), ","), ""), .yaml = item.mapping, - .deps = try dep_list_by_name(alloc, item.mapping, &.{"dependencies"}), + .deps = try dep_list_by_name(alloc, item.mapping, &.{"dependencies"}, for_build), .keep = std.mem.eql(u8, "true", item.mapping.get_string("keep")), .vcpkg = std.mem.eql(u8, "true", item.mapping.get_string("vcpkg")), + .for_build = for_build, }); } } diff --git a/src/util/module.zig b/src/util/module.zig index a987228..1d5540d 100644 --- a/src/util/module.zig +++ b/src/util/module.zig @@ -25,6 +25,7 @@ pub const Module = struct { deps: []Module, clean_path: string, dep: ?zigmod.Dep, + for_build: bool = false, pub fn from(alloc: *std.mem.Allocator, dep: zigmod.Dep, modpath: string, options: *common.CollectOptions) !Module { var moddeps = std.ArrayList(Module).init(alloc); @@ -50,6 +51,7 @@ pub const Module = struct { .except_os = dep.except_os, .yaml = dep.yaml, .dep = dep, + .for_build = dep.for_build, }; } diff --git a/zig.mod b/zig.mod index c984f4c..bdb2301 100644 --- a/zig.mod +++ b/zig.mod @@ -41,5 +41,5 @@ dependencies: - src: git https://github.com/nektro/zig-inquirer - src: git https://github.com/arqv/ini -dev_dependencies: +root_dependencies: - src: git https://github.com/marlersoft/zigwin32