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
use crate::path::{PathManager, path_to_string};
use crate::platform::Platform;
use crate::backup;
use crate::frontend::{install, gitclone, add_to_path};
use crate::capture::decode;
use std::fmt::{Display, Formatter, Error};
use std::fs::{create_dir_all, OpenOptions};
use std::io::prelude::*;
use std::path::PathBuf;

/// This object represents the core of the installation process.
/// In the future, I would like to make the scripts more complex.
/// Right now they're just a single instruction per line, and there
/// is very little to work with when writing a rapture script.
/// 
/// If I can implement an embedded language similar to but simpler than
/// lua, that would be helpful.
#[derive(Clone)]
pub struct Script {
    // This field determines whether or not the script has been declared
    // a package installer.
    // 
    // A package installer script creates a package installation directory in
    // the `~/.rapture` installation directory, while a non package installer
    // script does not.
    package_name: Option<String>,

    // Contains the contents of the script
    script: String,
}

/// This function splits a string into two pieces at the first
/// instance of whitespace. If there is no white space in the string,
/// The function returns Err(()).
fn split_first_space<'a>(s: String) -> Result<(String, String), ()> {
    let mut split_index = 0;
    // Iterate over characters in the string and update split_index
    for (i, c) in s.clone().chars().enumerate() {
        split_index = i;

        // If the character is whitespace, break
        match c {
            ' ' | '\t' | '\n' => break,
            _ => {}
        }
    }

    // If split_index is the last index in the string, return Err(())
    if split_index >= s.to_string().len() - 1 {
        return Err(())
    }
    // Otherwise, return the first and second half from the split index
    let head = &s.as_str()[..split_index];
    let tail = &s.as_str()[split_index..];
    return Ok((head.trim().to_string(), tail.trim().to_string()))
}

/// This object represents an executable rapture script
impl Script {
    /// This instantiates a new script with `script` as the script contents
    pub fn new<S: ToString>(script: S) -> Self {
        return Self {
            package_name: None,
            script: script.to_string(),
        }
    }

    /// Runs a given command on the proper operating system's shell.
    /// If the script has a package declaration, call the command
    /// within the package installation directory.
    /// 
    /// For example: if the package is named `wonderful`, and I call
    /// `cat main.rs`, the following command will be run instead:
    /// `cd /home/USERNAME/.rapture/wonderful; cat main.rs`
    /// 
    /// If I dont declare the package name before running a command,
    /// the exact command you gave this function is executed instead.
    pub fn command<S: Display>(&self, cmd: S) -> Result<(), String> {
        match self.package_name.clone() {
            Some(name) => {
                Platform::command(format!("cd {}; {};", PathManager::package_dir(name.to_string()), cmd))?;
            },
            None => {
                Platform::command(cmd)?;
            }
        }
        Ok(())
    }

