cli/scriptengine/
shebang_script.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//! # shebang_script
//!
//! Runs scripts using the executable defined in the shebang line.
//!

#[cfg(test)]
#[path = "shebang_script_test.rs"]
mod shebang_script_test;

use crate::error::CargoMakeError;
use crate::scriptengine::generic_script;
use std::path::Path;

#[cfg(target_os = "windows")]
const DEFAULT_EXTENSION: &'static str = "cmd.exe";
#[cfg(not(target_os = "windows"))]
const DEFAULT_EXTENSION: &'static str = "sh";

#[derive(Debug, Clone)]
/// Holds flow information
pub(crate) struct Shebang {
    /// The script runner
    pub(crate) runner: Option<String>,
    /// additional arguments
    pub(crate) arguments: Option<Vec<String>>,
}

impl Shebang {
    /// Creates and returns a new instance.
    fn new() -> Shebang {
        Shebang {
            runner: None,
            arguments: None,
        }
    }
}

pub(crate) fn get_shebang(script_text: &Vec<String>) -> Shebang {
    match script_text.first() {
        Some(line) => {
            let trimmed_line = line.trim();

            if trimmed_line.starts_with("#!") {
                let mut lines = trimmed_line.split("\n");
                match lines.next() {
                    Some(first_line) => {
                        let mut shebang_line = first_line.replace("#!", "");
                        shebang_line = shebang_line.trim().to_string();

                        if !shebang_line.is_empty() {
                            let mut values = shebang_line.split_whitespace();
                            let runner = match values.next() {
                                Some(value) => Some(value.trim().to_string()),
                                _ => None,
                            };

                            let mut argument_values = vec![];
                            for arg in values {
                                argument_values.push(arg.trim().to_string());
                            }

                            let arguments = if !argument_values.is_empty() {
                                Some(argument_values)
                            } else {
                                None
                            };

                            Shebang { runner, arguments }
                        } else {
                            Shebang::new()
                        }
                    }
                    _ => Shebang::new(),
                }
            } else {
                Shebang::new()
            }
        }
        None => Shebang::new(),
    }
}

fn get_extension_for_runner(runner: &str) -> String {
    let runner_no_extension = match Path::new(runner).file_stem() {
        Some(value) => value,
        None => return DEFAULT_EXTENSION.to_string(),
    };

    let extension = if runner_no_extension == "python" {
        "py"
    } else if runner_no_extension == "perl" {
        "pl"
    } else if runner_no_extension == "node" {
        "js"
    } else if runner_no_extension == "powershell" || runner_no_extension == "pwsh" {
        "ps1"
    } else {
        DEFAULT_EXTENSION
    };

    extension.to_string()
}

pub(crate) fn execute(
    script_text: &Vec<String>,
    extension: &Option<String>,
    cli_arguments: &Vec<String>,
    validate: bool,
) -> Result<bool, CargoMakeError> {
    let shebang = get_shebang(&script_text);

    match shebang.runner {
        Some(runner) => {
            let extension_str = match extension {
                Some(value) => value.to_string(),
                None => get_extension_for_runner(&runner),
            };

            generic_script::execute(
                &script_text,
                runner,
                extension_str,
                shebang.arguments,
                &cli_arguments,
                validate,
            )
        }
        None => {
            if validate {
                error!("Unable to execute script using shebang.");
            }

            Ok(false)
        }
    }
}