@@ -20,6 +20,28 @@ static void Main(string[] args)
2020 . WithParsed < ExtensionOptions > ( GenerateExtensions ) ;
2121 }
2222
23+ private static string ? GetSegments ( string ? ns , string ? baseNs )
24+ {
25+ if ( ns == null || string . IsNullOrEmpty ( baseNs ) || baseNs == ns )
26+ return null ;
27+ return ns . StartsWith ( baseNs + "." ) ? ns . Substring ( baseNs . Length + 1 ) : ns ;
28+ }
29+
30+ private static string ? CreateNamespace ( string ? ns , string ? segment , string ? typeNs )
31+ {
32+ if ( ns == null )
33+ return typeNs ;
34+ return segment == null ? ns : $ "{ ns } .{ segment } ";
35+ }
36+
37+ private static string GetOutput ( string baseOutput , string ? segment , string typeName )
38+ {
39+ var fullBasePath = Path . GetFullPath ( baseOutput ) ;
40+ return segment == null
41+ ? Path . Combine ( fullBasePath , typeName + ".g.cs" )
42+ : Path . Combine ( fullBasePath , segment . Replace ( '.' , '/' ) , typeName + ".g.cs" ) ;
43+ }
44+
2345 private static void WriteFile ( string code , string path )
2446 {
2547 var dir = Path . GetDirectoryName ( path ) ;
@@ -52,10 +74,11 @@ private static void GenerateMappers(MapperOptions opt)
5274
5375 Console . WriteLine ( $ "Processing: { type . FullName } ") ;
5476
77+ var segments = GetSegments ( type . Namespace , opt . BaseNamespace ) ;
5578 var definitions = new TypeDefinitions
5679 {
5780 Implements = new [ ] { type } ,
58- Namespace = opt . Namespace ?? type . Namespace ,
81+ Namespace = CreateNamespace ( opt . Namespace , segments , type . Namespace ) ,
5982 TypeName = attr . Name ?? GetImplName ( type . Name ) ,
6083 IsInternal = attr . IsInternal ,
6184 PrintFullTypeName = opt . PrintFullTypeName ,
@@ -103,7 +126,7 @@ private static void GenerateMappers(MapperOptions opt)
103126 }
104127
105128 var code = translator . ToString ( ) ;
106- var path = Path . Combine ( Path . GetFullPath ( opt . Output ) , definitions . TypeName + ".g.cs" ) ;
129+ var path = GetOutput ( opt . Output , segments , definitions . TypeName ) ;
107130 WriteFile ( code , path ) ;
108131 }
109132 }
@@ -156,10 +179,11 @@ private static void GenerateModels(ModelOptions opt)
156179 }
157180 private static void CreateModel ( ModelOptions opt , Type type , AdaptAttributeBuilder builder )
158181 {
182+ var segments = GetSegments ( type . Namespace , opt . BaseNamespace ) ;
159183 var attr = builder . Attribute ;
160184 var definitions = new TypeDefinitions
161185 {
162- Namespace = opt . Namespace ?? type . Namespace ,
186+ Namespace = CreateNamespace ( opt . Namespace , segments , type . Namespace ) ,
163187 TypeName = attr . Name ! . Replace ( "[name]" , type . Name ) ,
164188 PrintFullTypeName = opt . PrintFullTypeName ,
165189 IsRecordType = opt . IsRecordType ,
@@ -228,7 +252,7 @@ private static void CreateModel(ModelOptions opt, Type type, AdaptAttributeBuild
228252 }
229253
230254 var code = translator . ToString ( ) ;
231- var path = Path . Combine ( Path . GetFullPath ( opt . Output ) , definitions . TypeName + ".g.cs" ) ;
255+ var path = GetOutput ( opt . Output , segments , definitions . TypeName ) ;
232256 WriteFile ( code , path ) ;
233257
234258 static Type getPropType ( MemberInfo mem )
@@ -395,10 +419,11 @@ private static void GenerateExtensions(ExtensionOptions opt)
395419
396420 Console . WriteLine ( $ "Processing: { type . FullName } ") ;
397421
422+ var segments = GetSegments ( type . Namespace , opt . BaseNamespace ) ;
398423 var definitions = new TypeDefinitions
399424 {
400425 IsStatic = true ,
401- Namespace = opt . Namespace ?? type . Namespace ,
426+ Namespace = CreateNamespace ( opt . Namespace , segments , type . Namespace ) ,
402427 TypeName = mapperAttr . Name . Replace ( "[name]" , type . Name ) ,
403428 IsInternal = mapperAttr . IsInternal ,
404429 PrintFullTypeName = opt . PrintFullTypeName ,
@@ -435,7 +460,7 @@ private static void GenerateExtensions(ExtensionOptions opt)
435460 }
436461
437462 var code = translator . ToString ( ) ;
438- var path = Path . Combine ( Path . GetFullPath ( opt . Output ) , definitions . TypeName + ".g.cs" ) ;
463+ var path = GetOutput ( opt . Output , segments , definitions . TypeName ) ;
439464 WriteFile ( code , path ) ;
440465 }
441466 }
0 commit comments