diff --git a/src/generator/models/comment.rs b/src/generator/models/comment.rs index 54098da..0dfe133 100644 --- a/src/generator/models/comment.rs +++ b/src/generator/models/comment.rs @@ -56,11 +56,6 @@ impl ModelCommentGenerator { Self::generate_comment_content(table, config, &settings, new_settings)?; return Ok(file_content.replace(body, &comment)); } - - // let settings = settings.unwrap(); - // tracing::info!(?settings); - // let merged_settings = settings.merge(&config.comment); - // if merged_settings.enable { } } } diff --git a/src/generator/models/file.rs b/src/generator/models/file.rs index bea570d..7497746 100644 --- a/src/generator/models/file.rs +++ b/src/generator/models/file.rs @@ -9,6 +9,7 @@ use color_eyre::Result; use comfy_table::{ContentArrangement, Table as CTable}; use comment_parser::{CommentParser, Event}; use handlebars::Handlebars; +use heck::ToPascalCase; use sea_orm_codegen::OutputFile; use serde::Serialize; use tokio::fs; @@ -17,14 +18,14 @@ pub struct FileGenerator { filename: String, table: Table, } -// #[derive(Debug, Clone, Serialize)] -// pub struct FileContext { -// entities_path: String, -// model_path: Option, -// model_name: Option, -// active_model_name: Option, -// prelude_path: Option, -// } +#[derive(Debug, Clone, Serialize)] +pub struct ModelContext { + entities_path: String, + model_path: String, + model_name: String, + active_model_name: String, + prelude_path: Option, +} impl FileGenerator { pub async fn generate_file<'a>( @@ -44,6 +45,40 @@ impl FileGenerator { file_chunks .extend(Self::handle_existing_file(table, &filepath, config, handlebars).await?); } else { + let model_name = format!("{}Model", table.name).to_pascal_case(); + let active_model_name = format!("{}ActiveModel", table.name).to_pascal_case(); + let context = ModelContext { + entities_path: format!("super::{}", config.relative_entities_path), + model_path: table.name.clone(), + model_name, + active_model_name, + prelude_path: Some("super::prelude".to_string()), + }; + tracing::info!(?context, "Generating new file",); + + let content = if config.prelude { + file_chunks.push(GeneratedFileChunk { + path: config.models_path.join("prelude.rs"), + content: format!( + "pub use super::{}::{}::{{Model as {},ActiveModel as {},Entity as {}}};", + config.relative_entities_path, + table.name, + context.model_name, + context.active_model_name, + table.name.clone().to_pascal_case() + ), + priority: 0, + }); + handlebars.render("modelprelude", &context)? + } else { + handlebars.render("model", &context)? + }; + + file_chunks.push(GeneratedFileChunk { + path: filepath, + content, + priority: 0, + }); } // let filepath = config.output.path.join(&self.filename); diff --git a/src/generator/models/mod.rs b/src/generator/models/mod.rs index 619be39..f66328b 100644 --- a/src/generator/models/mod.rs +++ b/src/generator/models/mod.rs @@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize}; use std::path::PathBuf; use table::Table; -use super::file::{GeneratedFile, GeneratedFileChunk}; +use super::file::GeneratedFileChunk; pub mod column; pub mod comment; pub mod discover; @@ -131,6 +131,13 @@ pub async fn generate_models<'a>( for table in tables { files.extend(FileGenerator::generate_file(table, &model_config, handlebars).await?); } + if model_config.prelude { + files.push(GeneratedFileChunk { + path: model_config.models_path.join("mod.rs"), + content: String::from("pub mod prelude;"), + priority: 0, + }) + } } Ok(files) } diff --git a/src/main.rs b/src/main.rs index ffd7959..3d75d78 100644 --- a/src/main.rs +++ b/src/main.rs @@ -53,6 +53,10 @@ async fn main() -> Result<()> { let merged_outputs = generator::file::combine_chunks(outputs)?; for output in merged_outputs.iter() { tracing::info!(?output, "Merged file"); + let parent = output.path.parent().unwrap(); + if !parent.exists() { + fs::create_dir_all(parent).await?; + } let mut file = fs::File::create(&output.path).await?; file.write_all(output.content.as_bytes()).await?; } diff --git a/templates/model.hbs b/templates/model.hbs index 2223236..689c94c 100644 --- a/templates/model.hbs +++ b/templates/model.hbs @@ -1,4 +1,4 @@ -use {{entities_path}}::{{model_name}}::{ActiveModel, Model, Entity}; +use {{entities_path}}::{{model_path}}::{ActiveModel, Model, Entity}; use sea_orm::ActiveModelBehavior; #[async_trait::async_trait]