From 26fdfdac4713226140797de205a385cfbd1a2e8f Mon Sep 17 00:00:00 2001 From: LordMZTE Date: Sat, 1 Jul 2023 21:28:50 +0200 Subject: [PATCH] port withjava to zig --- scripts/withjava.sh | 17 ---------- scripts/withjava/.gitignore | 1 + scripts/withjava/build.zig | 29 ++++++++++++++++ scripts/withjava/src/main.zig | 53 ++++++++++++++++++++++++++++++ setup/commands/install-scripts.rkt | 2 +- 5 files changed, 84 insertions(+), 18 deletions(-) delete mode 100755 scripts/withjava.sh create mode 100644 scripts/withjava/.gitignore create mode 100644 scripts/withjava/build.zig create mode 100644 scripts/withjava/src/main.zig diff --git a/scripts/withjava.sh b/scripts/withjava.sh deleted file mode 100755 index d91bd22..0000000 --- a/scripts/withjava.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/sh -__usage=" -USAGE: - $0 - -ARGS: - the jvm to use - the command to run -" - -if [ 3 -gt $# ]; then - echo -e "$__usage" -else - export PATH="/usr/lib/jvm/$1/bin:$PATH" - ${@:2} -fi - diff --git a/scripts/withjava/.gitignore b/scripts/withjava/.gitignore new file mode 100644 index 0000000..fe95f8d --- /dev/null +++ b/scripts/withjava/.gitignore @@ -0,0 +1 @@ +/zig-* diff --git a/scripts/withjava/build.zig b/scripts/withjava/build.zig new file mode 100644 index 0000000..2b9a7d6 --- /dev/null +++ b/scripts/withjava/build.zig @@ -0,0 +1,29 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + const exe = b.addExecutable(.{ + .name = "withjava", + .root_source_file = .{ .path = "src/main.zig" }, + .target = target, + .optimize = optimize, + }); + + exe.strip = switch (optimize) { + .ReleaseSafe, .Debug => false, + .ReleaseFast, .ReleaseSmall => true, + }; + + b.installArtifact(exe); + + const run_cmd = b.addRunArtifact(exe); + run_cmd.step.dependOn(b.getInstallStep()); + if (b.args) |args| { + run_cmd.addArgs(args); + } + + const run_step = b.step("run", "Run the app"); + run_step.dependOn(&run_cmd.step); +} diff --git a/scripts/withjava/src/main.zig b/scripts/withjava/src/main.zig new file mode 100644 index 0000000..2f21a7d --- /dev/null +++ b/scripts/withjava/src/main.zig @@ -0,0 +1,53 @@ +const std = @import("std"); + +pub fn main() !u8 { + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer _ = gpa.deinit(); + const alloc = gpa.allocator(); + + if (std.os.argv.len < 3) { + std.log.err("Not enough arguments, expected at least 2, got {}!", .{std.os.argv.len - 1}); + return 1; + } + + var env = try std.process.getEnvMap(alloc); + defer env.deinit(); + + if (env.getPtr("PATH")) |path_p| { + const newpath = try std.fmt.allocPrint( + alloc, + "/usr/lib/jvm/{s}/bin:{s}", + .{ std.os.argv[1], path_p.* }, + ); + alloc.free(path_p.*); + path_p.* = newpath; + } else { + const newpath = try std.fmt.allocPrint(alloc, "/usr/lib/jvm/{s}/bin", .{std.os.argv[1]}); + errdefer alloc.free(newpath); + try env.putMove(try alloc.dupe(u8, "PATH"), newpath); + } + + { + const java_home = try std.fmt.allocPrint(alloc, "/usr/lib/jvm/{s}", .{std.os.argv[1]}); + errdefer alloc.free(java_home); + try env.putMove(try alloc.dupe(u8, "JAVA_HOME"), java_home); + } + + const child_argv = try alloc.alloc([]const u8, std.os.argv[2..].len); + defer alloc.free(child_argv); + + for (std.os.argv[2..], child_argv) |a1, *a2| + a2.* = std.mem.span(a1); + + var child = std.ChildProcess.init(child_argv, alloc); + child.env_map = &env; + const term = try child.spawnAndWait(); + + switch (term) { + .Exited => |ret| return ret, + .Signal, .Stopped, .Unknown => |ret| { + std.log.err("child signalled {}", .{ret}); + return 1; + }, + } +} diff --git a/setup/commands/install-scripts.rkt b/setup/commands/install-scripts.rkt index 65f6e5f..2fcbdba 100644 --- a/setup/commands/install-scripts.rkt +++ b/setup/commands/install-scripts.rkt @@ -18,7 +18,6 @@ (mklink "scripts/start-joshuto.sh" (bin-path "start-joshuto")) (mklink "scripts/startriver.sh" (bin-path "startriver")) (mklink "scripts/update-nvim-plugins.rkt" (bin-path "update-nvim-plugins")) - (mklink "scripts/withjava.sh" (bin-path "withjava")) ;; Compile scripts (install-rust "scripts/i3status") @@ -28,6 +27,7 @@ (install-zig "scripts/prompt") (install-zig "scripts/randomwallpaper") (install-zig "scripts/vinput") + (install-zig "scripts/withjava") (install-roswell "scripts/playvid.ros") null)