Skip to content

Commit

Permalink
zig.mod- retire dev_dependencies in favor of root_dependencies an…
Browse files Browse the repository at this point in the history
…d `build_dependencies`
  • Loading branch information
nektro committed Dec 2, 2021
1 parent df9cbb3 commit fb97f94
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 13 deletions.
1 change: 0 additions & 1 deletion deps.zig
Original file line number Diff line number Diff line change
Expand Up @@ -164,5 +164,4 @@ pub const pkgs = struct {
};

pub const imports = struct {
pub const zigmod = @import(".zigmod/deps/../../src/lib.zig");
};
6 changes: 5 additions & 1 deletion docs/zig.mod.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion src/cmd/aquila/add.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
8 changes: 7 additions & 1 deletion src/cmd/aquila/install.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions src/cmd/fetch.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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] });
}
Expand All @@ -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 });
}
Expand Down
9 changes: 7 additions & 2 deletions src/cmd/zpm/add.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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});
}
}

Expand Down
9 changes: 8 additions & 1 deletion src/common.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/util/dep.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 8 additions & 5 deletions src/util/modfile.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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, .{});
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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,
});
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/util/module.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -50,6 +51,7 @@ pub const Module = struct {
.except_os = dep.except_os,
.yaml = dep.yaml,
.dep = dep,
.for_build = dep.for_build,
};
}

Expand Down
2 changes: 1 addition & 1 deletion zig.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit fb97f94

Please sign in to comment.