Skip to content

Commit 96e7e64

Browse files
committed
changed the directory structure during entity file generation
Signed-off-by: involk-secure-1609 <kevintj916@gmail.com>
1 parent eddb6ca commit 96e7e64

File tree

3 files changed

+68
-13
lines changed

3 files changed

+68
-13
lines changed

sea-orm-cli/src/cli.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,13 @@ pub enum GenerateSubcommands {
296296
)]
297297
lib: bool,
298298

299+
#[arg(
300+
long,
301+
default_value = "false",
302+
help = "Use the new directory structure when generating the entity files."
303+
)]
304+
use_new_dir_structure: bool,
305+
299306
#[arg(
300307
long,
301308
value_delimiter = ',',

sea-orm-cli/src/commands/generate.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@ use sea_orm_codegen::{
33
DateTimeCrate as CodegenDateTimeCrate, EntityTransformer, EntityWriterContext, OutputFile,
44
WithPrelude, WithSerde,
55
};
6-
use std::{error::Error, fs, io::Write, path::Path, process::Command, str::FromStr};
6+
use std::{
7+
error::Error,
8+
fs,
9+
io::Write,
10+
path::{Path, PathBuf},
11+
process::Command,
12+
str::FromStr,
13+
};
714
use tracing_subscriber::{EnvFilter, prelude::*};
815
use url::Url;
916

