Skip to content

Commit 95ee219

Browse files
committed
(GH-18) Adding Yaml Configuration
- Based on the work that was done in GitVersion, add option to initialize a new Yaml Configuration File, as well as show the current configuration for the exe
1 parent ff13620 commit 95ee219

File tree

14 files changed

+321
-0
lines changed

14 files changed

+321
-0
lines changed

Source/GitHubReleaseManager.Cli/GitHubReleaseManager.Cli.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,11 @@
7777
<Compile Include="AssemblyInfo.cs" />
7878
<Compile Include="CommonSubOptions.cs" />
7979
<Compile Include="CreateSubOptions.cs" />
80+
<Compile Include="InitSubOptions.cs" />
8081
<Compile Include="Options.cs" />
8182
<Compile Include="Program.cs" />
8283
<Compile Include="PublishSubOptions.cs" />
84+
<Compile Include="ShowConfigSubOptions.cs" />
8385
</ItemGroup>
8486
<ItemGroup>
8587
<None Include="app.config" />
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//-----------------------------------------------------------------------
2+
// <copyright file="InitSubOptions.cs" company="gep13">
3+
// Copyright (c) gep13. All rights reserved.
4+
// </copyright>
5+
//-----------------------------------------------------------------------
6+
7+
namespace GitHubReleaseManager.Cli
8+
{
9+
public class InitSubOptions
10+
{
11+
}
12+
}

Source/GitHubReleaseManager.Cli/Options.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ public class Options
1717
[VerbOption("publish", HelpText = "Publishes the release notes and closes the milestone.")]
1818
public PublishSubOptions PublishVerb { get; set; }
1919

