| ... | ... | @@ -1,7 +1,6 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | 2 | const string = []const u8; |
| 3 | 3 | const builtin = @import("builtin"); |
| 4 | | const gpa = std.heap.c_allocator; |
| 5 | 4 | |
| 6 | 5 | const ansi = @import("ansi"); |
| 7 | 6 | |
| ... | ... | @@ -20,7 +19,7 @@ pub const CollectOptions = struct { |
| 20 | 19 | log: bool, |
| 21 | 20 | update: bool, |
| 22 | 21 | lock: ?[]const [4]string = null, |
| 23 | | alloc: *std.mem.Allocator = gpa, |
| 22 | alloc: *std.mem.Allocator = std.heap.c_allocator, |
| 24 | 23 | already_fetched: *std.ArrayList(string) = undefined, |
| 25 | 24 | |
| 26 | 25 | pub fn init(self: *CollectOptions) !void { |
| ... | ... | @@ -30,13 +29,13 @@ pub const CollectOptions = struct { |
| 30 | 29 | }; |
| 31 | 30 | |
| 32 | 31 | pub fn collect_deps_deep(cachepath: string, mdir: std.fs.Dir, options: *CollectOptions) !zigmod.Module { |
| 33 | | const m = try zigmod.ModFile.from_dir(gpa, mdir); |
| 32 | const m = try zigmod.ModFile.from_dir(options.alloc, mdir); |
| 34 | 33 | try options.init(); |
| 35 | | var moduledeps = std.ArrayList(zigmod.Module).init(gpa); |
| 34 | var moduledeps = std.ArrayList(zigmod.Module).init(options.alloc); |
| 36 | 35 | defer moduledeps.deinit(); |
| 37 | 36 | if (m.root_files.len > 0) { |
| 38 | 37 | try std.fs.cwd().makePath(".zigmod/deps/files"); |
| 39 | | try moduledeps.append(try add_files_package("root", mdir, m.root_files)); |
| 38 | try moduledeps.append(try add_files_package(options.alloc, "root", mdir, m.root_files)); |
| 40 | 39 | } |
| 41 | 40 | try moduledeps.append(try collect_deps(cachepath, mdir, options)); |
| 42 | 41 | for (m.devdeps) |*d| { |
| ... | ... | @@ -45,7 +44,7 @@ pub fn collect_deps_deep(cachepath: string, mdir: std.fs.Dir, options: *CollectO |
| 45 | 44 | } |
| 46 | 45 | } |
| 47 | 46 | return zigmod.Module{ |
| 48 | | .alloc = gpa, |
| 47 | .alloc = options.alloc, |
| 49 | 48 | .is_sys_lib = false, |
| 50 | 49 | .id = "root", |
| 51 | 50 | .name = "root", |
| ... | ... | @@ -58,12 +57,12 @@ pub fn collect_deps_deep(cachepath: string, mdir: std.fs.Dir, options: *CollectO |
| 58 | 57 | } |
| 59 | 58 | |
| 60 | 59 | pub fn collect_deps(cachepath: string, mdir: std.fs.Dir, options: *CollectOptions) anyerror!zigmod.Module { |
| 61 | | const m = try zigmod.ModFile.from_dir(gpa, mdir); |
| 62 | | var moduledeps = std.ArrayList(zigmod.Module).init(gpa); |
| 60 | const m = try zigmod.ModFile.from_dir(options.alloc, mdir); |
| 61 | var moduledeps = std.ArrayList(zigmod.Module).init(options.alloc); |
| 63 | 62 | defer moduledeps.deinit(); |
| 64 | 63 | if (m.files.len > 0) { |
| 65 | 64 | try std.fs.cwd().makePath(".zigmod/deps/files"); |
| 66 | | try moduledeps.append(try add_files_package(m.id, mdir, m.files)); |
| 65 | try moduledeps.append(try add_files_package(options.alloc, m.id, mdir, m.files)); |
| 67 | 66 | } |
| 68 | 67 | for (m.deps) |*d| { |
| 69 | 68 | if (try get_module_from_dep(d, cachepath, options)) |founddep| { |
| ... | ... | @@ -71,7 +70,7 @@ pub fn collect_deps(cachepath: string, mdir: std.fs.Dir, options: *CollectOption |
| 71 | 70 | } |
| 72 | 71 | } |
| 73 | 72 | return zigmod.Module{ |
| 74 | | .alloc = gpa, |
| 73 | .alloc = options.alloc, |
| 75 | 74 | .is_sys_lib = false, |
| 76 | 75 | .id = m.id, |
| 77 | 76 | .name = m.name, |
| ... | ... | @@ -97,8 +96,8 @@ pub fn collect_pkgs(mod: zigmod.Module, list: *std.ArrayList(zigmod.Module)) any |
| 97 | 96 | } |
| 98 | 97 | |
| 99 | 98 | pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) !string { |
| 100 | | const p = try std.fs.path.join(gpa, &.{ cachepath, try d.clean_path() }); |
| 101 | | const pv = try std.fs.path.join(gpa, &.{ cachepath, try d.clean_path_v() }); |
| 99 | const p = try std.fs.path.join(options.alloc, &.{ cachepath, try d.clean_path() }); |
| 100 | const pv = try std.fs.path.join(options.alloc, &.{ cachepath, try d.clean_path_v() }); |
| 102 | 101 | |
| 103 | 102 | const nocache = d.type == .local or d.type == .system_lib; |
| 104 | 103 | if (!nocache and u.list_contains(options.already_fetched.items, p)) return p; |
| ... | ... | @@ -136,13 +135,13 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) ! |
| 136 | 135 | if (try u.does_folder_exist(pv)) { |
| 137 | 136 | if (vers.id == .branch) { |
| 138 | 137 | if (options.update) { |
| 139 | | try d.type.update(gpa, pv, d.path); |
| 138 | try d.type.update(options.alloc, pv, d.path); |
| 140 | 139 | } |
| 141 | 140 | } |
| 142 | 141 | return pv; |
| 143 | 142 | } |
| 144 | | try d.type.pull(gpa, d.path, pv); |
| 145 | | if ((try u.run_cmd(gpa, pv, &.{ "git", "checkout", vers.string })) > 0) { |
| 143 | try d.type.pull(options.alloc, d.path, pv); |
| 144 | if ((try u.run_cmd(options.alloc, pv, &.{ "git", "checkout", vers.string })) > 0) { |
| 146 | 145 | u.fail("fetch: git: {s}: {s} {s} does not exist", .{ d.path, @tagName(vers.id), vers.string }); |
| 147 | 146 | } |
| 148 | 147 | if (builtin.os.tag != .windows and vers.id != .branch) { |
| ... | ... | @@ -152,20 +151,20 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) ! |
| 152 | 151 | return pv; |
| 153 | 152 | } |
| 154 | 153 | if (!try u.does_folder_exist(p)) { |
| 155 | | try d.type.pull(gpa, d.path, p); |
| 154 | try d.type.pull(options.alloc, d.path, p); |
| 156 | 155 | } else { |
| 157 | 156 | if (options.update) { |
| 158 | | try d.type.update(gpa, p, d.path); |
| 157 | try d.type.update(options.alloc, p, d.path); |
| 159 | 158 | } |
| 160 | 159 | } |
| 161 | 160 | return p; |
| 162 | 161 | }, |
| 163 | 162 | .hg => { |
| 164 | 163 | if (!try u.does_folder_exist(p)) { |
| 165 | | try d.type.pull(gpa, d.path, p); |
| 164 | try d.type.pull(options.alloc, d.path, p); |
| 166 | 165 | } else { |
| 167 | 166 | if (options.update) { |
| 168 | | try d.type.update(gpa, p, d.path); |
| 167 | try d.type.update(options.alloc, p, d.path); |
| 169 | 168 | } |
| 170 | 169 | } |
| 171 | 170 | return p; |
| ... | ... | @@ -174,14 +173,14 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) ! |
| 174 | 173 | if (try u.does_folder_exist(pv)) { |
| 175 | 174 | return pv; |
| 176 | 175 | } |
| 177 | | const file_name = try u.last(try u.split(gpa, d.path, "/")); |
| 176 | const file_name = try u.last(try u.split(options.alloc, d.path, "/")); |
| 178 | 177 | if (d.version.len > 0) { |
| 179 | 178 | if (try u.does_folder_exist(pv)) { |
| 180 | 179 | return pv; |
| 181 | 180 | } |
| 182 | | const file_path = try std.fs.path.join(gpa, &.{ pv, file_name }); |
| 183 | | try d.type.pull(gpa, d.path, pv); |
| 184 | | if (try u.validate_hash(gpa, d.version, file_path)) { |
| 181 | const file_path = try std.fs.path.join(options.alloc, &.{ pv, file_name }); |
| 182 | try d.type.pull(options.alloc, d.path, pv); |
| 183 | if (try u.validate_hash(options.alloc, d.version, file_path)) { |
| 185 | 184 | try std.fs.cwd().deleteFile(file_path); |
| 186 | 185 | return pv; |
| 187 | 186 | } |
| ... | ... | @@ -192,8 +191,8 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) ! |
| 192 | 191 | if (try u.does_folder_exist(p)) { |
| 193 | 192 | try std.fs.cwd().deleteTree(p); |
| 194 | 193 | } |
| 195 | | const file_path = try std.fs.path.join(gpa, &.{ p, file_name }); |
| 196 | | try d.type.pull(gpa, d.path, p); |
| 194 | const file_path = try std.fs.path.join(options.alloc, &.{ p, file_name }); |
| 195 | try d.type.pull(options.alloc, d.path, p); |
| 197 | 196 | try std.fs.deleteFileAbsolute(file_path); |
| 198 | 197 | return p; |
| 199 | 198 | }, |
| ... | ... | @@ -220,9 +219,9 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO |
| 220 | 219 | switch (d.type) { |
| 221 | 220 | .system_lib => { |
| 222 | 221 | return zigmod.Module{ |
| 223 | | .alloc = gpa, |
| 222 | .alloc = options.alloc, |
| 224 | 223 | .is_sys_lib = true, |
| 225 | | .id = try u.do_hash(gpa, std.crypto.hash.sha3.Sha3_384, d.path), |
| 224 | .id = try u.do_hash(options.alloc, std.crypto.hash.sha3.Sha3_384, d.path), |
| 226 | 225 | .name = d.path, |
| 227 | 226 | .only_os = d.only_os, |
| 228 | 227 | .except_os = d.except_os, |
| ... | ... | @@ -237,21 +236,21 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO |
| 237 | 236 | var dd = try collect_deps(cachepath, moddir, options) catch |e| switch (e) { |
| 238 | 237 | error.FileNotFound => { |
| 239 | 238 | if (d.main.len > 0 or d.c_include_dirs.len > 0 or d.c_source_files.len > 0) { |
| 240 | | var mod_from = try zigmod.Module.from(gpa, d.*, cachepath, options); |
| 239 | var mod_from = try zigmod.Module.from(options.alloc, d.*, cachepath, options); |
| 241 | 240 | if (d.type != .local) mod_from.clean_path = u.trim_prefix(modpath, cachepath)[1..]; |
| 242 | 241 | if (mod_from.is_for_this()) return mod_from; |
| 243 | 242 | return null; |
| 244 | 243 | } |
| 245 | 244 | const moddirO = try std.fs.cwd().openDir(modpath, .{}); |
| 246 | | const tryname = try u.detect_pkgname(gpa, "", modpath); |
| 247 | | const trymain = u.detct_mainfile(gpa, "", moddirO, tryname) catch |err| switch (err) { |
| 245 | const tryname = try u.detect_pkgname(options.alloc, "", modpath); |
| 246 | const trymain = u.detct_mainfile(options.alloc, "", moddirO, tryname) catch |err| switch (err) { |
| 248 | 247 | error.CantFindMain => null, |
| 249 | 248 | else => return err, |
| 250 | 249 | }; |
| 251 | 250 | if (trymain) |_| { |
| 252 | 251 | d.*.name = tryname; |
| 253 | 252 | d.*.main = trymain.?; |
| 254 | | var mod_from = try zigmod.Module.from(gpa, d.*, cachepath, options); |
| 253 | var mod_from = try zigmod.Module.from(options.alloc, d.*, cachepath, options); |
| 255 | 254 | if (d.type != .local) mod_from.clean_path = u.trim_prefix(modpath, cachepath)[1..]; |
| 256 | 255 | if (mod_from.is_for_this()) return mod_from; |
| 257 | 256 | return null; |
| ... | ... | @@ -263,7 +262,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO |
| 263 | 262 | dd.dep = d.*; |
| 264 | 263 | const save = dd; |
| 265 | 264 | if (d.type != .local) dd.clean_path = u.trim_prefix(modpath, cachepath)[1..]; |
| 266 | | if (dd.id.len == 0) dd.id = try u.random_string(gpa, 48); |
| 265 | if (dd.id.len == 0) dd.id = try u.random_string(options.alloc, 48); |
| 267 | 266 | if (d.name.len > 0) dd.name = d.name; |
| 268 | 267 | if (d.main.len > 0) dd.main = d.main; |
| 269 | 268 | if (d.c_include_dirs.len > 0) dd.c_include_dirs = d.c_include_dirs; |
| ... | ... | @@ -271,7 +270,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO |
| 271 | 270 | if (d.c_source_files.len > 0) dd.c_source_files = d.c_source_files; |
| 272 | 271 | if (d.only_os.len > 0) dd.only_os = d.only_os; |
| 273 | 272 | if (d.except_os.len > 0) dd.except_os = d.except_os; |
| 274 | | if (d.type == .local) dd.main = try std.fs.path.join(gpa, &.{ d.main, save.main }); |
| 273 | if (d.type == .local) dd.main = try std.fs.path.join(options.alloc, &.{ d.main, save.main }); |
| 275 | 274 | if (std.mem.eql(u8, modpath, "files")) dd.clean_path = modpath; |
| 276 | 275 | if (dd.is_for_this()) return dd; |
| 277 | 276 | return null; |
| ... | ... | @@ -279,28 +278,28 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO |
| 279 | 278 | } |
| 280 | 279 | } |
| 281 | 280 | |
| 282 | | pub fn add_files_package(pkg_name: string, mdir: std.fs.Dir, dirs: []const string) !zigmod.Module { |
| 281 | pub fn add_files_package(alloc: *std.mem.Allocator, pkg_name: string, mdir: std.fs.Dir, dirs: []const string) !zigmod.Module { |
| 283 | 282 | const destination = ".zigmod/deps/files"; |
| 284 | | const fname = try std.mem.join(gpa, "", &.{ pkg_name, ".zig" }); |
| 283 | const fname = try std.mem.join(alloc, "", &.{ pkg_name, ".zig" }); |
| 285 | 284 | |
| 286 | | const map = &std.StringHashMap(string).init(gpa); |
| 285 | const map = &std.StringHashMap(string).init(alloc); |
| 287 | 286 | defer map.deinit(); |
| 288 | 287 | |
| 289 | 288 | for (dirs) |dir_path| { |
| 290 | 289 | const dir = try mdir.openDir(dir_path, .{ .iterate = true }); |
| 291 | | var walker = try dir.walk(gpa); |
| 290 | var walker = try dir.walk(alloc); |
| 292 | 291 | defer walker.deinit(); |
| 293 | 292 | while (try walker.next()) |p| { |
| 294 | 293 | if (p.kind == .Directory) { |
| 295 | 294 | continue; |
| 296 | 295 | } |
| 297 | | const path = try std.mem.dupe(gpa, u8, p.path); |
| 298 | | try map.put(path, try std.fmt.allocPrint(gpa, "{s}/{s}", .{ dir_path, path })); |
| 296 | const path = try std.mem.dupe(alloc, u8, p.path); |
| 297 | try map.put(path, try std.fmt.allocPrint(alloc, "{s}/{s}", .{ dir_path, path })); |
| 299 | 298 | } |
| 300 | 299 | } |
| 301 | 300 | |
| 302 | | const cwdpath = try std.fs.cwd().realpathAlloc(gpa, "."); |
| 303 | | const mpath = try mdir.realpathAlloc(gpa, "."); |
| 301 | const cwdpath = try std.fs.cwd().realpathAlloc(alloc, "."); |
| 302 | const mpath = try mdir.realpathAlloc(alloc, "."); |
| 304 | 303 | var fpath = u.trim_prefix(mpath, cwdpath); |
| 305 | 304 | if (fpath.len == 0) fpath = std.fs.path.sep_str; |
| 306 | 305 | |
| ... | ... | @@ -330,7 +329,7 @@ pub fn add_files_package(pkg_name: string, mdir: std.fs.Dir, dirs: []const strin |
| 330 | 329 | ); |
| 331 | 330 | |
| 332 | 331 | var d: zigmod.Dep = .{ |
| 333 | | .alloc = gpa, |
| 332 | .alloc = alloc, |
| 334 | 333 | .type = .local, |
| 335 | 334 | .path = "files", |
| 336 | 335 | .id = "", |
| ... | ... | @@ -343,18 +342,19 @@ pub fn add_files_package(pkg_name: string, mdir: std.fs.Dir, dirs: []const strin |
| 343 | 342 | var options = CollectOptions{ |
| 344 | 343 | .log = false, |
| 345 | 344 | .update = false, |
| 345 | .alloc = alloc, |
| 346 | 346 | }; |
| 347 | 347 | return (try get_module_from_dep(&d, destination, &options)).?; |
| 348 | 348 | } |
| 349 | 349 | |
| 350 | | pub fn parse_lockfile(dir: std.fs.Dir) ![]const [4]string { |
| 351 | | var list = std.ArrayList([4]string).init(gpa); |
| 350 | pub fn parse_lockfile(alloc: *std.mem.Allocator, dir: std.fs.Dir) ![]const [4]string { |
| 351 | var list = std.ArrayList([4]string).init(alloc); |
| 352 | 352 | const max = std.math.maxInt(usize); |
| 353 | 353 | const f = try dir.openFile("zigmod.lock", .{}); |
| 354 | 354 | const r = f.reader(); |
| 355 | 355 | var i: usize = 0; |
| 356 | 356 | var v: usize = 1; |
| 357 | | while (try r.readUntilDelimiterOrEofAlloc(gpa, '\n', max)) |line| : (i += 1) { |
| 357 | while (try r.readUntilDelimiterOrEofAlloc(alloc, '\n', max)) |line| : (i += 1) { |
| 358 | 358 | if (i == 0 and std.mem.eql(u8, line, "2")) { |
| 359 | 359 | v = 2; |
| 360 | 360 | continue; |
| ... | ... | @@ -372,7 +372,7 @@ pub fn parse_lockfile(dir: std.fs.Dir) ![]const [4]string { |
| 372 | 372 | 2 => { |
| 373 | 373 | var iter = std.mem.split(u8, line, " "); |
| 374 | 374 | const asdep = zigmod.Dep{ |
| 375 | | .alloc = gpa, |
| 375 | .alloc = alloc, |
| 376 | 376 | .type = std.meta.stringToEnum(zigmod.DepType, iter.next().?).?, |
| 377 | 377 | .path = iter.next().?, |
| 378 | 378 | .version = iter.next().?, |