Skip to content
Myr

mod.rs

This file

Path

src/exec/mod.rs

177 lines · 5.5 KiB · Rust

Last changed by

Re-enable code style checksharkdpbefore adoptiona5f5ad6

History of this file

Take it

Relicense under MIT/Apache-2.0Antti Keränenbefore adoption701b8f2

lines 1–8why, one step back
1// Copyright (c) 2017 fd developers
2// 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.8

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

line 9why, one step back
9// TODO: Possible optimization could avoid pushing characters on a buffer.

Remove CommandTicket to avoid Vec allocationMatthias Reitingerbefore adoptionc18caaa

line 10why, one step back
10mod command;

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

lines 11–12why, one step back
11mod token;
12mod job;

Escape exec InputsMichael Aaron Murphybefore adoption6a01faf

line 13why, one step back
13mod input;

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

line 14why, one step back
14

Remove shell with --execMatthias Reitingerbefore adoption18709b1

line 15why, one step back
15use std::borrow::Cow;

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

line 16why, one step back
16use std::path::Path;

Remove CommandTicket to avoid Vec allocationMatthias Reitingerbefore adoptionc18caaa

line 17why, one step back
17use std::process::Command;

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

lines 18–19why, one step back
18use std::sync::{Arc, Mutex};
19

Remove shell with --execMatthias Reitingerbefore adoption18709b1

lines 20–22why, one step back
20use regex::Regex;
2122use self::input::{basename, dirname, remove_extension};

Remove CommandTicket to avoid Vec allocationMatthias Reitingerbefore adoptionc18caaa

line 23why, one step back
23use self::command::execute_command;

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

lines 24–26why, one step back
24use self::token::Token;
25pub use self::job::job;26

Rename TokenizedCommand to CommandTemplateMatthias Reitingerbefore adoption0f82fe7

line 27why, one step back
27/// Represents a template that is utilized to generate command strings.

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

line 28why, one step back
28///

Rename TokenizedCommand to CommandTemplateMatthias Reitingerbefore adoption0f82fe7

lines 29–30why, one step back
29/// The template is meant to be coupled with an input in order to generate a command. The
30/// `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 back
31#[derive(Debug, Clone, PartialEq)]

Rename TokenizedCommand to CommandTemplateMatthias Reitingerbefore adoption0f82fe7

lines 32–33why, one step back
32pub struct CommandTemplate {
33 args: Vec<ArgumentTemplate>,

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

lines 34–35why, one step back
34}
35

Rename TokenizedCommand to CommandTemplateMatthias Reitingerbefore adoption0f82fe7

lines 36–37why, one step back
36impl CommandTemplate {
37 pub fn new<I, S>(input: I) -> CommandTemplate

Remove shell with --execMatthias Reitingerbefore adoption18709b1

lines 38–42why, one step back
38 where
39 I: IntoIterator<Item = S>,40 S: AsRef<str>,41 {42 lazy_static! {

Remove CommandTicket to avoid Vec allocationMatthias Reitingerbefore adoptionc18caaa

line 43why, one step back
43 static ref PLACEHOLDER_PATTERN: Regex = Regex::new(r"\{(/?\.?|//)\}").unwrap();

Remove shell with --execMatthias Reitingerbefore adoption18709b1

lines 44–54why, one step back
44 }
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;54

Remove CommandTicket to avoid Vec allocationMatthias Reitingerbefore adoptionc18caaa

line 55why, one step back
55 for placeholder in PLACEHOLDER_PATTERN.find_iter(arg) {

Remove shell with --execMatthias Reitingerbefore adoption18709b1

lines 56–58why, one step back
56 // 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 back
59 }

Remove shell with --execMatthias Reitingerbefore adoption18709b1

lines 60–69why, one step back
60
61 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 back
70 }

Remove shell with --execMatthias Reitingerbefore adoption18709b1

lines 71–72why, one step back
71
72 has_placeholder = true;

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

lines 73–74why, one step back
73 }
74

Remove shell with --execMatthias Reitingerbefore adoption18709b1

