replace sed with a simple Zig program for templating desktop files

pull/7679/head
Jeffrey C. Ollie 2025-06-25 11:55:43 -05:00 committed by Mitchell Hashimoto
parent eb5a488b57
commit fa4f420768
2 changed files with 48 additions and 17 deletions

View File

@ -376,24 +376,26 @@ pub fn formatService(
src: std.Build.LazyPath,
dest: []const u8,
) *std.Build.Step {
var cmd = b.addSystemCommand(&.{"sed"});
cmd.setStdIn(.{ .lazy_path = src });
const output = cmd.captureStdOut();
var cmd = b.addExecutable(
.{
.name = "desktop-template",
.root_source_file = b.path("src/build/desktop_template.zig"),
.target = b.graph.host,
},
);
cmd.addArg(b.fmt(
"-e s!@@NAME@@!{s}!g",
.{name},
));
cmd.addArg(b.fmt(
"-e s!@@APPID@@!{s}!g",
.{app_id},
));
cmd.addArg(b.fmt(
"-e s!@@GHOSTTY@@!{s}/bin/ghostty!g",
.{b.install_prefix},
));
if (cfg.flatpak)
cmd.addArg("-e /^SystemdService=/d");
const options = b.addOptions();
options.addOption([]const u8, "app_id", app_id);
options.addOption([]const u8, "name", name);
options.addOption([]const u8, "ghostty", b.fmt("{s}/bin/ghostty", .{b.install_prefix}));
options.addOption(bool, "flatpak", cfg.flatpak);
cmd.root_module.addOptions("cfg", options);
const run = b.addRunArtifact(cmd);
run.setStdIn(.{ .lazy_path = src });
const output = run.captureStdOut();
return &b.addInstallFile(output, dest).step;
}

View File

@ -0,0 +1,29 @@
const std = @import("std");
const cfg = @import("cfg");
pub fn main() !void {
var debug: std.heap.DebugAllocator(.{}) = .init;
defer _ = debug.deinit();
const alloc = debug.allocator();
const stdin = std.io.getStdIn();
const stdout = std.io.getStdOut();
var input = stdin.reader();
while (try input.readUntilDelimiterOrEofAlloc(alloc, '\n', 4096)) |line| {
defer alloc.free(line);
const buf1 = try std.mem.replaceOwned(u8, alloc, line, "@@APPID@@", cfg.app_id);
defer alloc.free(buf1);
const buf2 = try std.mem.replaceOwned(u8, alloc, buf1, "@@NAME@@", cfg.name);
defer alloc.free(buf2);
const buf3 = try std.mem.replaceOwned(u8, alloc, buf2, "@@GHOSTTY@@", cfg.ghostty);
defer alloc.free(buf3);
if (cfg.flatpak and std.mem.startsWith(u8, buf3, "SystemdService=")) continue;
try stdout.writeAll(buf3);
try stdout.writeAll("\n");
}
}