-
Notifications
You must be signed in to change notification settings - Fork 16
/
Program.cs
186 lines (157 loc) · 6.06 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Xml.Linq;
using CommandLine;
namespace dotnet_setversion
{
internal static class Program
{
internal const int ExitSuccess = 0;
internal const int ExitFailure = 1;
internal static int Main(params string[] args)
{
return Parser.Default.ParseArguments<Options>(args)
.MapResult(Run, errs => int.MinValue);
}
private static int Run(Options options)
{
if (options.Recursive && options.CsprojFile != null)
{
Console.WriteLine("The --recursive option and csprojFile argument are mutually exclusive.");
return ExitFailure;
}
return options.Recursive ? RunRecursive(options.Version) : Run(options.Version, options.CsprojFile);
}
private static int Run(string version, string csprojFile)
{
if (csprojFile == null)
{
var csprojFiles = GetCsprojFiles(recursive: false);
if (!CheckCsprojFiles(csprojFiles, allowMultiple: false))
return ExitFailure;
csprojFile = csprojFiles[0];
}
if (!File.Exists(csprojFile))
{
Console.WriteLine($"Project file '{csprojFile}' does not exist.");
return ExitFailure;
}
if (ShouldExtractVersionFromFile(version, out var versionFile) &&
!TryExtractVersionFromFile(versionFile, out version))
{
return ExitFailure;
}
SetVersion(version, csprojFile);
PrintSuccessString(version, csprojFile);
return ExitSuccess;
}
private static int RunRecursive(string version)
{
var csprojFiles = GetCsprojFiles(recursive: true);
if (!CheckCsprojFiles(csprojFiles, allowMultiple: true))
return ExitFailure;
if (ShouldExtractVersionFromFile(version, out var versionFile) &&
!TryExtractVersionFromFile(versionFile, out version))
{
return ExitFailure;
}
foreach (var csprojFile in csprojFiles)
{
SetVersion(version, csprojFile);
}
PrintSuccessString(version, csprojFiles);
return ExitSuccess;
}
private static string[] GetCsprojFiles(bool recursive) => Directory
.EnumerateFileSystemEntries(Directory.GetCurrentDirectory(), "*.csproj",
recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly).ToArray();
private static bool CheckCsprojFiles(string[] csprojFiles, bool allowMultiple)
{
if (csprojFiles.Length == 0)
{
Console.WriteLine("Specify a project file. The current working directory does not contain a project file.");
return false;
}
if (!allowMultiple && csprojFiles.Length > 1)
{
Console.WriteLine("Specify which project file to use because this folder contains more than one project file.");
return false;
}
return true;
}
private static bool ShouldExtractVersionFromFile(string version, out string versionFile)
{
if (version.StartsWith("@"))
{
versionFile = version.Substring(1);
return true;
}
versionFile = null;
return false;
}
private static bool TryExtractVersionFromFile(string filename, out string version)
{
if (!File.Exists(filename))
{
Console.WriteLine($"The specified file to extract the version from was not found: {filename}");
version = null;
return false;
}
var versionFileText = File.ReadAllText(filename);
try
{
var versionModel = JsonSerializer.Deserialize<VersionModel>(versionFileText);
version = versionModel.ToString();
}
catch (JsonException)
{
// Simple Version Number
version = versionFileText;
}
return true;
}
private static void SetVersion(string version, string csprojFile)
{
if (version == null) throw new ArgumentNullException(nameof(version));
if (csprojFile == null) throw new ArgumentNullException(nameof(csprojFile));
var document = XDocument.Load(csprojFile);
var projectNode = document.GetOrCreateElement("Project");
var versionNode = projectNode
.Elements("PropertyGroup")
.SelectMany(it => it.Elements("Version"))
.SingleOrDefault() ?? projectNode
.GetOrCreateElement("PropertyGroup")
.GetOrCreateElement("Version");
versionNode.SetValue(version);
File.WriteAllText(csprojFile, document.ToString());
}
private static void PrintSuccessString(string version, string file)
{
Console.WriteLine($"Set version to {version} in {file}");
}
private static void PrintSuccessString(string version, params string[] files)
{
Console.WriteLine($"Set version to {version} in:");
foreach (var file in files)
{
Console.WriteLine($"\t> {file}");
}
}
}
public static class Extensions
{
/// <remarks>
/// Source: http://stackoverflow.com/a/14892813/1636276
/// </remarks>
public static XElement GetOrCreateElement(this XContainer container, string name)
{
var element = container.Element(name);
if (element != null) return element;
element = new XElement(name);
container.Add(element);
return element;
}
}
}