Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add experimental [no-shell] attribute #2531

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub(crate) enum Attribute<'src> {
NoCd,
NoExitMessage,
NoQuiet,
NoShell,
Openbsd,
PositionalArguments,
Private,
Expand All @@ -37,6 +38,7 @@ impl AttributeDiscriminant {
| Self::NoCd
| Self::NoExitMessage
| Self::NoQuiet
| Self::NoShell
| Self::Openbsd
| Self::PositionalArguments
| Self::Private
Expand Down Expand Up @@ -85,6 +87,7 @@ impl<'src> Attribute<'src> {
AttributeDiscriminant::NoCd => Self::NoCd,
AttributeDiscriminant::NoExitMessage => Self::NoExitMessage,
AttributeDiscriminant::NoQuiet => Self::NoQuiet,
AttributeDiscriminant::NoShell => Self::NoShell,
AttributeDiscriminant::Openbsd => Self::Openbsd,
AttributeDiscriminant::PositionalArguments => Self::PositionalArguments,
AttributeDiscriminant::Private => Self::Private,
Expand Down Expand Up @@ -134,6 +137,7 @@ impl Display for Attribute<'_> {
| Self::NoCd
| Self::NoExitMessage
| Self::NoQuiet
| Self::NoShell
| Self::Openbsd
| Self::PositionalArguments
| Self::Private
Expand Down
2 changes: 1 addition & 1 deletion src/completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl Shell {
}

fn clap(shell: clap_complete::Shell) -> RunResult<'static, String> {
fn replace(haystack: &mut String, needle: &str, replacement: &str) -> RunResult<'static, ()> {
fn replace(haystack: &mut String, needle: &str, replacement: &str) -> RunResult<'static> {
if let Some(index) = haystack.find(needle) {
haystack.replace_range(index..index + needle.len(), replacement);
Ok(())
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ pub(crate) use {
shebang::Shebang,
show_whitespace::ShowWhitespace,
source::Source,
statement::Statement,
string_delimiter::StringDelimiter,
string_kind::StringKind,
string_literal::StringLiteral,
Expand All @@ -97,6 +98,7 @@ pub(crate) use {
variables::Variables,
verbosity::Verbosity,
warning::Warning,
word::Word,
},
camino::Utf8Path,
clap::ValueEnum,
Expand Down Expand Up @@ -256,6 +258,7 @@ mod settings;
mod shebang;
mod show_whitespace;
mod source;
mod statement;
mod string_delimiter;
mod string_kind;
mod string_literal;
Expand All @@ -273,3 +276,4 @@ mod use_color;
mod variables;
mod verbosity;
mod warning;
mod word;
3 changes: 2 additions & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,6 @@ impl<'run, 'src> Parser<'run, 'src> {
}

Ok(Recipe {
shebang: shebang || script,
attributes,
body,
dependencies,
Expand All @@ -961,6 +960,8 @@ impl<'run, 'src> Parser<'run, 'src> {
priors,
private,
quiet,
shebang: shebang || script,
statements: None,
})
}

Expand Down
151 changes: 100 additions & 51 deletions src/recipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub(crate) struct Recipe<'src, D = Dependency<'src>> {
pub(crate) private: bool,
pub(crate) quiet: bool,
pub(crate) shebang: bool,
#[serde(skip)]
pub(crate) statements: Option<Vec<Statement<'src>>>,
}

impl<'src, D> Recipe<'src, D> {
Expand Down Expand Up @@ -84,7 +86,7 @@ impl<'src, D> Recipe<'src, D> {
Ok(true)
}

pub(crate) fn check_can_be_default_recipe(&self) -> RunResult<'src, ()> {
pub(crate) fn check_can_be_default_recipe(&self) -> RunResult<'src> {
let min_arguments = self.min_arguments();
if min_arguments > 0 {
return Err(Error::DefaultRecipeRequiresArguments {
Expand Down Expand Up @@ -163,7 +165,7 @@ impl<'src, D> Recipe<'src, D> {
scope: &Scope<'src, 'run>,
positional: &[String],
is_dependency: bool,
) -> RunResult<'src, ()> {
) -> RunResult<'src> {
let color = context.config.color.stderr().banner();
let prefix = color.prefix();
let suffix = color.suffix();
Expand All @@ -182,6 +184,8 @@ impl<'src, D> Recipe<'src, D> {

if self.is_script() {
self.run_script(context, scope, positional, evaluator)
} else if let Some(statements) = &self.statements {
self.run_statements(context, scope, evaluator, statements)
} else {
self.run_linewise(context, scope, positional, evaluator)
}
Expand All @@ -193,7 +197,7 @@ impl<'src, D> Recipe<'src, D> {
scope: &Scope<'src, 'run>,
positional: &[String],
mut evaluator: Evaluator<'src, 'run>,
) -> RunResult<'src, ()> {
) -> RunResult<'src> {
let config = &context.config;

let mut lines = self.body.iter().peekable();
Expand Down Expand Up @@ -267,71 +271,26 @@ impl<'src, D> Recipe<'src, D> {
eprintln!("{}", color.paint(command));
}

if config.dry_run {
continue;
}

let mut cmd = context.module.settings.shell_command(config);

if let Some(working_directory) = self.working_directory(context) {
cmd.current_dir(working_directory);
}

cmd.arg(command);

if self.takes_positional_arguments(&context.module.settings) {
cmd.arg(self.name.lexeme());
cmd.args(positional);
}

if config.verbosity.quiet() {
cmd.stderr(Stdio::null());
cmd.stdout(Stdio::null());
}

cmd.export(
&context.module.settings,
context.dotenv,
scope,
&context.module.unexports,
);

match InterruptHandler::guard(|| cmd.status()) {
Ok(exit_status) => {
if let Some(code) = exit_status.code() {
if code != 0 && !infallible_line {
return Err(Error::Code {
recipe: self.name(),
line_number: Some(line_number),
code,
print_message: self.print_exit_message(),
});
}
} else {
return Err(error_from_signal(
self.name(),
Some(line_number),
exit_status,
));
}
}
Err(io_error) => {
return Err(Error::Io {
recipe: self.name(),
io_error,
});
}
};
self.run_command(context, scope, cmd, infallible_line, line_number)?;
}
}

pub(crate) fn run_script<'run>(
fn run_script<'run>(
&self,
context: &ExecutionContext<'src, 'run>,
scope: &Scope<'src, 'run>,
positional: &[String],
mut evaluator: Evaluator<'src, 'run>,
) -> RunResult<'src, ()> {
) -> RunResult<'src> {
let config = &context.config;

let mut evaluated_lines = Vec::new();
Expand Down Expand Up @@ -458,6 +417,96 @@ impl<'src, D> Recipe<'src, D> {
}
}

fn run_statements<'run>(
&self,
context: &ExecutionContext<'src, 'run>,
scope: &Scope<'src, 'run>,
mut evaluator: Evaluator<'src, 'run>,
statements: &[Statement<'src>],
) -> RunResult<'src> {
for statement in statements {
let mut evaluated = Vec::new();

for word in &statement.words {
match word {
Word::Text(text) => evaluated.push(text.clone()),
Word::Expression(expression) => {
evaluated.push(evaluator.evaluate_expression(expression)?);
}
}
}

let Some(first) = evaluated.first() else {
continue;
};

let mut command = Command::new(first);

command.args(&evaluated[1..]);

self.run_command(context, scope, command, false, 0)?;
}

Ok(())
}

fn run_command<'run>(
&self,
context: &ExecutionContext<'src, 'run>,
scope: &Scope<'src, 'run>,
mut command: Command,
infallible_line: bool,
line_number: usize,
) -> RunResult<'src> {
if context.config.dry_run {
return Ok(());
}

