Add --root global argument. Fixes #899. (#932)

This commit is contained in:
ethereal 2020-01-21 14:52:24 -05:00 committed by Vincent Prouillet
parent 5ba4d47538
commit 94445ae745
7 changed files with 38 additions and 9 deletions

View file

@ -47,7 +47,7 @@ pub struct Site {
impl Site { impl Site {
/// Parse a site at the given path. Defaults to the current dir /// 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<P: AsRef<Path>>(path: P, config_file: &str) -> Result<Site> { pub fn new<P: AsRef<Path>>(path: P, config_file: &str) -> Result<Site> {
let path = path.as_ref(); let path = path.as_ref();
let mut config = get_config(path, config_file); let mut config = get_config(path, config_file);

View file

@ -50,12 +50,17 @@ You can override the default output directory `public` by passing another value
$ zola build --output-dir $DOCUMENT_ROOT $ 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 ```bash
$ zola --config config.staging.toml build $ 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. By default, drafts are not loaded. If you wish to include them, pass the `--drafts` flag.
## serve ## serve

View file

@ -6,6 +6,14 @@ pub fn build_cli() -> App<'static, 'static> {
.author(crate_authors!()) .author(crate_authors!())
.about(crate_description!()) .about(crate_description!())
.setting(AppSettings::SubcommandRequiredElseHelp) .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(
Arg::with_name("config") Arg::with_name("config")
.short("c") .short("c")

View file

@ -1,4 +1,4 @@
use std::env; use std::path::Path;
use errors::Result; use errors::Result;
use site::Site; use site::Site;
@ -6,12 +6,13 @@ use site::Site;
use crate::console; use crate::console;
pub fn build( pub fn build(
root_dir: &Path,
config_file: &str, config_file: &str,
base_url: Option<&str>, base_url: Option<&str>,
output_dir: &str, output_dir: &str,
include_drafts: bool, include_drafts: bool,
) -> Result<()> { ) -> 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); site.set_output_path(output_dir);
if let Some(b) = base_url { if let Some(b) = base_url {
site.set_base_url(b.to_string()); site.set_base_url(b.to_string());

View file

@ -1,5 +1,4 @@
use std::env; use std::path::{Path,PathBuf};
use std::path::PathBuf;
use errors::Result; use errors::Result;
use site::Site; use site::Site;
@ -7,12 +6,13 @@ use site::Site;
use crate::console; use crate::console;
pub fn check( pub fn check(
root_dir: &Path,
config_file: &str, config_file: &str,
base_path: Option<&str>, base_path: Option<&str>,
base_url: Option<&str>, base_url: Option<&str>,
include_drafts: bool, include_drafts: bool,
) -> Result<()> { ) -> 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)?; let mut site = Site::new(bp, config_file)?;
// Force the checking of external links // Force the checking of external links
site.config.enable_check_mode(); site.config.enable_check_mode();

View file

@ -158,6 +158,7 @@ fn rebuild_done_handling(broadcaster: &Option<Sender>, res: Result<()>, reload_p
} }
fn create_new_site( fn create_new_site(
root_dir: &Path,
interface: &str, interface: &str,
port: u16, port: u16,
output_dir: &str, output_dir: &str,
@ -165,7 +166,7 @@ fn create_new_site(
config_file: &str, config_file: &str,
include_drafts: bool, include_drafts: bool,
) -> Result<(Site, String)> { ) -> 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 base_address = format!("{}:{}", base_url, port);
let address = format!("{}:{}", interface, port); let address = format!("{}:{}", interface, port);
@ -190,6 +191,7 @@ fn create_new_site(
} }
pub fn serve( pub fn serve(
root_dir: &Path,
interface: &str, interface: &str,
port: u16, port: u16,
output_dir: &str, output_dir: &str,
@ -201,7 +203,7 @@ pub fn serve(
) -> Result<()> { ) -> Result<()> {
let start = Instant::now(); let start = Instant::now();
let (mut site, address) = 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); console::report_elapsed_time(start);
// Setup watchers // Setup watchers
@ -443,6 +445,7 @@ pub fn serve(
"-> Themes changed. The whole site will be reloaded.", "-> Themes changed. The whole site will be reloaded.",
); );
site = create_new_site( site = create_new_site(
root_dir,
interface, interface,
port, port,
output_dir, output_dir,
@ -457,6 +460,7 @@ pub fn serve(
ChangeKind::Config => { ChangeKind::Config => {
console::info("-> Config changed. The whole site will be reloaded. The browser needs to be refreshed to make the changes visible."); 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( site = create_new_site(
root_dir,
interface, interface,
port, port,
output_dir, output_dir,
@ -504,6 +508,7 @@ pub fn serve(
"-> Themes changed. The whole site will be reloaded.", "-> Themes changed. The whole site will be reloaded.",
); );
site = create_new_site( site = create_new_site(
root_dir,
interface, interface,
port, port,
output_dir, output_dir,
@ -518,6 +523,7 @@ pub fn serve(
(ChangeKind::Config, _) => { (ChangeKind::Config, _) => {
console::info("-> Config changed. The whole site will be reloaded. The browser needs to be refreshed to make the changes visible."); 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( site = create_new_site(
root_dir,
interface, interface,
port, port,
output_dir, output_dir,

View file

@ -1,3 +1,5 @@
use std::env;
use std::path::PathBuf;
use std::time::Instant; use std::time::Instant;
use utils::net::{get_available_port, port_is_available}; use utils::net::{get_available_port, port_is_available};
@ -10,6 +12,10 @@ mod prompt;
fn main() { fn main() {
let matches = cli::build_cli().get_matches(); 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(); let config_file = matches.value_of("config").unwrap();
match matches.subcommand() { match matches.subcommand() {
@ -27,6 +33,7 @@ fn main() {
let start = Instant::now(); let start = Instant::now();
let output_dir = matches.value_of("output_dir").unwrap(); let output_dir = matches.value_of("output_dir").unwrap();
match cmd::build( match cmd::build(
&root_dir,
config_file, config_file,
matches.value_of("base_url"), matches.value_of("base_url"),
output_dir, output_dir,
@ -70,6 +77,7 @@ fn main() {
let base_url = matches.value_of("base_url").unwrap(); let base_url = matches.value_of("base_url").unwrap();
console::info("Building site..."); console::info("Building site...");
match cmd::serve( match cmd::serve(
&root_dir,
interface, interface,
port, port,
output_dir, output_dir,
@ -90,6 +98,7 @@ fn main() {
console::info("Checking site..."); console::info("Checking site...");
let start = Instant::now(); let start = Instant::now();
match cmd::check( match cmd::check(
&root_dir,
config_file, config_file,
matches.value_of("base_path"), matches.value_of("base_path"),
matches.value_of("base_url"), matches.value_of("base_url"),