authorgravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2024-04-06 15:18:43-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-04-06 15:18:43-04:00
log9ab6d9106787e76ecba817b40c804be38aa6cfe7
treed1e616d24338563e4a8049800a73cc8027604c39
parent637b1d606d52ed2984013bfd1e196934b08de4a9
signaturebadge-check Signed by PGP key B5690EEEBB952194

zig std accepts --port and --no-open-browser (#19559)


1 files changed, 50 insertions(+), 10 deletions(-)

lib/compiler/std-docs.zig+50-10
...@@ -1,8 +1,24 @@...@@ -1,8 +1,24 @@
1const builtin = @import("builtin");1const builtin = @import("builtin");
2const std = @import("std");2const std = @import("std");
3const mem = std.mem;
4const io = std.io;
3const Allocator = std.mem.Allocator;5const Allocator = std.mem.Allocator;
4const assert = std.debug.assert;6const assert = std.debug.assert;
57
8fn usage() noreturn {
9 io.getStdOut().writeAll(
10 \\Usage: zig std [options]
11 \\
12 \\Options:
13 \\ -h, --help Print this help and exit
14 \\ -p [port], --port [port] Port to listen on. Default is 0, meaning an ephemeral port chosen by the system.
15 \\ --[no-]open-browser Force enabling or disabling opening a browser tab to the served website.
16 \\ By default, enabled unless a port is specified.
17 \\
18 ) catch {};
19 std.process.exit(1);
20}
21
6pub fn main() !void {22pub fn main() !void {
7 var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator);23 var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator);
8 defer arena_instance.deinit();24 defer arena_instance.deinit();
...@@ -11,23 +27,47 @@ pub fn main() !void {...@@ -11,23 +27,47 @@ pub fn main() !void {
11 var general_purpose_allocator: std.heap.GeneralPurposeAllocator(.{}) = .{};27 var general_purpose_allocator: std.heap.GeneralPurposeAllocator(.{}) = .{};
12 const gpa = general_purpose_allocator.allocator();28 const gpa = general_purpose_allocator.allocator();
1329
14 const args = try std.process.argsAlloc(arena);30 var argv = try std.process.argsWithAllocator(arena);
15 const zig_lib_directory = args[1];31 defer argv.deinit();
16 const zig_exe_path = args[2];32 assert(argv.skip());
17 const global_cache_path = args[3];33 const zig_lib_directory = argv.next().?;
34 const zig_exe_path = argv.next().?;
35 const global_cache_path = argv.next().?;
1836
19 var lib_dir = try std.fs.cwd().openDir(zig_lib_directory, .{});37 var lib_dir = try std.fs.cwd().openDir(zig_lib_directory, .{});
20 defer lib_dir.close();38 defer lib_dir.close();
2139
22 const listen_port: u16 = 0;40 var listen_port: u16 = 0;
41 var force_open_browser: ?bool = null;
42 while (argv.next()) |arg| {
43 if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) {
44 usage();
45 } else if (mem.eql(u8, arg, "-p") or mem.eql(u8, arg, "--port")) {
46 listen_port = std.fmt.parseInt(u16, argv.next() orelse usage(), 10) catch |err| {
47 std.log.err("expected port number: {}", .{err});
48 usage();
49 };
50 } else if (mem.eql(u8, arg, "--open-browser")) {
51 force_open_browser = true;
52 } else if (mem.eql(u8, arg, "--no-open-browser")) {
53 force_open_browser = false;
54 } else {
55 std.log.err("unrecognized argument: {s}", .{arg});
56 usage();
57 }
58 }
59 const should_open_browser = force_open_browser orelse (listen_port == 0);
60
23 const address = std.net.Address.parseIp("127.0.0.1", listen_port) catch unreachable;61 const address = std.net.Address.parseIp("127.0.0.1", listen_port) catch unreachable;
24 var http_server = try address.listen(.{});62 var http_server = try address.listen(.{});
25 const port = http_server.listen_address.in.getPort();63 const port = http_server.listen_address.in.getPort();
26 const url = try std.fmt.allocPrint(arena, "http://127.0.0.1:{d}/\n", .{port});64 const url_with_newline = try std.fmt.allocPrint(arena, "http://127.0.0.1:{d}/\n", .{port});
27 std.io.getStdOut().writeAll(url) catch {};65 std.io.getStdOut().writeAll(url_with_newline) catch {};
28 openBrowserTab(gpa, url[0 .. url.len - 1 :'\n']) catch |err| {66 if (should_open_browser) {
29 std.log.err("unable to open browser: {s}", .{@errorName(err)});67 openBrowserTab(gpa, url_with_newline[0 .. url_with_newline.len - 1 :'\n']) catch |err| {
30 };68 std.log.err("unable to open browser: {s}", .{@errorName(err)});
69 };
70 }
3171
32 var context: Context = .{72 var context: Context = .{
33 .gpa = gpa,73 .gpa = gpa,