fix generator template
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
[workspace]
|
||||
[package]
|
||||
name = "sea-orm-generator"
|
||||
version = "0.1.0"
|
||||
|
||||
@@ -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)]
|
||||
|
||||
@@ -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(())
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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;
|
||||
|
||||
16
src/main.rs
16
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<String>,
|
||||
#[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::<DocumentMut>()?;
|
||||
|
||||
generator::generate(&args.database_url, root_config).await?;
|
||||
generator::generate(args, root_config).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user