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 =...@@ -397,9 +397,11 @@ const usage_build_generic =
397397
398const repl_help =398const repl_help =
399 \\Commands:399 \\Commands:
400 \\ update Detect changes to source files and update output files.400 \\ update Detect changes to source files and update output files.
401 \\ help Print this text401 \\ run Execute the output file, if it is an executable or test.
402 \\ exit Quit this repl402 \\ update-and-run Perform an `update` followed by `run`.
403 \\ help Print this text
404 \\ exit Quit this repl
403 \\405 \\
404;406;
405407
...@@ -1990,6 +1992,15 @@ fn buildOutputType(...@@ -1990,6 +1992,15 @@ fn buildOutputType(
1990 const stderr = std.io.getStdErr().writer();1992 const stderr = std.io.getStdErr().writer();
1991 var repl_buf: [1024]u8 = undefined;1993 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
1993 while (watch) {2004 while (watch) {
1994 try stderr.print("(zig) ", .{});2005 try stderr.print("(zig) ", .{});
1995 try comp.makeBinFileExecutable();2006 try comp.makeBinFileExecutable();
...@@ -1998,36 +2009,78 @@ fn buildOutputType(...@@ -1998,36 +2009,78 @@ fn buildOutputType(
1998 continue;2009 continue;
1999 }) |line| {2010 }) |line| {
2000 const actual_line = mem.trimRight(u8, line, "\r\n ");2011 const actual_line = mem.trimRight(u8, line, "\r\n ");
20012012 const cmd: ReplCmd = blk: {
2002 if (mem.eql(u8, actual_line, "update")) {2013 if (mem.eql(u8, actual_line, "update")) {
2003 if (output_mode == .Exe) {2014 break :blk .update;
2004 try comp.makeBinFileWritable();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;
2005 }2028 }
2006 updateModule(gpa, comp, hook) catch |err| switch (err) {2029 };
2007 error.SemanticAnalyzeFail => continue,2030 last_cmd = cmd;
2008 else => |e| return e,2031 switch (cmd) {
2009 };2032 .update => {
2010 } else if (mem.eql(u8, actual_line, "exit")) {2033 if (output_mode == .Exe) {
2011 break;2034 try comp.makeBinFileWritable();
2012 } else if (mem.eql(u8, actual_line, "help")) {2035 }
2013 try stderr.writeAll(repl_help);2036 updateModule(gpa, comp, hook) catch |err| switch (err) {
2014 } else if (mem.eql(u8, actual_line, "run")) {2037 error.SemanticAnalyzeFail => continue,
2015 try runOrTest(2038 else => |e| return e,
2016 comp,2039 };
2017 gpa,2040 },
2018 arena,2041 .help => {
2019 emit_bin_loc,2042 try stderr.writeAll(repl_help);
2020 test_exec_args.items,2043 },
2021 self_exe_path,2044 .run => {
2022 arg_mode,2045 try runOrTest(
2023 target_info.target,2046 comp,
2024 watch,2047 gpa,
2025 &comp_destroyed,2048 arena,
2026 all_args,2049 emit_bin_loc,
2027 runtime_args_start,2050 test_exec_args.items,
2028 );2051 self_exe_path,
2029 } else {2052 arg_mode,
2030 try stderr.print("unknown command: {s}\n", .{actual_line});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 },
2031 }2084 }
2032 } else {2085 } else {
2033 break;2086 break;