Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub fn build(b: *std.Build) void {
});

const install_exe_zbuild = b.addInstallArtifact(exe_zbuild, .{});

const tls_install_exe_zbuild = b.step("build-exe:zbuild", "Install the zbuild executable");
tls_install_exe_zbuild.dependOn(&install_exe_zbuild.step);
b.getInstallStep().dependOn(&install_exe_zbuild.step);
Expand All @@ -33,7 +34,7 @@ pub fn build(b: *std.Build) void {
const test_zbuild = b.addTest(.{
.name = "zbuild",
.root_module = module_zbuild,
.filters = &[_][]const u8{ },
.filters = &[_][]const u8{},
});
const install_test_zbuild = b.addInstallArtifact(test_zbuild, .{});
const tls_install_test_zbuild = b.step("build-test:zbuild", "Install the zbuild test");
Expand All @@ -54,7 +55,7 @@ pub fn build(b: *std.Build) void {
const test_sync = b.addTest(.{
.name = "sync",
.root_module = module_sync,
.filters = &[_][]const u8{ },
.filters = &[_][]const u8{},
});
const install_test_sync = b.addInstallArtifact(test_sync, .{});
const tls_install_test_sync = b.step("build-test:sync", "Install the sync test");
Expand All @@ -66,5 +67,4 @@ pub fn build(b: *std.Build) void {
tls_run_test.dependOn(&run_test_sync.step);

module_sync.addImport("zbuild", module_zbuild);

}
2 changes: 1 addition & 1 deletion src/cmd_sync.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ pub fn exec(gpa: Allocator, arena: Allocator, global_opts: GlobalOptions, config
if (global_opts.no_sync) {
fatal("--no-sync is incompatible with the sync command", .{});
}
try syncBuildFile(gpa, arena, config, .{ .out_dir = global_opts.project_dir });
try syncBuildFile(gpa, arena, config, global_opts, .{ .out_dir = global_opts.project_dir });
try syncManifest(gpa, arena, global_opts, config, .{ .out_dir = global_opts.project_dir });
}
17 changes: 17 additions & 0 deletions src/run_zig.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,18 @@ pub fn runZigFetch(gpa: Allocator, arena: Allocator, child_opts: ChildOpts, env:
});
}

pub fn runZigFmt(gpa: Allocator, arena: Allocator, child_opts: ChildOpts, env: ZigEnv, args: []const []const u8) !void {
try runZig(gpa, arena, child_opts, env, .{
.fmt = .{
.args = args,
},
});
}

pub const ZigCmd = union(enum) {
build: Build,
fetch: Fetch,
fmt: Fmt,

pub const Build = struct {
step: ?[]const u8,
Expand All @@ -42,6 +51,10 @@ pub const ZigCmd = union(enum) {
exact: ?[]const u8,
};
};

pub const Fmt = struct {
args: []const []const u8,
};
};

pub const ChildOpts = struct {
Expand Down Expand Up @@ -87,6 +100,10 @@ pub fn runZig(gpa: Allocator, arena: Allocator, child_opts: ChildOpts, env: ZigE
},
}
},
.fmt => |fmt| {
try argv.append("fmt");
try argv.appendSlice(fmt.args);
},
}

var child = std.process.Child.init(argv.items, gpa);
Expand Down
17 changes: 14 additions & 3 deletions src/sync_build_file.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const Allocator = std.mem.Allocator;

const Config = @import("Config.zig");
const ConfigBuildgen = @import("ConfigBuildgen.zig");
const GlobalOptions = @import("GlobalOptions.zig");
const runZigFmt = @import("run_zig.zig").runZigFmt;

pub const SyncBuildFileOpts = struct {
out_dir: ?[]const u8 = null,
Expand All @@ -11,17 +13,26 @@ pub const SyncBuildFileOpts = struct {

const max_bytes_zbuild_file = 16_000;

pub fn syncBuildFile(gpa: Allocator, arena: Allocator, config: Config, opts: SyncBuildFileOpts) !void {
_ = gpa;
pub fn syncBuildFile(gpa: Allocator, arena: Allocator, config: Config, global_opts: GlobalOptions, opts: SyncBuildFileOpts) !void {
const build_root_directory = std.fs.cwd();

const out_dir = if (opts.out_dir) |o| try build_root_directory.openDir(o, .{}) else build_root_directory;
const out_file = try out_dir.createFile(opts.build_file orelse "build.zig", .{ .truncate = true });
defer out_file.close();
errdefer out_file.close();
const writer = out_file.writer().any();

var buildgen = ConfigBuildgen.init(arena, config, writer);
defer buildgen.deinit();

try buildgen.write();

// after writing, close the file and format it with `zig fmt`, which modifies files in-place
out_file.close();
try runZigFmt(
gpa,
arena,
.{ .cwd = global_opts.project_dir, .stderr_behavior = .Ignore, .stdout_behavior = .Ignore },
global_opts.getZigEnv(),
&[_][]const u8{opts.build_file orelse "build.zig"},
);
}