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
use std::{
    io::{BufRead, BufReader},
    process::{Child, Command, Stdio},
    sync::{Arc, Mutex},
    time::Duration,
};

use crossbeam_channel::{unbounded, Receiver, Sender};

use crate::{Error, FnTaskLogHandler, Result, ShellTaskLog};

/// A [`ShellTask`] runs commands and provides a passthrough log handler
/// for each log line.
/// # Examples
///
/// ```
/// use shell_candy::{Result, ShellTaskLog, ShellTask};
///
/// fn main() -> Result<()> {
///   // Create a task to check the current version of `rustc` that is installed
///   let task = ShellTask::new("rustc --version", |line| {
///     match line {
///       // print all log lines with an "info: " prefix
///       ShellTaskLog::Stderr(message) | ShellTaskLog::Stdout(message) => eprintln!("info: {}", &message),
///     }
///   })?;
///
///   // Run the task
///   task.run()?;
///
///   Ok(())
/// }
/// ```
pub struct ShellTask {
    bin: String,
    args: Vec<String>,
    full_command: String,
    log_sender: Sender<ShellTaskLog>,
    log_receiver: Receiver<ShellTaskLog>,
    log_handler: Arc<FnTaskLogHandler>,
}

impl ShellTask {
    /// Create a new [`ShellTask`] with a log line handler.
    pub fn new<F>(command: &str, log_handler: F) -> Result<Self>
    where
        F: Fn(ShellTaskLog) + Send + Sync + 'static,
    {
        let command = command.to_string();
        let args: Vec<&str> = command.split(' ').collect();
        let (bin, args) = match args.len() {
            0 => Err(Error::InvalidCommand {
                command: command.to_string(),
                reason: "it is not a valid command".to_string(),
            }),
            1 => Ok((args[0], Vec::new())),
            _ => Ok((args[0], Vec::from_iter(args[1..].iter()))),
        }?;

        if which::which(bin).is_err() {
            Err(Error::InvalidCommand {
                command: command.to_string(),
                reason: format!("'{}' is not installed on this machine", &bin),
            })
        } else {
            let (log_sender, log_receiver) = unbounded();
            Ok(Self {
                bin: bin.to_string(),
                args: args.iter().map(|s| s.to_string()).collect(),
                full_command: command,
                log_sender,
                log_receiver,
                log_handler: Arc::new(Box::new(log_handler)),
            })
        }
    }

    /// Run a [`ShellTask`], applying the log handler to each line.
    pub fn run(&self) -> Result<()> {
        let log_counter: Arc<Mutex<Option<usize>>> = Arc::new(Mutex::new(None));

        let log_decrementer = log_counter.clone();
        let log_incrementer = log_counter.clone();
        let log_handler = self.log_handler.clone();
        let log_receiver = self.log_receiver.clone();

        rayon::spawn(move || loop {
            match log_receiver.recv() {
                Ok(line) => {
                    (log_handler)(line);
                    if let Some(count) = log_decrementer.clone().lock().unwrap().as_mut() {
                        *count -= 1;
                    }
                }
                Err(_) => break,
            }
        });

        let mut task = ShellTaskRunner::run(
            &self.bin,
            &self.args,
            self.full_command.to_string(),
            self.log_sender.clone(),
            log_incrementer,
        )?;

        let exit_status = task.child.wait().map_err(|source| Error::CouldNotWait {
            command: self.full_command.to_string(),
            source,
        })?;

        loop {
            std::thread::sleep(Duration::from_millis(200));
            match log_counter.try_lock().map(|l| *l) {
                Ok(Some(0)) => break,
                _ => continue,
            }
        }

        if exit_status.success() {
            Ok(())
        } else {
            Err(Error::CommandFailure {
                command: self.full_command.to_string(),
                exit_status,
            })
        }
    }
}

/// Runs a [`ShellTask`] in the background, reporting all logs and errors
#[derive(Debug)]
struct ShellTaskRunner {
    child: Child,
}

impl ShellTaskRunner {
    fn run(
        bin: &str,
        args: &Vec<String>,
        full_command: String,
        log_sender: Sender<ShellTaskLog>,
        log_incrementer: Arc<Mutex<Option<usize>>>,
    ) -> Result<Self> {
        let mut command = Command::new(bin);
        command.args(args).env("SHELL_CANDY", "true");

        command.stdout(Stdio::piped()).stderr(Stdio::piped());

        let mut child = command.spawn().map_err(|source| Error::CouldNotSpawn {
            command: full_command,
            source,
        })?;

        let stdout_incrementer = log_incrementer.clone();
        let stderr_incrementer = log_incrementer.clone();

        if let Some(stdout) = child.stdout.take() {
            let log_sender = log_sender.clone();
            rayon::spawn(move || {
                let stdout = BufReader::new(stdout);
                stdout.lines().for_each(|line| {
                    if let Ok(line) = line {
                        let guard = stdout_incrementer.clone();

                        match guard.lock() {
                            Ok(mut guard) => match guard.as_mut() {
                                Some(s) => {
                                    *s += 1;
                                }
                                None => {
                                    *guard = Some(1);
                                }
                            },
                            Err(e) => panic!("{}", e),
                        }

                        log_sender
                            .send(ShellTaskLog::Stdout(line))
                            .expect("could not update stdout logs for command");
                    }
                });
            });
        }

        if let Some(stderr) = child.stderr.take() {
            rayon::spawn(move || {
                let stderr = BufReader::new(stderr);
                stderr.lines().for_each(|line| {
                    if let Ok(line) = line {
                        let guard = stderr_incrementer.clone();

                        match guard.lock() {
                            Ok(mut guard) => match guard.as_mut() {
                                Some(s) => {
                                    *s += 1;
                                }
                                None => {
                                    *guard = Some(1);
                                }
                            },
                            Err(e) => panic!("{}", e),
                        }

                        log_sender
                            .send(ShellTaskLog::Stderr(line))
                            .expect("could not update stderr logs for command");
                    }
                });
            });
        }

        Ok(Self { child })
    }
}