Skip to content

Commit

Permalink
Keep multi-line shebangs together (#2276)
Browse files Browse the repository at this point in the history
  • Loading branch information
vkstrm authored Jul 31, 2024
1 parent 317a85d commit 77260f8
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 34 deletions.
56 changes: 22 additions & 34 deletions src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,44 +92,32 @@ impl<'a> Executor<'a> {
// numbers in errors from generated script match justfile source lines.
pub(crate) fn script<D>(&self, recipe: &Recipe<D>, lines: &[String]) -> String {
let mut script = String::new();

match self {
Self::Shebang(shebang) => {
let mut n = 0;

for (i, (line, evaluated)) in recipe.body.iter().zip(lines).enumerate() {
if i == 0 {
if shebang.include_shebang_line() {
script.push_str(evaluated);
script.push('\n');
n += 1;
}
} else {
while n < line.number {
script.push('\n');
n += 1;
}

script.push_str(evaluated);
script.push('\n');
n += 1;
}
let mut n = 0;
let shebangs = recipe
.body
.iter()
.take_while(|line| line.is_shebang())
.count();

if let Self::Shebang(shebang) = self {
for shebang_line in &lines[..shebangs] {
if shebang.include_shebang_line() {
script.push_str(shebang_line);
}
script.push('\n');
n += 1;
}
Self::Command(_) => {
let mut n = 0;

for (line, evaluated) in recipe.body.iter().zip(lines) {
while n < line.number {
script.push('\n');
n += 1;
}
}

script.push_str(evaluated);
script.push('\n');
n += 1;
}
for (line, text) in recipe.body.iter().zip(lines).skip(n) {
while n < line.number {
script.push('\n');
n += 1;
}

script.push_str(text);
script.push('\n');
n += 1;
}

script
Expand Down
37 changes: 37 additions & 0 deletions tests/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,43 @@ b
c
",
)
.run();
}

#[cfg(not(windows))]
#[test]
fn multiline_shebang_line_numbers() {
Test::new()
.justfile(
"foo:
#!/usr/bin/env cat
#!shebang
#!shebang
a
b
c
",
)
.stdout(
"#!/usr/bin/env cat
#!shebang
#!shebang
a
b
c
",
)
Expand Down
12 changes: 12 additions & 0 deletions tests/shebang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ default:
stdout: "Hello-World\r\n",
}

#[cfg(windows)]
test! {
name: multi_line_cmd_shebangs_are_removed,
justfile: r#"
default:
#!cmd.exe /c
#!foo
@echo Hello-World
"#,
stdout: "Hello-World\r\n",
}

#[test]
fn simple() {
Test::new()
Expand Down

0 comments on commit 77260f8

Please sign in to comment.