@@ -12,6 +12,7 @@ public class JsonFile
1212 const string definingProjectFullPath = "DefiningProjectFullPath" ;
1313 const string definingProjectName = "DefiningProjectName" ;
1414 const string definingProjectExtension = "DefiningProjectExtension" ;
15+ const char dotChar = '.' ;
1516
1617 readonly ITaskItem item ;
1718 readonly string tempOutputPath ;
@@ -53,34 +54,36 @@ public JsonFile(string link, string filename, string extension, string fullPath,
5354
5455 public TaskItem WriteCompactTempFile ( )
5556 {
56- using var fs = File . OpenRead ( FullPath ) ;
57- using var jDoc = JsonDocument . Parse ( fs , new JsonDocumentOptions { AllowTrailingCommas = true , CommentHandling = JsonCommentHandling . Skip } ) ;
57+ using var frs = File . OpenRead ( FullPath ) ;
58+ using var jDoc = JsonDocument . Parse ( frs ,
59+ new JsonDocumentOptions { AllowTrailingCommas = true , CommentHandling = JsonCommentHandling . Skip } ) ;
5860
5961 var directory = Path . GetDirectoryName ( TempFullPath ) ;
62+
6063 if ( ! Directory . Exists ( directory ) )
6164 Directory . CreateDirectory ( directory ) ;
6265
6366 if ( File . Exists ( TempFullPath ) )
6467 File . Delete ( TempFullPath ) ;
6568
66- using var stream = File . OpenWrite ( TempFullPath ) ;
67- using var writer = new Utf8JsonWriter ( stream ) ;
69+ using var fws = File . OpenWrite ( TempFullPath ) ;
70+ using var writer = new Utf8JsonWriter ( fws ) ;
6871 jDoc . WriteTo ( writer ) ;
6972 writer . Flush ( ) ;
70- stream . Flush ( ) ;
73+ fws . Flush ( ) ;
7174
7275 return GetTaskItem ( ) ;
7376 }
7477
7578 public override string ToString ( ) => FullPath ;
7679
7780 TaskItem GetTaskItem ( )
78- => new TaskItem ( TempFullPath , new Dictionary < string , string >
81+ => new ( TempFullPath , new Dictionary < string , string >
7982 {
8083 { nameof ( Link ) , Link } ,
8184 { nameof ( FullPath ) , TempFullPath } ,
82- { nameof ( Filename ) , Filename } ,
83- { nameof ( Extension ) , Extension } ,
85+ { nameof ( Filename ) , Path . GetFileNameWithoutExtension ( TempFullPath ) } ,
86+ { nameof ( Extension ) , Path . GetExtension ( TempFullPath ) } ,
8487 { nameof ( DefiningProjectDirectory ) , DefiningProjectDirectory } ,
8588 { definingProjectFullPath , GetMetadata ( definingProjectFullPath ) } ,
8689 { definingProjectName , GetMetadata ( definingProjectName ) } ,
@@ -91,24 +94,41 @@ TaskItem GetTaskItem()
9194
9295 void Verify ( )
9396 {
94- if ( string . IsNullOrWhiteSpace ( DefiningProjectDirectory ) || string . IsNullOrWhiteSpace ( FullPath ) )
95- throw new Exception ( ) ;
97+ if ( DefiningProjectDirectory . IsEmpty ( ) || FullPath . IsEmpty ( ) )
98+ throw new KeyNotFoundException ( $ "{ nameof ( DefiningProjectDirectory ) } or { nameof ( FullPath ) } not found in { item . ToString ( null ) } ") ;
99+
100+ if ( ! File . Exists ( FullPath ) )
101+ throw new FileNotFoundException ( FullPath ) ;
102+
103+ if ( ! Directory . Exists ( DefiningProjectDirectory ) )
104+ throw new DirectoryNotFoundException ( DefiningProjectDirectory ) ;
105+
106+ VerifyFileNameWithExtension ( ) ;
96107
97- if ( string . IsNullOrWhiteSpace ( Link ) )
98- SetLink ( ) ;
108+ if ( Link . IsEmpty ( ) )
109+ Link = FullPath . StartsWith ( DefiningProjectDirectory )
110+ ? FullPath . Replace ( DefiningProjectDirectory , string . Empty )
111+ : $ "{ Filename } { Extension } ";
99112
100- if ( string . IsNullOrWhiteSpace ( Link ) )
101- throw new Exception ( ) ;
113+ if ( Link . IsEmpty ( ) )
114+ throw new ArgumentException ( $ "The relative { nameof ( Link ) } of the file location in the project could not be determined in { item . ToString ( null ) } " ) ;
102115
103116 TempFullPath = Path . Combine ( tempOutputPath , Link ) ;
104117 }
105118
106- void SetLink ( )
119+ void VerifyFileNameWithExtension ( )
107120 {
108- var locatedInProject = FullPath . StartsWith ( DefiningProjectDirectory ) ;
109- Link = locatedInProject
110- ? FullPath . Replace ( DefiningProjectDirectory , string . Empty )
111- : $ "{ Filename } { Extension } ";
121+ if ( Filename . IsEmpty ( ) )
122+ Filename = Path . GetFileNameWithoutExtension ( FullPath ) ;
123+
124+ if ( Extension . IsEmpty ( ) )
125+ Extension = Path . GetExtension ( FullPath ) ;
126+
127+ if ( ! Extension . IsEmpty ( ) && ! Extension . StartsWith ( dotChar ) )
128+ Extension = dotChar + Extension ;
129+
130+ if ( Filename . IsEmpty ( ) && Extension . IsEmpty ( ) )
131+ throw new ArgumentException ( $ "The { nameof ( Filename ) } and { nameof ( Extension ) } could not be determined in { item . ToString ( null ) } ") ;
112132 }
113133 }
114134}
0 commit comments