forked from microsoft/VFSForGit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerateGitVersionConstants.cs
More file actions
190 lines (155 loc) · 6.45 KB
/
GenerateGitVersionConstants.cs
File metadata and controls
190 lines (155 loc) · 6.45 KB
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
187
188
189
190
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using System;
using System.IO;
using System.Linq;
namespace GVFS.PreBuild
{
public class GenerateGitVersionConstants : Task
{
[Required]
public string GitPackageVersion { get; set; }
[Required]
public string PackagesPath { get; set; }
[Required]
public string OutputFile {get; set; }
[Output]
public string LatestInstaller { get; set; }
[Output]
public string LatestInstallerFilename { get; set; }
public override bool Execute()
{
this.Log.LogMessage(MessageImportance.High, "Creating constants for Git package " + this.GitPackageVersion);
string installerDirectory = Path.Combine(
this.PackagesPath,
"GitForWindows.GVFS.Installer." + this.GitPackageVersion,
"tools");
if (!Directory.Exists(installerDirectory))
{
this.Log.LogError("Git package not found. Re-run your build to allow the package to be downloaded by Visual Studio.");
return false;
}
this.LatestInstaller = Path.GetFullPath(Directory.EnumerateFiles(installerDirectory).Single());
this.LatestInstallerFilename = Path.GetFileName(this.LatestInstaller);
GitVersion version;
if (!GitVersion.TryParseInstallerName(this.LatestInstallerFilename, out version))
{
this.Log.LogError("Unable to parse git version: " + this.LatestInstallerFilename);
return false;
}
this.Log.LogMessage(MessageImportance.High, "Found Git version " + version.ToString());
string projectOutputPath = Path.GetDirectoryName(this.OutputFile);
if (!Directory.Exists(projectOutputPath))
{
Directory.CreateDirectory(projectOutputPath);
}
File.WriteAllText(
this.OutputFile,
string.Format(
@"// This file is auto-generated by GVFS.PreBuild.GenerateGitVersionConstants. Any changes made directly in this file will be lost.
using GVFS.Common.Git;
namespace GVFS.Common
{{
public static partial class GVFSConstants
{{
public static readonly GitVersion SupportedGitVersion = new GitVersion({0}, {1}, {2}, ""{3}"", {4}, {5});
}}
}}",
version.Major,
version.Minor,
version.Build,
version.Platform,
version.Revision,
version.MinorRevision));
return true;
}
// This class was partially copied from GVFS.Common.Git.GitVersion for the parsing
private class GitVersion
{
private GitVersion(int major, int minor, int build, string platform, int revision, int minorRevision)
{
this.Major = major;
this.Minor = minor;
this.Build = build;
this.Platform = platform;
this.Revision = revision;
this.MinorRevision = minorRevision;
}
public int Major { get; private set; }
public int Minor { get; private set; }
public string Platform { get; private set; }
public int Build { get; private set; }
public int Revision { get; private set; }
public int MinorRevision { get; private set; }
public static bool TryParseInstallerName(string input, out GitVersion version)
{
// Installer name is of the form
// Git-2.14.1.gvfs.1.1.gb16030b-64-bit.exe
version = null;
if (!input.StartsWith("Git-", StringComparison.InvariantCultureIgnoreCase))
{
return false;
}
if (!input.EndsWith("-64-bit.exe", StringComparison.InvariantCultureIgnoreCase))
{
return false;
}
return TryParseVersion(input.Substring(4, input.Length - 15), out version);
}
private static bool TryParseVersion(string input, out GitVersion version)
{
version = null;
int major, minor, build, revision, minorRevision;
if (string.IsNullOrWhiteSpace(input))
{
return false;
}
string[] parsedComponents = input.Split(new char[] { '.' });
int parsedComponentsLength = parsedComponents.Length;
if (parsedComponentsLength < 5)
{
return false;
}
if (!TryParseComponent(parsedComponents[0], out major))
{
return false;
}
if (!TryParseComponent(parsedComponents[1], out minor))
{
return false;
}
if (!TryParseComponent(parsedComponents[2], out build))
{
return false;
}
if (!TryParseComponent(parsedComponents[4], out revision))
{
return false;
}
if (parsedComponentsLength < 6 || !TryParseComponent(parsedComponents[5], out minorRevision))
{
minorRevision = 0;
}
string platform = parsedComponents[3];
version = new GitVersion(major, minor, build, platform, revision, minorRevision);
return true;
}
public override string ToString()
{
return string.Format("{0}.{1}.{2}.{3}.{4}.{5}", this.Major, this.Minor, this.Build, this.Platform, this.Revision, this.MinorRevision);
}
private static bool TryParseComponent(string component, out int parsedComponent)
{
if (!int.TryParse(component, out parsedComponent))
{
return false;
}
if (parsedComponent < 0)
{
return false;
}
return true;
}
}
}
}