mod.rs
This file
Path
src/exec/mod.rs
220 lines · 6.6 KiB · Rust
Last changed by
Add exec tests and cfg(test) compiler guardsJosh Leeb-du Toitbefore adoptionadc467b
History of this fileTake it
Relicense under MIT/Apache-2.0Antti Keränenbefore adoption701b8f2
lines 1–8why, one step back1// Copyright (c) 2017 fd developers2// Licensed under the Apache License, Version 2.03// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0>4// or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>,5// at your option. All files in the project carrying such6// notice may not be copied, modified, or distributed except7// according to those terms.8Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07
line 9why, one step back9// TODO: Possible optimization could avoid pushing characters on a buffer.Remove CommandTicket to avoid Vec allocationMatthias Reitingerbefore adoptionc18caaa
line 10why, one step back10mod command;Escape exec InputsMichael Aaron Murphybefore adoption6a01faf
line 11why, one step back11mod input;Run latest version of rustfmtsharkdpbefore adoption6a9f16e
lines 12–13why, one step back12mod job;13mod token;Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07
line 14why, one step back14Remove shell with --execMatthias Reitingerbefore adoption18709b1
line 15why, one step back15use std::borrow::Cow;Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07
line 16why, one step back16use std::path::Path;Remove CommandTicket to avoid Vec allocationMatthias Reitingerbefore adoptionc18caaa
line 17why, one step back17use std::process::Command;Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07
lines 18–19why, one step back18use std::sync::{Arc, Mutex};19Remove shell with --execMatthias Reitingerbefore adoption18709b1
lines 20–21why, one step back20use regex::Regex;21Remove CommandTicket to avoid Vec allocationMatthias Reitingerbefore adoptionc18caaa
line 22why, one step back22use self::command::execute_command;Run latest version of rustfmtsharkdpbefore adoption6a9f16e
line 23why, one step back23use self::input::{basename, dirname, remove_extension};Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07
line 24why, one step back24pub use self::job::job;Run latest version of rustfmtsharkdpbefore adoption6a9f16e
line 25why, one step back25use self::token::Token;Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07
line 26why, one step back26Rename TokenizedCommand to CommandTemplateMatthias Reitingerbefore adoption0f82fe7
line 27why, one step back27/// Represents a template that is utilized to generate command strings.Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07
line 28why, one step back28///Rename TokenizedCommand to CommandTemplateMatthias Reitingerbefore adoption0f82fe7
lines 29–30why, one step back29/// The template is meant to be coupled with an input in order to generate a command. The30/// `generate_and_execute()` method will be used to generate a command and execute it.Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07
line 31why, one step back31#[derive(Debug, Clone, PartialEq)]Rename TokenizedCommand to CommandTemplateMatthias Reitingerbefore adoption0f82fe7
lines 32–33why, one step back32pub struct CommandTemplate {33 args: Vec<ArgumentTemplate>,Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07
lines 34–35why, one step back34}35Rename TokenizedCommand to CommandTemplateMatthias Reitingerbefore adoption0f82fe7
lines 36–37why, one step back36impl CommandTemplate {37 pub fn new<I, S>(input: I) -> CommandTemplateRemove shell with --execMatthias Reitingerbefore adoption18709b1
lines 38–42why, one step back38 where39 I: IntoIterator<Item = S>,40 S: AsRef<str>,41 {42 lazy_static! {Remove CommandTicket to avoid Vec allocationMatthias Reitingerbefore adoptionc18caaa
line 43why, one step back43 static ref PLACEHOLDER_PATTERN: Regex = Regex::new(r"\{(/?\.?|//)\}").unwrap();Remove shell with --execMatthias Reitingerbefore adoption18709b1
lines 44–54why, one step back44 }4546 let mut args = Vec::new();47 let mut has_placeholder = false;4849 for arg in input {50 let arg = arg.as_ref();5152 let mut tokens = Vec::new();53 let mut start = 0;54Remove CommandTicket to avoid Vec allocationMatthias Reitingerbefore adoptionc18caaa
line 55why, one step back55 for placeholder in PLACEHOLDER_PATTERN.find_iter(arg) {Remove shell with --execMatthias Reitingerbefore adoption18709b1
lines 56–58why, one step back56 // Leading text before the placeholder.57 if placeholder.start() > start {58 tokens.push(Token::Text(arg[start..placeholder.start()].to_owned()));Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07
line 59why, one step back59 }Remove shell with --execMatthias Reitingerbefore adoption18709b1
lines 60–69why, one step back6061 start = placeholder.end();6263 match placeholder.as_str() {64 "{}" => tokens.push(Token::Placeholder),65 "{.}" => tokens.push(Token::NoExt),66 "{/}" => tokens.push(Token::Basename),67 "{//}" => tokens.push(Token::Parent),68 "{/.}" => tokens.push(Token::BasenameNoExt),69 _ => panic!("Unhandled placeholder"),Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07
line 70why, one step back70 }Remove shell with --execMatthias Reitingerbefore adoption18709b1
lines 71–72why, one step back7172 has_placeholder = true;Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07
lines 73–74why, one step back73 }74Remove shell with --execMatthias Reitingerbefore adoption18709b1
lines 75–76why, one step back75 // Without a placeholder, the argument is just fixed text.76 if tokens.is_empty() {Rename TokenizedCommand to CommandTemplateMatthias Reitingerbefore adoption0f82fe7
line 77why, one step back77 args.push(ArgumentTemplate::Text(arg.to_owned()));Remove shell with --execMatthias Reitingerbefore adoption18709b1
lines 78–85why, one step back78 continue;79 }8081 if start < arg.len() {82 // Trailing text after last placeholder.83 tokens.push(Token::Text(arg[start..].to_owned()));84 }85Rename TokenizedCommand to CommandTemplateMatthias Reitingerbefore adoption0f82fe7
line 86why, one step back86 args.push(ArgumentTemplate::Tokens(tokens));Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07
lines 87–89why, one step back87 }8889 // If a placeholder token was not supplied, append one at the end of the command.Remove shell with --execMatthias Reitingerbefore adoption18709b1
line 90why, one step back90 if !has_placeholder {Rename TokenizedCommand to CommandTemplateMatthias Reitingerbefore adoption0f82fe7
line 91why, one step back91 args.push(ArgumentTemplate::Tokens(vec![Token::Placeholder]));Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07
lines 92–93why, one step back92 }93Rename TokenizedCommand to CommandTemplateMatthias Reitingerbefore adoption0f82fe7
line 94why, one step back94 CommandTemplate { args: args }Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07
lines 95–96why, one step back95 }96Remove CommandTicket to avoid Vec allocationMatthias Reitingerbefore adoptionc18caaa
line 97why, one step back97 /// Generates and executes a command.Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07
line 98why, one step back98 ///Remove CommandTicket to avoid Vec allocationMatthias Reitingerbefore adoptionc18caaa
lines 99–101why, one step back99 /// Using the internal `args` field, and a supplied `input` variable, a `Command` will be100 /// build. Once all arguments have been processed, the command is executed.101 pub fn generate_and_execute(&self, input: &Path, out_perm: Arc<Mutex<()>>) {Remove shell with --execMatthias Reitingerbefore adoption18709b1
lines 102–107why, one step back102 let input = input103 .strip_prefix(".")104 .unwrap_or(input)105 .to_string_lossy()106 .into_owned();107Remove CommandTicket to avoid Vec allocationMatthias Reitingerbefore adoptionc18caaa
lines 108–110why, one step back108 let mut cmd = Command::new(self.args[0].generate(&input).as_ref());109 for arg in &self.args[1..] {110 cmd.arg(arg.generate(&input).as_ref());Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07
lines 111–112why, one step back111 }112Remove CommandTicket to avoid Vec allocationMatthias Reitingerbefore adoptionc18caaa
line 113why, one step back113 execute_command(cmd, out_perm)Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07
lines 114–116why, one step back114 }115}116Rename TokenizedCommand to CommandTemplateMatthias Reitingerbefore adoption0f82fe7
lines 117–150why, one step back117/// Represents a template for a single command argument.118///119/// The argument is either a collection of `Token`s including at least one placeholder variant, or120/// a fixed text.121#[derive(Clone, Debug, PartialEq)]122enum ArgumentTemplate {123 Tokens(Vec<Token>),124 Text(String),125}126127impl ArgumentTemplate {128 pub fn generate<'a>(&'a self, path: &str) -> Cow<'a, str> {129 use self::Token::*;130131 match *self {132 ArgumentTemplate::Tokens(ref tokens) => {133 let mut s = String::new();134 for token in tokens {135 match *token {136 Basename => s += basename(path),137 BasenameNoExt => s += remove_extension(basename(path)),138 NoExt => s += remove_extension(path),139 Parent => s += dirname(path),140 Placeholder => s += path,141 Text(ref string) => s += string,142 }143 }144 Cow::Owned(s)145 }146 ArgumentTemplate::Text(ref text) => Cow::Borrowed(text),147 }148 }149}150Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07
lines 151–152why, one step back151#[cfg(test)]152mod tests {Add exec tests and cfg(test) compiler guardsJosh Leeb-du Toitbefore adoptionadc467b
line 153why, one step back153 use super::*;Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07
lines 154–155why, one step back154155 #[test]Add exec tests and cfg(test) compiler guardsJosh Leeb-du Toitbefore adoptionadc467b
lines 156–167why, one step back156 fn tokens_with_placeholder() {157 assert_eq!(158 CommandTemplate::new(&[&"echo", &"${SHELL}:"]),159 CommandTemplate {160 args: vec![161 ArgumentTemplate::Text("echo".into()),162 ArgumentTemplate::Text("${SHELL}:".into()),163 ArgumentTemplate::Tokens(vec![Token::Placeholder]),164 ],165 }166 );167 }Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07
line 168why, one step back168Add exec tests and cfg(test) compiler guardsJosh Leeb-du Toitbefore adoptionadc467b
lines 169–170why, one step back169 #[test]170 fn tokens_with_no_extension() {Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07
line 171why, one step back171 assert_eq!(Rename TokenizedCommand to CommandTemplateMatthias Reitingerbefore adoption0f82fe7
lines 172–173why, one step back172 CommandTemplate::new(&["echo", "{.}"]),173 CommandTemplate {Remove shell with --execMatthias Reitingerbefore adoption18709b1
line 174why, one step back174 args: vec![Rename TokenizedCommand to CommandTemplateMatthias Reitingerbefore adoption0f82fe7
lines 175–176why, one step back175 ArgumentTemplate::Text("echo".into()),176 ArgumentTemplate::Tokens(vec![Token::NoExt]),Add exec tests and cfg(test) compiler guardsJosh Leeb-du Toitbefore adoptionadc467b
lines 177–215why, one step back177 ],178 }179 );180 }181182 #[test]183 fn tokens_with_basename() {184 assert_eq!(185 CommandTemplate::new(&["echo", "{/}"]),186 CommandTemplate {187 args: vec![188 ArgumentTemplate::Text("echo".into()),189 ArgumentTemplate::Tokens(vec![Token::Basename]),190 ],191 }192 );193 }194195 #[test]196 fn tokens_with_parent() {197 assert_eq!(198 CommandTemplate::new(&["echo", "{//}"]),199 CommandTemplate {200 args: vec![201 ArgumentTemplate::Text("echo".into()),202 ArgumentTemplate::Tokens(vec![Token::Parent]),203 ],204 }205 );206 }207208 #[test]209 fn tokens_with_basename_no_extension() {210 assert_eq!(211 CommandTemplate::new(&["echo", "{/.}"]),212 CommandTemplate {213 args: vec![214 ArgumentTemplate::Text("echo".into()),215 ArgumentTemplate::Tokens(vec![Token::BasenameNoExt]),Remove shell with --execMatthias Reitingerbefore adoption18709b1
lines 216–217why, one step back216 ],217 }Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07
lines 218–220why, one step back218 );219 }220}