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

Set shebang recipe script extension with [extension: 'EXT'] #2256

Merged
merged 2 commits into from
Jul 15, 2024
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1712,6 +1712,7 @@ Recipes may be annotated with attributes that change their behavior.
| `[confirm]`<sup>1.17.0</sup> | Require confirmation prior to executing recipe. |
| `[confirm('PROMPT')]`<sup>1.23.0</sup> | Require confirmation prior to executing recipe with a custom prompt. |
| `[doc('DOC')]`<sup>1.27.0</sup> | Set recipe's [documentation comment](#documentation-comments) to `DOC`. |
| `[extension('EXT')]`<sup>master</sup> | Set shebang recipe script's file extension to `EXT`. `EXT` should include a period if one is desired. |
| `[group('NAME')]`<sup>1.27.0</sup> | Put recipe in [recipe group](#recipe-groups) `NAME`. |
| `[linux]`<sup>1.8.0</sup> | Enable recipe on Linux. |
| `[macos]`<sup>1.8.0</sup> | Enable recipe on MacOS. |
Expand Down
14 changes: 14 additions & 0 deletions src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,20 @@ impl<'src> Analyzer<'src> {
continued = line.is_continuation();
}

if !recipe.shebang {
if let Some(attribute) = recipe
.attributes
.iter()
.find(|attribute| matches!(attribute, Attribute::Extension(_)))
{
return Err(recipe.name.error(InvalidAttribute {
item_kind: "Recipe",
item_name: recipe.name.lexeme(),
attribute: attribute.clone(),
}));
}
}

Ok(())
}

