Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for bun --revision #4027

Merged
merged 4 commits into from
Aug 9, 2023
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
1 change: 0 additions & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ pub fn build(b: *Build) !void {
.argv = &.{
"git",
"rev-parse",
"--short",
"HEAD",
},
.cwd = b.pathFromRoot("."),
Expand Down
11 changes: 11 additions & 0 deletions src/__global.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ else if (Environment.isDebug)
else
std.fmt.comptimePrint(BASE_VERSION ++ ".{d} ({s})", .{ build_id, Environment.git_sha[0..@min(Environment.git_sha.len, 8)] });

pub const package_json_version_with_revision = if (Environment.git_sha.len == 0)
package_json_version
else if (Environment.isDebug)
std.fmt.comptimePrint(BASE_VERSION ++ ".{d}-debug+{s}", .{ build_id, Environment.git_sha })
else if (Environment.is_canary)
std.fmt.comptimePrint(BASE_VERSION ++ ".{d}-canary+{s}", .{ build_id, Environment.git_sha })
else if (Environment.isTest)
std.fmt.comptimePrint(BASE_VERSION ++ ".{d}-test+{s}", .{ build_id, Environment.git_sha })
else
std.fmt.comptimePrint(BASE_VERSION ++ ".{d}+{s}", .{ build_id, Environment.git_sha });

pub const os_name = if (Environment.isWindows)
"win32"
else if (Environment.isMac)
Expand Down
11 changes: 11 additions & 0 deletions src/cli.zig
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ pub const Arguments = struct {
clap.parseParam("--main-fields <STR>... Main fields to lookup in package.json. Defaults to --target dependent") catch unreachable,
clap.parseParam("--no-summary Don't print a summary (when generating .bun)") catch unreachable,
clap.parseParam("-v, --version Print version and exit") catch unreachable,
clap.parseParam("--revision Print version with revision and exit") catch unreachable,
clap.parseParam("--tsconfig-override <STR> Load tsconfig from path instead of cwd/tsconfig.json") catch unreachable,
clap.parseParam("-d, --define <STR>... Substitute K:V while parsing, e.g. --define process.env.NODE_ENV:\"development\". Values are parsed as JSON.") catch unreachable,
clap.parseParam("-e, --external <STR>... Exclude module from transpilation (can use * wildcards). ex: -e react") catch unreachable,
Expand Down Expand Up @@ -231,6 +232,12 @@ pub const Arguments = struct {
Global.exit(0);
}

fn printRevisionAndExit() noreturn {
@setCold(true);
Output.writer().writeAll(Global.package_json_version_with_revision ++ "\n") catch {};
Global.exit(0);
}

pub fn loadConfigPath(allocator: std.mem.Allocator, auto_loaded: bool, config_path: [:0]const u8, ctx: *Command.Context, comptime cmd: Command.Tag) !void {
var config_file = std.fs.File{
.handle = std.os.openZ(config_path, std.os.O.RDONLY, 0) catch |err| {
Expand Down Expand Up @@ -362,6 +369,10 @@ pub const Arguments = struct {
printVersionAndExit();
}

if (args.flag("--revision")) {
printRevisionAndExit();
}

var cwd: []u8 = undefined;
if (args.option("--cwd")) |cwd_| {
cwd = brk: {
Expand Down
27 changes: 27 additions & 0 deletions test/cli/bun.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,31 @@ describe("bun", () => {
});
}
});

describe("revision", () => {
test("revision generates version numbers correctly", () => {
var { stdout, exitCode } = Bun.spawnSync({
cmd: [bunExe(), "--version"],
env: {},
stderr: "inherit",
});
var version = stdout.toString().trim();

var { stdout, exitCode } = Bun.spawnSync({
cmd: [bunExe(), "--revision"],
env: {},
stderr: "inherit",
});
var revision = stdout.toString().trim();

expect(exitCode).toBe(0);
expect(revision).toStartWith(version);
// https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
expect(revision).toMatch(
new RegExp(
"^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$",
),
);
});
});
});