if let Some(working_directory) = self.working_directory(context) {
command.current_dir(working_directory);
}

if context.config.verbosity.quiet() {
command.stderr(Stdio::null());
command.stdout(Stdio::null());
}

command.export(
&context.module.settings,
context.dotenv,
scope,
&context.module.unexports,
);

match InterruptHandler::guard(|| command.status()) {
Ok(exit_status) => {
if let Some(code) = exit_status.code() {
if code != 0 && !infallible_line {
return Err(Error::Code {
recipe: self.name(),
line_number: Some(line_number),
code,
print_message: self.print_exit_message(),
});
}
} else {
return Err(error_from_signal(
self.name(),
Some(line_number),
exit_status,
));
}
}
Err(io_error) => {
return Err(Error::Io {
recipe: self.name(),
io_error,
});
}
};
Ok(())
}

pub(crate) fn groups(&self) -> BTreeSet<String> {
self
.attributes
Expand Down
6 changes: 6 additions & 0 deletions src/statement.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use super::*;

#[derive(PartialEq, Debug, Clone, Serialize)]
pub(crate) struct Statement<'src> {
pub(crate) words: Vec<Word<'src>>,
}
2 changes: 1 addition & 1 deletion src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ impl Subcommand {
justfile.run(config, search, overrides, &recipes)
}

fn completions(shell: completions::Shell) -> RunResult<'static, ()> {
fn completions(shell: completions::Shell) -> RunResult<'static> {
println!("{}", shell.script()?);
Ok(())
}
Expand Down
27 changes: 27 additions & 0 deletions src/unresolved_recipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,32 @@ impl<'src> UnresolvedRecipe<'src> {
}
}

let statements = if self.attributes.contains(AttributeDiscriminant::NoShell) {
let mut statements = Vec::new();

for line in &self.body {
let mut words = Vec::new();
for fragment in &line.fragments {
match fragment {
Fragment::Text { token } => {
for word in token.lexeme().split_whitespace() {
words.push(Word::Text(word.into()));
}
}
Fragment::Interpolation { expression } => {
words.push(Word::Expression(expression.clone()));
}
}
}

statements.push(Statement { words });
}

Some(statements)
} else {
None
};

let dependencies = self
.dependencies
.into_iter()
Expand All @@ -58,6 +84,7 @@ impl<'src> UnresolvedRecipe<'src> {
private: self.private,
quiet: self.quiet,
shebang: self.shebang,
statements,
})
}
}
7 changes: 7 additions & 0 deletions src/word.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use super::*;

#[derive(PartialEq, Debug, Clone, Serialize)]
pub(crate) enum Word<'src> {
Expression(Expression<'src>),
Text(String),
}
Loading