From 49db42c82f757738b4ffd08fd0b4ce7e7519aec9 Mon Sep 17 00:00:00 2001 From: Teascade Date: Thu, 19 Apr 2018 01:12:18 +0300 Subject: [PATCH] Add resources and copying them --- config.toml | 31 ++++--- src/builder.rs | 89 +++++++++++++++++++ src/config_toml.rs | 8 ++ src/error.rs | 6 +- src/main.rs | 1 + test_page/resources/copied_resources/joku.txt | 1 + .../resources/copied_resources/toinen.txt | 1 + 7 files changed, 120 insertions(+), 17 deletions(-) create mode 100644 test_page/resources/copied_resources/joku.txt create mode 100644 test_page/resources/copied_resources/toinen.txt diff --git a/config.toml b/config.toml index 7d62119..6813725 100644 --- a/config.toml +++ b/config.toml @@ -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" \ No newline at end of file +[navbar.item.google] +title = "Google!" +link = "https://google.com" + +[resource.test] +source = "test_page/resources/copied_resources/" +destination = "stuff_folder" \ No newline at end of file diff --git a/src/builder.rs b/src/builder.rs index 7bc1fbe..7ff4332 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -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 + ), + )) + } +} diff --git a/src/config_toml.rs b/src/config_toml.rs index d0b0485..2a0a60a 100644 --- a/src/config_toml.rs +++ b/src/config_toml.rs @@ -13,6 +13,14 @@ use error::Error; pub struct GlobalConfigToml { pub website: WebsiteConfig, pub navbar: Option, + #[serde(rename = "resource")] + pub resources: Option>, +} + +#[derive(Deserialize, Serialize, Clone, Debug)] +pub struct ResourceConfig { + pub source: String, + pub destination: String, } #[derive(Deserialize, Serialize, Clone, Debug)] diff --git a/src/error.rs b/src/error.rs index 2526042..c94d8fd 100644 --- a/src/error.rs +++ b/src/error.rs @@ -28,8 +28,12 @@ impl Error { impl From 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, } } diff --git a/src/main.rs b/src/main.rs index 0bfd99f..a77f9bb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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) { diff --git a/test_page/resources/copied_resources/joku.txt b/test_page/resources/copied_resources/joku.txt new file mode 100644 index 0000000..05a682b --- /dev/null +++ b/test_page/resources/copied_resources/joku.txt @@ -0,0 +1 @@ +Hello! \ No newline at end of file diff --git a/test_page/resources/copied_resources/toinen.txt b/test_page/resources/copied_resources/toinen.txt new file mode 100644 index 0000000..90b23d0 --- /dev/null +++ b/test_page/resources/copied_resources/toinen.txt @@ -0,0 +1 @@ +Toinen! \ No newline at end of file