From a9b18731092ab00632ecc09551c185552d5e1e28 Mon Sep 17 00:00:00 2001 From: rainfall Date: Mon, 4 Nov 2024 19:11:53 -0500 Subject: [PATCH] refactor build process --- .gitignore | 1 + README.md | 8 ++--- build.zig | 66 +++++++++++++++----------------------- build.zig.zon | 12 +++++++ install.sh | 4 --- lib/websocket.zig | 1 - lib/zig-tls12 | 1 - lib/zmpl | 1 - src/{main.zig => test.zig} | 15 +++++---- 9 files changed, 49 insertions(+), 60 deletions(-) delete mode 100755 install.sh delete mode 160000 lib/websocket.zig delete mode 160000 lib/zig-tls12 delete mode 160000 lib/zmpl rename src/{main.zig => test.zig} (68%) diff --git a/.gitignore b/.gitignore index acb70f3..c067f5c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ lib/zlib .zig-cache +.env diff --git a/README.md b/README.md index 2bfa5d2..bc845c0 100644 --- a/README.md +++ b/README.md @@ -4,13 +4,9 @@ wget https://github.com/madler/zlib/releases/download/v1.2.13/zlib-1.2.13.tar.gz tar xvf zlib-1.2.13.tar.gz rm zlib-1.2.13.tar.gz -mv zlib-1.2.13 zlib -mv zlib lib/zlib -git clone https://github.com/yuzudev/websocket.zig.git ./lib/websocket.zig/ -git clone https://github.com/yuzudev/zig-tls12 ./lib/zig-tls12/ -git clone https://github.com/jetzig-framework/zmpl.git ./lib/zmpl/ +mv zlib-1.2.13 lib/zlib ``` -or simply run ./install.sh +or simply run ./install-zlib.sh # features * supports sharding for large bots diff --git a/build.zig b/build.zig index bf7618f..a128143 100644 --- a/build.zig +++ b/build.zig @@ -10,32 +10,17 @@ pub fn build(b: *std.Build) void { const optimize = b.standardOptimizeOption(.{ .preferred_optimize_mode = .ReleaseFast }); // this is your own program - const exe = b.addExecutable(.{ - // the name of your project - .name = "oculus-2", - // your main function - .root_source_file = b.path("src/main.zig"), - // references the ones you declared above - .target = target, - .optimize = optimize, + const dzig = b.addModule("discord.zig", .{ + .root_source_file = b.path("src/discord.zig"), .link_libc = true, }); - const test_comp = b.addTest(.{ - .root_source_file = b.path("src/test.zig"), + const websocket = b.dependency("websocket", .{ .target = target, .optimize = optimize, }); - const websocket = b.createModule(.{ - .root_source_file = b.path("lib/websocket.zig/src/websocket.zig"), - .target = target, - .optimize = optimize, - .link_libc = true, - }); - - const zig_tls = b.createModule(.{ - .root_source_file = b.path("lib/zig-tls12/src/entry.zig"), + const zig_tls = b.dependency("zig-tls", .{ .target = target, .optimize = optimize, }); @@ -48,11 +33,9 @@ pub fn build(b: *std.Build) void { .link_libc = true, }); - const zmpl = b.createModule(.{ - //.name = "zlib", + const zmpl = b.dependency("zmpl", .{ .target = target, .optimize = optimize, - .root_source_file = b.path("lib/zmpl/src/zmpl.zig"), }); const deque = b.dependency("zig-deque", .{ @@ -60,6 +43,14 @@ pub fn build(b: *std.Build) void { .optimize = optimize, }); + const marin = b.addExecutable(.{ + .name = "marin", + .root_source_file = b.path("src/test.zig"), + .target = target, + .optimize = optimize, + .link_libc = true, + }); + const srcs = &.{ "lib/zlib/adler32.c", "lib/zlib/compress.c", @@ -83,28 +74,23 @@ pub fn build(b: *std.Build) void { zlib_zig.addCSourceFiles(.{ .files = srcs, .flags = &.{"-std=c89"} }); zlib_zig.addIncludePath(b.path("lib/zlib/")); - websocket.addImport("zlib", zlib_zig); - websocket.addImport("tls12", zig_tls); - // now install your own executable after it's built correctly - exe.root_module.addImport("ws", websocket); - exe.root_module.addImport("tls12", zig_tls); - exe.root_module.addImport("zlib", zlib_zig); - exe.root_module.addImport("zmpl", zmpl); - exe.root_module.addImport("deque", deque.module("zig-deque")); + dzig.addImport("ws", websocket.module("websocket")); + dzig.addImport("tls12", zig_tls.module("zig-tls12")); + dzig.addImport("zlib", zlib_zig); + dzig.addImport("zmpl", zmpl.module("zmpl")); + dzig.addImport("deque", deque.module("zig-deque")); + + marin.root_module.addImport("discord.zig", dzig); + marin.root_module.addImport("ws", websocket.module("websocket")); + marin.root_module.addImport("tls12", zig_tls.module("zig-tls12")); + marin.root_module.addImport("zlib", zlib_zig); + marin.root_module.addImport("zmpl", zmpl.module("zmpl")); + marin.root_module.addImport("deque", deque.module("zig-deque")); // test - test_comp.root_module.addImport("ws", websocket); - test_comp.root_module.addImport("tls12", zig_tls); - test_comp.root_module.addImport("zlib", zlib_zig); - - const run_test_comp = b.addRunArtifact(test_comp); - const test_step = b.step("test", "Run unit tests"); - test_step.dependOn(&test_comp.step); - test_step.dependOn(&run_test_comp.step); - - const run_cmd = b.addRunArtifact(exe); + const run_cmd = b.addRunArtifact(marin); run_cmd.step.dependOn(b.getInstallStep()); const run_step = b.step("run", "Run the app"); diff --git a/build.zig.zon b/build.zig.zon index bddc386..1b14f3d 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -27,6 +27,18 @@ .url = "https://github.com/magurotuna/zig-deque/archive/refs/heads/main.zip", .hash = "1220d1bedf7d5cfc7475842b3d4e8f03f1308be2e724a036677cceb5c4db13c3da3d", }, + .zmpl = .{ + .url = "https://github.com/jetzig-framework/zmpl/archive/refs/heads/main.zip", + .hash = "1220d279461176a5554da195c19e025110096819004c6de86a6bb2e186ac13b63f85", + }, + .@"zig-tls" = .{ + .url = "https://github.com/yuzudev/zig-tls12/archive/refs/heads/main.zip", + .hash = "122079aad9eebc5945e207715c50722bbeca34e7b7a5876b3a322fad6468b28f2ed3", + }, + .websocket = .{ + .url = "https://github.com/yuzudev/websocket.zig/archive/refs/heads/master.zip", + .hash = "122062d9dda015ba25780d00697e0e2c1cbc3ffa5b5eae55c9ea28b39b6d99ef1d85", + }, }, .paths = .{ "build.zig", diff --git a/install.sh b/install.sh deleted file mode 100755 index 57c8b5a..0000000 --- a/install.sh +++ /dev/null @@ -1,4 +0,0 @@ -git clone https://github.com/yuzudev/websocket.zig.git ./lib/websocket.zig/ -git clone https://github.com/yuzudev/zig-tls12 ./lib/zig-tls12/ -git clone https://github.com/jetzig-framework/zmpl.git ./lib/zmpl/ -./install-zlib.sh diff --git a/lib/websocket.zig b/lib/websocket.zig deleted file mode 160000 index e93fa52..0000000 --- a/lib/websocket.zig +++ /dev/null @@ -1 +0,0 @@ -Subproject commit e93fa527f1d4deaeda5f74f51efaf7ec60cab396 diff --git a/lib/zig-tls12 b/lib/zig-tls12 deleted file mode 160000 index 56153d0..0000000 --- a/lib/zig-tls12 +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 56153d0f9f4551c7031b3b536845e90d23250d01 diff --git a/lib/zmpl b/lib/zmpl deleted file mode 160000 index a128d0a..0000000 --- a/lib/zmpl +++ /dev/null @@ -1 +0,0 @@ -Subproject commit a128d0a9c9cd70ca9f007405ae3d17e3a074ab34 diff --git a/src/main.zig b/src/test.zig similarity index 68% rename from src/main.zig rename to src/test.zig index 0036e94..6a00f47 100644 --- a/src/main.zig +++ b/src/test.zig @@ -1,9 +1,8 @@ -const Session = @import("shard.zig"); -const Discord = @import("types.zig"); +const Shard = @import("discord.zig").Shard; +const Discord = @import("discord.zig").Discord; const Intents = Discord.Intents; -const std = @import("std"); const Thread = std.Thread; -const token = "Bot MTI5ODgzOTgzMDY3OTEzMDE4OA.GNojts.iyblGKK0xTWU57QCG5n3hr2Be1whyylTGr44P0"; +const std = @import("std"); fn ready(payload: Discord.Ready) void { std.debug.print("logged in as {s}\n", .{payload.user.username}); @@ -18,10 +17,12 @@ pub fn main() !void { defer arena.deinit(); const allocator = arena.allocator(); - var handler = try Session.init(allocator, .{ + const token = std.posix.getenv("TOKEN") orelse unreachable; + + var handler = try Shard.init(allocator, .{ .token = token, .intents = Intents.fromRaw(37379), - .run = Session.GatewayDispatchEvent{ + .run = Shard.GatewayDispatchEvent{ .message_create = &message_create, .ready = &ready, }, @@ -29,6 +30,6 @@ pub fn main() !void { }); errdefer handler.deinit(); - const t = try Thread.spawn(.{}, Session.readMessage, .{ &handler, null }); + const t = try Thread.spawn(.{}, Shard.readMessage, .{ &handler, null }); defer t.join(); }