From 07422c1413d0486a4b2ea878b5398aa147e004b3 Mon Sep 17 00:00:00 2001 From: Jiacai Liu Date: Wed, 28 Jun 2023 22:22:15 +0800 Subject: [PATCH] feat: add version (#11) * feat: add version * Update CI --- .github/workflows/binary.yml | 7 +++- Makefile | 3 +- build.zig | 77 +++++++++++++----------------------- src/common.zig | 5 +++ src/pidof.zig | 9 +++++ 5 files changed, 49 insertions(+), 52 deletions(-) create mode 100644 src/common.zig diff --git a/.github/workflows/binary.yml b/.github/workflows/binary.yml index 18b786e..57fa5d9 100644 --- a/.github/workflows/binary.yml +++ b/.github/workflows/binary.yml @@ -40,9 +40,14 @@ jobs: - uses: goto-bus-stop/setup-zig@v2 with: version: ${{ matrix.zig-version }} + - name: Set Environment Variables + run: | + echo "BUILD_DATE=$(date +'%Y-%m-%dT%H:%M:%S%z')" >> $GITHUB_ENV - name: Build run: | - zig build -Dtarget=${{ matrix.targets }} -Doptimize=ReleaseSafe + zig build -Dtarget=${{ matrix.targets }} -Doptimize=ReleaseSafe \ + -Dgit_commit=${{ github.head_ref }}-${{ github.sha }} \ + -Dbuild_date=${{ env.BUILD_DATE }} # https://github.com/actions/upload-artifact#maintaining-file-permissions-and-case-sensitive-files tar -cvf zigcli.tar zig-out/bin/ - name: Upload diff --git a/Makefile b/Makefile index cbaa257..e98a12d 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ build: - zig build -Doptimize=ReleaseFast + zig build -Doptimize=ReleaseFast -Dbuild_date=$(shell date +"%Y-%m-%dT%H:%M:%S%z") \ + -Dgit_commit=$(shell git rev-parse --short HEAD) fmt: zig fmt --check . diff --git a/build.zig b/build.zig index 86d35ad..fce7607 100644 --- a/build.zig +++ b/build.zig @@ -6,27 +6,42 @@ pub fn build(b: *Build) void { const optimize = b.standardOptimizeOption(.{}); const target = b.standardTargetOptions(.{}); const simargs_dep = b.dependency("simargs", .{}); + const table_dep = b.dependency("table-helper", .{}); + + const opt = b.addOptions(); + opt.addOption([]const u8, "build_date", b.option([]const u8, "build_date", "Build date") orelse + b.fmt("{d}", .{std.time.milliTimestamp()})); + opt.addOption([]const u8, "git_commit", b.option([]const u8, "git_commit", "Git commit") orelse + "Unknown"); + b.modules.put("build_info", opt.createModule()) catch @panic("OOM"); + b.modules.put("simargs", simargs_dep.module("simargs")) catch @panic("OOM"); + b.modules.put("table-helper", table_dep.module("table-helper")) catch @panic("OOM"); var all_tests = std.ArrayList(*Build.Step).init(b.allocator); inline for (.{ .{ - buildTree(b, optimize, target, simargs_dep), + buildCli(b, "tree", optimize, target), "tree", }, .{ - buildLoc(b, optimize, target, simargs_dep), + buildCli(b, "loc", optimize, target), "loc", }, .{ - buildPidof(b, optimize, target, simargs_dep), + buildCli(b, "pidof", optimize, target), "pidof", }, .{ - buildYes(b, optimize, target), + buildCli(b, "yes", optimize, target), "yes", }, }) |prog| { if (prog.@"0") |exe| { + var deps = b.modules.iterator(); + while (deps.next()) |dep| { + exe.addModule(dep.key_ptr.*, dep.value_ptr.*); + } + const name = prog.@"1"; b.installArtifact(exe); const run_cmd = b.addRunArtifact(exe); @@ -57,55 +72,17 @@ fn buildTestStep(b: *std.Build, comptime name: []const u8, target: std.zig.Cross return test_step; } -fn buildTree(b: *std.Build, optimize: std.builtin.Mode, target: std.zig.CrossTarget, simargs_dep: *std.build.Dependency) ?*Build.CompileStep { - const exe = b.addExecutable(.{ - .name = "tree", - .root_source_file = FileSource.relative("src/tree.zig"), - .target = target, - .optimize = optimize, - }); - exe.addModule("simargs", simargs_dep.module("simargs")); - - return exe; -} - -fn buildLoc(b: *std.Build, optimize: std.builtin.Mode, target: std.zig.CrossTarget, simargs_dep: *std.build.Dependency) ?*Build.CompileStep { - const exe = b.addExecutable(.{ - .name = "loc", - .root_source_file = FileSource.relative("src/loc.zig"), - .target = target, - .optimize = optimize, - }); - exe.addModule("simargs", simargs_dep.module("simargs")); - const table_dep = b.dependency("table-helper", .{}); - exe.addModule("table-helper", table_dep.module("table-helper")); - - return exe; -} - -fn buildYes(b: *std.build.Builder, optimize: std.builtin.Mode, target: std.zig.CrossTarget) ?*Build.CompileStep { - const exe = b.addExecutable(.{ - .name = "yes", - .root_source_file = FileSource.relative("src/yes.zig"), - .target = target, - .optimize = optimize, - }); - - return exe; -} - -fn buildPidof(b: *std.Build, optimize: std.builtin.Mode, target: std.zig.CrossTarget, simargs_dep: *std.build.Dependency) ?*Build.CompileStep { - if (target.getOsTag() != .macos) { - return null; +fn buildCli(b: *std.Build, comptime name: []const u8, optimize: std.builtin.Mode, target: std.zig.CrossTarget) ?*Build.CompileStep { + if (std.mem.eql(u8, name, "pidof")) { + if (target.getOsTag() != .macos) { + return null; + } } - const exe = b.addExecutable(.{ - .name = "pidof", - .root_source_file = FileSource.relative("src/pidof.zig"), + return b.addExecutable(.{ + .name = name, + .root_source_file = FileSource.relative("src/" ++ name ++ ".zig"), .target = target, .optimize = optimize, }); - exe.addModule("simargs", simargs_dep.module("simargs")); - - return exe; } diff --git a/src/common.zig b/src/common.zig new file mode 100644 index 0000000..5e76355 --- /dev/null +++ b/src/common.zig @@ -0,0 +1,5 @@ +const info = @import("build_info"); + +pub fn print_build_info(writer: anytype) !void { + try writer.print("Build date: {s}\nGit commit: {s}", .{ info.build_date, info.git_commit }); +} diff --git a/src/pidof.zig b/src/pidof.zig index 3c6d4b6..408af0f 100644 --- a/src/pidof.zig +++ b/src/pidof.zig @@ -4,6 +4,7 @@ const std = @import("std"); const simargs = @import("simargs"); +const common = @import("common.zig"); const c = @cImport({ @cInclude("sys/sysctl.h"); }); @@ -11,16 +12,19 @@ const c = @cImport({ pub const Options = struct { single: bool = false, separator: []const u8 = " ", + version: bool = false, help: bool = false, pub const __shorts__ = .{ .single = .s, .separator = .S, + .version = .v, .help = .h, }; pub const __messages__ = .{ .single = "Single shot - this instructs the program to only return one pid.", .separator = "Use separator as a separator put between pids.", + .version = "Print version.", .help = "Print help message.", }; }; @@ -73,6 +77,11 @@ pub fn main() !void { const opt = try simargs.parse(allocator, Options, "[program]"); defer opt.deinit(); + if (opt.args.version) { + try common.print_build_info(std.io.getStdOut().writer()); + return; + } + if (opt.positional_args.items.len == 0) { std.debug.print("program is not given", .{}); std.os.exit(1);