    /// This function runs the rapture script. It iterates over the lines in the
    /// script, and matches the commands and the arguments. I would like to change
    /// this in the future, replacing it with an embeddable scripting language.
    pub fn run(&mut self) -> Result<(), String> {
        // The iterator for the lines in the script.
        let lines = self.script.lines();

        for line in lines {
            // Split each line by the whitespace.
            // The first string before the whitespace will be the command,
            // and the second string will be the argument.
            // 
            // If split_first_space cant find a space, it will return Err,
            // and this will skip that line and try to parse the next.
            let (command, args) = match split_first_space(line.to_string()) {
                Ok((c, a)) => (c, a),
                Err(_) => continue
            };

            // Match the command and args as a tuple of &str
            match (command.as_str(), args.as_str()) {
                // The current instruction is a package declaration.
                // First, we create the directory where the package contents
                // will be installed.
                // 
                // Then we add the directory to the user's path,
                // and give the running script the package name to use in future commands.
                ("package", name) => {
                    PathManager::make_package_dir(name.to_string())?;
                    PathManager::add_to_path(name.to_string())?;
                    self.package_name = Some(name.to_string());
                },
                // Clone a git repository into the current package.
                // If the current script is not a package installer, throw an error.
                ("git-clone", url) => {
                    match self.package_name.clone() {
                        Some(name) => {
                            gitclone(name, url.trim_start_matches("\"")
                                                .trim_start_matches("'")
                                                .trim_end_matches("\"")
                                                .trim_end_matches("'")
                                                .to_string())?;
                        },
                        None => {
                            return Err("Tried to clone repository into package install directory without declaring the install script as a package installer via the `package PACKAGE_NAME` rapture command.".to_string())
                        }
                    }
                },
                // Download a rapture script from url and install it before continuing.
                ("rapt-install", url) => {
                    install(url.to_string())?;
                },
                // Call the operating system's native package manager.
                ("backend-install", package) => {
                    backup::install(package.to_string())?;
                },
                // This is mainly a feature of the `capture` subcommand.
                // This is not meant for users to be messing around with.
                ("write-hex", path_hex) => {
                    match self.package_name.clone() {
                        Some(name) => {
                            // Split the argments into a path and the bytes to write
                            let (path, bytes) = match split_first_space(path_hex.to_string()) {
                                Ok((c, a)) => (c, a),
                                Err(_) => continue
                            };
                            
                            // Get the path relative to the package install directory
                            let package_dir = PathManager::package_dir(name.to_string());
                            let mut absolute_path = PathBuf::new();
                            absolute_path.push(package_dir);
                            absolute_path.push(path);
                            
                            // Open the file for writing
                            let mut file = match OpenOptions::new()
                                .create(true)
                                .write(true)
                                .open(absolute_path.clone()) 
                            {
                                Ok(f) => Ok(f),
                                Err(_) => Err(format!("Could not open file '{}'", path_to_string(absolute_path.clone())))
                            }?;

                            // Decode the hex string into a list of Vec<u8>.
                            // These are not UTF-8 characters!!! These are the 
                            // bytes to write directly to the opened file.
                            match decode(bytes.clone()) {
                                Ok(vector) => match file.write_all(&vector) {
                                    Ok(_) => {},
                                    Err(_) => return Err(format!("Could not write decoded bytes to file '{}'", path_to_string(absolute_path)))
                                },
                                Err(_) => return Err(format!("Could not decode hex code '{}'", bytes.clone()))
                            };
                        },
                        None => {
                            return Err("Tried to write hex to a file without declaring the install script as a package installer via the `package PACKAGE_NAME` rapture command.".to_string())
                        }
                    }
                },
                // Make a directory.
                // This can be a directory that has non-existant parent directories.
                // For example, if I invoke the rapture command:
                // `mkdir ./cmake/contrib/profiling`
                // Rapture will create each of the parent directories if they do not already exist.
                ("mkdir", path) => {
                    match self.package_name.clone() {
                        Some(name) => {
                            // Make the path a relative path to the package install directory
                            let package_dir = PathManager::package_dir(name.to_string());
                            let mut absolute_path = PathBuf::new();
                            absolute_path.push(package_dir);
                            absolute_path.push(path);

                            // Create the folder using create_dir_all.
                            // create_dir_all creates parent directories as needed,
                            // similar to mkdir -p DIRECTORY
                            match create_dir_all(absolute_path.clone()) {
                                Ok(()) => {},
                                Err(_) => return Err(format!("Failed to create directory {}", path_to_string(absolute_path)))
                            }
                        },
                        None => {
                            return Err("Tried to make directory without declaring the install script as a package installer via the `package PACKAGE_NAME` rapture command.".to_string())
                        }
                    }
                },
                // This prints a message to the console
                ("echo", string) => {
                    println!("{}", string);
                },
                // This command adds a path to the users path.
                // This is mainly used if there is a `bin` directory or another directory
                // within the package install directory that needs to be added to the path.
                // 
                // add-path can only be used after the package declaration.
                ("add-path", path) => {
                    match self.package_name.clone() {
                        Some(name) => {
                            // Get the path relative to the package dir
                            let package_dir = PathManager::package_dir(name.to_string());
                            let mut absolute_path = PathBuf::new();
                            absolute_path.push(package_dir);
                            absolute_path.push(path);

                            // Call frontend::add_to_path
                            add_to_path(path_to_string(absolute_path))?;
                        },
                        None => {
                            return Err("Tried to add to path without declaring the install script as a package installer via the `package PACKAGE_NAME` rapture command.".to_string())
                        }
                    }
                },
                // The hastag symbol denotes a comment
                ("#", _) => {},

                // The following commands run `arg` as a shell
                // command on their respective operating systems.
                ("WINDOWS", cmd) => {
                    if Platform::get() == Platform::Windows {
                        self.command(cmd)?;
                    }
                },
                ("MACOS", cmd) => {
                    if Platform::get() == Platform::MacOS {
                        self.command(cmd)?;
                    }
                },
                ("UBUNTU", cmd) | ("LINUX", cmd) => {
                    if Platform::get() == Platform::Ubuntu {
                        self.command(cmd)?;
                    }
                },
                ("UNKNOWN", cmd) => {
                    if Platform::get() == Platform::Unknown {
                        self.command(cmd)?;
                    }
                },
                // Runs a command on all operating systems
                ("*", cmd) => {
                    self.command(cmd)?;
                },
                ("", "") => {},
                // An unrecognized command was given, return Err
                (command, args) => {
                    return Err(format!("Unrecognized command '{} {}'", command, args));
                }
            }
        }
        Ok(())
    }
}

// Dummy Display impl used for debugging
impl Display for Script {
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
        write!(f, "{}", self.script)
    }
}