diff options
Diffstat (limited to 'src/port.zig')
| -rw-r--r-- | src/port.zig | 113 |
1 files changed, 71 insertions, 42 deletions
diff --git a/src/port.zig b/src/port.zig index a7d53e4..b6e788b 100644 --- a/src/port.zig +++ b/src/port.zig @@ -21,7 +21,7 @@ const shell = @import("shell.zig"); const Time = time.Time; const fatal = cli.fatal; -const distname_ext = ".tar.gz"; +const distfile_ext = ".tar.gz"; pub const Ctx = struct { env: *const Env, @@ -388,7 +388,7 @@ pub fn create(ctx: Ctx, info: Info, opts: Create_Options) !void { .branch = opts.repo_branch, }; - ctx.env.info("===> cloning repository: {s}", .{repo.path.name()}); + ctx.env.info("cloning repository: {s}", .{repo.path.name()}); try repo.clone(ctx.env); errdefer fs.rm(&repo.path) catch {}; @@ -404,7 +404,7 @@ pub fn create(ctx: Ctx, info: Info, opts: Create_Options) !void { const src_file = info.src_file(); try fs.mkdir_all(&port_root); - ctx.env.info("===> writing port data: {s}", .{info.root.name()}); + ctx.env.info("writing port data: {s}", .{info.root.name()}); try info.to_file(&info_file); try opts.initial_version.to_file(&version_file); try source.to_file(&src_file); @@ -446,7 +446,6 @@ pub fn update_version(ctx: Ctx, port_name: []const u8, options: Update_Options) var source: Source = try .from_file(ctx, info); defer source.deinit(); - ctx.env.info("===> {s}: updating repository", .{info.name}); const old_version: Version = try .from_file(ctx, info); const old_commit = source.commit; const new_commit = try source.repo.update(ctx.env); @@ -467,7 +466,6 @@ pub fn update_version(ctx: Ctx, port_name: []const u8, options: Update_Options) source.commit = new_commit; - ctx.env.info("===> {s}: updating port data", .{info.name}); const src_file = info.src_file(); const version_file = info.version_file(); try source.to_file(&src_file); @@ -619,41 +617,45 @@ fn render_port(ctx: Ctx, opts: Render_Options) !void { try predefined_vars.add_var(".category", opts.info.category); try predefined_vars.add_var(".distdir", dist_dir.name()); for (template_conf.archives[0..template_conf.archives_len]) |*archive| { - var distname_buf: [std.fs.max_name_bytes]u8 = undefined; + var distname_buf: [std.Io.Dir.max_name_bytes]u8 = undefined; + var distfile_buf: [std.Io.Dir.max_name_bytes]u8 = undefined; const distname = assemble_distname(&distname_buf, .{ .dist_id = archive.id, .port_name = opts.info.name, .version = version_str, }); + const distfile = concat(&distfile_buf, &.{ distname, distfile_ext }); if (archive.id.len > 0) { var key_buf: [512]u8 = undefined; try predefined_vars.add_var(concat(&key_buf, &.{ ".distname.", archive.id }), distname); + try predefined_vars.add_var(concat(&key_buf, &.{ ".distfile.", archive.id }), distfile); } else { try predefined_vars.add_var(".distname", distname); + try predefined_vars.add_var(".distfile", distfile); } } + var make_makeconf_buf: [fs.max_path_bytes]u8 = undefined; var make_portsdir_buf: [fs.max_path_bytes]u8 = undefined; var make_distdir_buf: [fs.max_path_bytes]u8 = undefined; + const make_makeconf = concat(&make_makeconf_buf, &.{ "__MAKE_CONF=", ctx.env.make_conf orelse "/etc/make.conf" }); const make_portsdir = concat(&make_portsdir_buf, &.{ "PORTSDIR=", ctx.env.ports_dir }); const make_distdir = concat(&make_distdir_buf, &.{ "DISTDIR=", dist_dir.name() }); - ctx.env.info("===> {s}: writing port files", .{opts.info.name}); + ctx.env.info("=======< {s}: writing port files >=======", .{opts.info.name}); + ctx.env.info("using template `{s}`", .{template.name}); const vars = [_]conf.Config{ predefined_vars, port_vars }; try template.render(ctx.env, &vars, &wrk_port_dir); try fs.mv_dir(&wrk_port_dir, &port_dir); + ctx.env.info("", .{}); // END OF TASK var new_distfiles_created = false; if (opts.source) |src| { - var prepared_dist = false; - defer { - if (prepared_dist) src.repo.reset(ctx.env); - } - + var dist_prepared = false; if (template_conf.prepare_target) |prepare_target| { - ctx.env.info("===> {s}: prepare distribution", .{opts.info.name}); + ctx.env.info("=======< {s}: prepare distribution >=======", .{opts.info.name}); - var make_target_buf: [std.fs.max_name_bytes]u8 = undefined; + var make_target_buf: [std.Io.Dir.max_name_bytes]u8 = undefined; const res = shell.exec(.{ .env = ctx.env, .argv = &.{ "make", "-V", concat(&make_target_buf, &.{ "${.ALLTARGETS:M", prepare_target, "}" }), make_portsdir }, @@ -672,36 +674,40 @@ fn render_port(ctx: Ctx, opts: Render_Options) !void { }, }; if (has_target) { + ctx.env.info("executing target `{s}`", .{prepare_target}); var make_wrksrc_buf: [fs.max_path_bytes]u8 = undefined; const make_wrksrc = concat(&make_wrksrc_buf, &.{ "WRKSRC=", src.repo.path.name() }); - shell.run(.{ + const succeeded = shell.run(.{ .env = ctx.env, - .argv = &.{ "make", make_portsdir, make_distdir, make_wrksrc, prepare_target }, + .argv = &.{ "make", make_makeconf, make_portsdir, make_distdir, make_wrksrc, prepare_target }, .cwd = &port_dir, }); - prepared_dist = true; + if (!succeeded) { + src.repo.reset(ctx.env); + std.process.exit(1); + } + dist_prepared = true; } else { - ctx.env.info("Target `{s}` not found (skipping)", .{prepare_target}); + ctx.env.info("target `{s}` not found (skipping)", .{prepare_target}); } + ctx.env.info("", .{}); // END OF TASK } + ctx.env.info("=======< {s}: creating distfiles >=======", .{opts.info.name}); for (template_conf.archives[0..template_conf.archives_len]) |*archive| { - var distname_buf: [std.fs.max_name_bytes]u8 = undefined; + var distname_buf: [std.Io.Dir.max_name_bytes]u8 = undefined; + var distfile_buf: [std.Io.Dir.max_name_bytes]u8 = undefined; const distname = assemble_distname(&distname_buf, .{ .dist_id = archive.id, .port_name = opts.info.name, .version = version_str, }); - const wrk_dist_file: fs.Path = .join(.{ wrk_dist_dir.name(), distname }); - const dist_file: fs.Path = .join(.{ dist_dir.name(), distname }); + const distfile = concat(&distfile_buf, &.{ distname, distfile_ext }); + const wrk_distfile: fs.Path = .join(.{ wrk_dist_dir.name(), distfile }); + const tgt_distfile: fs.Path = .join(.{ dist_dir.name(), distfile }); - if (archive.id.len > 0) { - ctx.env.info("===> {s}: creating distfile [{s}]", .{ opts.info.name, archive.id }); - } else { - ctx.env.info("===> {s}: creating distfile", .{opts.info.name}); - } var tar_rename_buf: [fs.max_path_bytes]u8 = undefined; - const tar_rename = concat(&tar_rename_buf, &.{ ",^,", distname[0 .. distname.len - distname_ext.len], "/," }); + const tar_rename = concat(&tar_rename_buf, &.{ ",^,", distname, "/," }); var root = src.repo.path; if (archive.root) |archive_root| root.append(.{archive_root}); @@ -710,7 +716,7 @@ fn render_port(ctx: Ctx, opts: Render_Options) !void { var tar_cmd_len: usize = 8; tar_cmd[0] = "tar"; tar_cmd[1] = "-czf"; - tar_cmd[2] = wrk_dist_file.name(); + tar_cmd[2] = wrk_distfile.name(); tar_cmd[3] = "-C"; tar_cmd[4] = root.name(); tar_cmd[5] = "-s"; @@ -728,7 +734,7 @@ fn render_port(ctx: Ctx, opts: Render_Options) !void { tar_cmd_len += 1; existings_paths += 1; } else { - ctx.env.warn("cannot find path in archive root : {s}", .{path}); + ctx.env.warn("cannot find path `{s}` in archive root: {s} ", .{ path, root.name() }); } } if (existings_paths == 0) { @@ -739,31 +745,47 @@ fn render_port(ctx: Ctx, opts: Render_Options) !void { } } - shell.run(.{ + if (archive.id.len > 0) { + ctx.env.info("creating distfile [{s}]: {s}", .{ archive.id, tgt_distfile.name() }); + } else { + ctx.env.info("creating distfile: {s}", .{tgt_distfile.name()}); + } + + const succeeded = shell.run(.{ .env = ctx.env, .argv = tar_cmd[0..tar_cmd_len], }); + if (!succeeded) { + if (dist_prepared) src.repo.reset(ctx.env); + std.process.exit(1); + } - try fs.mv_file(&wrk_dist_file, &dist_file); - ctx.env.info("distfile: {s}", .{dist_file.name()}); + try fs.mv_file(&wrk_distfile, &tgt_distfile); new_distfiles_created = true; } + + if (dist_prepared) src.repo.reset(ctx.env); + ctx.env.info("", .{}); // END OF TASK } - ctx.env.info("===> {s}: creating port's distinfo", .{opts.info.name}); - shell.run(.{ + ctx.env.info("=======< {s}: creating distinfo >=======", .{opts.info.name}); + const succeeded = shell.run(.{ .env = ctx.env, - .argv = &.{ "make", make_portsdir, make_distdir, "makesum" }, + .argv = &.{ "make", make_makeconf, make_portsdir, make_distdir, "makesum" }, .cwd = &port_dir, }); + if (!succeeded) std.process.exit(1); + ctx.env.info("", .{}); // END OF TASK if (new_distfiles_created) { - ctx.env.info("===> {s}: cleaning up old distfiles", .{opts.info.name}); + ctx.env.info("=======< {s}: cleaning up old distfiles >=======", .{opts.info.name}); var history = fetch_port_distfiles_sorted(ctx.env.allocator, opts.info.name, &dist_dir) catch |err| { ctx.env.warn("cannot fetch distfiles from {s} ({s})", .{ dist_dir.name(), @errorName(err) }); return; }; defer history.deinit(ctx.env.allocator); + + var removed_files = false; if (history.items.len > 0) { var version = history.items[0].version; var visited: usize = 1; @@ -773,12 +795,19 @@ fn render_port(ctx: Ctx, opts: Render_Options) !void { version = distfile.version; } if (visited > ctx.env.distfiles_history) { - fs.rm(&distfile.path) catch |err| { + if (fs.rm(&distfile.path)) |_| { + ctx.env.info("removed {s}", .{distfile.path.name()}); + } else |err| { ctx.env.warn("cannot remove {s} ({s})", .{ distfile.path.name(), @errorName(err) }); - }; + } + removed_files = true; } } } + if (!removed_files) { + ctx.env.info("nothing to do", .{}); + } + ctx.env.info("", .{}); // END OF TASK } } @@ -813,9 +842,9 @@ fn fetch_distfiles(allocator: std.mem.Allocator, dist_dir: *const fs.Path, filte if (entry.type != .file) continue; const distfile = entry.name; - if (std.mem.startsWith(u8, distfile, filter.port_name) and std.mem.endsWith(u8, distfile, distname_ext)) { + if (std.mem.startsWith(u8, distfile, filter.port_name) and std.mem.endsWith(u8, distfile, distfile_ext)) { const last_dash = std.mem.lastIndexOfScalar(u8, distfile, '-') orelse continue; - const version = Version.parse(distfile[last_dash + 1 .. distfile.len - distname_ext.len]) catch continue; + const version = Version.parse(distfile[last_dash + 1 .. distfile.len - distfile_ext.len]) catch continue; if (filter.version) |expected_version| { if (!Version.eq(expected_version, version)) { continue; @@ -839,9 +868,9 @@ const Distname_Parts = struct { /// Build a distname that matches FreeBSD's default value for ${DISTNAME}. fn assemble_distname(buf: []u8, parts: Distname_Parts) []const u8 { return if (parts.dist_id.len > 0) - concat(buf, &.{ parts.port_name, "-", parts.dist_id, "-", parts.version, distname_ext }) + concat(buf, &.{ parts.port_name, "-", parts.dist_id, "-", parts.version }) else - return concat(buf, &.{ parts.port_name, "-", parts.version, distname_ext }); + return concat(buf, &.{ parts.port_name, "-", parts.version }); } fn parse_int(comptime Int: type, text: []const u8) ?Int { |