Skip to content

Commit

Permalink
Make path to config.toml independent from root dir (#1023)
Browse files Browse the repository at this point in the history
fixes #1022
  • Loading branch information
satakuma authored May 23, 2020
1 parent bfbc16d commit 974ae16
Show file tree
Hide file tree
Showing 16 changed files with 71 additions and 47 deletions.
4 changes: 2 additions & 2 deletions completions/_zola.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion completions/zola.fish
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
6 changes: 3 additions & 3 deletions components/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion components/config/src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion components/rebuild/tests/rebuild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
42 changes: 28 additions & 14 deletions components/site/benches/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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());
Expand All @@ -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());
//}
Expand All @@ -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());
Expand All @@ -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());
//}
Expand All @@ -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());
Expand All @@ -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());
//}
Expand All @@ -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());
Expand All @@ -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());
}
Expand All @@ -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());
Expand All @@ -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());
//}
Expand All @@ -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());
Expand All @@ -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());
//}
Expand All @@ -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());
Expand Down
3 changes: 2 additions & 1 deletion components/site/benches/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 3 additions & 2 deletions components/site/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<P: AsRef<Path>>(path: P, config_file: &str) -> Result<Site> {
pub fn new<P: AsRef<Path>, P2: AsRef<Path>>(path: P, config_file: P2) -> Result<Site> {
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 =
Expand Down
6 changes: 4 additions & 2 deletions components/site/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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();
Expand Down
6 changes: 4 additions & 2 deletions components/site/tests/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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");
Expand Down
3 changes: 2 additions & 1 deletion components/site/tests/site_i18n.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 1 addition & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions src/cmd/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down Expand Up @@ -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,
Expand Down
19 changes: 11 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)) => {
Expand All @@ -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),
Expand Down Expand Up @@ -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,
Expand All @@ -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"),
Expand Down

0 comments on commit 974ae16

Please sign in to comment.