[cli] Add -l/--lib flag (#953)

* [cli] Add `-l`/`--lib` flag

* [cli] Change function name to reflect functionality
This commit is contained in:
Horu 2022-09-25 09:31:26 +07:00 committed by GitHub
parent 4acdaacebc
commit 6816e86f4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 3 deletions

View File

@ -191,6 +191,15 @@ pub enum GenerateSubcommands {
help = "The datetime crate to use for generating entities."
)]
date_time_crate: DateTimeCrate,
#[clap(
action,
long,
short = 'l',
default_value = "false",
help = "Generate index file as `lib.rs` instead of `mod.rs`."
)]
lib: bool,
},
}

View File

@ -26,6 +26,7 @@ pub async fn run_generate_command(
with_serde,
with_copy_enums,
date_time_crate,
lib,
} => {
if verbose {
let _ = tracing_subscriber::fmt()
@ -171,6 +172,7 @@ pub async fn run_generate_command(
with_copy_enums,
date_time_crate.into(),
schema_name,
lib,
);
let output = EntityTransformer::transform(table_stmts)?.generate(&writer_context);

View File

@ -42,6 +42,7 @@ pub struct EntityWriterContext {
pub(crate) with_copy_enums: bool,
pub(crate) date_time_crate: DateTimeCrate,
pub(crate) schema_name: Option<String>,
pub(crate) lib: bool,
}
impl WithSerde {
@ -101,6 +102,7 @@ impl EntityWriterContext {
with_copy_enums: bool,
date_time_crate: DateTimeCrate,
schema_name: Option<String>,
lib: bool,
) -> Self {
Self {
expanded_format,
@ -108,6 +110,7 @@ impl EntityWriterContext {
with_copy_enums,
date_time_crate,
schema_name,
lib,
}
}
}
@ -116,7 +119,7 @@ impl EntityWriter {
pub fn generate(self, context: &EntityWriterContext) -> WriterOutput {
let mut files = Vec::new();
files.extend(self.write_entities(context));
files.push(self.write_mod());
files.push(self.write_index_file(context.lib));
files.push(self.write_prelude());
if !self.enums.is_empty() {
files.push(
@ -168,7 +171,7 @@ impl EntityWriter {
.collect()
}
pub fn write_mod(&self) -> OutputFile {
pub fn write_index_file(&self, lib: bool) -> OutputFile {
let mut lines = Vec::new();
Self::write_doc_comment(&mut lines);
let code_blocks: Vec<TokenStream> = self.entities.iter().map(Self::gen_mod).collect();
@ -188,8 +191,14 @@ impl EntityWriter {
}],
);
}
let file_name = match lib {
true => "lib.rs".to_owned(),
false => "mod.rs".to_owned(),
};
OutputFile {
name: "mod.rs".to_owned(),
name: file_name,
content: lines.join("\n"),
}
}