zola/src/cmd/init.rs

69 lines
2.2 KiB
Rust
Raw Normal View History

2017-07-27 09:24:43 +00:00
use std::fs::{create_dir, canonicalize};
2016-12-06 05:51:33 +00:00
use std::path::Path;
2017-07-01 07:47:41 +00:00
use errors::Result;
use utils::fs::create_file;
2016-12-06 05:51:33 +00:00
2017-07-27 09:24:43 +00:00
use prompt::{ask_bool, ask_url};
use console;
2016-12-06 05:51:33 +00:00
2018-09-30 19:15:09 +00:00
const CONFIG: &str = r#"
2017-07-27 09:24:43 +00:00
# The URL the site will be built for
base_url = "%BASE_URL%"
# Whether to automatically compile all Sass files in the sass directory
compile_sass = %COMPILE_SASS%
# Whether to do syntax highlighting
# Theme can be customised by setting the `highlight_theme` variable to a theme supported by Gutenberg
highlight_code = %HIGHLIGHT%
2017-02-23 08:34:57 +00:00
2018-03-12 19:11:03 +00:00
# Whether to build a search index to be used later on by a JavaScript library
build_search_index = %SEARCH%
2017-02-23 08:34:57 +00:00
[extra]
# Put all your custom variables here
2016-12-06 05:51:33 +00:00
"#;
pub fn create_new_project(name: &str) -> Result<()> {
let path = Path::new(name);
2016-12-06 05:51:33 +00:00
// Better error message than the rust default
if path.exists() && path.is_dir() {
2016-12-09 11:24:05 +00:00
bail!("Folder `{}` already exists", path.to_string_lossy().to_string());
2016-12-06 05:51:33 +00:00
}
create_dir(path)?;
2017-07-27 09:24:43 +00:00
console::info("Welcome to Gutenberg!");
2016-12-06 05:51:33 +00:00
2017-07-27 09:24:43 +00:00
let base_url = ask_url("> What is the URL of your site?", "https://example.com")?;
let compile_sass = ask_bool("> Do you want to enable Sass compilation?", true)?;
let highlight = ask_bool("> Do you want to enable syntax highlighting?", false)?;
2018-03-12 19:11:03 +00:00
let search = ask_bool("> Do you want to build a search index of the content?", false)?;
2016-12-06 05:51:33 +00:00
2017-07-27 09:24:43 +00:00
let config = CONFIG
.trim_left()
.replace("%BASE_URL%", &base_url)
.replace("%COMPILE_SASS%", &format!("{}", compile_sass))
2018-03-12 19:11:03 +00:00
.replace("%SEARCH%", &format!("{}", search))
2017-07-27 09:24:43 +00:00
.replace("%HIGHLIGHT%", &format!("{}", highlight));
create_file(&path.join("config.toml"), &config)?;
2016-12-06 05:51:33 +00:00
2017-07-27 09:24:43 +00:00
create_dir(path.join("content"))?;
create_dir(path.join("templates"))?;
2016-12-06 05:51:33 +00:00
create_dir(path.join("static"))?;
2017-08-24 23:43:54 +00:00
create_dir(path.join("themes"))?;
2017-07-27 09:24:43 +00:00
if compile_sass {
create_dir(path.join("sass"))?;
}
2016-12-06 05:51:33 +00:00
2017-07-27 09:24:43 +00:00
println!();
console::success(&format!("Done! Your site was created in {:?}", canonicalize(path).unwrap()));
println!();
2017-11-27 17:09:09 +00:00
console::info("Get started by moving into the directory and using the built-in server: `gutenberg serve`");
println!("Visit https://www.getgutenberg.io for the full documentation.");
2016-12-06 05:51:33 +00:00
Ok(())
}