Something went wrong. Try again.
Zig bindings for all 506 operations of the Forgejo API, generated from its own description, and zfj, a command line built on them.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
const std = @import("std");
pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{});
const openapi2zig_dep = b.dependency("openapi2zig", .{ .target = target, .optimize = optimize, });
// `zig build update-spec` replaces api/swagger.v1.json with the API // description from a Forgejo release. It reaches the network, so it is a // step of its own that nothing else depends on -- an ordinary build never // runs it, and never needs to. { const forgejo_version = b.option( []const u8, "forgejo-version", "Forgejo release tag to take the API description from (default: the latest release)", );
const update_exe = b.addExecutable(.{ .name = "update-spec", .root_module = b.createModule(.{ .root_source_file = b.path("src/update-spec.zig"), .target = target, .optimize = optimize, }), });
const update_cmd = b.addRunArtifact(update_exe); if (forgejo_version) |version| update_cmd.addArg(version); // What the network answers is not a function of the command line, so // the run cannot be cached on its arguments the way the generator is. update_cmd.has_side_effects = true; const fetched_spec = update_cmd.captureStdOut(.{ .basename = "swagger.v1.json" });
const write_spec = b.addUpdateSourceFiles(); write_spec.addCopyFileToSource(fetched_spec, "api/swagger.v1.json");
const update_step = b.step("update-spec", "Update api/swagger.v1.json from a Forgejo release"); update_step.dependOn(&write_spec.step); }
// The bindings are generated during the build rather than checked in, so // the only thing this repository holds for them is the specification they // are derived from. const generate_exe = b.addExecutable(.{ .name = "generate", .root_module = b.createModule(.{ .root_source_file = b.path("src/generate.zig"), .target = target, // The generator runs once per build of a 850 KB document; the time // it takes to optimize is repaid the first time it runs. .optimize = .ReleaseFast, .imports = &.{ .{ .name = "openapi2zig", .module = openapi2zig_dep.module("openapi2zig"), }, }, }), .use_lld = false, .use_llvm = false, });
const generate_cmd = b.addRunArtifact(generate_exe); generate_cmd.setStdIn(.{ .lazy_path = b.path("api/swagger.v1.json") }); const api_zig = generate_cmd.captureStdOut(.{ .basename = "api.zig" });
const zfj_mod = b.addModule("zfj", .{ .root_source_file = api_zig, .target = target, .optimize = optimize, });
// Installed so that `zig build` leaves the generated source somewhere it // can be read: it is the reference for what the library actually exposes. const install_api_zig = b.addInstallFile(api_zig, "api.zig"); b.getInstallStep().dependOn(&install_api_zig.step);
const zfj_lib = b.addLibrary(.{ .name = "zfj", .root_module = zfj_mod, });
const install_docs = b.addInstallDirectory(.{ .source_dir = zfj_lib.getEmittedDocs(), .install_dir = .prefix, .install_subdir = "docs", });
const docs_step = b.step("docs", "Build the API documentation"); docs_step.dependOn(&install_docs.step);
// Compiling the generated module is the test that matters most: 506 // operations and 246 models either type check against each other or they // do not. The tests in src/ check what that cannot -- that the bindings // are shaped the way a caller expects. const zfj_tests = b.addTest(.{ .root_module = zfj_mod, }); const run_zfj_tests = b.addRunArtifact(zfj_tests);
const shape_tests = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("src/tests.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "zfj", .module = zfj_mod }, }, }), }); const run_shape_tests = b.addRunArtifact(shape_tests);
const test_step = b.step("test", "Run the tests"); test_step.dependOn(&run_zfj_tests.step); test_step.dependOn(&run_shape_tests.step);}