cli/plugin/
runner.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
//! # runner
//!
//! Runs task plugins.
//!

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

use crate::environment;
use crate::plugin::sdk;
use crate::plugin::types::Plugin;
use crate::scriptengine::duck_script;
use crate::types::{Config, DeprecationInfo, FlowInfo, FlowState, RunTaskOptions, Step};
use duckscript::runner::run_script;
use duckscript::types::command::Commands;
use duckscript::types::error::ScriptError;
use duckscript::types::runtime::Context;
use indexmap::IndexMap;
use serde_json::json;
use std::cell::RefCell;
use std::env;
use std::rc::Rc;

/// Resolve aliases to different plugins, checking for cycles
fn get_plugin_name_recursive(
    aliases: &IndexMap<String, String>,
    name: &str,
    seen: &mut Vec<String>,
) -> String {
    let name_string = name.to_string();
    if seen.contains(&name_string) {
        error!("Detected cycle while resolving plugin alias: {}", name);
    }
    seen.push(name_string);

    match aliases.get(name) {
        Some(target_name) => get_plugin_name_recursive(aliases, target_name, seen),
        None => name.to_string(),
    }
}

fn get_plugin(config: &Config, plugin_name: &str) -> Option<(String, Plugin)> {
    match &config.plugins {
        Some(plugins_config) => {
            let normalized_plugin_name = match plugins_config.aliases {
                Some(ref aliases) => {
                    let mut seen = vec![];
                    get_plugin_name_recursive(aliases, plugin_name, &mut seen)
                }
                None => plugin_name.to_string(),
            };

            match plugins_config.plugins.get(&normalized_plugin_name) {
                Some(plugin) => Some((normalized_plugin_name, plugin.clone())),
                None => None,
            }
        }
        None => None,
    }
}

fn run_plugin(
    flow_info: &FlowInfo,
    flow_state: Rc<RefCell<FlowState>>,
    step: &Step,
    plugin: Plugin,
    impl_plugin_name: String,
) {
    debug!(
        "Running Task: {} via plugin: {} script:\n{}",
        &step.name, &impl_plugin_name, &plugin.script
    );

    let cli_arguments = match flow_info.cli_arguments.clone() {
        Some(cli_arguments) => cli_arguments.clone(),
        None => vec![],
    };

    let mut context = duck_script::create_common_context(&cli_arguments);

    let mut script_text = "exit_on_error true\n".to_string();
    setup_script_globals(
        &mut context,
        flow_info,
        step,
        &impl_plugin_name,
        &mut script_text,
    );
    script_text.push_str(&plugin.script);

    match load_sdk(flow_info, flow_state, step, &mut context.commands) {
        Ok(_) => {
            let directory = env::current_dir();

            match run_script(&script_text, context, None) {
                Ok(_) => (),
                Err(error) => error!("Error while running plugin: {}", error),
            };

            // revert to originl working directory
            if let Ok(directory_path) = directory {
                let path = directory_path.to_string_lossy().into_owned();
                environment::setup_cwd(Some(&path));
            }
        }
        Err(error) => error!("Unable to load duckscript SDK: {}", error),
    };
}

fn setup_script_globals(
    context: &mut Context,
    flow_info: &FlowInfo,
    step: &Step,
    impl_plugin_name: &str,
    script: &mut String,
) {
    context
        .variables
        .insert("flow.task.name".to_string(), flow_info.task.to_string());
    script.push_str("flow.cli.args = array\n");
    if let Some(ref cli_arguments) = flow_info.cli_arguments {
        for arg in cli_arguments {
            script.push_str("array_push ${flow.cli.args} \"");
            script.push_str(&arg.replace("$", "\\$"));
            script.push_str("\"\n");
        }
    }
    context
        .variables
        .insert("plugin.impl.name".to_string(), impl_plugin_name.to_string());

    setup_script_globals_for_task(context, step, script);
}

