replace handlebars with jinja
This commit is contained in:
@@ -3,17 +3,19 @@ use std::path::PathBuf;
|
||||
use crate::generator::file::pathbuf_to_rust_path;
|
||||
|
||||
use super::{
|
||||
discovery::DiscoveredSchema, sea_orm::SeaOrmConfig, templates::TemplateConfig, Module,
|
||||
ModulesContext,
|
||||
discovery::{table::Table, DiscoveredSchema},
|
||||
sea_orm::SeaOrmConfig,
|
||||
templates::TemplateConfig,
|
||||
Module, ModulesContext,
|
||||
};
|
||||
use color_eyre::{
|
||||
eyre::{eyre, Context, ContextCompat},
|
||||
Result,
|
||||
};
|
||||
use handlebars::Handlebars;
|
||||
use heck::ToPascalCase;
|
||||
use minijinja::Environment;
|
||||
use serde::{Deserialize, Serialize};
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
#[serde(default)]
|
||||
pub struct ModelsConfig {
|
||||
pub enable: bool,
|
||||
@@ -33,27 +35,27 @@ impl Default for ModelsConfig {
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct ModelTemplateContext {
|
||||
entities_path: String,
|
||||
model_path: String,
|
||||
model_name: String,
|
||||
entity_name: String,
|
||||
active_model_name: String,
|
||||
prelude_path: String,
|
||||
entities_path: Option<String>,
|
||||
tables: Option<Vec<Table>>,
|
||||
prelude_path: Option<String>,
|
||||
table_name: Option<String>,
|
||||
config: ModelsConfig,
|
||||
}
|
||||
|
||||
impl ModelTemplateContext {
|
||||
pub fn new(entities_path: String, model_name: String, prelude_path: String) -> Self {
|
||||
let model_path = model_name.clone();
|
||||
let active_model_name = format!("{}ActiveModel", model_name).to_pascal_case();
|
||||
let model_name = format!("{}Model", model_name).to_pascal_case();
|
||||
let entity_name = model_path.clone().to_pascal_case();
|
||||
pub fn new(
|
||||
entities_path: Option<String>,
|
||||
prelude_path: Option<String>,
|
||||
tables: Option<Vec<Table>>,
|
||||
table_name: Option<String>,
|
||||
config: ModelsConfig,
|
||||
) -> Self {
|
||||
Self {
|
||||
entities_path,
|
||||
model_path,
|
||||
model_name,
|
||||
active_model_name,
|
||||
tables,
|
||||
prelude_path,
|
||||
entity_name,
|
||||
table_name,
|
||||
config,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -103,7 +105,7 @@ impl Module for ModelsModule {
|
||||
|
||||
if let (Some(config), Some(templates), Some(sea_orm_config), Some(schema)) = (
|
||||
map.get::<ModelsConfig>(),
|
||||
map.get::<Handlebars<'static>>(),
|
||||
map.get::<Environment<'static>>(),
|
||||
map.get::<SeaOrmConfig>(),
|
||||
map.get::<DiscoveredSchema>(),
|
||||
) {
|
||||
@@ -111,45 +113,57 @@ impl Module for ModelsModule {
|
||||
tracing::info!(?models_path, "Models path");
|
||||
let entities_path = sea_orm_config.path.clone().unwrap();
|
||||
let mod_path = models_path.join("mod.rs");
|
||||
|
||||
let relative_entities_path = pathdiff::diff_paths(&entities_path, &mod_path)
|
||||
.context("Failed to calculate relative path")?;
|
||||
let relative_entities_rust_path = pathbuf_to_rust_path(relative_entities_path);
|
||||
let context = ModelTemplateContext::new(
|
||||
Some(relative_entities_rust_path.clone()),
|
||||
None,
|
||||
Some(schema.tables.clone()),
|
||||
None,
|
||||
config.clone(),
|
||||
);
|
||||
if config.prelude {
|
||||
files.push((mod_path.clone(), "pub mod prelude;".to_string()));
|
||||
let prelude = templates
|
||||
.get_template("model_prelude")?
|
||||
.render(&context)
|
||||
.context("Failed to render model prelude part")?;
|
||||
files.push((models_path.join("prelude.rs"), prelude));
|
||||
}
|
||||
|
||||
let mod_content = templates
|
||||
.get_template("model_mod")?
|
||||
.render(&context)
|
||||
.context("Failed to render model mod")?;
|
||||
files.push((mod_path.clone(), mod_content));
|
||||
for table in &schema.tables {
|
||||
tracing::debug!(?table, "Generating model for table");
|
||||
let path = models_path.join(format!("{}.rs", table.name));
|
||||
|
||||
let relative_entities_path = pathdiff::diff_paths(&entities_path, &path)
|
||||
.context("Failed to calculate relative path")?;
|
||||
let relative_entities_rust_path = pathbuf_to_rust_path(relative_entities_path);
|
||||
let context = ModelTemplateContext::new(
|
||||
relative_entities_rust_path.clone(),
|
||||
table.name.clone(),
|
||||
"super::prelude".to_string(),
|
||||
Some(relative_entities_rust_path.clone()),
|
||||
if config.prelude {
|
||||
Some("super::prelude".to_string())
|
||||
} else {
|
||||
None
|
||||
},
|
||||
None,
|
||||
Some(table.name.clone()),
|
||||
config.clone(),
|
||||
);
|
||||
if config.prelude {
|
||||
let prelude_part = templates
|
||||
.render("model_prelude_part", &context)
|
||||
.context("Failed to render model prelude part")?;
|
||||
files.push((models_path.join("prelude.rs"), prelude_part.clone()));
|
||||
}
|
||||
|
||||
files.push((mod_path.clone(), format!("pub mod {};", table.name)));
|
||||
if path.exists() {
|
||||
tracing::debug!(?path, "Model file already exists");
|
||||
continue;
|
||||
}
|
||||
// files.push((mod_path.clone(), format!("pub mod {};", table.name)));
|
||||
// if path.exists() {
|
||||
// tracing::debug!(?path, "Model file already exists");
|
||||
// continue;
|
||||
// }
|
||||
|
||||
if config.prelude {
|
||||
let content = templates
|
||||
.render("model_prelude", &context)
|
||||
.context("Failed to render model prelude")?;
|
||||
files.push((path.clone(), content.clone()));
|
||||
} else {
|
||||
let content = templates
|
||||
.render("model", &context)
|
||||
.context("Failed to render model")?;
|
||||
files.push((path.clone(), content.clone()));
|
||||
}
|
||||
let content = templates
|
||||
.get_template("model")?
|
||||
.render(&context)
|
||||
.context("Failed to render model")?;
|
||||
files.push((path.clone(), content.clone()));
|
||||
}
|
||||
} else {
|
||||
// One or both keys are missing
|
||||
|
||||
Reference in New Issue
Block a user