Skip to content

Commit

Permalink
[cli] Add -l/--lib flag (#953)
Browse files Browse the repository at this point in the history
* [cli] Add `-l`/`--lib` flag

* [cli] Change function name to reflect functionality
  • Loading branch information
HigherOrderLogic authored Sep 25, 2022
1 parent 4acdaac commit 6816e86
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
9 changes: 9 additions & 0 deletions sea-orm-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}

Expand Down
2 changes: 2 additions & 0 deletions sea-orm-cli/src/commands/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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);

Expand Down
15 changes: 12 additions & 3 deletions sea-orm-codegen/src/entity/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -101,13 +102,15 @@ impl EntityWriterContext {
with_copy_enums: bool,
date_time_crate: DateTimeCrate,
schema_name: Option<String>,
lib: bool,
) -> Self {
Self {
expanded_format,
with_serde,
with_copy_enums,
date_time_crate,
schema_name,
lib,
}
}
}
Expand All @@ -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(
Expand Down Expand Up @@ -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();
Expand All @@ -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"),
}
}
Expand Down

0 comments on commit 6816e86

Please sign in to comment.