Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Sep 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions sea-orm-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,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