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
//! # rsscript
//!
//! Compiles and runs rust code.
//!

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

use command;
use installer;
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
use std::fs::{create_dir_all, remove_file, File};
use std::io::prelude::*;
use std::{env, iter};

fn install_crate() {
    // install dependencies
    installer::install_crate("script", "cargo-script", &None, true);
}

fn create_rust_file(rust_script: &Vec<String>) -> String {
    let name = env!("CARGO_PKG_NAME");
    let mut rng = thread_rng();
    let file_name: String = iter::repeat(())
        .map(|()| rng.sample(Alphanumeric))
        .take(10)
        .collect();

    let mut file_path = env::temp_dir();
    file_path.push(name);

    // create parent directory
    match create_dir_all(&file_path) {
        Ok(_) => debug!("Created temporary directory."),
        Err(error) => debug!(
            "Unable to create temporary directory: {} {:#?}",
            &file_path.to_str().unwrap_or("???"),
            error
        ),
    };

    file_path.push(file_name);
    file_path.set_extension("rs");

    let file_path_str = &file_path.to_str().unwrap_or("???");

    debug!("Creating temporary rust file: {}", &file_path_str);

    let mut file = match File::create(&file_path) {
        Err(error) => {
            error!(
                "Unable to create rust file: {} {:#?}",
                &file_path_str, &error
            );
            panic!("Unable to create rust file, error: {}", error);
        }
        Ok(file) => file,
    };

    let text = rust_script.join("\n");

    match file.write_all(text.as_bytes()) {
        Err(error) => {
            error!(
                "Unable to write to rust file: {} {:#?}",
                &file_path_str, &error
            );
            panic!("Unable to write to rust file, error: {}", error);
        }
        Ok(_) => debug!("Written rust file text:\n{}", &text),
    }

    file_path_str.to_string()
}

fn run_file(file: &str) -> bool {
    let exit_code = command::run_command(
        "cargo",
        &Some(vec!["script".to_string(), file.to_string()]),
        false,
    );
    debug!("Executed rust code, exit code: {}", exit_code);

    exit_code == 0
}

pub(crate) fn execute(rust_script: &Vec<String>) {
    install_crate();

    let file = create_rust_file(rust_script);

    let valid = run_file(&file);

    match remove_file(&file) {
        Ok(_) => debug!("Temporary file deleted: {}", &file),
        Err(error) => debug!("Unable to delete temporary file: {} {:#?}", &file, error),
    };

    if !valid {
        error!("Unable to execute rust code.");
    }
}