diff --git a/.gitignore b/.gitignore index 2482829..95fba8a 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ demo src/models/ smithy/zig-out/ libs/ +src/git_version.zig diff --git a/VersionStep.zig b/VersionStep.zig new file mode 100644 index 0000000..c01c118 --- /dev/null +++ b/VersionStep.zig @@ -0,0 +1,37 @@ +//! Publish Date: 2022-01-12 +//! This file is hosted at ??? and is meant to be copied +//! to projects that use it. Sample usage: +//! +//! ?? +//! ?? + +const std = @import("std"); +const Step = @This(); + +step: std.build.Step, +builder: *std.build.Builder, +version_path: []const u8, + +// Creates a step that will add the git version info in a file in src/ +// so it can be consumed by additional code. If version_path is not specified, +// it will default to "git_version.zig". This should be part of .gitignore +pub fn create(b: *std.build.Builder, version_path: ?[]const u8) *Step { + var result = b.allocator.create(Step) catch @panic("memory"); + result.* = Step{ + .step = std.build.Step.init(.custom, "create version file", b.allocator, make), + .builder = b, + .version_path = std.fs.path.resolve(b.allocator, &[_][]const u8{ + b.build_root, + "src", + version_path orelse "git_version.zig", + }) catch @panic("memory"), + }; + return result; +} + +fn make(step: *std.build.Step) !void { + const self = @fieldParentPtr(Step, "step", step); + const file = try std.fs.createFileAbsolute(self.version_path, .{}); + defer file.close(); + try file.writer().print("pub const version = {s};\n", .{"\"to be implemented\""}); +} diff --git a/build.zig b/build.zig index c6aa2a7..94abbd5 100644 --- a/build.zig +++ b/build.zig @@ -3,7 +3,8 @@ const builtin = @import("builtin"); const Builder = @import("std").build.Builder; const GitRepoStep = @import("GitRepoStep.zig"); const CopyStep = @import("CopyStep.zig"); -const @"test" = @import("build_test.zig"); +const tst = @import("build_test.zig"); +const VersionStep = @import("VersionStep.zig"); pub fn build(b: *Builder) !void { const zfetch_repo = GitRepoStep.create(b, .{ @@ -42,6 +43,8 @@ pub fn build(b: *Builder) !void { ); copy_deps.step.dependOn(&zfetch_repo.step); + const version = VersionStep.create(b, null); + exe.step.dependOn(&version.step); exe.step.dependOn(©_deps.step); // This import won't work unless we're already cloned. The way around @@ -60,7 +63,8 @@ pub fn build(b: *Builder) !void { const run_step = b.step("run", "Run the app"); run_step.dependOn(&run_cmd.step); - var test_step = try @"test".addTestStep(b, mode, exe.packages.items); + var test_step = try tst.addTestStep(b, mode, exe.packages.items); + test_step.dependOn(&version.step); if (target.getOs().tag == .linux) { // TODO: Support > linux with RunStep