From 974ae167a8b442471a81365513981112cc30e5a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tymoteusz=20Wi=C5=9Bniewski?= Date: Sat, 23 May 2020 11:55:45 +0200 Subject: [PATCH] Make path to config.toml independent from root dir (#1023) fixes #1022 --- completions/_zola.ps1 | 4 +-- completions/zola.fish | 2 +- components/config/src/lib.rs | 6 ++--- components/config/src/theme.rs | 2 +- components/rebuild/tests/rebuild.rs | 3 ++- components/site/benches/load.rs | 42 +++++++++++++++++++---------- components/site/benches/site.rs | 3 ++- components/site/src/lib.rs | 5 ++-- components/site/tests/common.rs | 6 +++-- components/site/tests/site.rs | 6 +++-- components/site/tests/site_i18n.rs | 3 ++- src/cli.rs | 3 +-- src/cmd/build.rs | 4 +-- src/cmd/check.rs | 2 +- src/cmd/serve.rs | 8 +++--- src/main.rs | 19 +++++++------ 16 files changed, 71 insertions(+), 47 deletions(-) diff --git a/completions/_zola.ps1 b/completions/_zola.ps1 index 89915d97..977ee071 100644 --- a/completions/_zola.ps1 +++ b/completions/_zola.ps1 @@ -20,8 +20,8 @@ Register-ArgumentCompleter -Native -CommandName 'zola' -ScriptBlock { $completions = @(switch ($command) { 'zola' { - [CompletionResult]::new('-c', 'c', [CompletionResultType]::ParameterName, 'Path to a config file other than config.toml') - [CompletionResult]::new('--config', 'config', [CompletionResultType]::ParameterName, 'Path to a config file other than config.toml') + [CompletionResult]::new('-c', 'c', [CompletionResultType]::ParameterName, 'Path to a config file other than config.toml in the root of project') + [CompletionResult]::new('--config', 'config', [CompletionResultType]::ParameterName, 'Path to a config file other than config.toml in the root of project') [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Prints help information') [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Prints help information') [CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Prints version information') diff --git a/completions/zola.fish b/completions/zola.fish index cb3302e5..a0d1bad8 100644 --- a/completions/zola.fish +++ b/completions/zola.fish @@ -1,4 +1,4 @@ -complete -c zola -n "__fish_use_subcommand" -s c -l config -d 'Path to a config file other than config.toml' +complete -c zola -n "__fish_use_subcommand" -s c -l config -d 'Path to a config file other than config.toml in the root of project' complete -c zola -n "__fish_use_subcommand" -s h -l help -d 'Prints help information' complete -c zola -n "__fish_use_subcommand" -s V -l version -d 'Prints version information' complete -c zola -n "__fish_use_subcommand" -f -a "init" -d 'Create a new Zola project' diff --git a/components/config/src/lib.rs b/components/config/src/lib.rs index 07fa104a..50eb9907 100644 --- a/components/config/src/lib.rs +++ b/components/config/src/lib.rs @@ -7,11 +7,11 @@ use std::path::Path; /// Get and parse the config. /// If it doesn't succeed, exit -pub fn get_config(path: &Path, filename: &str) -> Config { - match Config::from_file(path.join(filename)) { +pub fn get_config(filename: &Path) -> Config { + match Config::from_file(filename) { Ok(c) => c, Err(e) => { - println!("Failed to load {}", filename); + println!("Failed to load {}", filename.display()); println!("Error: {}", e); ::std::process::exit(1); } diff --git a/components/config/src/theme.rs b/components/config/src/theme.rs index c25b2c49..6c68401a 100644 --- a/components/config/src/theme.rs +++ b/components/config/src/theme.rs @@ -43,7 +43,7 @@ impl Theme { let content = read_file_with_error( path, "No `theme.toml` file found. \ - Is the `theme` defined in your `config.toml present in the `themes` directory \ + Is the `theme` defined in your `config.toml` present in the `themes` directory \ and does it have a `theme.toml` inside?", )?; Theme::parse(&content) diff --git a/components/rebuild/tests/rebuild.rs b/components/rebuild/tests/rebuild.rs index 3069f66d..06a7620e 100644 --- a/components/rebuild/tests/rebuild.rs +++ b/components/rebuild/tests/rebuild.rs @@ -20,7 +20,8 @@ macro_rules! load_and_build_site { dir::copy(&path, &$tmp_dir, &options).unwrap(); let site_path = $tmp_dir.path().join($site); - let mut site = Site::new(&site_path, "config.toml").unwrap(); + let config_file = site_path.join("config.toml"); + let mut site = Site::new(&site_path, &config_file).unwrap(); site.load().unwrap(); let public = &site_path.join("public"); site.set_output_path(&public); diff --git a/components/site/benches/load.rs b/components/site/benches/load.rs index 3cee15f5..65887b75 100644 --- a/components/site/benches/load.rs +++ b/components/site/benches/load.rs @@ -11,7 +11,8 @@ fn bench_loading_small_blog(b: &mut test::Bencher) { let mut path = env::current_dir().unwrap().to_path_buf(); path.push("benches"); path.push("small-blog"); - let mut site = Site::new(&path, "config.toml").unwrap(); + let config_file = path.join("config.toml"); + let mut site = Site::new(&path, &config_file).unwrap(); b.iter(|| site.load().unwrap()); } @@ -21,7 +22,8 @@ fn bench_loading_small_blog_with_syntax_highlighting(b: &mut test::Bencher) { let mut path = env::current_dir().unwrap().to_path_buf(); path.push("benches"); path.push("small-blog"); - let mut site = Site::new(&path, "config.toml").unwrap(); + let config_file = path.join("config.toml"); + let mut site = Site::new(&path, &config_file).unwrap(); site.config.highlight_code = true; b.iter(|| site.load().unwrap()); @@ -32,7 +34,8 @@ fn bench_loading_small_blog_with_syntax_highlighting(b: &mut test::Bencher) { // let mut path = env::current_dir().unwrap().to_path_buf(); // path.push("benches"); // path.push("medium-blog"); -// let mut site = Site::new(&path, "config.toml").unwrap(); +// let config_file = path.join("config.toml"); +// let mut site = Site::new(&path, &config_file).unwrap(); // // b.iter(|| site.load().unwrap()); //} @@ -42,7 +45,8 @@ fn bench_loading_small_blog_with_syntax_highlighting(b: &mut test::Bencher) { // let mut path = env::current_dir().unwrap().to_path_buf(); // path.push("benches"); // path.push("medium-blog"); -// let mut site = Site::new(&path, "config.toml").unwrap(); +// let config_file = path.join("config.toml"); +// let mut site = Site::new(&path, &config_file).unwrap(); // site.config.highlight_code = true; // // b.iter(|| site.load().unwrap()); @@ -53,7 +57,8 @@ fn bench_loading_small_blog_with_syntax_highlighting(b: &mut test::Bencher) { // let mut path = env::current_dir().unwrap().to_path_buf(); // path.push("benches"); // path.push("big-blog"); -// let mut site = Site::new(&path, "config.toml").unwrap(); +// let config_file = path.join("config.toml"); +// let mut site = Site::new(&path, &config_file).unwrap(); // // b.iter(|| site.load().unwrap()); //} @@ -63,7 +68,8 @@ fn bench_loading_small_blog_with_syntax_highlighting(b: &mut test::Bencher) { // let mut path = env::current_dir().unwrap().to_path_buf(); // path.push("benches"); // path.push("big-blog"); -// let mut site = Site::new(&path, "config.toml").unwrap(); +// let config_file = path.join("config.toml"); +// let mut site = Site::new(&path, &config_file).unwrap(); // site.config.highlight_code = true; // // b.iter(|| site.load().unwrap()); @@ -74,7 +80,8 @@ fn bench_loading_small_blog_with_syntax_highlighting(b: &mut test::Bencher) { // let mut path = env::current_dir().unwrap().to_path_buf(); // path.push("benches"); // path.push("huge-blog"); -// let mut site = Site::new(&path, "config.toml").unwrap(); +// let config_file = path.join("config.toml"); +// let mut site = Site::new(&path, &config_file).unwrap(); // // b.iter(|| site.load().unwrap()); //} @@ -84,7 +91,8 @@ fn bench_loading_small_blog_with_syntax_highlighting(b: &mut test::Bencher) { // let mut path = env::current_dir().unwrap().to_path_buf(); // path.push("benches"); // path.push("huge-blog"); -// let mut site = Site::new(&path, "config.toml").unwrap(); +// let config_file = path.join("config.toml"); +// let mut site = Site::new(&path, &config_file).unwrap(); // site.config.highlight_code = true; // // b.iter(|| site.load().unwrap()); @@ -95,7 +103,8 @@ fn bench_loading_small_kb(b: &mut test::Bencher) { let mut path = env::current_dir().unwrap().to_path_buf(); path.push("benches"); path.push("small-kb"); - let mut site = Site::new(&path, "config.toml").unwrap(); + let config_file = path.join("config.toml"); + let mut site = Site::new(&path, &config_file).unwrap(); b.iter(|| site.load().unwrap()); } @@ -105,7 +114,8 @@ fn bench_loading_small_kb_with_syntax_highlighting(b: &mut test::Bencher) { let mut path = env::current_dir().unwrap().to_path_buf(); path.push("benches"); path.push("small-kb"); - let mut site = Site::new(&path, "config.toml").unwrap(); + let config_file = path.join("config.toml"); + let mut site = Site::new(&path, &config_file).unwrap(); site.config.highlight_code = true; b.iter(|| site.load().unwrap()); @@ -116,7 +126,8 @@ fn bench_loading_small_kb_with_syntax_highlighting(b: &mut test::Bencher) { // let mut path = env::current_dir().unwrap().to_path_buf(); // path.push("benches"); // path.push("medium-kb"); -// let mut site = Site::new(&path, "config.toml").unwrap(); +// let config_file = path.join("config.toml"); +// let mut site = Site::new(&path, &config_file).unwrap(); // // b.iter(|| site.load().unwrap()); //} @@ -126,7 +137,8 @@ fn bench_loading_small_kb_with_syntax_highlighting(b: &mut test::Bencher) { // let mut path = env::current_dir().unwrap().to_path_buf(); // path.push("benches"); // path.push("medium-kb"); -// let mut site = Site::new(&path, "config.toml").unwrap(); +// let config_file = path.join("config.toml"); +// let mut site = Site::new(&path, &config_file).unwrap(); // site.config.highlight_code = Some(true); // // b.iter(|| site.load().unwrap()); @@ -137,7 +149,8 @@ fn bench_loading_small_kb_with_syntax_highlighting(b: &mut test::Bencher) { // let mut path = env::current_dir().unwrap().to_path_buf(); // path.push("benches"); // path.push("huge-kb"); -// let mut site = Site::new(&path, "config.toml").unwrap(); +// let config_file = path.join("config.toml"); +// let mut site = Site::new(&path, &config_file).unwrap(); // // b.iter(|| site.load().unwrap()); //} @@ -147,7 +160,8 @@ fn bench_loading_small_kb_with_syntax_highlighting(b: &mut test::Bencher) { // let mut path = env::current_dir().unwrap().to_path_buf(); // path.push("benches"); // path.push("huge-kb"); -// let mut site = Site::new(&path, "config.toml").unwrap(); +// let config_file = path.join("config.toml"); +// let mut site = Site::new(&path, &config_file).unwrap(); // site.config.highlight_code = Some(true); // // b.iter(|| site.load().unwrap()); diff --git a/components/site/benches/site.rs b/components/site/benches/site.rs index fd1514c0..754eb413 100644 --- a/components/site/benches/site.rs +++ b/components/site/benches/site.rs @@ -11,7 +11,8 @@ fn setup_site(name: &str) -> Site { let mut path = env::current_dir().unwrap().to_path_buf(); path.push("benches"); path.push(name); - let mut site = Site::new(&path, "config.toml").unwrap(); + let config_file = path.join("config.toml"); + let mut site = Site::new(&path, &config_file).unwrap(); site.load().unwrap(); site } diff --git a/components/site/src/lib.rs b/components/site/src/lib.rs index ee0cc89d..dc6e4331 100644 --- a/components/site/src/lib.rs +++ b/components/site/src/lib.rs @@ -62,9 +62,10 @@ impl<'a> SerializedTaxonomyItem<'a> { impl Site { /// Parse a site at the given path. Defaults to the current dir /// Passing in a path is used in tests and when --root argument is passed - pub fn new>(path: P, config_file: &str) -> Result { + pub fn new, P2: AsRef>(path: P, config_file: P2) -> Result { let path = path.as_ref(); - let mut config = get_config(path, config_file); + let config_file = config_file.as_ref(); + let mut config = get_config(config_file); config.load_extra_syntaxes(path)?; let tpl_glob = diff --git a/components/site/tests/common.rs b/components/site/tests/common.rs index a5908dae..24431706 100644 --- a/components/site/tests/common.rs +++ b/components/site/tests/common.rs @@ -38,7 +38,8 @@ macro_rules! file_contains { pub fn build_site(name: &str) -> (Site, TempDir, PathBuf) { let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf(); path.push(name); - let mut site = Site::new(&path, "config.toml").unwrap(); + let config_file = path.join("config.toml"); + let mut site = Site::new(&path, &config_file).unwrap(); site.load().unwrap(); let tmp_dir = tempdir().expect("create temp dir"); let public = &tmp_dir.path().join("public"); @@ -54,7 +55,8 @@ where { let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf(); path.push(name); - let site = Site::new(&path, "config.toml").unwrap(); + let config_file = path.join("config.toml"); + let site = Site::new(&path, &config_file).unwrap(); let (mut site, needs_loading) = setup_cb(site); if needs_loading { site.load().unwrap(); diff --git a/components/site/tests/site.rs b/components/site/tests/site.rs index fb285c04..5dbc7115 100644 --- a/components/site/tests/site.rs +++ b/components/site/tests/site.rs @@ -13,7 +13,8 @@ use site::Site; fn can_parse_site() { let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf(); path.push("test_site"); - let mut site = Site::new(&path, "config.toml").unwrap(); + let config_file = path.join("config.toml"); + let mut site = Site::new(&path, &config_file).unwrap(); site.load().unwrap(); let library = site.library.read().unwrap(); @@ -628,7 +629,8 @@ fn can_build_with_extra_syntaxes() { fn can_apply_page_templates() { let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf(); path.push("test_site"); - let mut site = Site::new(&path, "config.toml").unwrap(); + let config_file = path.join("config.toml"); + let mut site = Site::new(&path, &config_file).unwrap(); site.load().unwrap(); let template_path = path.join("content").join("applying_page_template"); diff --git a/components/site/tests/site_i18n.rs b/components/site/tests/site_i18n.rs index 0d35bc0d..d4ed0e34 100644 --- a/components/site/tests/site_i18n.rs +++ b/components/site/tests/site_i18n.rs @@ -9,7 +9,8 @@ use site::Site; fn can_parse_multilingual_site() { let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf(); path.push("test_site_i18n"); - let mut site = Site::new(&path, "config.toml").unwrap(); + let config_file = path.join("config.toml"); + let mut site = Site::new(&path, &config_file).unwrap(); site.load().unwrap(); let library = site.library.read().unwrap(); diff --git a/src/cli.rs b/src/cli.rs index 975bb88c..79d758ce 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -18,9 +18,8 @@ pub fn build_cli() -> App<'static, 'static> { Arg::with_name("config") .short("c") .long("config") - .default_value("config.toml") .takes_value(true) - .help("Path to a config file other than config.toml") + .help("Path to a config file other than config.toml in the root of project") ) .subcommands(vec![ SubCommand::with_name("init") diff --git a/src/cmd/build.rs b/src/cmd/build.rs index 44d5e4bc..3a2c7634 100644 --- a/src/cmd/build.rs +++ b/src/cmd/build.rs @@ -7,9 +7,9 @@ use crate::console; pub fn build( root_dir: &Path, - config_file: &str, + config_file: &Path, base_url: Option<&str>, - output_dir: &str, + output_dir: &Path, include_drafts: bool, ) -> Result<()> { let mut site = Site::new(root_dir, config_file)?; diff --git a/src/cmd/check.rs b/src/cmd/check.rs index e354e7b4..1ad57b92 100644 --- a/src/cmd/check.rs +++ b/src/cmd/check.rs @@ -7,7 +7,7 @@ use crate::console; pub fn check( root_dir: &Path, - config_file: &str, + config_file: &Path, base_path: Option<&str>, base_url: Option<&str>, include_drafts: bool, diff --git a/src/cmd/serve.rs b/src/cmd/serve.rs index 3d4c7860..8f696dd3 100644 --- a/src/cmd/serve.rs +++ b/src/cmd/serve.rs @@ -161,9 +161,9 @@ fn create_new_site( root_dir: &Path, interface: &str, port: u16, - output_dir: &str, + output_dir: &Path, base_url: &str, - config_file: &str, + config_file: &Path, include_drafts: bool, ) -> Result<(Site, String)> { let mut site = Site::new(root_dir, config_file)?; @@ -194,9 +194,9 @@ pub fn serve( root_dir: &Path, interface: &str, port: u16, - output_dir: &str, + output_dir: &Path, base_url: &str, - config_file: &str, + config_file: &Path, watch_only: bool, open: bool, include_drafts: bool, diff --git a/src/main.rs b/src/main.rs index 935e5474..28c192f3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,10 @@ fn main() { "." => env::current_dir().unwrap(), path => PathBuf::from(path), }; - let config_file = matches.value_of("config").unwrap(); + let config_file = match matches.value_of("config") { + Some(path) => PathBuf::from(path), + None => root_dir.join("config.toml"), + }; match matches.subcommand() { ("init", Some(matches)) => { @@ -31,12 +34,12 @@ fn main() { ("build", Some(matches)) => { console::info("Building site..."); let start = Instant::now(); - let output_dir = matches.value_of("output_dir").unwrap(); + let output_dir = PathBuf::from(matches.value_of("output_dir").unwrap()); match cmd::build( &root_dir, - config_file, + &config_file, matches.value_of("base_url"), - output_dir, + &output_dir, matches.is_present("drafts"), ) { Ok(()) => console::report_elapsed_time(start), @@ -73,16 +76,16 @@ fn main() { ::std::process::exit(1); } } - let output_dir = matches.value_of("output_dir").unwrap(); + let output_dir = PathBuf::from(matches.value_of("output_dir").unwrap()); let base_url = matches.value_of("base_url").unwrap(); console::info("Building site..."); match cmd::serve( &root_dir, interface, port, - output_dir, + &output_dir, base_url, - config_file, + &config_file, watch_only, open, include_drafts, @@ -99,7 +102,7 @@ fn main() { let start = Instant::now(); match cmd::check( &root_dir, - config_file, + &config_file, matches.value_of("base_path"), matches.value_of("base_url"), matches.is_present("drafts"),