yamis 1.2.0

Task runner for teams and individuals.
Documentation
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
use clap::ArgAction;
use colored::{ColoredString, Colorize};
use lazy_static::lazy_static;
use serde_derive::Deserialize;
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::error::Error;
use std::ffi::{OsStr, OsString};
use std::fs::File;
use std::path::Path;
use std::{env, fmt, fs};

use regex::Regex;

use crate::config_files::{ConfigFilePaths, ConfigFilesContainer};
use crate::print_utils::YamisOutput;
use crate::types::{DynErrResult, TaskArgs};
use crate::updater;

const HELP: &str = "The appropriate YAML or TOML config files need to exist \
in the directory or parents, or a file is specified with the `-f` or `--file` \
options. For help about the config files check https://github.com/adrianmrit/yamis";

/// Holds the data for running the given task.
struct TaskSubcommand {
    /// Task to run, if given
    pub task: String,
    /// Args to run the command with
    pub args: TaskArgs,
}

/// Enum of config file containers by version
enum ConfigFileContainerVersion {
    V1(ConfigFilesContainer),
}

fn default_version() -> Version {
    Version::V1
}

#[derive(Debug, Deserialize)]
pub struct ConfigFileVersionSerializer {
    #[serde(default = "default_version")]
    version: Version,
}

/// Enum of available config file versions
#[derive(Hash, Eq, PartialEq, Debug, Deserialize)]
enum Version {
    #[serde(alias = "v1")]
    #[serde(rename = "1")]
    V1,
}

/// Holds all the config file containers, regardless of the version they are supposed to handle
struct ConfigFileContainers {
    /// Holds the config file containers for each version
    containers: HashMap<Version, ConfigFileContainerVersion>,
}

/// Argument errors
#[derive(Debug, PartialEq, Eq)]
enum ArgsError {
    /// Raised when no task to run is given
    MissingTaskArg,
}

impl fmt::Display for ArgsError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            ArgsError::MissingTaskArg => write!(f, "No task was given."),
        }
    }
}

impl Error for ArgsError {
    fn description(&self) -> &str {
        match *self {
            ArgsError::MissingTaskArg => "no task given",
        }
    }

    fn cause(&self) -> Option<&dyn Error> {
        None
    }
}

/// Sets the color when printing the task name
fn colorize_task_name(val: &str) -> ColoredString {
    val.bright_cyan()
}

/// Sets the color when printing the config file path
fn colorize_config_file_path(val: &str) -> ColoredString {
    val.bright_blue()
}

impl ConfigFileContainers {
    /// Creates a new instance of `ConfigFileContainers`
    fn new() -> Self {
        let mut containers = HashMap::new();
        containers.insert(
            Version::V1,
            ConfigFileContainerVersion::V1(ConfigFilesContainer::new()),
        );
        Self { containers }
    }

    /// Peeks at the file and returns the version of the config file.
    ///
    /// # Arguments
    ///
    /// * `path`: path to the file to extract the version from
    ///
    /// returns: Result<String, Box<dyn Error, Global>>
    pub(crate) fn get_file_version(path: &Path) -> DynErrResult<Version> {
        let extension = path
            .extension()
            .unwrap_or_else(|| OsStr::new(""))
            .to_string_lossy()
            .to_string();

        let is_yaml = match extension.as_str() {
            "yaml" => true,
            "yml" => true,
            "toml" => false,
            _ => {
                panic!("Unknown file extension: {}", extension);
            }
        };

        let file = match File::open(path) {
            Ok(file_contents) => file_contents,
            Err(e) => return Err(format!("There was an error reading the file:\n{}", e).into()),
        };

        let result: ConfigFileVersionSerializer = if is_yaml {
            serde_yaml::from_reader(file)?
        } else {
            // A bytes list should be slightly faster than a string list
            toml::from_slice(&fs::read(path)?)?
        };

        Ok(result.version)
    }

