Skip to content

Commit 30b3de4

Browse files
committed
Change phase setup
1 parent d5267ab commit 30b3de4

File tree

7 files changed

+71
-111
lines changed

7 files changed

+71
-111
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ cargo install runscript
3636

3737
Licensed under either of
3838

39-
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
40-
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
39+
- Apache License, Version 2.0, (LICENSE-APACHE or <http://www.apache.org/licenses/LICENSE-2.0>)
40+
- MIT license (LICENSE-MIT or <http://opensource.org/licenses/MIT>)
4141

4242
at your option.
4343

4444
### Contribution
4545

46-
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
46+
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

src/exec.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ struct ShellContext {
471471
/// PID of most recent job
472472
pid: i32,
473473
/// Exit code of most recent top-level command
474-
exit_code: i32, //TODO: This will always be 0..
474+
exit_code: i32, //TODO: This will always be 0...
475475
}
476476

477477
impl ShellContext {

src/main.rs

+37-70
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,6 @@ you ran it on.
9393
}
9494

9595
fn main() {
96-
std::panic::set_hook(Box::new(panic_hook));
97-
9896
let mut args = env::args().skip(1).collect::<Vec<String>>();
9997
loop {
10098
match std::panic::catch_unwind(|| {
@@ -115,42 +113,33 @@ fn main() {
115113
}
116114

117115
pub fn run(args: &[String]) -> ExitCode {
118-
let output_stream = Arc::new(StandardStream::stderr(ColorChoice::Auto));
119-
120116
let mut app = clap::App::new("run")
121117
.about("Project script manager and executor")
122118
.version(VERSION)
123119
.author("TheOnlyMrCat")
124120
.setting(clap::AppSettings::NoBinaryName)
125121
.setting(clap::AppSettings::TrailingVarArg)
126122
.setting(clap::AppSettings::DeriveDisplayOrder)
127-
.override_usage("run [OPTIONS] [TARGET] [ARGS]")
123+
.override_usage("run [OPTIONS] [TARGET:PHASE] [--] [ARGS]")
128124
.arg(arg!([target] "Target to run in the script").hide(true))
129125
.arg(arg!([args] ... "Arguments to pass to the script").hide(true))
130126
.arg(
131127
arg!(-c --command <COMMAND> "Execute a command")
132128
.required(false)
133129
.value_hint(ValueHint::CommandString)
134-
.conflicts_with_all(&["file", "list", "phase", "target", "args"])
130+
.conflicts_with_all(&["file", "list", "target", "args", "build", "run", "test"])
135131
)
136132
.arg(arg!(-f --file <FILE> "Explicitly specify a script file to run").required(false).value_hint(ValueHint::FilePath))
137-
.arg(arg!(-l --list "List targets in the script file"))
138-
.help_heading("PHASE SELECTION")
139-
.arg(
140-
arg!(-p --phase <PHASE> "Run a specific phase of the script")
141-
.required(false)
142-
.use_delimiter(true)
143-
.default_value_if("build", None, Some("build"))
144-
.default_value_if("run", None, Some("run"))
145-
.conflicts_with("list")
146-
)
133+
.arg(arg!(-l --list "List targets in the script file").conflicts_with_all(&["build", "run", "test"]))
134+
.arg(arg!(--color <WHEN>).possible_values(&["auto", "always", "ansi", "never"]).required(false).default_value("auto"))
147135
.arg(arg!(-b --build "Shorthand for `--phase build`"))
148-
.arg(arg!(-r --run "Shorthand for `--phase run`"));
136+
.arg(arg!(-r --run "Shorthand for `--phase run`"))
137+
.arg(arg!(-t --test "Shorthand for `--phase test`"));
149138

150139
let options = match app.try_get_matches_from_mut(args) {
151140
Ok(m) => m,
152141
Err(clap::Error { kind: clap::ErrorKind::DisplayHelp, .. }) => {
153-
app.print_help().expect("Failed to write clap help");
142+
app.print_help().expect("Failed to print clap help");
154143
return exitcode::OK;
155144
}
156145
Err(clap::Error { kind: clap::ErrorKind::DisplayVersion, .. }) => {
@@ -171,12 +160,12 @@ pub fn run(args: &[String]) -> ExitCode {
171160
config.set_default("colors.phases.enabled", true).unwrap();
172161
if std::env::var_os("\x52\x55\x4E\x53\x43\x52\x49\x50\x54\x5F\x54\x52\x41\x4E\x53").is_some() {
173162
config.set_default("colors.phases.build", 6).unwrap();
174-
config.set_default("colors.phases.exec", 7).unwrap();
175163
config.set_default("colors.phases.run", 13).unwrap();
164+
config.set_default("colors.phases.test", 7).unwrap();
176165
} else {
177166
config.set_default("colors.phases.build", 1).unwrap();
178-
config.set_default("colors.phases.exec", 2).unwrap();
179167
config.set_default("colors.phases.run", 4).unwrap();
168+
config.set_default("colors.phases.test", 2).unwrap();
180169
}
181170
config.set_default("dev.panic", false).unwrap();
182171

@@ -195,7 +184,7 @@ pub fn run(args: &[String]) -> ExitCode {
195184
} else {
196185
#[cfg(unix)]
197186
{
198-
config_dir = Some(PathBuf::from("~/.config/runscript"));
187+
config_dir = None;
199188
}
200189
#[cfg(windows)]
201190
unsafe {
@@ -226,8 +215,10 @@ pub fn run(args: &[String]) -> ExitCode {
226215
config
227216
};
228217

229-
if config.get_bool("dev.panic").unwrap_or(false) {
230-
let _ = std::panic::take_hook();
218+
let output_stream = Arc::new(StandardStream::stderr(ColorChoice::Auto));
219+
220+
if !config.get_bool("dev.panic").unwrap_or(false) {
221+
std::panic::set_hook(Box::new(panic_hook));
231222
}
232223

233224
let cwd = match env::current_dir() {
@@ -326,59 +317,34 @@ pub fn run(args: &[String]) -> ExitCode {
326317
positional_args: options.values_of("args").into_iter().flatten().map(ToOwned::to_owned).collect(),
327318
};
328319

329-
let target = options.value_of("target");
320+
let target = options.value_of("target").unwrap_or("");
321+
let (target, phase) = target.split_once(':').unwrap_or((target, "run"));
330322

331-
match match &target {
332-
Some(target) => rf.get_target(target),
333-
None => rf.get_target(""),
323+
match if target.is_empty() {
324+
rf.get_default_target()
325+
} else {
326+
rf.get_target(target)
334327
} {
335-
Some(target_scripts) => {
336-
let phase = options.value_of("phase").unwrap_or("exec");
337-
if let Some(script) = target_scripts.get(phase) {
328+
Some((name, scripts)) => {
329+
if let Some(script) = scripts.get(phase) {
338330
out::phase_message(
339331
&output_stream,
340332
&config,
341333
phase,
342-
target.unwrap_or("default"),
334+
name,
343335
);
344336
exec_script(script, &exec_cfg).unwrap();
345337
exitcode::OK
346-
} else if phase == "exec" {
347-
let build_script = target_scripts.get("build");
348-
let run_script = target_scripts.get("run");
349-
if build_script.is_none() && run_script.is_none() {
350-
out::bad_script_phase(&output_stream);
351-
exitcode::NOINPUT
352-
} else {
353-
if let Some(script) = build_script {
354-
out::phase_message(
355-
&output_stream,
356-
&config,
357-
"build",
358-
target.unwrap_or("default"),
359-
);
360-
exec_script(script, &exec_cfg).unwrap();
361-
}
362-
if let Some(script) = run_script {
363-
out::phase_message(
364-
&output_stream,
365-
&config,
366-
"run",
367-
target.unwrap_or("default"),
368-
);
369-
exec_script(script, &exec_cfg).unwrap();
370-
}
371-
exitcode::OK
372-
}
373338
} else {
374339
out::bad_script_phase(&output_stream);
375340
exitcode::NOINPUT
376341
}
377342
}
378343
None => {
379-
match target {
380-
Some(name) => out::bad_target(&output_stream, name),
381-
None => out::bad_default(&output_stream),
344+
if target.is_empty() {
345+
out::bad_default(&output_stream);
346+
} else {
347+
out::bad_target(&output_stream, target);
382348
}
383349
exitcode::NOINPUT
384350
}
@@ -468,6 +434,7 @@ fn print_phase_list(
468434
name_length
469435
)
470436
.expect("Failed to write");
437+
//TODO: This could be configured
471438
if target.contains_key("build") {
472439
lock.set_color(
473440
ColorSpec::new()
@@ -487,47 +454,47 @@ fn print_phase_list(
487454
.expect("Failed to set colour");
488455
write!(lock, ".").unwrap();
489456
}
490-
if target.contains_key("exec") {
457+
if target.contains_key("run") {
491458
lock.set_color(
492459
ColorSpec::new()
493460
.set_bold(true)
494461
.set_intense(true)
495-
.set_fg(Some(out::phase_color(config, "exec"))),
462+
.set_fg(Some(out::phase_color(config, "run"))),
496463
)
497464
.expect("Failed to set colour");
498-
write!(lock, "&").unwrap();
465+
write!(lock, "R").unwrap();
499466
} else {
500467
lock.set_color(
501468
ColorSpec::new()
502469
.set_bold(false)
503470
.set_intense(false)
504-
.set_fg(Some(out::phase_color(config, "exec"))),
471+
.set_fg(Some(out::phase_color(config, "run"))),
505472
)
506473
.expect("Failed to set colour");
507474
write!(lock, ".").unwrap();
508475
}
509-
if target.contains_key("run") {
476+
if target.contains_key("test") {
510477
lock.set_color(
511478
ColorSpec::new()
512479
.set_bold(true)
513480
.set_intense(true)
514-
.set_fg(Some(out::phase_color(config, "run"))),
481+
.set_fg(Some(out::phase_color(config, "test"))),
515482
)
516483
.expect("Failed to set colour");
517-
write!(lock, "R").unwrap();
484+
write!(lock, "T").unwrap();
518485
} else {
519486
lock.set_color(
520487
ColorSpec::new()
521488
.set_bold(false)
522489
.set_intense(false)
523-
.set_fg(Some(out::phase_color(config, "run"))),
490+
.set_fg(Some(out::phase_color(config, "test"))),
524491
)
525492
.expect("Failed to set colour");
526493
write!(lock, ".").unwrap();
527494
}
528495

529496
for phase in target.keys() {
530-
if phase == "exec" || phase == "build" || phase == "run" {
497+
if phase == "test" || phase == "build" || phase == "run" {
531498
continue;
532499
}
533500
lock.set_color(

src/out.rs

+9-26
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,11 @@ pub fn process_finish(status: &crate::exec::ProcessExit) {
407407
77 => "exit 77 (EX_NOPERM)".to_owned(),
408408
78 => "exit 78 (EX_CONFIG)".to_owned(),
409409
101 => "exit 101 (Process panicked)".to_owned(),
410-
_ => format!("exit {}", code),
410+
_ => format!("exit {code}"),
411411
}
412412
}
413413

414-
fn signal(signal: i32, core: Option<bool>) -> String {
414+
fn signal(signal: i32, core: bool) -> String {
415415
use std::ffi::CStr;
416416

417417
// SAFETY: No input is invalid.
@@ -423,29 +423,12 @@ pub fn process_finish(status: &crate::exec::ProcessExit) {
423423
// SAFETY: The returned string is valid until the next call to strsignal, and has been verified to be non-null.
424424
let sigstr = unsafe { CStr::from_ptr(sigstr_ptr) };
425425
format!(
426-
"signal {} ({}{})",
427-
signal,
426+
"signal {signal} ({}){}",
428427
sigstr.to_string_lossy(),
429-
match core {
430-
Some(true) => " - core dumped",
431-
Some(false) => "",
432-
None => {
433-
// Signals that core dump by default.
434-
if signal == 3
435-
|| signal == 4
436-
|| signal == 5
437-
|| signal == 6
438-
|| signal == 7
439-
|| signal == 8
440-
|| signal == 10
441-
|| signal == 11
442-
|| signal == 12
443-
{
444-
" - core dumped?"
445-
} else {
446-
""
447-
}
448-
}
428+
if core {
429+
" - core dumped"
430+
} else {
431+
""
449432
}
450433
)
451434
}
@@ -464,7 +447,7 @@ pub fn process_finish(status: &crate::exec::ProcessExit) {
464447
} else {
465448
use std::os::unix::process::ExitStatusExt;
466449

467-
println!("=> {}", signal(status.signal().unwrap(), None));
450+
println!("=> {}", signal(status.signal().unwrap(), status.core_dumped()));
468451
}
469452
}
470453
}
@@ -475,7 +458,7 @@ pub fn process_finish(status: &crate::exec::ProcessExit) {
475458
}
476459
}
477460
nix::sys::wait::WaitStatus::Signaled(_, sig, core) => {
478-
signal((*sig) as i32, Some(*core));
461+
signal((*sig) as i32, *core);
479462
}
480463
_ => println!("=> exit ?"),
481464
},

src/parser.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ fn parse_root<T: Iterator<Item = (usize, char)> + std::fmt::Debug>(
155155
location: context.get_loc(0),
156156
};
157157
let mut current_script_target = "".to_owned();
158-
let mut current_script_phase = "exec".to_owned();
158+
let mut current_script_phase = "run".to_owned();
159159
while let Some(tk) = context.iterator.peek().copied() {
160160
match tk {
161161
// Comment
@@ -173,9 +173,12 @@ fn parse_root<T: Iterator<Item = (usize, char)> + std::fmt::Debug>(
173173
.collect::<String>();
174174
let phase = name
175175
.rfind(':')
176-
.map(|idx| name.split_off(idx + 1))
177-
.unwrap_or_else(|| "exec".to_owned());
178-
name.pop();
176+
.map(|idx| {
177+
let phase = name.split_off(idx + 1);
178+
name.pop(); // To remove the `:` as well
179+
phase
180+
})
181+
.unwrap_or_else(|| "run".to_owned());
179182
(name, phase)
180183
};
181184

src/script.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,17 @@ impl Runscript {
5656
}
5757
}
5858

59-
pub fn get_target(&self, target: &str) -> Option<&HashMap<String, Script>> {
59+
pub fn get_default_target(&self) -> Option<(&String, &HashMap<String, Script>)> {
6060
self
6161
.scripts
6262
.targets
63-
.get(target)
63+
.get_index(0)
64+
}
65+
66+
pub fn get_target(&self, target: &str) -> Option<(&String, &HashMap<String, Script>)> {
67+
self
68+
.scripts
69+
.targets
70+
.get_key_value(target)
6471
}
6572
}

0 commit comments

Comments
 (0)