summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorEric Huss <eric@huss.org>2020-12-30 17:26:59 -0800
committerEric Huss <eric@huss.org>2020-12-30 17:46:29 -0800
commit9a65c8ab9233c0791d1b0dbf5d684ff196f8ebbe (patch)
tree9e19fa10a0b6c19b650bc27cd1f5f487f62d4456 /src
parenta64a7b747080dc3cbc9d57aab36d83e609444020 (diff)
Fix relative paths for renderer commands.
Diffstat (limited to 'src')
-rw-r--r--src/renderer/mod.rs42
1 files changed, 36 insertions, 6 deletions
diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs
index f14a9f30..8af2fabc 100644
--- a/src/renderer/mod.rs
+++ b/src/renderer/mod.rs
@@ -20,7 +20,7 @@ mod markdown_renderer;
use shlex::Shlex;
use std::fs;
use std::io::{self, ErrorKind, Read};
-use std::path::PathBuf;
+use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use crate::book::Book;
@@ -133,14 +133,44 @@ impl CmdRenderer {
CmdRenderer { name, cmd }
}
- fn compose_command(&self) -> Result<Command> {
+ fn compose_command(&self, root: &Path, destination: &Path) -> Result<Command> {
let mut words = Shlex::new(&self.cmd);
- let executable = match words.next() {
- Some(e) => e,
+ let exe = match words.next() {
+ Some(e) => PathBuf::from(e),
None => bail!("Command string was empty"),
};
- let mut cmd = Command::new(executable);
+ let exe = if exe.components().count() == 1 {
+ // Search PATH for the executable.
+ exe
+ } else {
+ // Relative paths are preferred to be relative to the book root.
+ let abs_exe = root.join(&exe);
+ if abs_exe.exists() {
+ abs_exe
+ } else {
+ // Historically paths were relative to the destination, but
+ // this is not the preferred way.
+ let legacy_path = destination.join(&exe);
+ if legacy_path.exists() {
+ warn!(
+ "Renderer command `{}` uses a path relative to the \
+ renderer output directory `{}`. This was previously \
+ accepted, but has been deprecated. Relative executable \
+ paths should be relative to the book root.",
+ exe.display(),
+ destination.display()
+ );
+ legacy_path
+ } else {
+ // Let this bubble through to later be handled by
+ // handle_render_command_error.
+ abs_exe.to_path_buf()
+ }
+ }
+ };
+
+ let mut cmd = Command::new(exe);
for arg in words {
cmd.arg(arg);
@@ -195,7 +225,7 @@ impl Renderer for CmdRenderer {
let _ = fs::create_dir_all(&ctx.destination);
let mut child = match self
- .compose_command()?
+ .compose_command(&ctx.root, &ctx.destination)?
.stdin(Stdio::piped())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())