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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//! # scriptengine
//!
//! Facade for all different non OS scripts.
//!

mod duck_script;
pub(crate) mod generic_script;
mod os_script;
mod rsscript;
pub(crate) mod script_utils;
mod shebang_script;
mod shell_to_batch;

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

use crate::environment;
use crate::io;
use crate::types::{ScriptValue, Task};
use std::path::PathBuf;

#[derive(Debug, Clone, PartialEq)]
/// The currently supported engine types
pub(crate) enum EngineType {
    /// OS native script
    OS,
    /// Duckscript script runner
    Duckscript,
    /// Rust language
    Rust,
    /// shell to windows batch conversion
    Shell2Batch,
    /// Generic script runner
    Generic,
    /// Shebang script runner
    Shebang,
    /// Unsupported type
    Unsupported,
}

pub(crate) fn get_script_text(script: &ScriptValue) -> Vec<String> {
    match script {
        ScriptValue::Text(text) => text.clone(),
        ScriptValue::File(info) => {
            let mut file_path_string = String::new();
            if !info.absolute_path.unwrap_or(false) {
                file_path_string.push_str("${CARGO_MAKE_WORKING_DIRECTORY}/");
            }
            file_path_string.push_str(&info.file);

            // expand env
            let expanded_value = environment::expand_value(&file_path_string);

            let mut file_path = PathBuf::new();
            file_path.push(expanded_value);

            let script_text = io::read_text_file(&file_path);
            let lines: Vec<&str> = script_text.split('\n').collect();

            let mut script_lines: Vec<String> = vec![];

            for line in lines.iter() {
                script_lines.push(line.to_string());
            }

            script_lines
        }
    }
}

fn get_internal_runner(script_runner: &str) -> EngineType {
    if script_runner == "@duckscript" {
        debug!("Duckscript detected.");
        EngineType::Duckscript
    } else if script_runner == "@rust" {
        debug!("Rust script detected.");
        EngineType::Rust
    } else if script_runner == "@shell" {
        debug!("Shell to batch detected.");
        EngineType::Shell2Batch
    } else {
        EngineType::Unsupported
    }
}

pub(crate) fn get_engine_type(
    script: &ScriptValue,
    script_runner: &Option<String>,
    script_extension: &Option<String>,
) -> EngineType {
    match script_runner {
        Some(ref runner) => {
            debug!("Checking script runner: {}", runner);

            let engine_type = get_internal_runner(runner);

            match engine_type {
                EngineType::Unsupported => {
                    if script_extension.is_some() {
                        // if both script runner and extension is defined, we use generic script runner
                        debug!("Generic script detected.");
                        EngineType::Generic
                    } else {
                        // use default OS extension with custom runner
                        debug!("OS script with custom runner detected.");
                        EngineType::OS
                    }
                }
                _ => engine_type,
            }
        }
        None => {
            // if no runner specified, try to extract it from script content
            let script_text = get_script_text(&script);

            let shebang = shebang_script::get_shebang(&script_text);

            match shebang.runner {
                Some(script_runner) => {
                    if shebang.arguments.is_none() {
                        let engine_type = get_internal_runner(&script_runner);

                        match engine_type {
                            EngineType::Unsupported => EngineType::Shebang,
                            _ => engine_type,
                        }
                    } else {
                        EngineType::Shebang
                    }
                }
                None => EngineType::OS,
            }
        }
    }
}

pub(crate) fn invoke(task: &Task, cli_arguments: &Vec<String>) -> bool {
    match task.script {
        Some(ref script) => {
            let validate = !task.should_ignore_errors();

            invoke_script(
                script,
                task.script_runner.clone(),
                task.script_extension.clone(),
                validate,
                cli_arguments,
            )
        }
        None => false,
    }
}

pub(crate) fn invoke_script(
    script: &ScriptValue,
    script_runner: Option<String>,
    script_extension: Option<String>,
    validate: bool,
    cli_arguments: &Vec<String>,
) -> bool {
    let engine_type = get_engine_type(script, &script_runner, &script_extension);

    match engine_type {
        EngineType::OS => {
            let script_text = get_script_text(script);
            os_script::execute(&script_text, script_runner, cli_arguments, validate);

            true
        }
        EngineType::Duckscript => {
            let script_text = get_script_text(script);
            duck_script::execute(&script_text, cli_arguments, validate);

            true
        }
        EngineType::Rust => {
            let script_text = get_script_text(script);
            rsscript::execute(&script_text, cli_arguments, validate);

            true
        }
        EngineType::Shell2Batch => {
            let script_text = get_script_text(script);
            shell_to_batch::execute(&script_text, cli_arguments, validate);

            true
        }
        EngineType::Generic => {
            let script_text = get_script_text(script);
            let extension = script_extension.clone().unwrap();
            generic_script::execute(
                &script_text,
                script_runner.unwrap(),
                extension,
                None,
                cli_arguments,
                validate,
            );

            true
        }
        EngineType::Shebang => {
            let script_text = get_script_text(script);
            let extension = script_extension.clone();
            shebang_script::execute(&script_text, &extension, cli_arguments, validate);

            true
        }
        EngineType::Unsupported => false,
    }
}