    /// prints config file paths and their tasks
    fn print_tasks_list(&mut self, paths: ConfigFilePaths) -> DynErrResult<()> {
        for path in paths {
            let path = path?;
            let version = ConfigFileContainers::get_file_version(&path)?;
            match version {
                Version::V1 => {
                    println!("{}:", colorize_config_file_path(&path.to_string_lossy()));
                    let container = self.containers.get_mut(&Version::V1).unwrap();
                    let ConfigFileContainerVersion::V1(container) = container;
                    let config_file_ptr = container.read_config_file(path.clone())?;
                    let config_file_lock = config_file_ptr.lock().unwrap();
                    let task_names = config_file_lock.get_public_task_names();
                    if task_names.is_empty() {
                        println!("  {}", "No tasks found.".red());
                    } else {
                        for task in task_names {
                            println!(" - {}", colorize_task_name(task));
                        }
                    }
                }
            }
        }
        Ok(())
    }

    /// Prints help for the given task
    fn print_task_info(&mut self, paths: ConfigFilePaths, task: &str) -> DynErrResult<()> {
        for path in paths {
            let path = path?;
            let version = ConfigFileContainers::get_file_version(&path)?;
            match version {
                Version::V1 => {
                    let container = self.containers.get_mut(&Version::V1).unwrap();
                    let ConfigFileContainerVersion::V1(container) = container;
                    let config_file_ptr = container.read_config_file(path.clone())?;
                    let config_file_lock = config_file_ptr.lock().unwrap();
                    let task = config_file_lock.get_task(task);
                    match task {
                        Some(task) => {
                            println!("{}:", colorize_config_file_path(&path.to_string_lossy()));
                            print!(" - {}", colorize_task_name(task.get_name()));
                            if task.is_private() {
                                print!(" {}", "(private)".red());
                            }
                            println!();
                            let prefix = "     ";
                            match task.get_help().trim() {
                                "" => println!("{}{}", prefix, "No help to display".yellow()),
                                help => {
                                    //                 " -   "  Two spaces after the dash
                                    let help_lines: Vec<&str> = help.lines().collect();
                                    println!(
                                        "{}{}",
                                        prefix,
                                        help_lines.join(&format!("\n{}", prefix)).green()
                                    )
                                }
                            }
                            return Ok(());
                        }
                        None => continue,
                    }
                }
            }
        }
        Err(format!("Task {} not found", task).into())
    }

    /// Runs the given task
    fn run_task(&mut self, paths: ConfigFilePaths, task: &str, args: TaskArgs) -> DynErrResult<()> {
        for path in paths {
            let path = path?;
            let version = match ConfigFileContainers::get_file_version(&path) {
                Ok(version) => version,
                Err(e) => {
                    // So the user knows where the error occurred
                    let e = format!("{}:\n{}", &path.to_string_lossy().red(), e);
                    return Err(e.into());
                }
            };
            match version {
                Version::V1 => {
                    let container = self.containers.get_mut(&Version::V1).unwrap();
                    let ConfigFileContainerVersion::V1(container) = container;
                    let config_file_ptr = match container.read_config_file(path.clone()) {
                        Ok(val) => val,
                        Err(e) => {
                            let e = format!("{}:\n{}", &path.to_string_lossy().red(), e);
                            return Err(e.into());
                        }
                    };
                    let config_file_lock = config_file_ptr.lock().unwrap();
                    let task = config_file_lock.get_public_task(task);
                    match task {
                        Some(task) => {
                            if config_file_lock.debug_config.print_file_path {
                                println!("{}", &path.to_string_lossy().yamis_info());
                            }
                            return match task.run(&args, &config_file_lock) {
                                Ok(val) => Ok(val),
                                Err(e) => {
                                    let e = format!("{}:\n{}", &path.to_string_lossy().red(), e);
                                    Err(e.into())
                                }
                            };
                        }
                        None => continue,
                    }
                }
            }
        }
        Err(format!("Task {} not found", task).into())
    }
}

