@@ -13,57 +13,122 @@ namespace ManagedCode.MimeTypes.Generator;
1313[ Generator ]
1414public class MimeTypeSourceGenerator : ISourceGenerator
1515{
16- public void Initialize ( GeneratorInitializationContext context ) { }
17-
18- public void Execute ( GeneratorExecutionContext context )
16+ public void Initialize ( GeneratorInitializationContext context )
1917 {
20-
2118#if DEBUG
2219 if ( ! Debugger . IsAttached )
2320 {
2421 Debugger . Launch ( ) ;
2522 }
2623#endif
27- var mime = JObject . Parse ( File . ReadAllText ( "mimeTypes.json" ) ) . Properties ( ) . ToList ( ) ;
28-
29- StringBuilder defineDictionaryBuilder = new ( ) ;
30- StringBuilder propertyBuilder = new ( ) ;
31- Dictionary < string , string > types = new Dictionary < string , string > ( ) ;
24+ }
25+
26+ public void Execute ( GeneratorExecutionContext context )
27+ {
28+ try
29+ {
30+ // Find the mimeTypes.json file
31+ var mimeTypesPath = GetMimeTypesPath ( context ) ;
32+
33+ if ( ! File . Exists ( mimeTypesPath ) )
34+ {
35+ context . ReportDiagnostic ( Diagnostic . Create (
36+ new DiagnosticDescriptor (
37+ "MIME001" ,
38+ "MimeTypes.json not found" ,
39+ "Could not find mimeTypes.json at {0}" ,
40+ "MimeTypes" ,
41+ DiagnosticSeverity . Error ,
42+ true ) ,
43+ Location . None ,
44+ mimeTypesPath ) ) ;
45+ return ;
46+ }
3247
33- foreach ( var item in mime )
34- {
35- defineDictionaryBuilder . AppendLine ( $ "MimeTypes.Add(string.Intern(\" { item . Name } \" ),string.Intern(\" { item . Value } \" ));") ;
36-
37- types [ ParseKey ( item . Name ) ] = item . Value . ToString ( ) ;
38-
39- }
48+ var mime = JObject . Parse ( File . ReadAllText ( mimeTypesPath ) ) ;
49+ var properties = mime . Properties ( ) . ToList ( ) ;
50+
51+ context . ReportDiagnostic ( Diagnostic . Create (
52+ new DiagnosticDescriptor (
53+ "MIME002" ,
54+ "MimeTypes loaded" ,
55+ "Successfully loaded {0} mime types" ,
56+ "MimeTypes" ,
57+ DiagnosticSeverity . Info ,
58+ true ) ,
59+ Location . None ,
60+ properties . Count ) ) ;
4061
41- foreach ( var item in types )
42- {
43- propertyBuilder . AppendLine ( $ "public static string { item . Key } => \" { item . Value } \" ;") ;
44- }
45-
46-
47- context . AddSource ( "MimeHelper.Properties.cs" , SourceText . From ( @$ "
62+ StringBuilder defineDictionaryBuilder = new ( ) ;
63+ StringBuilder propertyBuilder = new ( ) ;
64+ Dictionary < string , string > types = new Dictionary < string , string > ( ) ;
65+
66+ foreach ( var item in properties )
67+ {
68+ defineDictionaryBuilder . AppendLine ( $ "MimeTypes.Add(string.Intern(\" { item . Name } \" ),string.Intern(\" { item . Value } \" ));") ;
69+ types [ ParseKey ( item . Name ) ] = item . Value . ToString ( ) ;
70+ }
71+
72+ foreach ( var item in types )
73+ {
74+ propertyBuilder . AppendLine ( $ "public static string { item . Key } => \" { item . Value } \" ;") ;
75+ }
76+
77+ context . AddSource ( "MimeHelper.Properties.cs" , SourceText . From ( @$ "
4878namespace ManagedCode.MimeTypes
4979{{
5080public static partial class MimeHelper
5181{{
5282static partial void Init()
5383{{
54- { defineDictionaryBuilder . ToString ( ) }
84+ { defineDictionaryBuilder }
5585}}
56- { propertyBuilder . ToString ( ) }
86+ { propertyBuilder }
5787}}
5888}}
5989" , Encoding . UTF8 ) ) ;
90+ }
91+ catch ( Exception ex )
92+ {
93+ context . ReportDiagnostic ( Diagnostic . Create (
94+ new DiagnosticDescriptor (
95+ "MIME003" ,
96+ "Generator Error" ,
97+ "Error generating mime types: {0}" ,
98+ "MimeTypes" ,
99+ DiagnosticSeverity . Error ,
100+ true ) ,
101+ Location . None ,
102+ ex . ToString ( ) ) ) ;
103+ }
104+ }
105+
106+ private string GetMimeTypesPath ( GeneratorExecutionContext context )
107+ {
108+ // Try to find mimeTypes.json in the project directory
109+ var compilation = context . Compilation ;
110+ var projectDir = Path . GetDirectoryName ( compilation . SyntaxTrees . First ( ) . FilePath ) ;
111+
112+ var possiblePaths = new [ ]
113+ {
114+ // Try current directory
115+ Path . Combine ( Directory . GetCurrentDirectory ( ) , "mimeTypes.json" ) ,
116+ // Try project directory
117+ Path . Combine ( projectDir ?? "" , "mimeTypes.json" ) ,
118+ // Try one level up (solution directory)
119+ Path . Combine ( Directory . GetParent ( projectDir ?? "" ) ? . FullName ?? "" , "mimeTypes.json" ) ,
120+ // Try in the Generator project
121+ Path . Combine ( AppDomain . CurrentDomain . BaseDirectory , "mimeTypes.json" )
122+ } ;
123+
124+ return possiblePaths . FirstOrDefault ( File . Exists ) ?? possiblePaths [ 0 ] ;
60125 }
61126
62127 private string ParseKey ( string key )
63128 {
64129 if ( char . IsDigit ( key [ 0 ] ) )
65130 {
66- key = $ "_" + key ;
131+ key = "_" + key ;
67132 }
68133
69134 key = key . Replace ( "-" , "_" ) ;
0 commit comments