port withjava to zig

This commit is contained in:
LordMZTE 2023-07-01 21:28:50 +02:00
parent b55f0ae22d
commit 26fdfdac47
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6
5 changed files with 84 additions and 18 deletions

View file

@ -1,17 +0,0 @@
#!/bin/sh
__usage="
USAGE:
$0 <java-name> <command...>
ARGS:
<java-name> the jvm to use
<command...> the command to run
"
if [ 3 -gt $# ]; then
echo -e "$__usage"
else
export PATH="/usr/lib/jvm/$1/bin:$PATH"
${@:2}
fi

1
scripts/withjava/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/zig-*

View file

@ -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);
}

View file

@ -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;
},
}
}

View file

@ -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)