-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathProgram.cs
executable file
·136 lines (114 loc) · 4.51 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
using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using GitVersion;
using GitVersion.Helpers;
using Newtonsoft.Json.Linq;
namespace dotnet.gitversion
{
public class Program
{
private const string ProjectJsonFile = "project.json";
private const string CsprojFile = ".csproj";
private static readonly Regex s_regex = new Regex(@"(?<json>project\.json)|(?<csproj>\.csproj$)");
public static int Main(string[] args)
{
Logger.SetLoggers(
Console.WriteLine,
Console.WriteLine,
Console.WriteLine);
var options = Options.Parse(args);
if (options.Help)
{
Console.WriteLine(Options.GetHelpMessage());
return 0;
}
if (options.NoFilesSpecified && !TryFindNetCoreProject(out options))
{
Console.Error.WriteLine($"Could not locate {ProjectJsonFile} or {CsprojFile} in current directory and no command line arguments were specified.");
Console.WriteLine(Options.GetHelpMessage());
return -1;
}
var fs = new FileSystem();
var gv = new GitVersion.ExecuteCore(fs);
var auth = new GitVersion.Authentication();
var version = gv.ExecuteGitVersion(null, null, auth, null, false, ".", null);
Console.WriteLine($"Setting version: {version.LegacySemVerPadded}");
if (!string.IsNullOrEmpty(options.ProjectJson))
{
UpdateProjectJson(options.ProjectJson, version.LegacySemVerPadded);
}
if (!string.IsNullOrEmpty(options.CsProj))
{
UpdateCsproj(options.CsProj, version.LegacySemVerPadded);
}
return 0;
}
private static bool TryFindNetCoreProject(out Options options)
{
options = new Options();
var found = false;
foreach (var file in new DirectoryInfo(Directory.GetCurrentDirectory()).GetFiles())
{
var match = s_regex.Match(file.Name);
if (!match.Success)
{
continue;
}
if (match.Groups["json"].Success)
{
found = true;
options.ProjectJson = file.FullName;
}
else if (match.Groups["csproj"].Success)
{
var document = XDocument.Load(file.FullName);
var sdkAttribute = document.Root.DescendantsAndSelf("Project")?.FirstOrDefault()?.Attribute("Sdk");
var isNetCoreProject = string.Equals("Microsoft.NET.Sdk", sdkAttribute?.Value, StringComparison.OrdinalIgnoreCase);
if (isNetCoreProject)
{
found = true;
options.CsProj = file.FullName;
}
}
}
return found;
}
private static void UpdateProjectJson(string projectJsonFile, string legacySemVerPadded)
{
var projectJson = JObject.Parse(File.ReadAllText(projectJsonFile));
projectJson["version"] = legacySemVerPadded;
File.WriteAllText(projectJsonFile, projectJson.ToString());
}
/// <summary>
/// Looks for msbuild element PackageVersion to update with the name.
/// https://docs.microsoft.com/en-us/nuget/schema/msbuild-targets
/// </summary>
/// <param name="csproj"></param>
/// <param name="legacySemVerPadded"></param>
private static void UpdateCsproj(string csproj, string legacySemVerPadded)
{
var document = XDocument.Load(csproj);
var packageVersion = XName.Get("PackageVersion");
var isSet = false;
foreach (var node in document.Root.Descendants(packageVersion))
{
node.Value = legacySemVerPadded;
isSet = true;
}
if (!isSet)
{
var propertyGroup = new XElement("PropertyGroup");
var version = new XElement(packageVersion)
{
Value = legacySemVerPadded
};
propertyGroup.Add(version);
document.Root.AddFirst(propertyGroup);
}
document.Save(File.OpenWrite(csproj));
}
}
}