From 94445ae7456a2539dfbd19f637c3f207324ac1cc Mon Sep 17 00:00:00 2001 From: ethereal Date: Tue, 21 Jan 2020 14:52:24 -0500 Subject: [PATCH] Add --root global argument. Fixes #899. (#932) --- components/site/src/lib.rs | 2 +- .../content/documentation/getting-started/cli-usage.md | 7 ++++++- src/cli.rs | 8 ++++++++ src/cmd/build.rs | 5 +++-- src/cmd/check.rs | 6 +++--- src/cmd/serve.rs | 10 ++++++++-- src/main.rs | 9 +++++++++ 7 files changed, 38 insertions(+), 9 deletions(-) diff --git a/components/site/src/lib.rs b/components/site/src/lib.rs index 9c156cd..c83c860 100644 --- a/components/site/src/lib.rs +++ b/components/site/src/lib.rs @@ -47,7 +47,7 @@ pub struct Site { impl Site { /// Parse a site at the given path. Defaults to the current dir - /// Passing in a path is only used in tests + /// Passing in a path is used in tests and when --root argument is passed pub fn new>(path: P, config_file: &str) -> Result { let path = path.as_ref(); let mut config = get_config(path, config_file); diff --git a/docs/content/documentation/getting-started/cli-usage.md b/docs/content/documentation/getting-started/cli-usage.md index 8ba95d3..284eb57 100644 --- a/docs/content/documentation/getting-started/cli-usage.md +++ b/docs/content/documentation/getting-started/cli-usage.md @@ -50,12 +50,17 @@ You can override the default output directory `public` by passing another value $ zola build --output-dir $DOCUMENT_ROOT ``` -You can also point to a config file other than `config.toml` like so (note that the position of the `config` option is important): +You can point to a config file other than `config.toml` like so (note that the position of the `config` option is important): ```bash $ zola --config config.staging.toml build ``` +You can also process a project from a different directory with the `root` flag. If building a project 'out-of-tree' with the `root` flag, you may want to combine it with the `output-dir` flag. (Note that like `config`, the position is important): +```bash +$ zola --root /path/to/project build +``` + By default, drafts are not loaded. If you wish to include them, pass the `--drafts` flag. ## serve diff --git a/src/cli.rs b/src/cli.rs index 0a57632..975bb88 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -6,6 +6,14 @@ pub fn build_cli() -> App<'static, 'static> { .author(crate_authors!()) .about(crate_description!()) .setting(AppSettings::SubcommandRequiredElseHelp) + .arg( + Arg::with_name("root") + .short("r") + .long("root") + .takes_value(true) + .default_value(".") + .help("Directory to use as root of project") + ) .arg( Arg::with_name("config") .short("c") diff --git a/src/cmd/build.rs b/src/cmd/build.rs index 9d3e3c2..44d5e4b 100644 --- a/src/cmd/build.rs +++ b/src/cmd/build.rs @@ -1,4 +1,4 @@ -use std::env; +use std::path::Path; use errors::Result; use site::Site; @@ -6,12 +6,13 @@ use site::Site; use crate::console; pub fn build( + root_dir: &Path, config_file: &str, base_url: Option<&str>, output_dir: &str, include_drafts: bool, ) -> Result<()> { - let mut site = Site::new(env::current_dir().unwrap(), config_file)?; + let mut site = Site::new(root_dir, config_file)?; site.set_output_path(output_dir); if let Some(b) = base_url { site.set_base_url(b.to_string()); diff --git a/src/cmd/check.rs b/src/cmd/check.rs index be6c299..de2e841 100644 --- a/src/cmd/check.rs +++ b/src/cmd/check.rs @@ -1,5 +1,4 @@ -use std::env; -use std::path::PathBuf; +use std::path::{Path,PathBuf}; use errors::Result; use site::Site; @@ -7,12 +6,13 @@ use site::Site; use crate::console; pub fn check( + root_dir: &Path, config_file: &str, base_path: Option<&str>, base_url: Option<&str>, include_drafts: bool, ) -> Result<()> { - let bp = base_path.map(PathBuf::from).unwrap_or_else(|| env::current_dir().unwrap()); + let bp = base_path.map(PathBuf::from).unwrap_or_else(|| PathBuf::from(root_dir)); let mut site = Site::new(bp, config_file)?; // Force the checking of external links site.config.enable_check_mode(); diff --git a/src/cmd/serve.rs b/src/cmd/serve.rs index b2f538d..3c98fea 100644 --- a/src/cmd/serve.rs +++ b/src/cmd/serve.rs @@ -158,6 +158,7 @@ fn rebuild_done_handling(broadcaster: &Option, res: Result<()>, reload_p } fn create_new_site( + root_dir: &Path, interface: &str, port: u16, output_dir: &str, @@ -165,7 +166,7 @@ fn create_new_site( config_file: &str, include_drafts: bool, ) -> Result<(Site, String)> { - let mut site = Site::new(env::current_dir().unwrap(), config_file)?; + let mut site = Site::new(root_dir, config_file)?; let base_address = format!("{}:{}", base_url, port); let address = format!("{}:{}", interface, port); @@ -190,6 +191,7 @@ fn create_new_site( } pub fn serve( + root_dir: &Path, interface: &str, port: u16, output_dir: &str, @@ -201,7 +203,7 @@ pub fn serve( ) -> Result<()> { let start = Instant::now(); let (mut site, address) = - create_new_site(interface, port, output_dir, base_url, config_file, include_drafts)?; + create_new_site(root_dir, interface, port, output_dir, base_url, config_file, include_drafts)?; console::report_elapsed_time(start); // Setup watchers @@ -443,6 +445,7 @@ pub fn serve( "-> Themes changed. The whole site will be reloaded.", ); site = create_new_site( + root_dir, interface, port, output_dir, @@ -457,6 +460,7 @@ pub fn serve( ChangeKind::Config => { console::info("-> Config changed. The whole site will be reloaded. The browser needs to be refreshed to make the changes visible."); site = create_new_site( + root_dir, interface, port, output_dir, @@ -504,6 +508,7 @@ pub fn serve( "-> Themes changed. The whole site will be reloaded.", ); site = create_new_site( + root_dir, interface, port, output_dir, @@ -518,6 +523,7 @@ pub fn serve( (ChangeKind::Config, _) => { console::info("-> Config changed. The whole site will be reloaded. The browser needs to be refreshed to make the changes visible."); site = create_new_site( + root_dir, interface, port, output_dir, diff --git a/src/main.rs b/src/main.rs index 06b2eb5..935e547 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +use std::env; +use std::path::PathBuf; use std::time::Instant; use utils::net::{get_available_port, port_is_available}; @@ -10,6 +12,10 @@ mod prompt; fn main() { let matches = cli::build_cli().get_matches(); + let root_dir = match matches.value_of("root").unwrap() { + "." => env::current_dir().unwrap(), + path => PathBuf::from(path), + }; let config_file = matches.value_of("config").unwrap(); match matches.subcommand() { @@ -27,6 +33,7 @@ fn main() { let start = Instant::now(); let output_dir = matches.value_of("output_dir").unwrap(); match cmd::build( + &root_dir, config_file, matches.value_of("base_url"), output_dir, @@ -70,6 +77,7 @@ fn main() { let base_url = matches.value_of("base_url").unwrap(); console::info("Building site..."); match cmd::serve( + &root_dir, interface, port, output_dir, @@ -90,6 +98,7 @@ fn main() { console::info("Checking site..."); let start = Instant::now(); match cmd::check( + &root_dir, config_file, matches.value_of("base_path"), matches.value_of("base_url"),