Skip to content

Commit c551fa7

Browse files
committed
Error handling
1 parent 7cca5c4 commit c551fa7

File tree

3 files changed

+35
-25
lines changed

3 files changed

+35
-25
lines changed

Options.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class Options
1414
HelpText = "A text file containing one line that gets parsed as the target version.")]
1515
public string TargetVersionFile { get; set; }
1616

17-
[Option("target-file",
17+
[Option("target-file", Required = true,
1818
HelpText = "The target file to get parsed and modified.")]
1919
public string TargetFile { get; set; }
2020

TextFileHelper.cs

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,48 @@
1-
using System.IO;
1+
using System;
2+
using System.IO;
23
using System.Text;
34
using System.Text.RegularExpressions;
45
using 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
}

vpatch.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
<ApplicationIcon>favicon.ico</ApplicationIcon>
77
</PropertyGroup>
88

9-
<PropertyGroup>
9+
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
1010
<RuntimeIdentifier>win10-x64</RuntimeIdentifier>
1111
<PublishSingleFile>true</PublishSingleFile>
1212
<PublishTrimmed>true</PublishTrimmed>
1313
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
1414
<DebugType>None</DebugType>
1515
<DebugSymbols>false</DebugSymbols>
16+
</PropertyGroup>
17+
18+
<PropertyGroup>
1619
<Authors>Benjamin Höglinger-Stelzer</Authors>
1720
<Company>Nefarius Software Solutions e.U.</Company>
1821
<Copyright>(C) 2020-2022 Benjamin Höglinger-Stelzer</Copyright>

0 commit comments

Comments
 (0)