From a6f117a908943c835a3feb6c12ac920a3fa29465 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Fri, 10 Sep 2021 23:08:30 +0800 Subject: [PATCH] sea-orm-cli with `--expanded-format` & `--compact-format` flags --- sea-orm-cli/Cargo.toml | 4 ++-- sea-orm-cli/src/cli.rs | 14 ++++++++++++++ sea-orm-cli/src/main.rs | 3 ++- sea-orm-codegen/Cargo.toml | 2 +- sea-orm-codegen/src/entity/writer.rs | 12 ++++++++---- 5 files changed, 27 insertions(+), 8 deletions(-) diff --git a/sea-orm-cli/Cargo.toml b/sea-orm-cli/Cargo.toml index 46340f7b..5646ffc7 100644 --- a/sea-orm-cli/Cargo.toml +++ b/sea-orm-cli/Cargo.toml @@ -22,8 +22,8 @@ clap = { version = "^2.33.3" } dotenv = { version = "^0.15" } async-std = { version = "^1.9", features = [ "attributes" ] } sea-orm = { version = "^0.1.2", features = [ "sqlx-all" ] } -sea-orm-codegen = { version = "^0.2.0" } -sea-schema = { version = "^0.2.7", default-features = false, features = [ +sea-orm-codegen = { version = "^0.2.0", path = "../sea-orm-codegen" } +sea-schema = { version = "^0.2.7", git = "https://github.com/SeaQL/sea-schema.git", branch = "update-sea-query-version", default-features = false, features = [ "sqlx-mysql", "sqlx-postgres", "discovery", diff --git a/sea-orm-cli/src/cli.rs b/sea-orm-cli/src/cli.rs index 8f2919fb..400374c5 100644 --- a/sea-orm-cli/src/cli.rs +++ b/sea-orm-cli/src/cli.rs @@ -40,6 +40,20 @@ pub fn build_cli() -> App<'static, 'static> { .long("include-hidden-tables") .help("Generate entity file for hidden tables (i.e. table name starts with an underscore)") .takes_value(false), + ) + .arg( + Arg::with_name("EXPANDED_FORMAT") + .long("expanded-format") + .help("Generate entity file of expanded format") + .takes_value(false) + .conflicts_with("COMPACT_FORMAT"), + ) + .arg( + Arg::with_name("COMPACT_FORMAT") + .long("compact-format") + .help("Generate entity file of compact format") + .takes_value(false) + .conflicts_with("EXPANDED_FORMAT"), ), ) .setting(AppSettings::SubcommandRequiredElseHelp); diff --git a/sea-orm-cli/src/main.rs b/sea-orm-cli/src/main.rs index c3cc60dd..2f51aefb 100644 --- a/sea-orm-cli/src/main.rs +++ b/sea-orm-cli/src/main.rs @@ -25,6 +25,7 @@ async fn run_generate_command(matches: &ArgMatches<'_>) -> Result<(), Box bool { if include_hidden_tables { true @@ -66,7 +67,7 @@ async fn run_generate_command(matches: &ArgMatches<'_>) -> Result<(), Box WriterOutput { + pub fn generate(self, expanded_format: bool) -> WriterOutput { let mut files = Vec::new(); - files.extend(self.write_entities()); + files.extend(self.write_entities(expanded_format)); files.push(self.write_mod()); files.push(self.write_prelude()); WriterOutput { files } } - pub fn write_entities(&self) -> Vec { + pub fn write_entities(&self, expanded_format: bool) -> Vec { self.entities .iter() .map(|entity| { let mut lines = Vec::new(); Self::write_doc_comment(&mut lines); - let code_blocks = Self::gen_expanded_code_blocks(entity); + let code_blocks = if expanded_format { + Self::gen_expanded_code_blocks(entity) + } else { + Self::gen_compact_code_blocks(entity) + }; Self::write(&mut lines, code_blocks); OutputFile { name: format!("{}.rs", entity.get_table_name_snake_case()),