Skip to content

Commit

Permalink
feat: add version (#11)
Browse files Browse the repository at this point in the history
* feat: add version

* Update CI
  • Loading branch information
jiacai2050 committed Jun 28, 2023
1 parent 1150d21 commit 07422c1
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 52 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/binary.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -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 .
Expand Down
77 changes: 27 additions & 50 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
5 changes: 5 additions & 0 deletions src/common.zig
Original file line number Diff line number Diff line change
@@ -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 });
}
9 changes: 9 additions & 0 deletions src/pidof.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,27 @@

const std = @import("std");
const simargs = @import("simargs");
const common = @import("common.zig");
const c = @cImport({
@cInclude("sys/sysctl.h");
});

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.",
};
};
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 07422c1

Please sign in to comment.