add prelude and fix empty gen

This commit is contained in:
2025-04-05 11:06:26 +04:00
parent f380513725
commit 613ade1878
5 changed files with 56 additions and 15 deletions

View File

@@ -56,11 +56,6 @@ impl ModelCommentGenerator {
Self::generate_comment_content(table, config, &settings, new_settings)?; Self::generate_comment_content(table, config, &settings, new_settings)?;
return Ok(file_content.replace(body, &comment)); 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 {
} }
} }
} }

View File

@@ -9,6 +9,7 @@ use color_eyre::Result;
use comfy_table::{ContentArrangement, Table as CTable}; use comfy_table::{ContentArrangement, Table as CTable};
use comment_parser::{CommentParser, Event}; use comment_parser::{CommentParser, Event};
use handlebars::Handlebars; use handlebars::Handlebars;
use heck::ToPascalCase;
use sea_orm_codegen::OutputFile; use sea_orm_codegen::OutputFile;
use serde::Serialize; use serde::Serialize;
use tokio::fs; use tokio::fs;
@@ -17,14 +18,14 @@ pub struct FileGenerator {
filename: String, filename: String,
table: Table, table: Table,
} }
// #[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
// pub struct FileContext { pub struct ModelContext {
// entities_path: String, entities_path: String,
// model_path: Option<String>, model_path: String,
// model_name: Option<String>, model_name: String,
// active_model_name: Option<String>, active_model_name: String,
// prelude_path: Option<String>, prelude_path: Option<String>,
// } }
impl FileGenerator { impl FileGenerator {
pub async fn generate_file<'a>( pub async fn generate_file<'a>(
@@ -44,6 +45,40 @@ impl FileGenerator {
file_chunks file_chunks
.extend(Self::handle_existing_file(table, &filepath, config, handlebars).await?); .extend(Self::handle_existing_file(table, &filepath, config, handlebars).await?);
} else { } 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); // let filepath = config.output.path.join(&self.filename);

View File

@@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize};
use std::path::PathBuf; use std::path::PathBuf;
use table::Table; use table::Table;
use super::file::{GeneratedFile, GeneratedFileChunk}; use super::file::GeneratedFileChunk;
pub mod column; pub mod column;
pub mod comment; pub mod comment;
pub mod discover; pub mod discover;
@@ -131,6 +131,13 @@ pub async fn generate_models<'a>(
for table in tables { for table in tables {
files.extend(FileGenerator::generate_file(table, &model_config, handlebars).await?); 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) Ok(files)
} }

View File

@@ -53,6 +53,10 @@ async fn main() -> Result<()> {
let merged_outputs = generator::file::combine_chunks(outputs)?; let merged_outputs = generator::file::combine_chunks(outputs)?;
for output in merged_outputs.iter() { for output in merged_outputs.iter() {
tracing::info!(?output, "Merged file"); 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?; let mut file = fs::File::create(&output.path).await?;
file.write_all(output.content.as_bytes()).await?; file.write_all(output.content.as_bytes()).await?;
} }

View File

@@ -1,4 +1,4 @@
use {{entities_path}}::{{model_name}}::{ActiveModel, Model, Entity}; use {{entities_path}}::{{model_path}}::{ActiveModel, Model, Entity};
use sea_orm::ActiveModelBehavior; use sea_orm::ActiveModelBehavior;
#[async_trait::async_trait] #[async_trait::async_trait]