lines 75–76why, one step back
75 // Without a placeholder, the argument is just fixed text.
76 if tokens.is_empty() {

Rename TokenizedCommand to CommandTemplateMatthias Reitingerbefore adoption0f82fe7

line 77why, one step back
77 args.push(ArgumentTemplate::Text(arg.to_owned()));

Remove shell with --execMatthias Reitingerbefore adoption18709b1

lines 78–85why, one step back
78 continue;
79 }8081 if start < arg.len() {82 // Trailing text after last placeholder.83 tokens.push(Token::Text(arg[start..].to_owned()));84 }85

Rename TokenizedCommand to CommandTemplateMatthias Reitingerbefore adoption0f82fe7

line 86why, one step back
86 args.push(ArgumentTemplate::Tokens(tokens));

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

lines 87–89why, one step back
87 }
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 back
90 if !has_placeholder {

Rename TokenizedCommand to CommandTemplateMatthias Reitingerbefore adoption0f82fe7

line 91why, one step back
91 args.push(ArgumentTemplate::Tokens(vec![Token::Placeholder]));

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

lines 92–93why, one step back
92 }
93

Rename TokenizedCommand to CommandTemplateMatthias Reitingerbefore adoption0f82fe7

line 94why, one step back
94 CommandTemplate { args: args }

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

lines 95–96why, one step back
95 }
96

Remove CommandTicket to avoid Vec allocationMatthias Reitingerbefore adoptionc18caaa

line 97why, one step back
97 /// Generates and executes a command.

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

line 98why, one step back
98 ///

Remove CommandTicket to avoid Vec allocationMatthias Reitingerbefore adoptionc18caaa

lines 99–101why, one step back
99 /// Using the internal `args` field, and a supplied `input` variable, a `Command` will be
100 /// 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 back
102 let input = input
103 .strip_prefix(".")104 .unwrap_or(input)105 .to_string_lossy()106 .into_owned();107

Remove CommandTicket to avoid Vec allocationMatthias Reitingerbefore adoptionc18caaa

lines 108–110why, one step back
108 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 back
111 }
112

Remove CommandTicket to avoid Vec allocationMatthias Reitingerbefore adoptionc18caaa

line 113why, one step back
113 execute_command(cmd, out_perm)

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

lines 114–116why, one step back
114 }
115}116

Rename TokenizedCommand to CommandTemplateMatthias Reitingerbefore adoption0f82fe7

lines 117–150why, one step back
117/// 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}150

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

lines 151–152why, one step back
151#[cfg(test)]
152mod tests {

Re-enable code style checksharkdpbefore adoptiona5f5ad6

line 153why, one step back
153 use super::{ArgumentTemplate, CommandTemplate, Token};

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

lines 154–156why, one step back
154
155 #[test]156 fn tokens() {

Rename TokenizedCommand to CommandTemplateMatthias Reitingerbefore adoption0f82fe7

line 157why, one step back
157 let expected = CommandTemplate {

Remove shell with --execMatthias Reitingerbefore adoption18709b1

line 158why, one step back
158 args: vec![

Rename TokenizedCommand to CommandTemplateMatthias Reitingerbefore adoption0f82fe7

lines 159–161why, one step back
159 ArgumentTemplate::Text("echo".into()),
160 ArgumentTemplate::Text("${SHELL}:".into()),161 ArgumentTemplate::Tokens(vec![Token::Placeholder]),

Remove shell with --execMatthias Reitingerbefore adoption18709b1

line 162why, one step back
162 ],

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

lines 163–164why, one step back
163 };
164

Rename TokenizedCommand to CommandTemplateMatthias Reitingerbefore adoption0f82fe7

line 165why, one step back
165 assert_eq!(CommandTemplate::new(&[&"echo", &"${SHELL}:"]), expected);

Remove shell with --execMatthias Reitingerbefore adoption18709b1

line 166why, one step back
166

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

line 167why, one step back
167 assert_eq!(

Rename TokenizedCommand to CommandTemplateMatthias Reitingerbefore adoption0f82fe7

lines 168–169why, one step back
168 CommandTemplate::new(&["echo", "{.}"]),
169 CommandTemplate {

Remove shell with --execMatthias Reitingerbefore adoption18709b1

line 170why, one step back
170 args: vec![

Rename TokenizedCommand to CommandTemplateMatthias Reitingerbefore adoption0f82fe7

lines 171–172why, one step back
171 ArgumentTemplate::Text("echo".into()),
172 ArgumentTemplate::Tokens(vec![Token::NoExt]),

Remove shell with --execMatthias Reitingerbefore adoption18709b1

lines 173–174why, one step back
173 ],
174 }

Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07

lines 175–177why, one step back
175 );
176 }177}