Expand Down
35 changes: 17 additions & 18 deletions src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use super::*;
pub(crate) enum Attribute<'src> {
Confirm(Option<StringLiteral<'src>>),
Doc(Option<StringLiteral<'src>>),
Extension(StringLiteral<'src>),
Group(StringLiteral<'src>),
Linux,
Macos,
Expand All @@ -27,7 +28,7 @@ impl AttributeDiscriminant {
fn argument_range(self) -> RangeInclusive<usize> {
match self {
Self::Confirm | Self::Doc => 0..=1,
Self::Group => 1..=1,
Self::Group | Self::Extension => 1..=1,
Self::Linux
| Self::Macos
| Self::NoCd
Expand All @@ -46,8 +47,6 @@ impl<'src> Attribute<'src> {
name: Name<'src>,
argument: Option<StringLiteral<'src>>,
) -> CompileResult<'src, Self> {
use AttributeDiscriminant::*;

let discriminant = name
.lexeme()
.parse::<AttributeDiscriminant>()
Expand All @@ -72,18 +71,19 @@ impl<'src> Attribute<'src> {
}

Ok(match discriminant {
Confirm => Self::Confirm(argument),
Doc => Self::Doc(argument),
Group => Self::Group(argument.unwrap()),
Linux => Self::Linux,
Macos => Self::Macos,
NoCd => Self::NoCd,
NoExitMessage => Self::NoExitMessage,
NoQuiet => Self::NoQuiet,
PositionalArguments => Self::PositionalArguments,
Private => Self::Private,
Unix => Self::Unix,
Windows => Self::Windows,
AttributeDiscriminant::Confirm => Self::Confirm(argument),
AttributeDiscriminant::Doc => Self::Doc(argument),
AttributeDiscriminant::Extension => Self::Extension(argument.unwrap()),
AttributeDiscriminant::Group => Self::Group(argument.unwrap()),
AttributeDiscriminant::Linux => Self::Linux,
AttributeDiscriminant::Macos => Self::Macos,
AttributeDiscriminant::NoCd => Self::NoCd,
AttributeDiscriminant::NoExitMessage => Self::NoExitMessage,
AttributeDiscriminant::NoQuiet => Self::NoQuiet,
AttributeDiscriminant::PositionalArguments => Self::PositionalArguments,
AttributeDiscriminant::Private => Self::Private,
AttributeDiscriminant::Unix => Self::Unix,
AttributeDiscriminant::Windows => Self::Windows,
})
}

Expand All @@ -93,9 +93,8 @@ impl<'src> Attribute<'src> {

fn argument(&self) -> Option<&StringLiteral> {
match self {
Self::Confirm(prompt) => prompt.as_ref(),
Self::Doc(doc) => doc.as_ref(),
Self::Group(group) => Some(group),
Self::Confirm(argument) | Self::Doc(argument) => argument.as_ref(),
Self::Extension(argument) | Self::Group(argument) => Some(argument),
Self::Linux
| Self::Macos
| Self::NoCd
Expand Down
11 changes: 10 additions & 1 deletion src/recipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,16 @@ impl<'src, D> Recipe<'src, D> {
io_error: error,
})?;
let mut path = tempdir.path().to_path_buf();
path.push(shebang.script_filename(self.name()));

let extension = self.attributes.iter().find_map(|attribute| {
if let Attribute::Extension(extension) = attribute {
Some(extension.cooked.as_str())
} else {
None
}
});

path.push(shebang.script_filename(self.name(), extension));

{
let mut f = fs::File::create(&path).map_err(|error| Error::TempdirIo {
Expand Down
59 changes: 46 additions & 13 deletions src/shebang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ impl<'line> Shebang<'line> {
.unwrap_or(self.interpreter)
}

pub(crate) fn script_filename(&self, recipe: &str) -> String {
match self.interpreter_filename() {
"cmd" | "cmd.exe" => format!("{recipe}.bat"),
"powershell" | "powershell.exe" | "pwsh" | "pwsh.exe" => format!("{recipe}.ps1"),
_ => recipe.to_owned(),
}
pub(crate) fn script_filename(&self, recipe: &str, extension: Option<&str>) -> String {
let extension = extension.unwrap_or_else(|| match self.interpreter_filename() {
"cmd" | "cmd.exe" => ".bat",
"powershell" | "powershell.exe" | "pwsh" | "pwsh.exe" => ".ps1",
_ => "",
});

format!("{recipe}{extension}")
}

pub(crate) fn include_shebang_line(&self) -> bool {
Expand Down Expand Up @@ -138,15 +140,17 @@ mod tests {
#[test]
fn powershell_script_filename() {
assert_eq!(
Shebang::new("#!powershell").unwrap().script_filename("foo"),
Shebang::new("#!powershell")
.unwrap()
.script_filename("foo", None),
"foo.ps1"
);
}

#[test]
fn pwsh_script_filename() {
assert_eq!(
Shebang::new("#!pwsh").unwrap().script_filename("foo"),
Shebang::new("#!pwsh").unwrap().script_filename("foo", None),
"foo.ps1"
);
}
Expand All @@ -156,38 +160,45 @@ mod tests {
assert_eq!(
Shebang::new("#!powershell.exe")
.unwrap()
.script_filename("foo"),
.script_filename("foo", None),
"foo.ps1"
);
}

#[test]
fn pwsh_exe_script_filename() {
assert_eq!(
Shebang::new("#!pwsh.exe").unwrap().script_filename("foo"),
Shebang::new("#!pwsh.exe")
.unwrap()
.script_filename("foo", None),
"foo.ps1"
);
}

#[test]
fn cmd_script_filename() {
assert_eq!(
Shebang::new("#!cmd").unwrap().script_filename("foo"),
Shebang::new("#!cmd").unwrap().script_filename("foo", None),
"foo.bat"
);
}

#[test]
fn cmd_exe_script_filename() {
assert_eq!(
Shebang::new("#!cmd.exe").unwrap().script_filename("foo"),
Shebang::new("#!cmd.exe")
.unwrap()
.script_filename("foo", None),
"foo.bat"
);
}

#[test]
fn plain_script_filename() {
assert_eq!(Shebang::new("#!bar").unwrap().script_filename("foo"), "foo");
assert_eq!(
Shebang::new("#!bar").unwrap().script_filename("foo", None),
"foo"
);
}

#[test]
Expand All @@ -211,4 +222,26 @@ mod tests {
fn include_shebang_line_other_windows() {
assert!(!Shebang::new("#!foo -c").unwrap().include_shebang_line());
}

#[test]
fn filename_with_extension() {
assert_eq!(
Shebang::new("#!bar")
.unwrap()
.script_filename("foo", Some(".sh")),
"foo.sh"
);
assert_eq!(
Shebang::new("#!pwsh.exe")
.unwrap()
.script_filename("foo", Some(".sh")),
"foo.sh"
);
assert_eq!(
Shebang::new("#!cmd.exe")
.unwrap()
.script_filename("foo", Some(".sh")),
"foo.sh"
);
}
}
37 changes: 37 additions & 0 deletions tests/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,40 @@ fn doc_multiline() {
)
.run();
}

#[test]
fn extension() {
Test::new()
.justfile(
"
[extension: '.txt']
baz:
#!/bin/sh
echo $0
",
)
.stdout_regex(r"*baz\.txt\n")
.run();
}

#[test]
fn extension_on_linewise_error() {
Test::new()
.justfile(
"
[extension: '.txt']
baz:
",
)
.stderr(
"
error: Recipe `baz` has invalid attribute `extension`
——▶ justfile:2:1
2 │ baz:
│ ^^^
",
)
.status(EXIT_FAILURE)
.run();
}