sea-orm-cli with --expanded-format & --compact-format flags

This commit is contained in:
Billy Chan 2021-09-10 23:08:30 +08:00
parent 9c3aba8c0e
commit a6f117a908
No known key found for this signature in database
GPG Key ID: A2D690CAC7DF3CC7
5 changed files with 27 additions and 8 deletions

View File

@ -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",

View File

@ -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);

View File

@ -25,6 +25,7 @@ async fn run_generate_command(matches: &ArgMatches<'_>) -> Result<(), Box<dyn Er
let url = args.value_of("DATABASE_URL").unwrap();
let output_dir = args.value_of("OUTPUT_DIR").unwrap();
let include_hidden_tables = args.is_present("INCLUDE_HIDDEN_TABLES");
let expanded_format = args.is_present("EXPANDED_FORMAT");
let filter_hidden_tables = |table: &str| -> bool {
if include_hidden_tables {
true
@ -66,7 +67,7 @@ async fn run_generate_command(matches: &ArgMatches<'_>) -> Result<(), Box<dyn Er
panic!("This database is not supported ({})", url)
};
let output = EntityTransformer::transform(table_stmts)?.generate();
let output = EntityTransformer::transform(table_stmts)?.generate(expanded_format);
let dir = Path::new(output_dir);
fs::create_dir_all(dir)?;

View File

@ -15,7 +15,7 @@ name = "sea_orm_codegen"
path = "src/lib.rs"
[dependencies]
sea-query = { version = "0.16.1", git = "https://github.com/SeaQL/sea-query.git", branch = "foreign-key-getters" }
sea-query = { version = "^0.16.1", git = "https://github.com/SeaQL/sea-query.git", branch = "foreign-key-getters" }
syn = { version = "^1", default-features = false, features = [
"derive",
"parsing",

View File

@ -17,21 +17,25 @@ pub struct OutputFile {
}
impl EntityWriter {
pub fn generate(self) -> 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<OutputFile> {
pub fn write_entities(&self, expanded_format: bool) -> Vec<OutputFile> {
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()),