authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-10-28 02:43:49 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-10-28 02:43:49 -07:00
logd55b67f873b850892dc08f2f5346f95cc3d3fedd
tree454d078dfa7ccd1d6f36b72d790c987f88d4844e
parent70ce68b8e7b7f00821d74b4f559ef80bfacdaed4

move Module to lib entry point


7 files changed, 35 insertions(+), 31 deletions(-)

src/cmd/ci.zig+2-1
...@@ -2,6 +2,7 @@ const std = @import("std");...@@ -2,6 +2,7 @@ const std = @import("std");
2const string = []const u8;2const string = []const u8;
3const gpa = std.heap.c_allocator;3const gpa = std.heap.c_allocator;
44
5const zigmod = @import("../lib.zig");
5const u = @import("./../util/index.zig");6const u = @import("./../util/index.zig");
6const common = @import("./../common.zig");7const common = @import("./../common.zig");
78
...@@ -24,7 +25,7 @@ pub fn do(cachepath: string, dir: std.fs.Dir) !void {...@@ -24,7 +25,7 @@ pub fn do(cachepath: string, dir: std.fs.Dir) !void {
24 };25 };
25 const top_module = try common.collect_deps_deep(cachepath, dir, &options);26 const top_module = try common.collect_deps_deep(cachepath, dir, &options);
2627
27 var list = std.ArrayList(u.Module).init(gpa);28 var list = std.ArrayList(zigmod.Module).init(gpa);
28 try common.collect_pkgs(top_module, &list);29 try common.collect_pkgs(top_module, &list);
2930
30 const fetch = @import("./fetch.zig");31 const fetch = @import("./fetch.zig");
src/cmd/fetch.zig+13-12
...@@ -2,6 +2,7 @@ const std = @import("std");...@@ -2,6 +2,7 @@ const std = @import("std");
2const string = []const u8;2const string = []const u8;
3const gpa = std.heap.c_allocator;3const gpa = std.heap.c_allocator;
44
5const zigmod = @import("../lib.zig");
5const u = @import("./../util/index.zig");6const u = @import("./../util/index.zig");
6const common = @import("./../common.zig");7const common = @import("./../common.zig");
78
...@@ -26,7 +27,7 @@ pub fn execute(args: [][]u8) !void {...@@ -26,7 +27,7 @@ pub fn execute(args: [][]u8) !void {
26 };27 };
27 const top_module = try common.collect_deps_deep(cachepath, dir, &options);28 const top_module = try common.collect_deps_deep(cachepath, dir, &options);
2829
29 var list = std.ArrayList(u.Module).init(gpa);30 var list = std.ArrayList(zigmod.Module).init(gpa);
30 try common.collect_pkgs(top_module, &list);31 try common.collect_pkgs(top_module, &list);
3132
32 try create_depszig(cachepath, dir, top_module, &list);33 try create_depszig(cachepath, dir, top_module, &list);
...@@ -38,7 +39,7 @@ pub fn execute(args: [][]u8) !void {...@@ -38,7 +39,7 @@ pub fn execute(args: [][]u8) !void {
38 try diff_lockfile();39 try diff_lockfile();
39}40}
4041
41pub fn create_depszig(cachepath: string, dir: std.fs.Dir, top_module: u.Module, list: *std.ArrayList(u.Module)) !void {42pub fn create_depszig(cachepath: string, dir: std.fs.Dir, top_module: zigmod.Module, list: *std.ArrayList(zigmod.Module)) !void {
42 const f = try dir.createFile("deps.zig", .{});43 const f = try dir.createFile("deps.zig", .{});
43 defer f.close();44 defer f.close();
4445
...@@ -92,14 +93,14 @@ pub fn create_depszig(cachepath: string, dir: std.fs.Dir, top_module: u.Module,...@@ -92,14 +93,14 @@ pub fn create_depszig(cachepath: string, dir: std.fs.Dir, top_module: u.Module,
92 try w.writeAll("};\n\n");93 try w.writeAll("};\n\n");
9394
94 try w.writeAll("pub const package_data = struct {\n");95 try w.writeAll("pub const package_data = struct {\n");
95 var duped = std.ArrayList(u.Module).init(gpa);96 var duped = std.ArrayList(zigmod.Module).init(gpa);
96 for (list.items) |mod| {97 for (list.items) |mod| {
97 if (mod.is_sys_lib) {98 if (mod.is_sys_lib) {
98 continue;99 continue;
99 }100 }
100 try duped.append(mod);101 try duped.append(mod);
101 }102 }
102 try print_pkg_data_to(w, &duped, &std.ArrayList(u.Module).init(gpa));103 try print_pkg_data_to(w, &duped, &std.ArrayList(zigmod.Module).init(gpa));
103 try w.writeAll("};\n\n");104 try w.writeAll("};\n\n");
104105
105 try w.writeAll("pub const packages = ");106 try w.writeAll("pub const packages = ");
...@@ -115,7 +116,7 @@ pub fn create_depszig(cachepath: string, dir: std.fs.Dir, top_module: u.Module,...@@ -115,7 +116,7 @@ pub fn create_depszig(cachepath: string, dir: std.fs.Dir, top_module: u.Module,
115 try w.writeAll("};\n");116 try w.writeAll("};\n");
116}117}
117118
118fn create_lockfile(list: *std.ArrayList(u.Module), path: string, dir: std.fs.Dir) !void {119fn create_lockfile(list: *std.ArrayList(zigmod.Module), path: string, dir: std.fs.Dir) !void {
119 const fl = try dir.createFile("zigmod.lock", .{});120 const fl = try dir.createFile("zigmod.lock", .{});
120 defer fl.close();121 defer fl.close();
121122
...@@ -225,7 +226,7 @@ fn diff_printchange(comptime testt: string, comptime replacement: string, item:...@@ -225,7 +226,7 @@ fn diff_printchange(comptime testt: string, comptime replacement: string, item:
225 return false;226 return false;
226}227}
227228
228fn print_dirs(w: std.fs.File.Writer, list: []const u.Module) !void {229fn print_dirs(w: std.fs.File.Writer, list: []const zigmod.Module) !void {
229 for (list) |mod| {230 for (list) |mod| {
230 if (mod.is_sys_lib) continue;231 if (mod.is_sys_lib) continue;
231 if (std.mem.eql(u8, mod.id, "root")) {232 if (std.mem.eql(u8, mod.id, "root")) {
...@@ -236,7 +237,7 @@ fn print_dirs(w: std.fs.File.Writer, list: []const u.Module) !void {...@@ -236,7 +237,7 @@ fn print_dirs(w: std.fs.File.Writer, list: []const u.Module) !void {
236 }237 }
237}238}
238239
239fn print_deps(w: std.fs.File.Writer, m: u.Module) !void {240fn print_deps(w: std.fs.File.Writer, m: zigmod.Module) !void {
240 try w.writeAll("&[_]Package{\n");241 try w.writeAll("&[_]Package{\n");
241 for (m.deps) |d| {242 for (m.deps) |d| {
242 if (d.main.len == 0) {243 if (d.main.len == 0) {
...@@ -247,7 +248,7 @@ fn print_deps(w: std.fs.File.Writer, m: u.Module) !void {...@@ -247,7 +248,7 @@ fn print_deps(w: std.fs.File.Writer, m: u.Module) !void {
247 try w.writeAll("}");248 try w.writeAll("}");
248}249}
249250
250fn print_pkg_data_to(w: std.fs.File.Writer, notdone: *std.ArrayList(u.Module), done: *std.ArrayList(u.Module)) !void {251fn print_pkg_data_to(w: std.fs.File.Writer, notdone: *std.ArrayList(zigmod.Module), done: *std.ArrayList(zigmod.Module)) !void {
251 var len: usize = notdone.items.len;252 var len: usize = notdone.items.len;
252 while (notdone.items.len > 0) {253 while (notdone.items.len > 0) {
253 for (notdone.items) |mod, i| {254 for (notdone.items) |mod, i| {
...@@ -328,16 +329,16 @@ fn print_pkg_data_to(w: std.fs.File.Writer, notdone: *std.ArrayList(u.Module), d...@@ -328,16 +329,16 @@ fn print_pkg_data_to(w: std.fs.File.Writer, notdone: *std.ArrayList(u.Module), d
328}329}
329330
330/// returns if all of the zig modules in needles are in haystack331/// returns if all of the zig modules in needles are in haystack
331fn contains_all(needles: []u.Module, haystack: []const u.Module) bool {332fn contains_all(needles: []zigmod.Module, haystack: []const zigmod.Module) bool {
332 for (needles) |item| {333 for (needles) |item| {
333 if (item.main.len > 0 and !u.list_contains_gen(u.Module, haystack, item)) {334 if (item.main.len > 0 and !u.list_contains_gen(zigmod.Module, haystack, item)) {
334 return false;335 return false;
335 }336 }
336 }337 }
337 return true;338 return true;
338}339}
339340
340fn print_pkgs(w: std.fs.File.Writer, m: u.Module) !void {341fn print_pkgs(w: std.fs.File.Writer, m: zigmod.Module) !void {
341 try w.writeAll("struct {\n");342 try w.writeAll("struct {\n");
342 for (m.deps) |d| {343 for (m.deps) |d| {
343 if (d.main.len == 0) {344 if (d.main.len == 0) {
...@@ -349,7 +350,7 @@ fn print_pkgs(w: std.fs.File.Writer, m: u.Module) !void {...@@ -349,7 +350,7 @@ fn print_pkgs(w: std.fs.File.Writer, m: u.Module) !void {
349 try w.writeAll("}");350 try w.writeAll("}");
350}351}
351352
352fn print_imports(w: std.fs.File.Writer, m: u.Module, path: string) !void {353fn print_imports(w: std.fs.File.Writer, m: zigmod.Module, path: string) !void {
353 for (m.deps) |d| {354 for (m.deps) |d| {
354 if (d.main.len == 0) {355 if (d.main.len == 0) {
355 continue;356 continue;
src/cmd/license.zig+3-2
...@@ -4,11 +4,12 @@ const gpa = std.heap.c_allocator;...@@ -4,11 +4,12 @@ const gpa = std.heap.c_allocator;
4const style = @import("ansi").style;4const style = @import("ansi").style;
5const licenses = @import("licenses");5const licenses = @import("licenses");
66
7const zigmod = @import("../lib.zig");
7const u = @import("./../util/index.zig");8const u = @import("./../util/index.zig");
8const common = @import("./../common.zig");9const common = @import("./../common.zig");
910
10const Module = u.Module;11const Module = zigmod.Module;
11const List = std.ArrayList(u.Module);12const List = std.ArrayList(zigmod.Module);
12const Map = std.StringArrayHashMap(*List);13const Map = std.StringArrayHashMap(*List);
1314
14// Inspired by:15// Inspired by:
src/cmd/sum.zig+2-1
...@@ -1,6 +1,7 @@...@@ -1,6 +1,7 @@
1const std = @import("std");1const std = @import("std");
2const gpa = std.heap.c_allocator;2const gpa = std.heap.c_allocator;
33
4const zigmod = @import("../lib.zig");
4const u = @import("./../util/index.zig");5const u = @import("./../util/index.zig");
5const common = @import("./../common.zig");6const common = @import("./../common.zig");
67
...@@ -25,7 +26,7 @@ pub fn execute(args: [][]u8) !void {...@@ -25,7 +26,7 @@ pub fn execute(args: [][]u8) !void {
25 const w = f.writer();26 const w = f.writer();
2627
27 //28 //
28 var module_list = std.ArrayList(u.Module).init(gpa);29 var module_list = std.ArrayList(zigmod.Module).init(gpa);
29 try common.collect_pkgs(top_module, &module_list);30 try common.collect_pkgs(top_module, &module_list);
3031
31 for (module_list.items) |m| {32 for (module_list.items) |m| {
src/common.zig+14-14
...@@ -29,10 +29,10 @@ pub const CollectOptions = struct {...@@ -29,10 +29,10 @@ pub const CollectOptions = struct {
29 }29 }
30};30};
3131
32pub fn collect_deps_deep(cachepath: string, mdir: std.fs.Dir, options: *CollectOptions) !u.Module {32pub fn collect_deps_deep(cachepath: string, mdir: std.fs.Dir, options: *CollectOptions) !zigmod.Module {
33 const m = try zigmod.ModFile.from_dir(gpa, mdir);33 const m = try zigmod.ModFile.from_dir(gpa, mdir);
34 try options.init();34 try options.init();
35 var moduledeps = std.ArrayList(u.Module).init(gpa);35 var moduledeps = std.ArrayList(zigmod.Module).init(gpa);
36 defer moduledeps.deinit();36 defer moduledeps.deinit();
37 if (m.root_files.len > 0) {37 if (m.root_files.len > 0) {
38 try std.fs.cwd().makePath(".zigmod/deps/files");38 try std.fs.cwd().makePath(".zigmod/deps/files");
...@@ -44,7 +44,7 @@ pub fn collect_deps_deep(cachepath: string, mdir: std.fs.Dir, options: *CollectO...@@ -44,7 +44,7 @@ pub fn collect_deps_deep(cachepath: string, mdir: std.fs.Dir, options: *CollectO
44 try moduledeps.append(founddep);44 try moduledeps.append(founddep);
45 }45 }
46 }46 }
47 return u.Module{47 return zigmod.Module{
48 .is_sys_lib = false,48 .is_sys_lib = false,
49 .id = "root",49 .id = "root",
50 .name = "root",50 .name = "root",
...@@ -56,9 +56,9 @@ pub fn collect_deps_deep(cachepath: string, mdir: std.fs.Dir, options: *CollectO...@@ -56,9 +56,9 @@ pub fn collect_deps_deep(cachepath: string, mdir: std.fs.Dir, options: *CollectO
56 };56 };
57}57}
5858
59pub fn collect_deps(cachepath: string, mdir: std.fs.Dir, options: *CollectOptions) anyerror!u.Module {59pub fn collect_deps(cachepath: string, mdir: std.fs.Dir, options: *CollectOptions) anyerror!zigmod.Module {
60 const m = try zigmod.ModFile.from_dir(gpa, mdir);60 const m = try zigmod.ModFile.from_dir(gpa, mdir);
61 var moduledeps = std.ArrayList(u.Module).init(gpa);61 var moduledeps = std.ArrayList(zigmod.Module).init(gpa);
62 defer moduledeps.deinit();62 defer moduledeps.deinit();
63 if (m.files.len > 0) {63 if (m.files.len > 0) {
64 try std.fs.cwd().makePath(".zigmod/deps/files");64 try std.fs.cwd().makePath(".zigmod/deps/files");
...@@ -69,7 +69,7 @@ pub fn collect_deps(cachepath: string, mdir: std.fs.Dir, options: *CollectOption...@@ -69,7 +69,7 @@ pub fn collect_deps(cachepath: string, mdir: std.fs.Dir, options: *CollectOption
69 try moduledeps.append(founddep);69 try moduledeps.append(founddep);
70 }70 }
71 }71 }
72 return u.Module{72 return zigmod.Module{
73 .is_sys_lib = false,73 .is_sys_lib = false,
74 .id = m.id,74 .id = m.id,
75 .name = m.name,75 .name = m.name,
...@@ -84,8 +84,8 @@ pub fn collect_deps(cachepath: string, mdir: std.fs.Dir, options: *CollectOption...@@ -84,8 +84,8 @@ pub fn collect_deps(cachepath: string, mdir: std.fs.Dir, options: *CollectOption
84 };84 };
85}85}
8686
87pub fn collect_pkgs(mod: u.Module, list: *std.ArrayList(u.Module)) anyerror!void {87pub fn collect_pkgs(mod: zigmod.Module, list: *std.ArrayList(zigmod.Module)) anyerror!void {
88 if (u.list_contains_gen(u.Module, list.items, mod)) {88 if (u.list_contains_gen(zigmod.Module, list.items, mod)) {
89 return;89 return;
90 }90 }
91 try list.append(mod);91 try list.append(mod);
...@@ -198,7 +198,7 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) !...@@ -198,7 +198,7 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) !
198 }198 }
199}199}
200200
201pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectOptions) anyerror!?u.Module {201pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectOptions) anyerror!?zigmod.Module {
202 if (options.lock) |lock| {202 if (options.lock) |lock| {
203 for (lock) |item| {203 for (lock) |item| {
204 if (std.mem.eql(u8, item[0], try d.clean_path())) {204 if (std.mem.eql(u8, item[0], try d.clean_path())) {
...@@ -217,14 +217,14 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO...@@ -217,14 +217,14 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO
217217
218 switch (d.type) {218 switch (d.type) {
219 .system_lib => {219 .system_lib => {
220 return u.Module{220 return zigmod.Module{
221 .is_sys_lib = true,221 .is_sys_lib = true,
222 .id = try u.do_hash(std.crypto.hash.sha3.Sha3_384, d.path),222 .id = try u.do_hash(std.crypto.hash.sha3.Sha3_384, d.path),
223 .name = d.path,223 .name = d.path,
224 .only_os = d.only_os,224 .only_os = d.only_os,
225 .except_os = d.except_os,225 .except_os = d.except_os,
226 .main = "",226 .main = "",
227 .deps = &[_]u.Module{},227 .deps = &[_]zigmod.Module{},
228 .clean_path = d.path,228 .clean_path = d.path,
229 .yaml = null,229 .yaml = null,
230 .dep = d.*,230 .dep = d.*,
...@@ -234,7 +234,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO...@@ -234,7 +234,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO
234 var dd = try collect_deps(cachepath, moddir, options) catch |e| switch (e) {234 var dd = try collect_deps(cachepath, moddir, options) catch |e| switch (e) {
235 error.FileNotFound => {235 error.FileNotFound => {
236 if (d.main.len > 0 or d.c_include_dirs.len > 0 or d.c_source_files.len > 0) {236 if (d.main.len > 0 or d.c_include_dirs.len > 0 or d.c_source_files.len > 0) {
237 var mod_from = try u.Module.from(d.*, cachepath, options);237 var mod_from = try zigmod.Module.from(d.*, cachepath, options);
238 if (d.type != .local) mod_from.clean_path = u.trim_prefix(modpath, cachepath)[1..];238 if (d.type != .local) mod_from.clean_path = u.trim_prefix(modpath, cachepath)[1..];
239 if (mod_from.is_for_this()) return mod_from;239 if (mod_from.is_for_this()) return mod_from;
240 return null;240 return null;
...@@ -248,7 +248,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO...@@ -248,7 +248,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO
248 if (trymain) |_| {248 if (trymain) |_| {
249 d.*.name = tryname;249 d.*.name = tryname;
250 d.*.main = trymain.?;250 d.*.main = trymain.?;
251 var mod_from = try u.Module.from(d.*, cachepath, options);251 var mod_from = try zigmod.Module.from(d.*, cachepath, options);
252 if (d.type != .local) mod_from.clean_path = u.trim_prefix(modpath, cachepath)[1..];252 if (d.type != .local) mod_from.clean_path = u.trim_prefix(modpath, cachepath)[1..];
253 if (mod_from.is_for_this()) return mod_from;253 if (mod_from.is_for_this()) return mod_from;
254 return null;254 return null;
...@@ -276,7 +276,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO...@@ -276,7 +276,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO
276 }276 }
277}277}
278278
279pub fn add_files_package(pkg_name: string, mdir: std.fs.Dir, dirs: []const string) !u.Module {279pub fn add_files_package(pkg_name: string, mdir: std.fs.Dir, dirs: []const string) !zigmod.Module {
280 const destination = ".zigmod/deps/files";280 const destination = ".zigmod/deps/files";
281 const fname = try std.mem.join(gpa, "", &.{ pkg_name, ".zig" });281 const fname = try std.mem.join(gpa, "", &.{ pkg_name, ".zig" });
282282
src/lib.zig+1
...@@ -26,3 +26,4 @@ pub fn deinit() void {...@@ -26,3 +26,4 @@ pub fn deinit() void {
26pub const DepType = @import("./util/dep_type.zig").DepType;26pub const DepType = @import("./util/dep_type.zig").DepType;
27pub const Dep = @import("./util/dep.zig").Dep;27pub const Dep = @import("./util/dep.zig").Dep;
28pub const ModFile = @import("./util/modfile.zig").ModFile;28pub const ModFile = @import("./util/modfile.zig").ModFile;
29pub const Module = @import("./util/module.zig").Module;
src/util/index.zig-1
...@@ -1,2 +1 @@...@@ -1,2 +1 @@
1usingnamespace @import("./funcs.zig");1usingnamespace @import("./funcs.zig");
2usingnamespace @import("./module.zig");