fn setup_script_globals_for_task(context: &mut Context, step: &Step, script: &mut String) {
    // all task data as json
    let task = step.config.clone();
    let json_string = json!(task);
    context
        .variables
        .insert("task.as_json".to_string(), json_string.to_string());

    // meta info
    context.variables.insert(
        "task.has_condition".to_string(),
        (task.condition.is_some() || task.condition_script.is_some()).to_string(),
    );
    context.variables.insert(
        "task.has_env".to_string(),
        (task.env_files.is_some() || task.env.is_some()).to_string(),
    );
    context.variables.insert(
        "task.has_install_instructions".to_string(),
        (task.install_crate.is_some()
            || task.install_crate_args.is_some()
            || task.install_script.is_some())
        .to_string(),
    );
    context.variables.insert(
        "task.has_command".to_string(),
        task.command.is_some().to_string(),
    );
    context.variables.insert(
        "task.has_script".to_string(),
        task.script.is_some().to_string(),
    );
    context.variables.insert(
        "task.has_run_task".to_string(),
        task.run_task.is_some().to_string(),
    );
    context.variables.insert(
        "task.has_dependencies".to_string(),
        task.dependencies.is_some().to_string(),
    );
    context.variables.insert(
        "task.has_toolchain_specifier".to_string(),
        task.toolchain.is_some().to_string(),
    );

    context
        .variables
        .insert("task.name".to_string(), step.name.clone());

    context.variables.insert(
        "task.description".to_string(),
        task.description.unwrap_or("".to_string()),
    );
    context.variables.insert(
        "task.category".to_string(),
        task.category.unwrap_or("".to_string()),
    );
    context.variables.insert(
        "task.disabled".to_string(),
        task.disabled.unwrap_or(false).to_string(),
    );
    context.variables.insert(
        "task.private".to_string(),
        task.private.unwrap_or(false).to_string(),
    );
    let deprecated = match task.deprecated {
        Some(value) => match value {
            DeprecationInfo::Boolean(value) => value,
            DeprecationInfo::Message(_) => true,
        },
        None => false,
    };
    context
        .variables
        .insert("task.deprecated".to_string(), deprecated.to_string());
    context.variables.insert(
        "task.workspace".to_string(),
        task.workspace.unwrap_or(false).to_string(),
    );
    context.variables.insert(
        "task.plugin.name".to_string(),
        task.plugin.unwrap_or("".to_string()),
    );
    context
        .variables
        .insert("task.watch".to_string(), task.watch.is_some().to_string());
    context.variables.insert(
        "task.ignore_errors".to_string(),
        task.ignore_errors.unwrap_or(false).to_string(),
    );
    context.variables.insert(
        "task.cwd".to_string(),
        task.cwd.unwrap_or("".to_string()).to_string(),
    );
    context.variables.insert(
        "task.command".to_string(),
        task.command.unwrap_or("".to_string()).to_string(),
    );
    script.push_str("task.args = array\n");
    if let Some(args) = task.args {
        for arg in &args {
            script.push_str("array_push ${task.args} \"");
            script.push_str(&arg.replace("$", "\\$"));
            script.push_str("\"\n");
        }
    }
    context.variables.insert(
        "task.script_runner".to_string(),
        task.script_runner.unwrap_or("".to_string()).to_string(),
    );
    script.push_str("task.script_runner_args = array\n");
    if let Some(args) = task.script_runner_args {
        for arg in &args {
            script.push_str("array_push ${task.script_runner_args} \"");
            script.push_str(arg);
            script.push_str("\"\n");
        }
    }
    context.variables.insert(
        "task.script_extension".to_string(),
        task.script_extension.unwrap_or("".to_string()).to_string(),
    );
}

fn load_sdk(
    flow_info: &FlowInfo,
    flow_state: Rc<RefCell<FlowState>>,
    step: &Step,
    commands: &mut Commands,
) -> Result<(), ScriptError> {
    duck_script::load_sdk(commands, Some(flow_info), Some(flow_state.clone()))?;
    sdk::load(flow_info, flow_state, step, commands)?;

    Ok(())
}

pub(crate) fn run_task(
    flow_info: &FlowInfo,
    flow_state: Rc<RefCell<FlowState>>,
    step: &Step,
    options: &RunTaskOptions,
) -> bool {
    if !options.plugins_enabled {
        false
    } else {
        let plugin_name_option = match flow_state.borrow().forced_plugin {
            Some(ref value) => Some(value.clone()),
            None => match step.config.plugin {
                Some(ref value) => Some(value.clone()),
                None => None,
            },
        };

        match plugin_name_option {
            Some(ref plugin_name) => match get_plugin(&flow_info.config, plugin_name) {
                Some((normalized_plugin_name, plugin)) => {
                    debug!(
                        "Running Task: {} via plugin: {}",
                        &step.name, &normalized_plugin_name
                    );

                    run_plugin(flow_info, flow_state, step, plugin, normalized_plugin_name);

                    true
                }
                None => {
                    error!(
                        "Invalid task: {}, unknown plugin: {}",
                        &step.name, plugin_name
                    );
                    false
                }
            },
            None => false,
        }
    }
}