Skip to content

Commit

Permalink
Added last cmd exit_code support
Browse files Browse the repository at this point in the history
  • Loading branch information
prsabahrami committed Sep 9, 2024
1 parent af25f25 commit b8bcff4
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 2 deletions.
4 changes: 3 additions & 1 deletion crates/deno_task_shell/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ pub enum WordPart {
Quoted(Vec<WordPart>),
/// Tilde prefix (ex. `~user/path` or `~/bin`)
Tilde(TildePrefix),
/// Exit status of the last command (ex. `$?`)
ExitStatus,
}

#[cfg_attr(feature = "serialization", derive(serde::Serialize))]
Expand Down Expand Up @@ -704,7 +706,7 @@ fn parse_word(pair: Pair<Rule>) -> Result<Word> {
Rule::UNQUOTED_PENDING_WORD => {
for part in pair.into_inner() {
match part.as_rule() {
Rule::EXIT_STATUS => parts.push(WordPart::Variable("?".to_string())),
Rule::EXIT_STATUS => parts.push(WordPart::ExitStatus),
Rule::UNQUOTED_CHAR => {
if let Some(WordPart::Text(ref mut text)) = parts.last_mut() {
text.push(part.as_str().chars().next().unwrap());
Expand Down
5 changes: 5 additions & 0 deletions crates/deno_task_shell/src/shell/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,11 @@ fn evaluate_word_parts(
}
continue;
}
WordPart::ExitStatus => {
let exit_code = state.last_command_exit_code();
current_text.push(TextPart::Text(exit_code.to_string()));
continue;
}
};

// This text needs to be turned into a vector of strings.
Expand Down
10 changes: 10 additions & 0 deletions crates/deno_task_shell/src/shell/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub struct ShellState {
git_root: PathBuf, // Path to the root (`$git_root/.git/HEAD` exists)
git_branch: String, // Contents of `$git_root/.git/HEAD`
last_command_cd: bool, // Was last command a `cd` (thus git_branch is current)?
last_command_exit_code: i32, // Exit code of the last command
}

impl ShellState {
Expand All @@ -57,6 +58,7 @@ impl ShellState {
git_root: PathBuf::new(),
git_branch: String::new(),
last_command_cd: false,
last_command_exit_code: 0,
};
// ensure the data is normalized
for (name, value) in env_vars {
Expand Down Expand Up @@ -86,6 +88,14 @@ impl ShellState {
self.last_command_cd
}

pub fn set_last_command_exit_code(&mut self, exit_code: i32) {
self.last_command_exit_code = exit_code;
}

pub fn last_command_exit_code(&self) -> i32 {
self.last_command_exit_code
}

pub fn env_vars(&self) -> &HashMap<String, String> {
&self.env_vars
}
Expand Down
3 changes: 2 additions & 1 deletion crates/shell/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,10 @@ async fn interactive() -> anyhow::Result<()> {
rl.add_history_entry(line.as_str())?;

// Process the input (here we just echo it back)
_prev_exit_code = execute(&line, &mut state)
let prev_exit_code = execute(&line, &mut state)
.await
.context("Failed to execute")?;
state.set_last_command_exit_code(prev_exit_code);

// Check for exit command
if line.trim().eq_ignore_ascii_case("exit") {
Expand Down
1 change: 1 addition & 0 deletions scripts/exit_code.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
echo $?

0 comments on commit b8bcff4

Please sign in to comment.