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 @@
11const builtin = @import("builtin");
22const std = @import("std");
3const mem = std.mem;
4const io = std.io;
35const Allocator = std.mem.Allocator;
46const 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
622pub fn main() !void {
723 var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator);
824 defer arena_instance.deinit();
......@@ -11,23 +27,47 @@ pub fn main() !void {
1127 var general_purpose_allocator: std.heap.GeneralPurposeAllocator(.{}) = .{};
1228 const gpa = general_purpose_allocator.allocator();
1329
14 const args = try std.process.argsAlloc(arena);
15 const zig_lib_directory = args[1];
16 const zig_exe_path = args[2];
17 const global_cache_path = args[3];
30 var argv = try std.process.argsWithAllocator(arena);
31 defer argv.deinit();
32 assert(argv.skip());
33 const zig_lib_directory = argv.next().?;
34 const zig_exe_path = argv.next().?;
35 const global_cache_path = argv.next().?;
1836
1937 var lib_dir = try std.fs.cwd().openDir(zig_lib_directory, .{});
2038 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
2361 const address = std.net.Address.parseIp("127.0.0.1", listen_port) catch unreachable;
2462 var http_server = try address.listen(.{});
2563 const port = http_server.listen_address.in.getPort();
26 const url = try std.fmt.allocPrint(arena, "http://127.0.0.1:{d}/\n", .{port});
27 std.io.getStdOut().writeAll(url) catch {};
28 openBrowserTab(gpa, url[0 .. url.len - 1 :'\n']) catch |err| {
29 std.log.err("unable to open browser: {s}", .{@errorName(err)});
30 };
64 const url_with_newline = try std.fmt.allocPrint(arena, "http://127.0.0.1:{d}/\n", .{port});
65 std.io.getStdOut().writeAll(url_with_newline) catch {};
66 if (should_open_browser) {
67 openBrowserTab(gpa, url_with_newline[0 .. url_with_newline.len - 1 :'\n']) catch |err| {
68 std.log.err("unable to open browser: {s}", .{@errorName(err)});
69 };
70 }
3171
3272 var context: Context = .{
3373 .gpa = gpa,