Skip to content
Open
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
7 changes: 7 additions & 0 deletions sea-orm-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,13 @@ pub enum GenerateSubcommands {
)]
lib: bool,

#[arg(
long,
default_value = "false",
help = "Use the new directory structure when generating the entity files."
)]
use_new_dir_structure: bool,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some alternatives:

  • (use_)new_module_(style/structure/layout): bool
  • module_(style/structure/layout): (old, 2018)

@tyt2y3 What do you think?


#[arg(
long,
value_delimiter = ',',
Expand Down
27 changes: 21 additions & 6 deletions sea-orm-cli/src/commands/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ use sea_orm_codegen::{
DateTimeCrate as CodegenDateTimeCrate, EntityTransformer, EntityWriterContext, OutputFile,
WithPrelude, WithSerde,
};
use std::{error::Error, fs, io::Write, path::Path, process::Command, str::FromStr};
use std::{
error::Error,
fs,
io::Write,
path::{Path, PathBuf},
process::Command,
str::FromStr,
};
use tracing_subscriber::{EnvFilter, prelude::*};
use url::Url;

Expand All @@ -28,6 +35,7 @@ pub async fn run_generate_command(
database_url,
with_prelude,
with_serde,
use_new_dir_structure,
serde_skip_deserializing_primary_key,
serde_skip_hidden_column,
with_copy_enums,
Expand Down Expand Up @@ -227,6 +235,7 @@ pub async fn run_generate_command(
date_time_crate.into(),
schema_name,
lib,
use_new_dir_structure,
serde_skip_deserializing_primary_key,
serde_skip_hidden_column,
model_extra_derives,
Expand All @@ -239,19 +248,25 @@ pub async fn run_generate_command(
);
let output = EntityTransformer::transform(table_stmts)?.generate(&writer_context);

let dir = Path::new(&output_dir);
fs::create_dir_all(dir)?;

let output_dir = Path::new(&(output_dir));
if use_new_dir_structure {
let entities_dir = output_dir.to_path_buf().join("entities");
fs::create_dir_all(&entities_dir)?;
} else {
fs::create_dir_all(output_dir)?;
}
for OutputFile { name, content } in output.files.iter() {
let file_path = dir.join(name);
let file_path = output_dir.join(name);
println!("Writing {}", file_path.display());
let mut file = fs::File::create(file_path)?;
file.write_all(content.as_bytes())?;
}

// Format each of the files
for OutputFile { name, .. } in output.files.iter() {
let exit_status = Command::new("rustfmt").arg(dir.join(name)).status()?; // Get the status code
let exit_status = Command::new("rustfmt")
.arg(output_dir.join(name))
.status()?; // Get the status code
if !exit_status.success() {
// Propagate the error if any
return Err(format!("Fail to format file `{name}`").into());
Expand Down
47 changes: 40 additions & 7 deletions sea-orm-codegen/src/entity/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub struct EntityWriterContext {
pub(crate) lib: bool,
pub(crate) serde_skip_hidden_column: bool,
pub(crate) serde_skip_deserializing_primary_key: bool,
pub(crate) use_new_dir_structure: bool,
pub(crate) model_extra_derives: TokenStream,
pub(crate) model_extra_attributes: TokenStream,
pub(crate) enum_extra_derives: TokenStream,
Expand Down Expand Up @@ -172,6 +173,7 @@ impl EntityWriterContext {
date_time_crate: DateTimeCrate,
schema_name: Option<String>,
lib: bool,
use_new_dir_structure: bool,
serde_skip_deserializing_primary_key: bool,
serde_skip_hidden_column: bool,
model_extra_derives: Vec<String>,
Expand All @@ -191,6 +193,7 @@ impl EntityWriterContext {
date_time_crate,
schema_name,
lib,
use_new_dir_structure,
serde_skip_deserializing_primary_key,
serde_skip_hidden_column,
model_extra_derives: bonus_derive(model_extra_derives),
Expand All @@ -209,9 +212,18 @@ impl EntityWriter {
let mut files = Vec::new();
files.extend(self.write_entities(context));
let with_prelude = context.with_prelude != WithPrelude::None;
files.push(self.write_index_file(context.lib, with_prelude, context.seaography));
files.push(self.write_index_file(
context.lib,
with_prelude,
context.seaography,
context.use_new_dir_structure,
));
if with_prelude {
files.push(self.write_prelude(context.with_prelude, context.frontend_format));
files.push(self.write_prelude(
context.with_prelude,
context.frontend_format,
context.use_new_dir_structure,
));
}
if !self.enums.is_empty() {
files.push(self.write_sea_orm_active_enums(
Expand All @@ -229,7 +241,10 @@ impl EntityWriter {
self.entities
.iter()
.map(|entity| {
let entity_file = format!("{}.rs", entity.get_table_name_snake_case());
let entity_file = match context.use_new_dir_structure {
false => format!("{}.rs", entity.get_table_name_snake_case()),
true => format!("entities/{}.rs", entity.get_table_name_snake_case()),
};
let column_info = entity
.columns
.iter()
Expand Down Expand Up @@ -304,7 +319,13 @@ impl EntityWriter {
.collect()
}

pub fn write_index_file(&self, lib: bool, prelude: bool, seaography: bool) -> OutputFile {
pub fn write_index_file(
&self,
lib: bool,
prelude: bool,
seaography: bool,
use_new_dir_structure: 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 Down Expand Up @@ -335,7 +356,10 @@ impl EntityWriter {

let file_name = match lib {
true => "lib.rs".to_owned(),
false => "mod.rs".to_owned(),
false => match use_new_dir_structure {
true => "entities.rs".to_owned(),
false => "mod.rs".to_owned(),
},
};

OutputFile {
Expand All @@ -344,7 +368,12 @@ impl EntityWriter {
}
}

pub fn write_prelude(&self, with_prelude: WithPrelude, frontend_format: bool) -> OutputFile {
pub fn write_prelude(
&self,
with_prelude: WithPrelude,
frontend_format: bool,
use_new_dir_structure: bool,
) -> OutputFile {
let mut lines = Vec::new();
Self::write_doc_comment(&mut lines);
if with_prelude == WithPrelude::AllAllowUnusedImports {
Expand All @@ -362,8 +391,12 @@ impl EntityWriter {
})
.collect();
Self::write(&mut lines, code_blocks);
let file_name = match use_new_dir_structure {
true => "entities/prelude.rs".to_owned(),
false => "prelude.rs".to_owned(),
};
OutputFile {
name: "prelude.rs".to_owned(),
name: file_name,
content: lines.join("\n"),
}
}
Expand Down