|
| 1 | +const std = @import("std"); |
| 2 | +const mem = std.mem; |
| 3 | +const Allocator = std.mem.Allocator; |
| 4 | + |
| 5 | +const usage = |
| 6 | + \\Usage: |
| 7 | + \\ |
| 8 | + \\ roc [options] [roc_file] [args] |
| 9 | + \\ roc [command] [options] |
| 10 | + \\ |
| 11 | + \\Commands: |
| 12 | + \\ |
| 13 | + \\ build Build a binary from the given .roc file, but don't run it |
| 14 | + \\ test Run all top-level `expect`s in a main module and any modules it imports |
| 15 | + \\ repl Launch the interactive Read Eval Print Loop (REPL) |
| 16 | + \\ format Format a .roc file or the .roc files contained in a directory using standard Roc formatting |
| 17 | + \\ version Print the Roc compiler’s version, which is currently built from commit 90db3b2db0, committed at 2025-01-28 18:26:51 UTC |
| 18 | + \\ check Check the code for problems, but don’t build or run it |
| 19 | + \\ docs Generate documentation for a Roc package |
| 20 | + \\ glue Generate glue code between a platform's Roc API and its host language |
| 21 | + \\ |
| 22 | + \\General Options: |
| 23 | + \\ |
| 24 | + \\ -h, --help Print command-specific usage |
| 25 | +; |
| 26 | + |
| 27 | +pub fn fatal(comptime format: []const u8, args: anytype) noreturn { |
| 28 | + std.log.err(format, args); |
| 29 | + std.process.exit(1); |
| 30 | +} |
| 31 | + |
| 32 | +pub fn log( |
| 33 | + comptime level: std.log.Level, |
| 34 | + comptime format: []const u8, |
| 35 | + args: anytype, |
| 36 | +) void { |
| 37 | + const prefix = comptime level.asText(); |
| 38 | + |
| 39 | + // Print the message to stderr, silently ignoring any errors |
| 40 | + std.debug.print(prefix ++ ": " ++ format ++ "\n", args); |
| 41 | +} |
| 42 | + |
| 43 | +pub fn main() !void { |
| 44 | + var general_purpose_allocator = std.heap.GeneralPurposeAllocator(.{}){}; |
| 45 | + defer { |
| 46 | + _ = general_purpose_allocator.deinit(); |
| 47 | + } |
| 48 | + const gpa = general_purpose_allocator.allocator(); |
| 49 | + |
| 50 | + var arena_instance = std.heap.ArenaAllocator.init(gpa); |
| 51 | + defer arena_instance.deinit(); |
| 52 | + const arena = arena_instance.allocator(); |
| 53 | + |
| 54 | + const args = try std.process.argsAlloc(arena); |
| 55 | + |
| 56 | + return mainArgs(gpa, arena, args); |
| 57 | +} |
| 58 | + |
| 59 | +fn mainArgs(gpa: Allocator, arena: Allocator, args: []const []const u8) !void { |
| 60 | + _ = gpa; |
| 61 | + _ = arena; |
| 62 | + |
| 63 | + if (args.len <= 1) { |
| 64 | + std.log.info("{s}", .{usage}); |
| 65 | + fatal("expected command argument", .{}); |
| 66 | + } |
| 67 | + |
| 68 | + const cmd = args[1]; |
| 69 | + // const cmd_args = args[2..]; |
| 70 | + if (mem.eql(u8, cmd, "build")) { |
| 71 | + log(.info, "TODO roc build", .{}); |
| 72 | + } else if (mem.eql(u8, cmd, "test")) { |
| 73 | + log(.info, "TODO roc test", .{}); |
| 74 | + } else if (mem.eql(u8, cmd, "repl")) { |
| 75 | + log(.info, "TODO roc repl", .{}); |
| 76 | + } else if (mem.eql(u8, cmd, "format")) { |
| 77 | + log(.info, "TODO roc format", .{}); |
| 78 | + } else if (mem.eql(u8, cmd, "version")) { |
| 79 | + log(.info, "TODO roc version", .{}); |
| 80 | + } else if (mem.eql(u8, cmd, "check")) { |
| 81 | + log(.info, "TODO roc check", .{}); |
| 82 | + } else if (mem.eql(u8, cmd, "docs")) { |
| 83 | + log(.info, "TODO roc docs", .{}); |
| 84 | + } else if (mem.eql(u8, cmd, "glue")) { |
| 85 | + log(.info, "TODO roc glue", .{}); |
| 86 | + } else if (mem.eql(u8, cmd, "help") or mem.eql(u8, cmd, "-h") or mem.eql(u8, cmd, "--help")) { |
| 87 | + return std.io.getStdOut().writeAll(usage); |
| 88 | + } |
| 89 | + |
| 90 | + fatal("subcommand not yet implemented", .{}); |
| 91 | +} |
0 commit comments