From 9346daa04e7db2db78b3c00fb84f698bd3268053 Mon Sep 17 00:00:00 2001 From: Emil Lerch Date: Fri, 20 Oct 2023 16:45:11 -0700 Subject: [PATCH] add build deploy step --- build.zig | 8 +++++ src/CloudflareDeployStep.zig | 61 ++++++++++++++++++++++++++++++++++++ src/main.zig | 2 +- 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 src/CloudflareDeployStep.zig diff --git a/build.zig b/build.zig index 37de913..c924b3a 100644 --- a/build.zig +++ b/build.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const CloudflareDeployStep = @import("src/CloudflareDeployStep.zig"); // Although this function looks imperative, note that its job is to // declaratively construct a build graph that will be executed by an external @@ -46,6 +47,13 @@ pub fn build(b: *std.Build) void { run_cmd.addArgs(args); } + const deploy_cmd = CloudflareDeployStep.create(b, "zigwasi", .{ .path = "index.js" }, .{}); + + // This creates a build step. It will be visible in the `zig build --help` menu, + // and can be selected like this: `zig build run` + // This will evaluate the `run` step rather than the default, which is "install". + const deploy_step = b.step("deploy", "Deploy test function to Cloudflare"); + deploy_step.dependOn(&deploy_cmd.step); // This creates a build step. It will be visible in the `zig build --help` menu, // and can be selected like this: `zig build run` // This will evaluate the `run` step rather than the default, which is "install". diff --git a/src/CloudflareDeployStep.zig b/src/CloudflareDeployStep.zig new file mode 100644 index 0000000..e3c2810 --- /dev/null +++ b/src/CloudflareDeployStep.zig @@ -0,0 +1,61 @@ +const std = @import("std"); +const cloudflare = @import("main.zig"); +const CloudflareDeployStep = @This(); + +pub const base_id: std.Build.Step.Id = .custom; + +step: std.Build.Step, +primary_javascript_path: std.Build.LazyPath, +worker_name: []const u8, + +pub const Options = struct {}; + +pub fn create( + owner: *std.Build, + worker_name: []const u8, + primary_javascript_path: std.Build.LazyPath, + options: Options, +) *CloudflareDeployStep { + _ = options; + const self = owner.allocator.create(CloudflareDeployStep) catch @panic("OOM"); + self.* = CloudflareDeployStep{ + .step = std.Build.Step.init(.{ + .id = base_id, + .name = owner.fmt("cloudflare deploy {s}", .{primary_javascript_path.getDisplayName()}), + .owner = owner, + .makeFn = make, + }), + .primary_javascript_path = primary_javascript_path, + .worker_name = worker_name, + }; + primary_javascript_path.addStepDependencies(&self.step); + return self; +} + +fn make(step: *std.Build.Step, prog_node: *std.Progress.Node) !void { + _ = prog_node; + const b = step.owner; + const self = @fieldParentPtr(CloudflareDeployStep, "step", step); + + var client = std.http.Client{ .allocator = b.allocator }; + defer client.deinit(); + + const script = try std.fs.cwd().readFileAlloc(b.allocator, self.primary_javascript_path.path, std.math.maxInt(usize)); + + var al = std.ArrayList(u8).init(b.allocator); + defer al.deinit(); + try cloudflare.pushWorker( + b.allocator, + &client, + self.worker_name, + script, + al.writer(), + std.io.getStdErr().writer(), + ); + const start = std.mem.lastIndexOf(u8, al.items, "http").?; + step.name = try std.fmt.allocPrint( + b.allocator, + "cloudflare deploy {s} to {s}", + .{ self.primary_javascript_path.getDisplayName(), al.items[start .. al.items.len - 1] }, + ); +} diff --git a/src/main.zig b/src/main.zig index 22103d9..7dedb5d 100644 --- a/src/main.zig +++ b/src/main.zig @@ -69,7 +69,7 @@ const Wasm = struct { } }; -fn pushWorker( +pub fn pushWorker( allocator: std.mem.Allocator, client: *std.http.Client, worker_name: []const u8,