Skip to content

Commit 7d8bac7

Browse files
implement a shell of the cli
1 parent 90db3b2 commit 7d8bac7

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed

build.zig

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const std = @import("std");
2+
3+
pub fn build(b: *std.Build) void {
4+
const target = b.standardTargetOptions(.{});
5+
const optimize = b.standardOptimizeOption(.{});
6+
7+
const exe = b.addExecutable(.{
8+
.name = "roc",
9+
.root_source_file = b.path("src/main.zig"),
10+
.target = target,
11+
.optimize = optimize,
12+
});
13+
14+
b.installArtifact(exe);
15+
16+
const run_cmd = b.addRunArtifact(exe);
17+
18+
run_cmd.step.dependOn(b.getInstallStep());
19+
20+
if (b.args) |args| {
21+
run_cmd.addArgs(args);
22+
}
23+
24+
const run_step = b.step("run", "Build and run the roc cli");
25+
run_step.dependOn(&run_cmd.step);
26+
}

build.zig.zon

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.{
2+
.name = "roc",
3+
.version = "0.0.0",
4+
.minimum_zig_version = "0.13.0",
5+
.dependencies = .{},
6+
.paths = .{
7+
"build.zig",
8+
"build.zig.zon",
9+
"src",
10+
"LICENSE",
11+
"LEGAL_DETAILS",
12+
},
13+
}

src/main.zig

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
}

test.roc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
app [main!] { pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.19.0/Hj-J_zxz7V9YurCSTFcFdu6cQJie4guzsPMUi5kBYUk.tar.br" }
2+
3+
main! = |_|
4+
if Bool.true then
5+
return Err(Exit(1,"This is a test error message"))
6+
else
7+
Ok({})

0 commit comments

Comments
 (0)