From 6ace5186842d92baa58c4dfd80dc3f4a003bc130 Mon Sep 17 00:00:00 2001 From: yuma14 Date: Fri, 10 Nov 2023 16:48:51 +0900 Subject: [PATCH] fmt --- src/lib.rs | 230 +++++++++++++++++++++++++++-------------------------- 1 file changed, 117 insertions(+), 113 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 764397f..5125411 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,113 +1,117 @@ -use std::{collections::HashMap, fs, io, path::Path}; - -use anyhow::Context; -use path_absolutize::Absolutize; -use tracing::{debug, error}; -use walkdir::WalkDir; - -use crate::project_config::ProjectConfig; - -pub mod cli; -pub mod directory; -pub mod handlebars_helpers; -mod loader; -pub mod pipeline; -pub mod project_config; -pub mod store; -mod template_engine; -pub mod transformer; -mod value; - -pub use loader::*; -pub use template_engine::*; -pub use value::*; - -#[tracing::instrument(ret)] -pub fn build(project_root: &Path) -> anyhow::Result<()> { - let project_config_path = directory::get_project_config_path(project_root); - let config: ProjectConfig = serde_yaml::from_str( - &fs::read_to_string(&project_config_path) - .with_context(|| format!("could not load file {}", project_config_path.display()))?, - ) - .with_context(|| { - format!( - "failed to parse project config {}", - project_config_path.display() - ) - })?; - - dbg!(&config); - - let src_dir = directory::get_src_directory(project_root); - let abs_project_root = project_root.absolutize().unwrap(); - - let mut jobs = vec![]; - - for filepath in WalkDir::new(&src_dir) - .into_iter() - .filter_map(|entry| entry.ok()) - .map(|entry| entry.path().to_path_buf()) - .filter(|path| path.is_file()) - { - debug!("found file {}", filepath.display()); - let abs_filepath = filepath.absolutize().unwrap(); - let relative_filepath = directory::get_relative_path(&abs_filepath, &abs_project_root); - let mut selected_pipeline = None; - for pipeline in config.pipelines.iter() { - if pipeline.accepts(&relative_filepath) { - debug!( - "found pipeline \"{}\" for file \"{}\"", - pipeline.name, - relative_filepath.display() - ); - selected_pipeline = Some(pipeline); - break; - } - } - - match selected_pipeline { - None => { - error!("File {}: No pipeline found", relative_filepath.display()); - continue; - } - Some(pipeline) => jobs.push(pipeline.to_job(&abs_filepath, &abs_project_root)), - } - } - - let mut resources = HashMap::new(); - // 各パイプラインごとに(各Jobごとではない)必要なリソースを事前読込する - for pipeline in config.pipelines.iter() { - resources.insert( - &pipeline.name, - pipeline - .prefetch_resources(&abs_project_root) - .with_context(|| { - format!( - "failed to prefetch files for pipeline \"{}\"", - pipeline.name - ) - })?, - ); - } - - for job in jobs { - job.execute(resources.get(&&job.pipeline().name).unwrap_or_else(|| panic!("could not find prefetched resource for pipeline \"{}\"", - job.pipeline().name))) - .with_context(|| { - format!( - "failed to complete job for pipeline \"{}\" and entry file \"{}\"", - job.pipeline().name, - job.input_path().display() - ) - })?; - } - - Ok(()) -} - -#[tracing::instrument(ret)] -pub fn init(project_root: &Path) -> io::Result<()> { - println!("not implemented yet"); - - Ok(()) -} +use std::{collections::HashMap, fs, io, path::Path}; + +use anyhow::Context; +use path_absolutize::Absolutize; +use tracing::{debug, error}; +use walkdir::WalkDir; + +use crate::project_config::ProjectConfig; + +pub mod cli; +pub mod directory; +pub mod handlebars_helpers; +mod loader; +pub mod pipeline; +pub mod project_config; +pub mod store; +mod template_engine; +pub mod transformer; +mod value; + +pub use loader::*; +pub use template_engine::*; +pub use value::*; + +#[tracing::instrument(ret)] +pub fn build(project_root: &Path) -> anyhow::Result<()> { + let project_config_path = directory::get_project_config_path(project_root); + let config: ProjectConfig = serde_yaml::from_str( + &fs::read_to_string(&project_config_path) + .with_context(|| format!("could not load file {}", project_config_path.display()))?, + ) + .with_context(|| { + format!( + "failed to parse project config {}", + project_config_path.display() + ) + })?; + + dbg!(&config); + + let src_dir = directory::get_src_directory(project_root); + let abs_project_root = project_root.absolutize().unwrap(); + + let mut jobs = vec![]; + + for filepath in WalkDir::new(&src_dir) + .into_iter() + .filter_map(|entry| entry.ok()) + .map(|entry| entry.path().to_path_buf()) + .filter(|path| path.is_file()) + { + debug!("found file {}", filepath.display()); + let abs_filepath = filepath.absolutize().unwrap(); + let relative_filepath = directory::get_relative_path(&abs_filepath, &abs_project_root); + let mut selected_pipeline = None; + for pipeline in config.pipelines.iter() { + if pipeline.accepts(&relative_filepath) { + debug!( + "found pipeline \"{}\" for file \"{}\"", + pipeline.name, + relative_filepath.display() + ); + selected_pipeline = Some(pipeline); + break; + } + } + + match selected_pipeline { + None => { + error!("File {}: No pipeline found", relative_filepath.display()); + continue; + } + Some(pipeline) => jobs.push(pipeline.to_job(&abs_filepath, &abs_project_root)), + } + } + + let mut resources = HashMap::new(); + // 各パイプラインごとに(各Jobごとではない)必要なリソースを事前読込する + for pipeline in config.pipelines.iter() { + resources.insert( + &pipeline.name, + pipeline + .prefetch_resources(&abs_project_root) + .with_context(|| { + format!( + "failed to prefetch files for pipeline \"{}\"", + pipeline.name + ) + })?, + ); + } + + for job in jobs { + job.execute(resources.get(&&job.pipeline().name).unwrap_or_else(|| { + panic!( + "could not find prefetched resource for pipeline \"{}\"", + job.pipeline().name + ) + })) + .with_context(|| { + format!( + "failed to complete job for pipeline \"{}\" and entry file \"{}\"", + job.pipeline().name, + job.input_path().display() + ) + })?; + } + + Ok(()) +} + +#[tracing::instrument(ret)] +pub fn init(project_root: &Path) -> io::Result<()> { + println!("not implemented yet"); + + Ok(()) +}