// TODO: Handle
impl TaskSubcommand {
    /// Returns a new TaskSubcommand
    pub(crate) fn new(args: &clap::ArgMatches) -> Result<TaskSubcommand, ArgsError> {
        let mut kwargs = TaskArgs::new();

        let (task_name, task_args) = match args.subcommand() {
            None => return Err(ArgsError::MissingTaskArg),
            Some(command) => command,
        };

        if let Some(args) = task_args.get_many::<OsString>("") {
            // All args are pushed into a vector as they are
            let all_args = args
                .clone()
                .map(|s| s.to_string_lossy().to_string())
                .collect::<Vec<String>>();
            kwargs.insert(String::from("*"), all_args);

            // kwarg found that could be a key
            let mut possible_kwarg_key = None;

            // looping over the args to find kwargs
            for arg in args {
                let arg = arg.to_string_lossy().to_string();
                // if a kwarg key was previously found, assume this is the value, even if
                // it starts with - or --
                if let Some(possible_kwarg) = possible_kwarg_key {
                    match kwargs.entry(possible_kwarg) {
                        Entry::Occupied(mut e) => {
                            e.get_mut().push(arg);
                        }
                        Entry::Vacant(e) => {
                            let args_vec: Vec<String> = vec![arg];
                            e.insert(args_vec);
                        }
                    }
                    possible_kwarg_key = None;
                    continue;
                }

                // Quick check to see if the arg is a kwarg key or key-value pair
                // if it is a positional value, we just continue
                if !arg.starts_with('-') {
                    continue;
                }

                // Check if this is a kwarg key-value pair
                if let Some((key, val)) = Self::get_kwarg(&arg) {
                    match kwargs.entry(key) {
                        Entry::Occupied(mut e) => {
                            e.get_mut().push(val);
                        }
                        Entry::Vacant(e) => {
                            let args_vec: Vec<String> = vec![val];
                            e.insert(args_vec);
                        }
                    }
                    continue;
                }

                // Otherwise it could be a kwarg key, for which we need to check the next arg
                if let Some(key) = Self::get_kwarg_key(&arg) {
                    possible_kwarg_key = Some(key);
                    continue;
                }

                // Finally if it is not a kwarg key or key-value pair, it is a positional arg,
                // i.e. -0
            }
        } else {
            kwargs.insert(String::from("*"), vec![]);
        }

        Ok(TaskSubcommand {
            task: String::from(task_name),
            args: kwargs,
        })
    }

    /// Returns the key if the arg represents a kwarg key, otherwise None
    fn get_kwarg_key(arg: &str) -> Option<String> {
        lazy_static! {
            static ref KWARG_KEY_REGEX: Regex = Regex::new(r"-{1,2}(?P<key>[a-zA-Z]+\w*)").unwrap();
        }
        let kwarg_match = KWARG_KEY_REGEX.captures(arg);
        if let Some(arg_match) = kwarg_match {
            let key = String::from(arg_match.name("key").unwrap().as_str());
            Some(key)
        } else {
            None
        }
    }

    /// Returns the key and value if the arg represents a kwarg key-value pair, otherwise None
    fn get_kwarg(arg: &str) -> Option<(String, String)> {
        lazy_static! {
            static ref KWARG_REGEX: Regex =
                Regex::new(r"-{1,2}(?P<key>[a-zA-Z]+\w*)=(?P<val>[\s\S]*)").unwrap();
        }
        let kwarg_match = KWARG_REGEX.captures(arg);
        if let Some(arg_match) = kwarg_match {
            let key = String::from(arg_match.name("key").unwrap().as_str());
            let val = String::from(arg_match.name("val").unwrap().as_str());
            Some((key, val))
        } else {
            None
        }
    }
}

