Make add_to_beginning better

This commit is contained in:
Sofia 2018-04-19 01:54:03 +03:00
parent c3da02b2a1
commit 316ec09319
3 changed files with 18 additions and 25 deletions

View File

@ -14,6 +14,8 @@ mobile_viewport = true
meta_description = true
meta_og = true
output = "public"
[google]
google_robots = "all"
google_site_verification = "some_verification"
@ -39,4 +41,4 @@ link = "https://google.com"
[resource.test]
source = "test_page/resources/copied_resources/"
destination = "stuff_folder"
destination = ""

View File

@ -41,7 +41,7 @@ pub fn build(logger: &Logger, opt: &Opt, _: &BuildOpt) -> Result<(), Error> {
.website
.output
.unwrap_or("public".to_owned()),
)?;
);
logger.log(LogLevel::DETAIL, format!("Adding {:?}", css_path));
file_writer::write_file(&css_path, DEFAULT_CSS, opt.overwrite)?;
}
@ -128,7 +128,7 @@ pub fn build(logger: &Logger, opt: &Opt, _: &BuildOpt) -> Result<(), Error> {
.website
.output
.unwrap_or("public".to_owned()),
)?;
);
logger.log(LogLevel::DETAILER, format!("Writing {:?}", html_path));
if let Some(render) = renders.get(idx) {
file_writer::write_file(&html_path, render.clone(), opt.overwrite)?;
@ -145,11 +145,19 @@ pub fn build(logger: &Logger, opt: &Opt, _: &BuildOpt) -> Result<(), Error> {
for resource in resources.values() {
let path = Path::new(&resource.source);
if path.exists() {
let dest = file_writer::add_to_beginning(
PathBuf::from(resource.destination.clone()),
config
.clone()
.global_config
.website
.output
.unwrap_or("public".to_owned()),
);
match write_recursive_resource(
config.clone(),
path.to_path_buf(),
PathBuf::from(resource.source.clone()),
PathBuf::from(resource.destination.clone()),
dest,
) {
Ok(_) => logger.log(LogLevel::DETAIL, "Resource successfully copied."),
Err(err) => return Err(err),
@ -169,7 +177,6 @@ pub fn build(logger: &Logger, opt: &Opt, _: &BuildOpt) -> Result<(), Error> {
}
fn write_recursive_resource(
config: Config,
resource_path: PathBuf,
source: PathBuf,
destination: PathBuf,
@ -180,7 +187,6 @@ fn write_recursive_resource(
match item {
Ok(item) => {
match write_recursive_resource(
config.clone(),
item.path(),
source.clone(),
destination.clone(),
@ -198,14 +204,7 @@ fn write_recursive_resource(
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(),
config
.global_config
.website
.output
.unwrap_or("public".to_owned()),
)?;
let mut dest_path = destination.clone();
if source.is_dir() {
dest_path = dest_path.join(relative.clone());

View File

@ -9,16 +9,8 @@ use toml;
use error::Error;
use logger::LogLevel;
pub fn add_to_beginning<T: Into<String>>(path: PathBuf, to_add: T) -> Result<PathBuf, Error> {
if let (Some(parent), Some(file_name)) = (path.clone().parent(), path.clone().file_name()) {
let parent = Path::new(&to_add.into()).join(parent);
Ok(parent.join(file_name))
} else {
return Err(Error::new(
LogLevel::SEVERE,
format!("Could not find parent folder or file name for {:?}", path),
));
}
pub fn add_to_beginning<T: Into<String>>(path: PathBuf, to_add: T) -> PathBuf {
Path::new(&to_add.into()).join(path)
}
pub fn write_toml<T: Serialize>(path: &PathBuf, data: &T, overwrite: bool) -> Result<(), Error> {