Make path to config.toml independent from root dir (#1023)

fixes #1022
This commit is contained in:
Tymoteusz Wiśniewski 2020-05-23 11:55:45 +02:00 committed by GitHub
parent bfbc16d3ad
commit 974ae167a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 71 additions and 47 deletions

View File

@ -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')

View File

@ -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'

View File

@ -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);
}

View File

@ -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)

View File

@ -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);

View File

@ -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());

View File

@ -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
}

View File

@ -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 =

View File

@ -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();

View File

@ -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");

View File

@ -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();

View File

@ -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")

View File

@ -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)?;

View File

@ -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,

View File

@ -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,

View File

@ -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"),