-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmain.zig
More file actions
109 lines (88 loc) · 3.99 KB
/
main.zig
File metadata and controls
109 lines (88 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
pub fn main() !void {
var dbg = std.heap.DebugAllocator(.{}).init;
const allocator = switch (builtin.os.tag) {
.wasi, .freestanding => std.heap.wasm_allocator,
else => switch (builtin.mode) {
.Debug => dbg.allocator(),
.ReleaseFast, .ReleaseSafe, .ReleaseSmall => std.heap.smp_allocator,
},
};
defer if (builtin.mode == .Debug) std.debug.assert(dbg.deinit() == .ok);
if (comptime (!build_options.exclude_lsp)) {
var args = try std.process.argsWithAllocator(allocator);
defer args.deinit();
_ = args.next();
const subcmd = args.next();
if (std.mem.eql(u8, subcmd orelse "", "lsp")) return try lsp.main();
}
if (builtin.os.tag == .wasi) return try main_wasm();
if (builtin.os.tag == .windows) _ = std.os.windows.kernel32.SetConsoleOutputCP(65001);
var stdout_writer = std.fs.File.stdout().writerStreaming(&.{});
var stdout = &stdout_writer.interface;
var buf: [4096]u8 = undefined;
var stdin_reader = std.fs.File.stdin().readerStreaming(&buf);
const stdin = &stdin_reader.interface;
const root = try cli.build(stdout, stdin, allocator);
defer root.deinit();
root.execute(.{}) catch |err| {
const c = tui.Colors;
const err_name = @errorName(err);
const base_url = std.fmt.comptimePrint("{s}/issues/new", .{zx.info.repository});
var url_buf: [512]u8 = undefined;
const full_url = std.fmt.bufPrint(&url_buf, "{s}?title=CLI%20Error:%20{s}&body=**Error:**%20{s}%0A**Version:**%20{s}", .{
base_url,
err_name,
err_name,
zx.info.version,
}) catch base_url;
// OSC 8 hyperlink: \x1b]8;;URL\x07DISPLAY_TEXT\x1b]8;;\x07
std.debug.print("\n{s}An unexpected problem occurred while running ZX CLI.{s}\n", .{ c.red, c.reset });
std.debug.print("Please report it at {s}\x1b]8;;{s}\x07{s}\x1b]8;;\x07{s}\n", .{ c.cyan, full_url, base_url, c.reset });
std.debug.print("{s}Details: {s}{s}\n\n", .{ c.gray, err_name, c.reset });
};
try stdout.flush();
}
fn main_wasm() !void {
var dbg = std.heap.DebugAllocator(.{}).init;
const allocator = dbg.allocator();
var args = try std.process.argsWithAllocator(allocator);
defer args.deinit();
// --- Sub Command --- //
var is_transpile = false;
var is_fmt = false;
var is_lsp = false;
_ = args.next(); // Drop executable name
const sub_cmd = args.next() orelse return error.InvalidCommand;
if (std.mem.eql(u8, sub_cmd, "transpile")) is_transpile = true;
if (std.mem.eql(u8, sub_cmd, "fmt")) is_fmt = true;
if (std.mem.eql(u8, sub_cmd, "lsp")) is_lsp = true;
if (is_lsp) return try lsp.main();
var files = std.ArrayList([]const u8).empty;
while (args.next()) |arg| {
try files.append(allocator, arg);
}
var cwd = try std.fs.openDirAbsolute("/codes", .{});
defer cwd.close();
// Transpile/Fmt file_path.zx and write with file_path.zig
for (files.items) |file_path| {
const zx_source = try cwd.readFileAlloc(allocator, file_path, std.math.maxInt(usize));
const zx_sourcez = try allocator.dupeZ(u8, zx_source);
const ast = try zx.Ast.parse(allocator, zx_sourcez, .{});
const output = if (is_transpile) ast.zig_source else ast.zx_source;
try std.fs.File.stdout().writeAll(output);
}
}
const std = @import("std");
const builtin = @import("builtin");
const build_options = @import("build_options");
const zx = @import("zx");
const cli = @import("cli/root.zig");
const tui = @import("tui/main.zig");
const lsp = if (build_options.exclude_lsp) void else @import("lsp/main.zig");
pub const std_options = std.Options{
.log_scope_levels = &[_]std.log.ScopeLevel{
.{ .scope = .cli, .level = if (builtin.mode == .Debug) .info else .info },
.{ .scope = .devserver, .level = if (builtin.mode == .Debug) .info else .info },
.{ .scope = .builder, .level = if (builtin.mode == .Debug) .info else .info },
},
};