Skip to content

Commit

Permalink
Add windows-shell setting (#1198)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored May 31, 2022
1 parent ccc54fb commit c24a194
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 57 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -671,9 +671,22 @@ foo:

`just` passes the command to be executed as an argument. Many shells will need an additional flag, often `-c`, to make them evaluate the first argument.

##### Windows Shell

`just` uses `sh` on Windows by default. To use a different shell on Windows, use `windows-shell`:

```make
set windows-shell := ["pwsh.exe", "-NoLogo", "-Command"]

hello:
Write-Host "Hello, world!"
```

##### Windows PowerShell

`just` uses `sh` on Windows by default. To use PowerShell instead, set `windows-powershell` to true.
*`set windows-powershell` uses the legacy `powershell.exe` binary, and is no longer recommended. See the `windows-shell` setting above for a more flexible way to control which shell is used on Windows.*

`just` uses `sh` on Windows by default. To use `powershell.exe` instead, set `windows-powershell` to true.

```make
set windows-powershell := true
Expand Down
4 changes: 3 additions & 1 deletion src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ impl<'src> Analyzer<'src> {
settings.positional_arguments = positional_arguments;
}
Setting::Shell(shell) => {
assert!(settings.shell.is_none());
settings.shell = Some(shell);
}
Setting::WindowsPowerShell(windows_powershell) => {
settings.windows_powershell = windows_powershell;
}
Setting::WindowsShell(windows_shell) => {
settings.windows_shell = Some(windows_shell);
}
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub(crate) use ::{
};

// modules
pub(crate) use crate::{completions, config, config_error, keyed, setting};
pub(crate) use crate::{completions, config, config_error, keyed};

// functions
pub(crate) use crate::{load_dotenv::load_dotenv, output::output, unindent::unindent};
Expand All @@ -64,11 +64,11 @@ pub(crate) use crate::{
parser::Parser, platform::Platform, position::Position, positional::Positional, recipe::Recipe,
recipe_context::RecipeContext, recipe_resolver::RecipeResolver, scope::Scope, search::Search,
search_config::SearchConfig, search_error::SearchError, set::Set, setting::Setting,
settings::Settings, shebang::Shebang, show_whitespace::ShowWhitespace, string_kind::StringKind,
string_literal::StringLiteral, subcommand::Subcommand, suggestion::Suggestion, table::Table,
thunk::Thunk, token::Token, token_kind::TokenKind, unresolved_dependency::UnresolvedDependency,
unresolved_recipe::UnresolvedRecipe, use_color::UseColor, variables::Variables,
verbosity::Verbosity, warning::Warning,
settings::Settings, shebang::Shebang, shell::Shell, show_whitespace::ShowWhitespace,
string_kind::StringKind, string_literal::StringLiteral, subcommand::Subcommand,
suggestion::Suggestion, table::Table, thunk::Thunk, token::Token, token_kind::TokenKind,
unresolved_dependency::UnresolvedDependency, unresolved_recipe::UnresolvedRecipe,
use_color::UseColor, variables::Variables, verbosity::Verbosity, warning::Warning,
};

// type aliases
Expand Down
1 change: 1 addition & 0 deletions src/keyword.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub(crate) enum Keyword {
Shell,
True,
WindowsPowershell,
WindowsShell,
}

impl Keyword {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ mod set;
mod setting;
mod settings;
mod shebang;
mod shell;
mod show_whitespace;
mod string_kind;
mod string_literal;
Expand Down
15 changes: 7 additions & 8 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,20 +215,19 @@ impl<'src> Node<'src> for Fragment<'src> {

impl<'src> Node<'src> for Set<'src> {
fn tree(&self) -> Tree<'src> {
use Setting::*;

let mut set = Tree::atom(Keyword::Set.lexeme());
set.push_mut(self.name.lexeme().replace('-', "_"));

match &self.value {
AllowDuplicateRecipes(value)
| DotenvLoad(value)
| Export(value)
| PositionalArguments(value)
| WindowsPowerShell(value) => {
Setting::AllowDuplicateRecipes(value)
| Setting::DotenvLoad(value)
| Setting::Export(value)
| Setting::PositionalArguments(value)
| Setting::WindowsPowerShell(value) => {
set.push_mut(value.to_string());
}
Shell(setting::Shell { command, arguments }) => {
Setting::Shell(Shell { command, arguments })
| Setting::WindowsShell(Shell { command, arguments }) => {
set.push_mut(Tree::string(&command.cooked));
for argument in arguments {
set.push_mut(Tree::string(&argument.cooked));
Expand Down
48 changes: 29 additions & 19 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -764,26 +764,13 @@ impl<'tokens, 'src> Parser<'tokens, 'src> {
self.expect(ColonEquals)?;

if name.lexeme() == Keyword::Shell.lexeme() {
self.expect(BracketL)?;

let command = self.parse_string_literal()?;

let mut arguments = Vec::new();

if self.accepted(Comma)? {
while !self.next_is(BracketR) {
arguments.push(self.parse_string_literal()?);

if !self.accepted(Comma)? {
break;
}
}
}

self.expect(BracketR)?;

Ok(Set {
value: Setting::Shell(setting::Shell { arguments, command }),
value: Setting::Shell(self.parse_shell()?),
name,
})
} else if name.lexeme() == Keyword::WindowsShell.lexeme() {
Ok(Set {
value: Setting::WindowsShell(self.parse_shell()?),
name,
})
} else {
Expand All @@ -792,6 +779,29 @@ impl<'tokens, 'src> Parser<'tokens, 'src> {
}))
}
}

/// Parse a shell setting value
fn parse_shell(&mut self) -> CompileResult<'src, Shell<'src>> {
self.expect(BracketL)?;

let command = self.parse_string_literal()?;

let mut arguments = Vec::new();

if self.accepted(Comma)? {
while !self.next_is(BracketR) {
arguments.push(self.parse_string_literal()?);

if !self.accepted(Comma)? {
break;
}
}
}

self.expect(BracketR)?;

Ok(Shell { arguments, command })
}
}

#[cfg(test)]
Expand Down
21 changes: 2 additions & 19 deletions src/setting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ pub(crate) enum Setting<'src> {
PositionalArguments(bool),
Shell(Shell<'src>),
WindowsPowerShell(bool),
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub(crate) struct Shell<'src> {
pub(crate) arguments: Vec<StringLiteral<'src>>,
pub(crate) command: StringLiteral<'src>,
WindowsShell(Shell<'src>),
}

impl<'src> Display for Setting<'src> {
Expand All @@ -24,19 +19,7 @@ impl<'src> Display for Setting<'src> {
| Setting::Export(value)
| Setting::PositionalArguments(value)
| Setting::WindowsPowerShell(value) => write!(f, "{}", value),
Setting::Shell(shell) => write!(f, "{}", shell),
Setting::Shell(shell) | Setting::WindowsShell(shell) => write!(f, "{}", shell),
}
}
}

impl<'src> Display for Shell<'src> {
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
write!(f, "[{}", self.command)?;

for argument in &self.arguments {
write!(f, ", {}", argument)?;
}

write!(f, "]")
}
}
14 changes: 11 additions & 3 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ pub(crate) struct Settings<'src> {
pub(crate) dotenv_load: Option<bool>,
pub(crate) export: bool,
pub(crate) positional_arguments: bool,
pub(crate) shell: Option<setting::Shell<'src>>,
pub(crate) shell: Option<Shell<'src>>,
pub(crate) windows_powershell: bool,
pub(crate) windows_shell: Option<Shell<'src>>,
}

impl<'src> Settings<'src> {
Expand All @@ -24,6 +25,7 @@ impl<'src> Settings<'src> {
positional_arguments: false,
shell: None,
windows_powershell: false,
windows_shell: None,
}
}

Expand All @@ -42,6 +44,8 @@ impl<'src> Settings<'src> {
shell.command.cooked.as_ref()
} else if let Some(shell) = &config.shell {
shell
} else if let (true, Some(shell)) = (cfg!(windows), &self.windows_shell) {
shell.command.cooked.as_ref()
} else if cfg!(windows) && self.windows_powershell {
WINDOWS_POWERSHELL_SHELL
} else {
Expand All @@ -60,6 +64,12 @@ impl<'src> Settings<'src> {
.collect()
} else if let Some(shell_args) = &config.shell_args {
shell_args.iter().map(String::as_ref).collect()
} else if let (true, Some(shell)) = (cfg!(windows), &self.windows_shell) {
shell
.arguments
.iter()
.map(|argument| argument.cooked.as_ref())
.collect()
} else if cfg!(windows) && self.windows_powershell {
WINDOWS_POWERSHELL_ARGS.to_vec()
} else {
Expand All @@ -70,8 +80,6 @@ impl<'src> Settings<'src> {

#[cfg(test)]
mod tests {
use crate::setting::Shell;

use super::*;

#[test]
Expand Down
19 changes: 19 additions & 0 deletions src/shell.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use crate::common::*;

#[derive(Debug, Clone, PartialEq, Serialize)]
pub(crate) struct Shell<'src> {
pub(crate) arguments: Vec<StringLiteral<'src>>,
pub(crate) command: StringLiteral<'src>,
}

impl<'src> Display for Shell<'src> {
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
write!(f, "[{}", self.command)?;

for argument in &self.arguments {
write!(f, ", {}", argument)?;
}

write!(f, "]")
}
}
15 changes: 15 additions & 0 deletions tests/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ fn alias() {
"positional_arguments": false,
"shell": null,
"windows_powershell": false,
"windows_shell": null,
},
"warnings": [],
}),
Expand Down Expand Up @@ -73,6 +74,7 @@ fn assignment() {
"positional_arguments": false,
"shell": null,
"windows_powershell": false,
"windows_shell": null,
},
"warnings": [],
}),
Expand Down Expand Up @@ -114,6 +116,7 @@ fn body() {
"positional_arguments": false,
"shell": null,
"windows_powershell": false,
"windows_shell": null,
},
"warnings": [],
}),
Expand Down Expand Up @@ -165,6 +168,7 @@ fn dependencies() {
"positional_arguments": false,
"shell": null,
"windows_powershell": false,
"windows_shell": null,
},
"warnings": [],
}),
Expand Down Expand Up @@ -253,6 +257,7 @@ fn dependency_argument() {
"positional_arguments": false,
"shell": null,
"windows_powershell": false,
"windows_shell": null,
},
"warnings": [],
}),
Expand Down Expand Up @@ -305,6 +310,7 @@ fn duplicate_recipes() {
"positional_arguments": false,
"shell": null,
"windows_powershell": false,
"windows_shell": null,
},
"warnings": [],
}),
Expand Down Expand Up @@ -339,6 +345,7 @@ fn doc_comment() {
"positional_arguments": false,
"shell": null,
"windows_powershell": false,
"windows_shell": null,
},
"warnings": [],
}),
Expand All @@ -361,6 +368,7 @@ fn empty_justfile() {
"positional_arguments": false,
"shell": null,
"windows_powershell": false,
"windows_shell": null,
},
"warnings": [],
}),
Expand Down Expand Up @@ -492,6 +500,7 @@ fn parameters() {
"positional_arguments": false,
"shell": null,
"windows_powershell": false,
"windows_shell": null,
},
"warnings": [],
}),
Expand Down Expand Up @@ -562,6 +571,7 @@ fn priors() {
"positional_arguments": false,
"shell": null,
"windows_powershell": false,
"windows_shell": null,
},
"warnings": [],
}),
Expand Down Expand Up @@ -596,6 +606,7 @@ fn private() {
"positional_arguments": false,
"shell": null,
"windows_powershell": false,
"windows_shell": null,
},
"warnings": [],
}),
Expand Down Expand Up @@ -630,6 +641,7 @@ fn quiet() {
"positional_arguments": false,
"shell": null,
"windows_powershell": false,
"windows_shell": null,
},
"warnings": [],
}),
Expand Down Expand Up @@ -685,6 +697,7 @@ fn settings() {
"command": "a",
},
"windows_powershell": false,
"windows_shell": null,
},
"warnings": [],
}),
Expand Down Expand Up @@ -722,6 +735,7 @@ fn shebang() {
"positional_arguments": false,
"shell": null,
"windows_powershell": false,
"windows_shell": null,
},
"warnings": [],
}),
Expand Down Expand Up @@ -756,6 +770,7 @@ fn simple() {
"positional_arguments": false,
"shell": null,
"windows_powershell": false,
"windows_shell": null,
},
"warnings": [],
}),
Expand Down
Loading

0 comments on commit c24a194

Please sign in to comment.