From 6c3ab59c9acdc9528786993f47d225358e8b951a Mon Sep 17 00:00:00 2001 From: curly Date: Tue, 3 Jan 2023 12:53:09 -0700 Subject: almost done probably --- TODO | 5 +-- src/lib.rs | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++++++----- src/main.rs | 64 ++---------------------------------- 3 files changed, 102 insertions(+), 74 deletions(-) diff --git a/TODO b/TODO index 1b92c0f..1bb5016 100644 --- a/TODO +++ b/TODO @@ -1,4 +1 @@ -- Scan sets of paths and recurse on finding a directory, - creating at the same time in the build folder - - Checking the exclude and no-scan lists should be done - at this time \ No newline at end of file +Implement check_path() \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 3edd379..78d068b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,12 @@ use std::{fs, path}; use std::io::ErrorKind; use toml; +enum PathType { + Exclude, + DoNotScan, + Okay, +} + pub struct Config { config: toml::Value, commands: toml::Value, @@ -72,8 +78,8 @@ impl Config { } -pub fn create_dir(path: &str) -> Result<(), ()> { - match fs::create_dir(std::path::PathBuf::from(path)) { +fn create_dir(path: &path::PathBuf) -> Result<(), ()> { + match fs::create_dir(path) { Ok(_) => (), Err(n) => match n.kind() { ErrorKind::AlreadyExists => (), @@ -83,7 +89,7 @@ pub fn create_dir(path: &str) -> Result<(), ()> { return Ok(()) } -pub fn remove_path_head(path: &path::PathBuf) -> path::PathBuf { +fn remove_path_head(path: &path::PathBuf) -> path::PathBuf { let mut path = path.to_str().unwrap().split("/"); path.next(); let path: path::PathBuf = path.collect(); @@ -91,18 +97,103 @@ pub fn remove_path_head(path: &path::PathBuf) -> path::PathBuf { return path } -pub fn append_path_head(path: &path::PathBuf, head: &str) -> path::PathBuf { +fn append_path_head(path: &path::PathBuf, head: &str) -> path::PathBuf { let mut head = path::PathBuf::from(head); head.push(path); return head } -pub fn replace_path_head(path: &path::PathBuf, head: &str) -> path::PathBuf { +fn replace_path_head(path: &path::PathBuf, head: &str) -> path::PathBuf { let path = remove_path_head(path); append_path_head(&path, head) } -pub fn remove_path_tail(path: &mut path::PathBuf) -> &path::PathBuf { - path.pop(); - path +pub fn run(config: Config) { + let source = config.get_str("source").unwrap(); + let build = config.get_str("build_dir").unwrap(); + + // Create build directory or panic + create_dir(&path::PathBuf::from(build)).unwrap(); + + + directory_scan(path::PathBuf::from(source), &config); +} + +fn check_path(path: &path::PathBuf, config: &Config) -> PathType { + // GET EXCLUDE AND DNS LISTS + + // CHECK FOR EXCLUDES AND DO NOT SCANS + // Excludes + // for ex_path in excludes { + // if &ex_path == &path { + // return PathType::Exclude; + // } + // } + + // // Do not scans (copy files) + // for dns_path in dns { + // if &dns_path == &path { + + // return PathType::DoNotScan; + // } + // } + + PathType::Exclude +} + +fn copy_file(path: &path::PathBuf) {} + +fn directory_scan(path: path::PathBuf, config: &Config) { + println!("Scanning: {:?}", &path); + + // Check the path + match check_path(&path, config) { + PathType::Exclude => return, + _ => (), + }; + + // Create the dir in the build tree + let build_path = replace_path_head(&path, "build/"); + create_dir(&build_path).unwrap(); + + // Iterate over path contents + for x in fs::read_dir(path).unwrap() { + let x = x.as_ref().unwrap().path(); + if x.is_dir() { + directory_scan(x, &config); + } else { + file_scan(x, &config); + } + + + } +} + +fn file_scan(path: path::PathBuf, config: &Config) { + // Check path + match check_path(&path, config) { + PathType::Okay => (), + PathType::DoNotScan => {copy_file(&path); return}, + PathType::Exclude => return, + }; + + // File scanning + let mut file_string = String::new(); + + for line in fs::read_to_string(&path).unwrap().lines() { + if line.contains(":?") { + let v: Vec<&str> = line.trim().split(":?").collect(); + let v = v.get(1).unwrap(); + + println!("INSERTING {} into {:?}", v, path); + + file_string.push_str(fs::read_to_string(v).unwrap().as_ref()); + } else { + file_string.push_str(line); + file_string.push('\n') + } + } + + let dest = replace_path_head(&path, "build/"); + fs::write(dest, file_string).expect("Could not write file"); } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index a6f8e1f..9f24daf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,66 +7,6 @@ fn main() { Err(n) => {print!("\"settings.toml\": {}: ", n); simple_static_site_generator::Config::new()}, }; - println!("{}", config.get_str("source").unwrap()); - - let mut path_vec = vec![]; - let mut ex_path_vec = vec![]; - - let dirs = fs::read_dir("src").unwrap(); - for dir in dirs { - - let path = dir.as_ref().unwrap().path(); - if path != std::path::PathBuf::from("assets") { - path_vec.push(path); - } else { - ex_path_vec.push(path); - } - } - - println!("INCLUDED"); - for x in &path_vec { - println!("{:?}", x) - } - println!("EXCLUDED"); - for x in &ex_path_vec { - println!("{:?}", x) - } - println!(); - - - // create build directory or panic - simple_static_site_generator::create_dir("build").unwrap(); - - for path in path_vec { - let mut file_string = String::new(); - - if path.is_dir() { - // RECURSE HERE - continue - } - - for line in fs::read_to_string(&path).unwrap().lines() { - if line.contains(":?") { - let v: Vec<&str> = line.trim().split(":?").collect(); - let v = v.get(1).unwrap(); - - println!("INSERTING {} into {:?}", v, path); - - file_string.push_str(fs::read_to_string(v).unwrap().as_ref()); - } else { - file_string.push_str(line); - file_string.push('\n') - } - } - - - let dest = simple_static_site_generator::replace_path_head(&path, "build/"); - - // NEEDS TO CREATE DIRECTORY STRUCTURE - fs::write(dest, file_string).expect("Could not write file"); - - } - - - // NEEDS TO COPY NOT SCANNED DIRECTORIES + // Run + simple_static_site_generator::run(config); } \ No newline at end of file -- cgit v1.2.3