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!" website_name = "Test Website Name!"
built_pages = ["test_page/about.toml", "test_page/other.toml"] built_pages = ["test_page/about.toml", "test_page/other.toml"]
use_default_css = true use_default_css = true
javascript = ["test.js"] javascript = []
css = ["test.css"] css = []
before_navbar_url = "test_page/resources/before_navbar.html" before_navbar_url = "test_page/resources/before_navbar.html"
before_content_url = "test_page/resources/before_content.html" before_content_url = "test_page/resources/before_content.html"
after_content_url = "test_page/resources/after_content.html" after_content_url = "test_page/resources/after_content.html"
@ -12,11 +12,6 @@ after_content_url = "test_page/resources/after_content.html"
#google_robots = "all" #google_robots = "all"
#google_site_verification = "" #google_site_verification = ""
[resource.test] todo
source = "source_path"
destination = "destination_path"
is_recursive_folder = false
[navbar] [navbar]
items = ["about", "other", "google"] items = ["about", "other", "google"]
@ -31,3 +26,7 @@ items = ["about", "other", "google"]
[navbar.item.google] [navbar.item.google]
title = "Google!" title = "Google!"
link = "https://google.com" 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::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 config::Config;
use template::Template; 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!"); logger.log(LogLevel::INFO, "Done!");
Ok(()) 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 struct GlobalConfigToml {
pub website: WebsiteConfig, pub website: WebsiteConfig,
pub navbar: Option<NavbarConfig>, 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)] #[derive(Deserialize, Serialize, Clone, Debug)]

View File

@ -28,8 +28,12 @@ impl Error {
impl From<IOError> for Error { impl From<IOError> for Error {
fn from(err: IOError) -> 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 { Error {
description: err.description().to_owned(), description: format!("IOError: {} {}", err.description().to_owned(), os_error),
severity: LogLevel::SEVERE, severity: LogLevel::SEVERE,
} }
} }

View File

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

View File

@ -0,0 +1 @@
Hello!

View File

@ -0,0 +1 @@
Toinen!