diff --git a/Cargo.toml b/Cargo.toml index 03d558a..44d5ecb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,3 +1,4 @@ +[workspace] [package] name = "sea-orm-generator" version = "0.1.0" diff --git a/src/generator/file.rs b/src/generator/file.rs index a722963..bcf29c7 100644 --- a/src/generator/file.rs +++ b/src/generator/file.rs @@ -81,7 +81,7 @@ impl FileManager { } pub async fn write_files(&self) -> Result<()> { for (file, content) in &self.files { - tracing::debug!(?file, "Writing file"); + tracing::info!(?file, "Writing file"); let parent = file.parent().unwrap(); if !parent.exists() { tokio::fs::create_dir_all(parent).await?; @@ -91,6 +91,16 @@ impl FileManager { } Ok(()) } + pub async fn format_files(&self) -> Result<()> { + for file in self.files.keys() { + tracing::info!(?file, "Formatting file"); + tokio::process::Command::new("rustfmt") + .arg(file) + .output() + .await?; + } + Ok(()) + } } #[cfg(test)] diff --git a/src/generator/mod.rs b/src/generator/mod.rs index 34296ea..e6ef948 100644 --- a/src/generator/mod.rs +++ b/src/generator/mod.rs @@ -2,15 +2,17 @@ pub mod file; pub mod modules; use color_eyre::Result; use toml_edit::DocumentMut; + +use crate::Args; #[derive(Clone, Debug)] pub struct DatabaseUrl(String); -pub async fn generate(database_url: &str, root_config: DocumentMut) -> Result<()> { +pub async fn generate(args: Args, root_config: DocumentMut) -> Result<()> { let mut module_manager = modules::ModuleManager::new(root_config); module_manager.init()?; let ctx = module_manager.get_context_mut(); ctx.get_anymap_mut() - .insert(DatabaseUrl(database_url.to_owned())); + .insert(DatabaseUrl(args.database_url.to_owned())); module_manager.validate().await?; module_manager.execute().await?; module_manager @@ -18,6 +20,13 @@ pub async fn generate(database_url: &str, root_config: DocumentMut) -> Result<() .get_file_manager() .write_files() .await?; + if args.rustfmt { + module_manager + .get_context_mut() + .get_file_manager() + .format_files() + .await?; + } Ok(()) } diff --git a/src/generator/modules/annotate/comment.rs b/src/generator/modules/annotate/comment.rs index 97e00b9..736817a 100644 --- a/src/generator/modules/annotate/comment.rs +++ b/src/generator/modules/annotate/comment.rs @@ -44,7 +44,7 @@ pub fn generate_comment( header.push("Attrs"); } column_info_table - .load_preset(" -+=++ + ++") + .load_preset("|| -+=++ + ++") .set_content_arrangement(ContentArrangement::Dynamic) .set_header(header); if let Some(width) = config.max_wdith { diff --git a/src/generator/modules/models/mod.rs b/src/generator/modules/models/mod.rs index bf39093..0014c8b 100644 --- a/src/generator/modules/models/mod.rs +++ b/src/generator/modules/models/mod.rs @@ -152,7 +152,6 @@ impl Module for ModelsModule { config.clone(), ); - files.push((mod_path.clone(), format!("pub mod {};", table.name))); if path.exists() { tracing::debug!(?path, "Model file already exists"); continue; diff --git a/src/main.rs b/src/main.rs index a589aec..5fa7044 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,11 +8,15 @@ use toml_edit::DocumentMut; use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter}; #[derive(Parser, Debug)] -struct Args { +pub struct Args { #[clap(short, long, default_value = "generator.toml")] config: String, #[clap(short, long, env = "DATABASE_URL")] database_url: String, + #[clap(short, long)] + workdir: Option, + #[clap(short, long, default_value = "true")] + rustfmt: bool, } #[tokio::main] @@ -24,11 +28,15 @@ async fn main() -> Result<()> { .with(EnvFilter::from_default_env()) .init(); let args = Args::parse(); - tracing::info!(?args); - let config = fs::read_to_string(args.config).await?; + tracing::info!(?args); + // change workdir + if let Some(workdir) = &args.workdir { + std::env::set_current_dir(workdir)?; + } + let config = fs::read_to_string(&args.config).await?; let root_config = config.parse::()?; - generator::generate(&args.database_url, root_config).await?; + generator::generate(args, root_config).await?; Ok(()) }