Add resources and copying them

This commit is contained in:
Sofia 2018-04-19 01:12:18 +03:00
parent 2659bb61a1
commit 49db42c82f
7 changed files with 120 additions and 17 deletions

View File

@ -2,8 +2,8 @@
website_name = "Test Website Name!"
built_pages = ["test_page/about.toml", "test_page/other.toml"]
use_default_css = true
javascript = ["test.js"]
css = ["test.css"]
javascript = []
css = []
before_navbar_url = "test_page/resources/before_navbar.html"
before_content_url = "test_page/resources/before_content.html"
after_content_url = "test_page/resources/after_content.html"
@ -12,22 +12,21 @@ after_content_url = "test_page/resources/after_content.html"
#google_robots = "all"
#google_site_verification = ""
[resource.test] todo
source = "source_path"
destination = "destination_path"
is_recursive_folder = false
[navbar]
items = ["about", "other", "google"]
[navbar.item.about]
title = "About"
link = "/"
[navbar.item.about]
title = "About"
link = "/"
[navbar.item.other]
title = "Other"
link = "/other"
[navbar.item.other]
title = "Other"
link = "/other"
[navbar.item.google]
title = "Google!"
link = "https://google.com"
[navbar.item.google]
title = "Google!"
link = "https://google.com"
[resource.test]
source = "test_page/resources/copied_resources/"
destination = "stuff_folder"

View File

@ -1,4 +1,10 @@
use std::path::PathBuf;
use std::path::Path;
use std::fs::File;
use std::io::Read;
use std::error::Error as STDError;
use pathdiff;
use config::Config;
use template::Template;
@ -118,6 +124,89 @@ pub fn build(logger: &Logger, opt: &Opt, _: &BuildOpt) -> Result<(), Error> {
}
}
logger.log(LogLevel::INFO, "Copying resources");
if let Some(resources) = config.global_config.resources {
for resource in resources.values() {
let path = Path::new(&resource.source);
if path.exists() {
match write_recursive_resource(
path.to_path_buf(),
PathBuf::from(resource.source.clone()),
PathBuf::from(resource.destination.clone()),
) {
Ok(_) => logger.log(LogLevel::DETAIL, "Resource successfully copied."),
Err(err) => return Err(err),
}
} else {
logger.log(
LogLevel::WARNING,
format!("Resource does not exist: {:?}", path),
);
}
}
}
logger.log(LogLevel::INFO, "Done!");
Ok(())
}
fn write_recursive_resource(
resource_path: PathBuf,
source: PathBuf,
destination: PathBuf,
) -> Result<(), Error> {
if let Some(relative) = pathdiff::diff_paths(&resource_path, &source) {
if resource_path.is_dir() {
for item in resource_path.read_dir().unwrap() {
match item {
Ok(item) => {
match write_recursive_resource(
item.path(),
source.clone(),
destination.clone(),
) {
Ok(_) => {}
Err(err) => return Err(err),
};
}
Err(error) => return Err(Error::from(error)),
}
}
Ok(())
} else {
match File::open(resource_path.clone()) {
Ok(mut read_file) => {
let mut contents = String::new();
read_file.read_to_string(&mut contents)?;
let mut dest_path =
file_writer::add_to_beginning(destination.clone(), "public")?;
if source.is_dir() {
dest_path = dest_path.join(relative.clone());
dest_path.set_file_name(resource_path.file_name().unwrap());
}
file_writer::write_file(&dest_path, contents, true)?;
Ok(())
}
Err(err) => Err(Error::new(
LogLevel::SEVERE,
format!(
"Failed to open read file {:?}: {}",
resource_path,
err.description().to_owned()
),
)),
}
}
} else {
Err(Error::new(
LogLevel::SEVERE,
format!(
"Internal Error: resource relative path invalid: {:?} to {:?}",
&resource_path, &source
),
))
}
}

View File

@ -13,6 +13,14 @@ use error::Error;
pub struct GlobalConfigToml {
pub website: WebsiteConfig,
pub navbar: Option<NavbarConfig>,
#[serde(rename = "resource")]
pub resources: Option<HashMap<String, ResourceConfig>>,
}
#[derive(Deserialize, Serialize, Clone, Debug)]
pub struct ResourceConfig {
pub source: String,
pub destination: String,
}
#[derive(Deserialize, Serialize, Clone, Debug)]

View File

@ -28,8 +28,12 @@ impl Error {
impl From<IOError> for Error {
fn from(err: IOError) -> Error {
let mut os_error = String::new();
if let Some(error) = err.raw_os_error() {
os_error = format!("OS Error: {}", error);
}
Error {
description: err.description().to_owned(),
description: format!("IOError: {} {}", err.description().to_owned(), os_error),
severity: LogLevel::SEVERE,
}
}

View File

@ -113,6 +113,7 @@ fn run() -> Result<(), ()> {
google_site_verification: None,
},
navbar: navbar,
resources: None,
};
match file_writer::write_toml(&PathBuf::from("config.toml"), &config, opt.overwrite) {

View File

@ -0,0 +1 @@
Hello!

View File

@ -0,0 +1 @@
Toinen!