authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-06 17:35:21-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-06 17:35:21-07:00
log9b9ea405ef1d11af377057067118b07a021b990f
treec940510196504e966edfa4d7d9e30750e3b1b3df
parentcdea22f5d7f1ef7a44cf871eeafab3383495ab6c

CLI: add an update-and-run cmd and make enter re-run last cmd


1 files changed, 85 insertions(+), 32 deletions(-)

src/main.zig+85-32
......@@ -397,9 +397,11 @@ const usage_build_generic =
397397
398398const repl_help =
399399 \\Commands:
400 \\ update Detect changes to source files and update output files.
401 \\ help Print this text
402 \\ exit Quit this repl
400 \\ update Detect changes to source files and update output files.
401 \\ run Execute the output file, if it is an executable or test.
402 \\ update-and-run Perform an `update` followed by `run`.
403 \\ help Print this text
404 \\ exit Quit this repl
403405 \\
404406;
405407
......@@ -1990,6 +1992,15 @@ fn buildOutputType(
19901992 const stderr = std.io.getStdErr().writer();
19911993 var repl_buf: [1024]u8 = undefined;
19921994
1995 const ReplCmd = enum {
1996 update,
1997 help,
1998 run,
1999 update_and_run,
2000 };
2001
2002 var last_cmd: ReplCmd = .help;
2003
19932004 while (watch) {
19942005 try stderr.print("(zig) ", .{});
19952006 try comp.makeBinFileExecutable();
......@@ -1998,36 +2009,78 @@ fn buildOutputType(
19982009 continue;
19992010 }) |line| {
20002011 const actual_line = mem.trimRight(u8, line, "\r\n ");
2001
2002 if (mem.eql(u8, actual_line, "update")) {
2003 if (output_mode == .Exe) {
2004 try comp.makeBinFileWritable();
2012 const cmd: ReplCmd = blk: {
2013 if (mem.eql(u8, actual_line, "update")) {
2014 break :blk .update;
2015 } else if (mem.eql(u8, actual_line, "exit")) {
2016 break;
2017 } else if (mem.eql(u8, actual_line, "help")) {
2018 break :blk .help;
2019 } else if (mem.eql(u8, actual_line, "run")) {
2020 break :blk .run;
2021 } else if (mem.eql(u8, actual_line, "update-and-run")) {
2022 break :blk .update_and_run;
2023 } else if (actual_line.len == 0) {
2024 break :blk last_cmd;
2025 } else {
2026 try stderr.print("unknown command: {s}\n", .{actual_line});
2027 continue;
20052028 }
2006 updateModule(gpa, comp, hook) catch |err| switch (err) {
2007 error.SemanticAnalyzeFail => continue,
2008 else => |e| return e,
2009 };
2010 } else if (mem.eql(u8, actual_line, "exit")) {
2011 break;
2012 } else if (mem.eql(u8, actual_line, "help")) {
2013 try stderr.writeAll(repl_help);
2014 } else if (mem.eql(u8, actual_line, "run")) {
2015 try runOrTest(
2016 comp,
2017 gpa,
2018 arena,
2019 emit_bin_loc,
2020 test_exec_args.items,
2021 self_exe_path,
2022 arg_mode,
2023 target_info.target,
2024 watch,
2025 &comp_destroyed,
2026 all_args,
2027 runtime_args_start,
2028 );
2029 } else {
2030 try stderr.print("unknown command: {s}\n", .{actual_line});
2029 };
2030 last_cmd = cmd;
2031 switch (cmd) {
2032 .update => {
2033 if (output_mode == .Exe) {
2034 try comp.makeBinFileWritable();
2035 }
2036 updateModule(gpa, comp, hook) catch |err| switch (err) {
2037 error.SemanticAnalyzeFail => continue,
2038 else => |e| return e,
2039 };
2040 },
2041 .help => {
2042 try stderr.writeAll(repl_help);
2043 },
2044 .run => {
2045 try runOrTest(
2046 comp,
2047 gpa,
2048 arena,
2049 emit_bin_loc,
2050 test_exec_args.items,
2051 self_exe_path,
2052 arg_mode,
2053 target_info.target,
2054 watch,
2055 &comp_destroyed,
2056 all_args,
2057 runtime_args_start,
2058 );
2059 },
2060 .update_and_run => {
2061 if (output_mode == .Exe) {
2062 try comp.makeBinFileWritable();
2063 }
2064 updateModule(gpa, comp, hook) catch |err| switch (err) {
2065 error.SemanticAnalyzeFail => continue,
2066 else => |e| return e,
2067 };
2068 try comp.makeBinFileExecutable();
2069 try runOrTest(
2070 comp,
2071 gpa,
2072 arena,
2073 emit_bin_loc,
2074 test_exec_args.items,
2075 self_exe_path,
2076 arg_mode,
2077 target_info.target,
2078 watch,
2079 &comp_destroyed,
2080 all_args,
2081 runtime_args_start,
2082 );
2083 },
20312084 }
20322085 } else {
20332086 break;