20+
[VerbOption("init", HelpText = "Creates a sample Yaml Configuration file in root directory")]
21+
public InitSubOptions InitVerb { get; set; }
22+
23+
[VerbOption("showconfig", HelpText = "Shows the current configuration")]
24+
public ShowConfigSubOptions ShowConfigVerb { get; set; }
25+
2026
[HelpVerbOption]
2127
public string DoHelpForVerb(string verbName)
2228
{

Source/GitHubReleaseManager.Cli/Program.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ namespace GitHubReleaseManager.Cli
1111
using System.Linq;
1212
using System.Threading.Tasks;
1313
using CommandLine;
14+
using GitHubReleaseManager.Configuration;
15+
using GitHubReleaseManager.Helpers;
1416
using Octokit;
1517
using FileMode = System.IO.FileMode;
1618

@@ -28,6 +30,10 @@ private static int Main(string[] args)
2830
options,
2931
(verb, subOptions) =>
3032
{
33+
var fileSystem = new FileSystem();
34+
var currentFilePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
35+
var currentDirectory = Path.GetDirectoryName(currentFilePath);
36+
3137
if (verb == "create")
3238
{
3339
result = CreateReleaseAsync((CreateSubOptions)subOptions).Result;
@@ -37,6 +43,18 @@ private static int Main(string[] args)
3743
{
3844
result = PublishReleaseAsync((PublishSubOptions)subOptions).Result;
3945
}
46+
47+
if (verb == "init")
48+
{
49+
ConfigurationProvider.WriteSample(currentDirectory, fileSystem);
50+
result = 0;
51+
}
52+
53+
if (verb == "showconfig")
54+
{
55+
Console.WriteLine(ConfigurationProvider.GetEffectiveConfigAsString(currentDirectory, fileSystem));
56+
result = 0;
57+
}
4058
}))
4159
{
4260
return 1;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//-----------------------------------------------------------------------
2+
// <copyright file="ShowConfigSubOptions.cs" company="gep13">
3+
// Copyright (c) gep13. All rights reserved.
4+
// </copyright>
5+
//-----------------------------------------------------------------------
6+
7+
namespace GitHubReleaseManager.Cli
8+
{
9+
public class ShowConfigSubOptions
10+
{
11+
}
12+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//-----------------------------------------------------------------------
2+
// <copyright file="Config.cs" company="gep13">
3+
// Copyright (c) gep13. All rights reserved.
4+
// </copyright>
5+
//-----------------------------------------------------------------------
6+
7+
namespace GitHubReleaseManager.Configuration
8+
{
9+
using YamlDotNet.Serialization;
10+
11+
public class Config
12+
{
13+
public Config()
14+
{
15+
this.ExportRegex = @"### Where to get it(\r\n)*You can .*\)";
16+
}
17+
18+
[YamlMember(Alias = "export-regex")]
19+
public string ExportRegex { get; set; }
20+
}
21+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//-----------------------------------------------------------------------
2+
// <copyright file="ConfigSerializer.cs" company="gep13">
3+
// Copyright (c) gep13. All rights reserved.
4+
// </copyright>
5+
//-----------------------------------------------------------------------
6+
7+
namespace GitHubReleaseManager.Configuration
8+
{
9+
using System;
10+
using System.IO;
11+
using YamlDotNet.Serialization;
12+
using YamlDotNet.Serialization.NamingConventions;
13+
14+
public static class ConfigSerializer
15+
{
16+
public static Config Read(TextReader reader)
17+
{
18+
var deserializer = new Deserializer(null, new HyphenatedNamingConvention());
19+
var deserialize = deserializer.Deserialize<Config>(reader);
20+
21+
if (deserialize == null)
22+
{
23+
return new Config();
24+
}
25+
26+
return deserialize;
27+
}
28+
29+
public static void Write(Config config, TextWriter writer)
30+
{
31+
var serializer = new Serializer(SerializationOptions.None, new HyphenatedNamingConvention());
32+
serializer.Serialize(writer, config);
33+
}
34+
35+
public static void WriteSample(TextWriter writer)
36+
{
37+
if (writer == null)
38+
{
39+
throw new ArgumentNullException("writer");
40+
}
41+
42+
writer.WriteLine(@"# export-regex: ### Where to get it(\r\n)*You can .*\)");
43+
}
44+
}
45+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//-----------------------------------------------------------------------
2+
// <copyright file="ConfigurationProvider.cs" company="gep13">
3+
// Copyright (c) gep13. All rights reserved.
4+
// </copyright>
5+
//-----------------------------------------------------------------------
6+
7+
namespace GitHubReleaseManager.Configuration
8+
{
9+
using System;
10+
using System.Globalization;
11+
using System.IO;
12+
using System.Text;
13+
using GitHubReleaseManager.Helpers;
14+
15+
public static class ConfigurationProvider
16+
{
17+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Not required, as direct return of object")]
18+
public static Config Provide(string currentDirectory, IFileSystem fileSystem)
19+
{
20+
if (fileSystem == null)
21+
{
22+
throw new ArgumentNullException("fileSystem");
23+
}
24+
25+
var configFilePath = GetConfigFilePath(currentDirectory);
26+
27+
if (fileSystem.Exists(configFilePath))
28+
{
29+
var readAllText = fileSystem.ReadAllText(configFilePath);
30+
31+
return ConfigSerializer.Read(new StringReader(readAllText));
32+
}
33+
34+
return new Config();
35+
}
36+
37+
public static string GetEffectiveConfigAsString(string currentDirectory, IFileSystem fileSystem)
38+
{
39+
var config = Provide(currentDirectory, fileSystem);
40+
var stringBuilder = new StringBuilder();
41+
using (var stream = new StringWriter(stringBuilder, CultureInfo.InvariantCulture))
42+
{
43+
ConfigSerializer.Write(config, stream);
44+
stream.Flush();
45+
}
46+
47+
return stringBuilder.ToString();
48+
}
49+
50+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times", Justification = "Works as expected")]
51+
public static void WriteSample(string currentDirectory, IFileSystem fileSystem)
52+
{
53+
if (fileSystem == null)
54+
{
55+
throw new ArgumentNullException("fileSystem");
56+
}
57+
58+
var configFilePath = GetConfigFilePath(currentDirectory);
59+
60+
if (!fileSystem.Exists(configFilePath))
61+
{
62+
using (var stream = fileSystem.OpenWrite(configFilePath))
63+
using (var writer = new StreamWriter(stream))
64+
{
65+
ConfigSerializer.WriteSample(writer);
66+
}
67+
}
68+
else
69+
{
70+
Logger.WriteError("Cannot write sample, GitHubReleaseManager.yaml already exists");
71+
}
72+
}
73+
74+
private static string GetConfigFilePath(string currentDirectory)
75+
{
76+
return Path.Combine(currentDirectory, "GitHubReleaseManager.yaml");
77+
}
78+
}
79+
}

Source/GitHubReleaseManager/GitHubReleaseManager.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,21 @@
5353
<Reference Include="Microsoft.CSharp" />
5454
<Reference Include="System.Data" />
5555
<Reference Include="System.Xml" />
56+
<Reference Include="YamlDotNet">
57+
<HintPath>..\packages\YamlDotNet.3.5.1\lib\net35\YamlDotNet.dll</HintPath>
58+
</Reference>
5659
</ItemGroup>
5760
<ItemGroup>
5861
<Compile Include="AssemblyInfo.cs" />
62+
<Compile Include="Configuration\Config.cs" />
63+
<Compile Include="Configuration\ConfigSerializer.cs" />
64+
<Compile Include="Configuration\ConfigurationProvider.cs" />
5965
<Compile Include="DefaultGitHubClient.cs" />
66+
<Compile Include="GlobalSuppressions.cs" />
67+
<Compile Include="Helpers\FileSystem.cs" />
68+
<Compile Include="Helpers\IFileSystem.cs" />
6069
<Compile Include="IGitHubClient.cs" />
70+
<Compile Include="Logger.cs" />
6171
<Compile Include="ReleaseNotesBuilder.cs" />
6272
<Compile Include="OctokitExtensions.cs" />
6373
<Compile Include="MilestoneExtensions.cs" />
Binary file not shown.

0 commit comments

Comments
 (0)