/// Executes the program. If errors are encountered during the execution these
/// are returned immediately. The wrapping method needs to take care of formatting
/// and displaying these errors appropriately.
pub fn exec() -> DynErrResult<()> {
    let app = clap::Command::new(clap::crate_name!())
        .version(clap::crate_version!())
        .about(clap::crate_description!())
        .author(clap::crate_authors!())
        .after_help(HELP)
        .allow_external_subcommands(true)
        .arg(
            clap::Arg::new("list")
                .short('l')
                .long("list")
                .help("Lists configuration files that can be reached from the current directory")
                .conflicts_with_all(["file"])
                .action(ArgAction::SetTrue),
        )
        .arg(
            clap::Arg::new("list-tasks")
                .short('t')
                .long("list-tasks")
                .help("Lists tasks")
                .conflicts_with_all(["task-info"])
                .action(ArgAction::SetTrue),
        )
        .arg(
            clap::Arg::new("task-info")
                .short('i')
                .long("task-info")
                .action(ArgAction::Set)
                .help("Displays information about the given task")
                .value_name("TASK"),
        )
        .arg(
            clap::Arg::new("file")
                .short('f')
                .long("file")
                .action(ArgAction::Set)
                .help("Search for tasks in the given file")
                .value_name("FILE"),
        )
        .arg(
            clap::Arg::new("update")
                .long("update")
                .help("Checks for updates and updates the binary if necessary")
                .exclusive(true)
                .action(ArgAction::SetTrue),
        );
    let matches = app.get_matches();

    if matches.get_one::<bool>("update").cloned().unwrap_or(false) {
        updater::update()?;
        return Ok(());
    } else {
        match updater::check_update_available() {
            Ok(result) => {
                if let Some(msg) = result {
                    println!("{}", msg.yamis_prefix_info());
                }
            }
            Err(e) => {
                let err_msg = format!("Error checking for updates: {}", e);
                eprintln!("{}", err_msg.yamis_error());
            }
        }
    }

    let current_dir = env::current_dir()?;
    let mut file_containers = ConfigFileContainers::new();

    let config_file_paths = match matches.get_one::<String>("file") {
        None => ConfigFilePaths::new(&current_dir),
        Some(file_path) => ConfigFilePaths::only(file_path)?,
    };

    if matches
        .get_one::<bool>("list-tasks")
        .cloned()
        .unwrap_or(false)
    {
        file_containers.print_tasks_list(config_file_paths)?;
        return Ok(());
    };

    if let Some(task_name) = matches.get_one::<String>("task-info") {
        file_containers.print_task_info(config_file_paths, task_name)?;
        return Ok(());
    };

    if matches.get_one::<bool>("list").cloned().unwrap_or(false) {
        for path in config_file_paths {
            let path = path?;
            println!("{}", colorize_config_file_path(&path.to_string_lossy()));
        }
        return Ok(());
    }

    let task_command = TaskSubcommand::new(&matches)?;

    file_containers.run_task(config_file_paths, &task_command.task, task_command.args)
}

#[cfg(test)]
mod tests {
    use crate::config_files::ConfigFilePaths;
    use assert_cmd::Command;
    use assert_fs::TempDir;
    use predicates::prelude::predicate;
    use std::fs::File;
    use std::io::Write;

    #[test]
    #[ignore = "Fails but works fine when run manually"]
    fn test_list() -> Result<(), Box<dyn std::error::Error>> {
        let tmp_dir = TempDir::new().unwrap();
        let global_config_dir = ConfigFilePaths::get_global_config_file_dir();

        // Global config dir should not be the same as the current dir
        assert_ne!(tmp_dir.path(), &global_config_dir);

        // Should always return the same global dir
        assert_eq!(
            &ConfigFilePaths::get_global_config_file_dir(),
            &global_config_dir
        );

        let global_config_path = global_config_dir.join("user.yamis.toml");
        let mut global_config_file = File::create(global_config_path.as_path()).unwrap();
        global_config_file
            .write_all(
                r#"
                [tasks.hello_global]
                script = "echo hello project"
                help = "Some help here"
                "#
                .as_bytes(),
            )
            .unwrap();

        let mut file = File::create(tmp_dir.join("project.yamis.toml"))?;
        file.write_all(
            r#"

    [tasks.hello.windows]
    script = "echo %greeting%, one plus one is %one_plus_one%"
    private=true

    [tasks.hello]
    script = "echo $greeting, one plus one is $one_plus_one"
    "#
            .as_bytes(),
        )?;
        let expected = format!(
            "{tmp_dir}/project.yamis.toml\n{global_config_dir}/user.yamis.toml\n",
            tmp_dir = tmp_dir.path().to_str().unwrap(),
            global_config_dir = global_config_dir.to_str().unwrap()
        );
        let mut cmd = Command::cargo_bin("yamis")?;
        cmd.current_dir(tmp_dir.path());
        cmd.arg("--list");
        cmd.assert()
            .success()
            .stdout(predicate::str::contains(expected));
        Ok(())
    }
}