-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProgram.cs
160 lines (136 loc) · 4.74 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
using System;
using System.Diagnostics;
using System.IO;
using CommandLine;
using NuGet.Versioning;
namespace dotnetCampus.TagToVersion
{
class Program
{
static void Main(string[] args)
{
try
{
Parser.Default.ParseArguments<Program>(args)
.WithParsed(option =>
{
option.ParseTagVersion();
})
.WithNotParsed(errors =>
{
Console.WriteLine("dotnetCampus.TagToVersion");
Console.WriteLine($"Current directory {Environment.CurrentDirectory}");
Console.WriteLine($"Args {Environment.CommandLine}");
});
}
catch (Exception e)
{
Console.WriteLine(e);
Environment.Exit(-1);
}
}
private void ParseTagVersion()
{
Log("dotnetCampus.TagToVersion");
Log($"Current directory {Environment.CurrentDirectory}");
Log($"Read tag version. Tag name is {Tag}");
var version = ReadTagVersion(Tag);
Log($"Tag version is {version}");
if (ToConsole)
{
Console.WriteLine(version);
}
else
{
Log($"Find the version.props file. ");
var versionFile = FindVersionFile(VersionFile);
WriteVersionToFile(version, versionFile);
}
}
private void Log(string message)
{
if (ToConsole)
{
Debug.WriteLine(message);
}
else
{
Console.WriteLine(message);
}
}
private static void WriteVersionToFile(SemanticVersion version, FileInfo versionFile)
{
Console.WriteLine($"Start writing version {version} to file {versionFile.FullName}");
var text = $@"<Project>
<PropertyGroup>
<Version>{version}</Version>
</PropertyGroup>
</Project>";
File.WriteAllText(versionFile.FullName, text);
Console.WriteLine("Finish write");
}
private static FileInfo FindVersionFile(string versionFile)
{
if (string.IsNullOrEmpty(versionFile))
{
Console.WriteLine($"Auto finding version file");
var buildFolder = Path.GetFullPath("build");
if (!Directory.Exists(buildFolder))
{
buildFolder = Path.GetFullPath("Build");
if (!Directory.Exists(buildFolder))
{
throw new ArgumentException($"Can not find build folder {buildFolder}");
}
}
versionFile = Path.Combine(buildFolder, "Version.props");
if (!File.Exists(versionFile))
{
throw new ArgumentException($"Can not find {versionFile}");
}
}
else
{
versionFile = Path.GetFullPath(versionFile);
if (!File.Exists(versionFile))
{
throw new ArgumentException($"Can not find {versionFile}");
}
}
Console.WriteLine($"Version file is {versionFile}");
return new FileInfo(versionFile);
}
private static SemanticVersion ReadTagVersion(string tag)
{
const string githubRef = "refs/tags/";
if (tag.StartsWith(githubRef))
{
// github
tag = tag.Substring(githubRef.Length);
}
if (tag.StartsWith("v", StringComparison.OrdinalIgnoreCase))
{
tag = tag.Substring(1);
}
if (string.IsNullOrEmpty(tag))
{
throw new ArgumentException("Could not find the tag version");
}
// 使用 NuGetVersion 而不是 SemanticVersion 是因为不支持 rc 版本号
if (NuGetVersion.TryParse(tag, out NuGetVersion tagVersion))
{
return tagVersion;
}
else
{
throw new ArgumentException("Can not parse to version");
}
}
[Option('c', "toconsole", Required = false, HelpText = "Output the tag version to console")]
public bool ToConsole { set; get; }
[Option('t', "tag", Required = true, HelpText = "The Tag Name")]
public string Tag { set; get; }
[Option('f', "file", HelpText = "The version file path")]
public string VersionFile { set; get; }
}
}