1- using System . IO ;
1+ using System ;
2+ using System . IO ;
23using System . Text ;
34using System . Text . RegularExpressions ;
45using Microsoft . Build . Construction ;
56
6- namespace vpatch
7+ namespace vpatch ;
8+
9+ public static class TextFileHelper
710{
8- public static class TextFileHelper
11+ public static void RegexReplace ( string path , string pattern , string replacement , Encoding encoding )
912 {
10- public static void RegexReplace ( string path , string pattern , string replacement , Encoding encoding )
11- {
12- string content ;
13+ string content ;
1314
14- using ( var reader = new StreamReader ( path , encoding ) )
15- {
16- content = reader . ReadToEnd ( ) ;
17- }
15+ using ( var reader = new StreamReader ( path , encoding ) )
16+ {
17+ content = reader . ReadToEnd ( ) ;
18+ }
1819
19- content = Regex . Replace ( content , pattern , replacement , RegexOptions . Multiline ) ;
20+ content = Regex . Replace ( content , pattern , replacement , RegexOptions . Multiline ) ;
2021
21- File . WriteAllText ( path , content , encoding ) ;
22- }
22+ File . WriteAllText ( path , content , encoding ) ;
23+ }
2324
24- public static void RegexReplace ( string path , string pattern , string replacement )
25- {
26- RegexReplace ( path , pattern , replacement , Encoding . UTF8 ) ;
27- }
25+ public static void RegexReplace ( string path , string pattern , string replacement )
26+ {
27+ RegexReplace ( path , pattern , replacement , Encoding . UTF8 ) ;
28+ }
2829
29- public static void PatchProjectFile ( string path , string propertyName , string replacement )
30- {
31- var projectRootElement = ProjectRootElement . Open ( path ) ;
30+ public static void PatchProjectFile ( string projectFile , string propertyName , string replacement )
31+ {
32+ var projectRootElement = ProjectRootElement . Open ( projectFile ) ;
3233
33- if ( projectRootElement is null )
34- throw new IOException ( "Couldn't open project file for reading." ) ;
34+ if ( projectRootElement is null )
35+ throw new IOException ( "Couldn't open project file for reading." ) ;
3536
37+ try
38+ {
3639 projectRootElement . AddProperty ( propertyName , replacement ) ;
3740
38- projectRootElement . Save ( ) ;
41+ projectRootElement . Save ( projectFile ) ;
42+ }
43+ catch ( Exception ex )
44+ {
45+ throw new Exception ( "Failed to parse project file." , ex ) ;
3946 }
4047 }
4148}
0 commit comments