add build deploy step

This commit is contained in:
Emil Lerch 2023-10-20 16:45:11 -07:00
parent 6752730cd1
commit 9346daa04e
Signed by: lobo
GPG Key ID: A7B62D657EF764F8
3 changed files with 70 additions and 1 deletions

View File

@ -1,4 +1,5 @@
const std = @import("std"); const std = @import("std");
const CloudflareDeployStep = @import("src/CloudflareDeployStep.zig");
// Although this function looks imperative, note that its job is to // Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external // 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); 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, // This creates a build step. It will be visible in the `zig build --help` menu,
// and can be selected like this: `zig build run` // and can be selected like this: `zig build run`
// This will evaluate the `run` step rather than the default, which is "install". // This will evaluate the `run` step rather than the default, which is "install".

View File

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

View File

@ -69,7 +69,7 @@ const Wasm = struct {
} }
}; };
fn pushWorker( pub fn pushWorker(
allocator: std.mem.Allocator, allocator: std.mem.Allocator,
client: *std.http.Client, client: *std.http.Client,
worker_name: []const u8, worker_name: []const u8,