-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from mfontanini/verify-exporter-version
Verify exporter version
- Loading branch information
Showing
7 changed files
with
158 additions
and
66 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
use std::{ | ||
io::{self, Write}, | ||
process::{Command, Output, Stdio}, | ||
}; | ||
|
||
use itertools::Itertools; | ||
|
||
const MAX_ERROR_LINES: usize = 10; | ||
|
||
pub(crate) struct ThirdPartyTools; | ||
|
||
impl ThirdPartyTools { | ||
pub(crate) fn pandoc(args: &[&str]) -> Tool { | ||
Tool::new("pandoc", args) | ||
} | ||
|
||
pub(crate) fn typst(args: &[&str]) -> Tool { | ||
Tool::new("typst", args) | ||
} | ||
|
||
pub(crate) fn presenterm_export(args: &[&str]) -> Tool { | ||
Tool::new("presenterm-export", args).inherit_stdout() | ||
} | ||
} | ||
|
||
pub(crate) struct Tool { | ||
command_name: &'static str, | ||
command: Command, | ||
stdin: Option<Vec<u8>>, | ||
} | ||
|
||
impl Tool { | ||
fn new(command_name: &'static str, args: &[&str]) -> Self { | ||
let mut command = Command::new(command_name); | ||
command.args(args).stdout(Stdio::null()).stderr(Stdio::piped()); | ||
Self { command_name, command, stdin: None } | ||
} | ||
|
||
pub(crate) fn stdin(mut self, stdin: Vec<u8>) -> Self { | ||
self.stdin = Some(stdin); | ||
self | ||
} | ||
|
||
pub(crate) fn inherit_stdout(mut self) -> Self { | ||
self.command.stdout(Stdio::inherit()); | ||
self | ||
} | ||
|
||
pub(crate) fn run(self) -> Result<(), ExecutionError> { | ||
self.spawn()?; | ||
Ok(()) | ||
} | ||
|
||
pub(crate) fn run_and_capture_stdout(mut self) -> Result<Vec<u8>, ExecutionError> { | ||
self.command.stdout(Stdio::piped()); | ||
|
||
let output = self.spawn()?; | ||
Ok(output.stdout) | ||
} | ||
|
||
fn spawn(mut self) -> Result<Output, ExecutionError> { | ||
use ExecutionError::*; | ||
if self.stdin.is_some() { | ||
self.command.stdin(Stdio::piped()); | ||
} | ||
let mut child = self.command.spawn().map_err(|error| Spawn { command: self.command_name, error })?; | ||
if let Some(data) = &self.stdin { | ||
let mut stdin = child.stdin.take().expect("no stdin"); | ||
stdin | ||
.write_all(data) | ||
.and_then(|_| stdin.flush()) | ||
.map_err(|error| Communication { command: self.command_name, error })?; | ||
} | ||
let output = child.wait_with_output().map_err(|error| Communication { command: self.command_name, error })?; | ||
self.validate_output(&output)?; | ||
Ok(output) | ||
} | ||
|
||
fn validate_output(self, output: &Output) -> Result<(), ExecutionError> { | ||
if output.status.success() { | ||
Ok(()) | ||
} else { | ||
let stderr = String::from_utf8_lossy(&output.stderr).lines().take(MAX_ERROR_LINES).join("\n"); | ||
Err(ExecutionError::Execution { command: self.command_name, stderr }) | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, thiserror::Error)] | ||
pub enum ExecutionError { | ||
#[error("spawning '{command}' failed: {error}")] | ||
Spawn { command: &'static str, error: io::Error }, | ||
|
||
#[error("communicating with '{command}' failed: {error}")] | ||
Communication { command: &'static str, error: io::Error }, | ||
|
||
#[error("'{command}' execution failed: \n{stderr}")] | ||
Execution { command: &'static str, stderr: String }, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters