Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jan 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/site/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<P: AsRef<Path>>(path: P, config_file: &str) -> Result<Site> {
let path = path.as_ref();
let mut config = get_config(path, config_file);
Expand Down
7 changes: 6 additions & 1 deletion docs/content/documentation/getting-started/cli-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
5 changes: 3 additions & 2 deletions src/cmd/build.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
use std::env;
use std::path::Path;

use errors::Result;
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());
Expand Down
6 changes: 3 additions & 3 deletions src/cmd/check.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use std::env;
use std::path::PathBuf;
use std::path::{Path,PathBuf};

use errors::Result;
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();
Expand Down
10 changes: 8 additions & 2 deletions src/cmd/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,15 @@ fn rebuild_done_handling(broadcaster: &Option<Sender>, res: Result<()>, reload_p
}

fn create_new_site(
root_dir: &Path,
interface: &str,
port: u16,
output_dir: &str,
base_url: &str,
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);
Expand All @@ -190,6 +191,7 @@ fn create_new_site(
}

pub fn serve(
root_dir: &Path,
interface: &str,
port: u16,
output_dir: &str,
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::env;
use std::path::PathBuf;
use std::time::Instant;

use utils::net::{get_available_port, port_is_available};
Expand All @@ -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() {
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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"),
Expand Down