@@ -66,6 +66,15 @@ pub enum EntityFormat {
6666 Dense ,
6767}
6868
69+ #[ derive( Debug , Default , PartialEq , Eq , Copy , Clone ) ]
70+ pub enum BannerVersion {
71+ Off ,
72+ Major ,
73+ #[ default]
74+ Minor ,
75+ Patch ,
76+ }
77+
6978#[ derive( Debug ) ]
7079pub struct EntityWriterContext {
7180 pub ( crate ) entity_format : EntityFormat ,
@@ -85,6 +94,7 @@ pub struct EntityWriterContext {
8594 pub ( crate ) column_extra_derives : TokenStream ,
8695 pub ( crate ) seaography : bool ,
8796 pub ( crate ) impl_active_model_behavior : bool ,
97+ pub ( crate ) banner_version : BannerVersion ,
8898}
8999
90100impl WithSerde {
@@ -222,6 +232,7 @@ impl EntityWriterContext {
222232 column_extra_derives : Vec < String > ,
223233 seaography : bool ,
224234 impl_active_model_behavior : bool ,
235+ banner_version : BannerVersion ,
225236 ) -> Self {
226237 Self {
227238 entity_format,
@@ -241,6 +252,7 @@ impl EntityWriterContext {
241252 column_extra_derives : bonus_derive ( column_extra_derives) ,
242253 seaography,
243254 impl_active_model_behavior,
255+ banner_version,
244256 }
245257 }
246258
@@ -257,9 +269,18 @@ impl EntityWriter {
257269 let mut files = Vec :: new ( ) ;
258270 files. extend ( self . write_entities ( context) ) ;
259271 let with_prelude = context. with_prelude != WithPrelude :: None ;
260- files. push ( self . write_index_file ( context. lib , with_prelude, context. seaography ) ) ;
272+ files. push ( self . write_index_file (
273+ context. lib ,
274+ with_prelude,
275+ context. seaography ,
276+ context. banner_version ,
277+ ) ) ;
261278 if with_prelude {
262- files. push ( self . write_prelude ( context. with_prelude , context. entity_format ) ) ;
279+ files. push ( self . write_prelude (
280+ context. with_prelude ,
281+ context. entity_format ,
282+ context. banner_version ,
283+ ) ) ;
263284 }
264285 if !self . enums . is_empty ( ) {
265286 files. push ( self . write_sea_orm_active_enums (
@@ -268,6 +289,7 @@ impl EntityWriter {
268289 & context. enum_extra_derives ,
269290 & context. enum_extra_attributes ,
270291 context. entity_format ,
292+ context. banner_version ,
271293 ) ) ;
272294 }
273295 WriterOutput { files }
@@ -299,7 +321,7 @@ impl EntityWriter {
299321 }
300322
301323 let mut lines = Vec :: new ( ) ;
302- Self :: write_doc_comment ( & mut lines) ;
324+ Self :: write_doc_comment ( & mut lines, context . banner_version ) ;
303325 let code_blocks = if context. entity_format == EntityFormat :: Frontend {
304326 Self :: gen_frontend_code_blocks (
305327 entity,
@@ -366,9 +388,15 @@ impl EntityWriter {
366388 . collect ( )
367389 }
368390
369- pub fn write_index_file ( & self , lib : bool , prelude : bool , seaography : bool ) -> OutputFile {
391+ pub fn write_index_file (
392+ & self ,
393+ lib : bool ,
394+ prelude : bool ,
395+ seaography : bool ,
396+ banner_version : BannerVersion ,
397+ ) -> OutputFile {
370398 let mut lines = Vec :: new ( ) ;
371- Self :: write_doc_comment ( & mut lines) ;
399+ Self :: write_doc_comment ( & mut lines, banner_version ) ;
372400 let code_blocks: Vec < TokenStream > = self . entities . iter ( ) . map ( Self :: gen_mod) . collect ( ) ;
373401 if prelude {
374402 Self :: write (
@@ -410,9 +438,10 @@ impl EntityWriter {
410438 & self ,
411439 with_prelude : WithPrelude ,
412440 entity_format : EntityFormat ,
441+ banner_version : BannerVersion ,
413442 ) -> OutputFile {
414443 let mut lines = Vec :: new ( ) ;
415- Self :: write_doc_comment ( & mut lines) ;
444+ Self :: write_doc_comment ( & mut lines, banner_version ) ;
416445 if with_prelude == WithPrelude :: AllAllowUnusedImports {
417446 Self :: write_allow_unused_imports ( & mut lines)
418447 }
@@ -441,9 +470,10 @@ impl EntityWriter {
441470 extra_derives : & TokenStream ,
442471 extra_attributes : & TokenStream ,
443472 entity_format : EntityFormat ,
473+ banner_version : BannerVersion ,
444474 ) -> OutputFile {
445475 let mut lines = Vec :: new ( ) ;
446- Self :: write_doc_comment ( & mut lines) ;
476+ Self :: write_doc_comment ( & mut lines, banner_version ) ;
447477 if entity_format == EntityFormat :: Frontend {
448478 Self :: write ( & mut lines, vec ! [ Self :: gen_import_serde( with_serde) ] ) ;
449479 } else {
@@ -479,10 +509,30 @@ impl EntityWriter {
479509 ) ;
480510 }
481511
482- pub fn write_doc_comment ( lines : & mut Vec < String > ) {
512+ pub fn write_doc_comment ( lines : & mut Vec < String > , banner_version : BannerVersion ) {
483513 let ver = env ! ( "CARGO_PKG_VERSION" ) ;
514+ let version_str = match banner_version {
515+ BannerVersion :: Off => String :: new ( ) ,
516+ BannerVersion :: Patch => ver. to_owned ( ) ,
517+ _ => {
518+ let parts: Vec < & str > = ver. split ( '.' ) . collect ( ) ;
519+ match banner_version {
520+ BannerVersion :: Major => {
521+ parts. first ( ) . map ( |x| ( * x) . to_owned ( ) ) . unwrap_or_default ( )
522+ }
523+ BannerVersion :: Minor => {
524+ if parts. len ( ) >= 2 {
525+ format ! ( "{}.{}" , parts[ 0 ] , parts[ 1 ] )
526+ } else {
527+ ver. to_owned ( )
528+ }
529+ }
530+ _ => unreachable ! ( ) ,
531+ }
532+ }
533+ } ;
484534 let comments = vec ! [ format!(
485- "//! `SeaORM` Entity, @generated by sea-orm-codegen {ver }"
535+ "//! `SeaORM` Entity, @generated by sea-orm-codegen {version_str }"
486536 ) ] ;
487537 lines. extend ( comments) ;
488538 lines. push ( "" . to_owned ( ) ) ;
0 commit comments