@@ -28,6 +35,7 @@ pub async fn run_generate_command(
2835
database_url,
2936
with_prelude,
3037
with_serde,
38+
use_new_dir_structure,
3139
serde_skip_deserializing_primary_key,
3240
serde_skip_hidden_column,
3341
with_copy_enums,
@@ -227,6 +235,7 @@ pub async fn run_generate_command(
227235
date_time_crate.into(),
228236
schema_name,
229237
lib,
238+
use_new_dir_structure,
230239
serde_skip_deserializing_primary_key,
231240
serde_skip_hidden_column,
232241
model_extra_derives,
@@ -239,19 +248,25 @@ pub async fn run_generate_command(
239248
);
240249
let output = EntityTransformer::transform(table_stmts)?.generate(&writer_context);
241250

242-
let dir = Path::new(&output_dir);
243-
fs::create_dir_all(dir)?;
244-
251+
let output_dir = Path::new(&(output_dir));
252+
if use_new_dir_structure {
253+
let entities_dir = output_dir.to_path_buf().join("entities");
254+
fs::create_dir_all(&entities_dir)?;
255+
} else {
256+
fs::create_dir_all(output_dir)?;
257+
}
245258
for OutputFile { name, content } in output.files.iter() {
246-
let file_path = dir.join(name);
259+
let file_path = output_dir.join(name);
247260
println!("Writing {}", file_path.display());
248261
let mut file = fs::File::create(file_path)?;
249262
file.write_all(content.as_bytes())?;
250263
}
251264

252265
// Format each of the files
253266
for OutputFile { name, .. } in output.files.iter() {
254-
let exit_status = Command::new("rustfmt").arg(dir.join(name)).status()?; // Get the status code
267+
let exit_status = Command::new("rustfmt")
268+
.arg(output_dir.join(name))
269+
.status()?; // Get the status code
255270
if !exit_status.success() {
256271
// Propagate the error if any
257272
return Err(format!("Fail to format file `{name}`").into());

sea-orm-codegen/src/entity/writer.rs

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ pub struct EntityWriterContext {
5555
pub(crate) lib: bool,
5656
pub(crate) serde_skip_hidden_column: bool,
5757
pub(crate) serde_skip_deserializing_primary_key: bool,
58+
pub(crate) use_new_dir_structure: bool,
5859
pub(crate) model_extra_derives: TokenStream,
5960
pub(crate) model_extra_attributes: TokenStream,
6061
pub(crate) enum_extra_derives: TokenStream,
@@ -172,6 +173,7 @@ impl EntityWriterContext {
172173
date_time_crate: DateTimeCrate,
173174
schema_name: Option<String>,
174175
lib: bool,
176+
use_new_dir_structure: bool,
175177
serde_skip_deserializing_primary_key: bool,
176178
serde_skip_hidden_column: bool,
177179
model_extra_derives: Vec<String>,
@@ -191,6 +193,7 @@ impl EntityWriterContext {
191193
date_time_crate,
192194
schema_name,
193195
lib,
196+
use_new_dir_structure,
194197
serde_skip_deserializing_primary_key,
195198
serde_skip_hidden_column,
196199
model_extra_derives: bonus_derive(model_extra_derives),
@@ -209,9 +212,18 @@ impl EntityWriter {
209212
let mut files = Vec::new();
210213
files.extend(self.write_entities(context));
211214
let with_prelude = context.with_prelude != WithPrelude::None;
212-
files.push(self.write_index_file(context.lib, with_prelude, context.seaography));
215+
files.push(self.write_index_file(
216+
context.lib,
217+
with_prelude,
218+
context.seaography,
219+
context.use_new_dir_structure,
220+
));
213221
if with_prelude {
214-
files.push(self.write_prelude(context.with_prelude, context.frontend_format));
222+
files.push(self.write_prelude(
223+
context.with_prelude,
224+
context.frontend_format,
225+
context.use_new_dir_structure,
226+
));
215227
}
216228
if !self.enums.is_empty() {
217229
files.push(self.write_sea_orm_active_enums(
@@ -229,7 +241,10 @@ impl EntityWriter {
229241
self.entities
230242
.iter()
231243
.map(|entity| {
232-
let entity_file = format!("{}.rs", entity.get_table_name_snake_case());
244+
let entity_file = match context.use_new_dir_structure {
245+
false => format!("{}.rs", entity.get_table_name_snake_case()),
246+
true => format!("entities/{}.rs", entity.get_table_name_snake_case()),
247+
};
233248
let column_info = entity
234249
.columns
235250
.iter()
@@ -304,7 +319,13 @@ impl EntityWriter {
304319
.collect()
305320
}
306321

307-
pub fn write_index_file(&self, lib: bool, prelude: bool, seaography: bool) -> OutputFile {
322+
pub fn write_index_file(
323+
&self,
324+
lib: bool,
325+
prelude: bool,
326+
seaography: bool,
327+
use_new_dir_structure: bool,
328+
) -> OutputFile {
308329
let mut lines = Vec::new();
309330
Self::write_doc_comment(&mut lines);
310331
let code_blocks: Vec<TokenStream> = self.entities.iter().map(Self::gen_mod).collect();
@@ -335,7 +356,10 @@ impl EntityWriter {
335356

336357
let file_name = match lib {
337358
true => "lib.rs".to_owned(),
338-
false => "mod.rs".to_owned(),
359+
false => match use_new_dir_structure {
360+
true => "entities.rs".to_owned(),
361+
false => "mod.rs".to_owned(),
362+
},
339363
};
340364

341365
OutputFile {
@@ -344,7 +368,12 @@ impl EntityWriter {
344368
}
345369
}
346370

347-
pub fn write_prelude(&self, with_prelude: WithPrelude, frontend_format: bool) -> OutputFile {
371+
pub fn write_prelude(
372+
&self,
373+
with_prelude: WithPrelude,
374+
frontend_format: bool,
375+
use_new_dir_structure: bool,
376+
) -> OutputFile {
348377
let mut lines = Vec::new();
349378
Self::write_doc_comment(&mut lines);
350379
if with_prelude == WithPrelude::AllAllowUnusedImports {
@@ -362,8 +391,12 @@ impl EntityWriter {
362391
})
363392
.collect();
364393
Self::write(&mut lines, code_blocks);
394+
let file_name = match use_new_dir_structure {
395+
true => "entities/prelude.rs".to_owned(),
396+
false => "prelude.rs".to_owned(),
397+
};
365398
OutputFile {
366-
name: "prelude.rs".to_owned(),
399+
name: file_name,
367400
content: lines.join("\n"),
368401
}
369402
}

0 commit comments

Comments
 (0)