Skip to content

Commit 01d4f84

Browse files
committed
Extracted the GitVersion execution code into it's own class
1 parent 2484fc1 commit 01d4f84

File tree

3 files changed

+143
-129
lines changed

3 files changed

+143
-129
lines changed

GitVersionExe/GitVersionExe.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
<Compile Include="Program.cs" />
6262
<Compile Include="AssemblyInfo.cs" />
6363
<Compile Include="AssemblyInfoFileUpdate.cs" />
64+
<Compile Include="SpecifiedArgumentRunner.cs" />
6465
</ItemGroup>
6566
<ItemGroup>
6667
<None Include="GemAssets\.rspec" />

GitVersionExe/Program.cs

Lines changed: 11 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ namespace GitVersion
1111
class Program
1212
{
1313
static StringBuilder log = new StringBuilder();
14-
const string MsBuild = @"c:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe";
1514

1615
static void Main()
1716
{
18-
var exitCode = Run();
17+
var exitCode = VerifyArgumentsAndRun();
1918

2019
if (Debugger.IsAttached)
2120
{
@@ -31,10 +30,12 @@ static void Main()
3130
Environment.Exit(exitCode);
3231
}
3332

34-
static int Run()
33+
static int VerifyArgumentsAndRun()
3534
{
3635
try
3736
{
37+
var fileSystem = new FileSystem();
38+
3839
Arguments arguments;
3940
var argumentsWithoutExeName = GetArgumentsWithoutExeName();
4041
try
@@ -48,108 +49,32 @@ static int Run()
4849
HelpWriter.Write();
4950
return 1;
5051
}
51-
5252
if (arguments.IsHelp)
5353
{
5454
HelpWriter.Write();
5555
return 0;
5656
}
5757

58-
if (!string.IsNullOrEmpty(arguments.Proj) || !string.IsNullOrEmpty(arguments.Exec))
59-
{
60-
arguments.Output = OutputType.BuildServer;
61-
}
62-
63-
ConfigureLogging(arguments);
64-
65-
var gitPreparer = new GitPreparer(arguments);
66-
var gitDirectory = gitPreparer.Prepare();
67-
if (string.IsNullOrEmpty(gitDirectory))
68-
{
69-
Console.Error.WriteLine("Failed to prepare or find the .git directory in path '{0}'", arguments.TargetPath);
70-
return 1;
71-
}
72-
73-
var fileSystem = new FileSystem();
7458
if (arguments.Init)
7559
{
76-
ConfigurationProvider.WriteSample(gitDirectory, fileSystem);
60+
ConfigurationProvider.WriteSample(arguments.TargetPath, fileSystem);
7761
return 0;
7862
}
7963
if (arguments.ShowConfig)
8064
{
81-
Console.WriteLine(ConfigurationProvider.GetEffectiveConfigAsString(gitDirectory, fileSystem));
65+
Console.WriteLine(ConfigurationProvider.GetEffectiveConfigAsString(arguments.TargetPath, fileSystem));
8266
return 0;
8367
}
8468

85-
var workingDirectory = Directory.GetParent(gitDirectory).FullName;
86-
Logger.WriteInfo("Working directory: " + workingDirectory);
87-
var applicableBuildServers = GetApplicableBuildServers(arguments.Authentication).ToList();
88-
89-
foreach (var buildServer in applicableBuildServers)
90-
{
91-
buildServer.PerformPreProcessingSteps(gitDirectory);
92-
}
93-
VersionVariables variables;
94-
var versionFinder = new GitVersionFinder();
95-
var configuration = ConfigurationProvider.Provide(gitDirectory, fileSystem);
96-
97-
using (var repo = RepositoryLoader.GetRepo(gitDirectory))
98-
{
99-
var gitVersionContext = new GitVersionContext(repo, configuration, commitId: arguments.CommitId);
100-
var semanticVersion = versionFinder.FindVersion(gitVersionContext);
101-
var config = gitVersionContext.Configuration;
102-
variables = VariableProvider.GetVariablesFor(semanticVersion, config.AssemblyVersioningScheme, config.VersioningMode, config.ContinuousDeploymentFallbackTag, gitVersionContext.IsCurrentCommitTagged);
103-
}
104-
105-
if (arguments.Output == OutputType.BuildServer)
106-
{
107-
foreach (var buildServer in applicableBuildServers)
108-
{
109-
buildServer.WriteIntegration(Console.WriteLine, variables);
110-
}
111-
}
112-
113-
if (arguments.Output == OutputType.Json)
69+
if (!string.IsNullOrEmpty(arguments.Proj) || !string.IsNullOrEmpty(arguments.Exec))
11470
{
115-
switch (arguments.ShowVariable)
116-
{
117-
case null:
118-
Console.WriteLine(JsonOutputFormatter.ToJson(variables));
119-
break;
120-
121-
default:
122-
string part;
123-
if (!variables.TryGetValue(arguments.ShowVariable, out part))
124-
{
125-
throw new WarningException(string.Format("'{0}' variable does not exist", arguments.ShowVariable));
126-
}
127-
Console.WriteLine(part);
128-
break;
129-
}
71+
arguments.Output = OutputType.BuildServer;
13072
}
13173

132-
using (var assemblyInfoUpdate = new AssemblyInfoFileUpdate(arguments, workingDirectory, variables, fileSystem))
133-
{
134-
var execRun = RunExecCommandIfNeeded(arguments, workingDirectory, variables);
135-
var msbuildRun = RunMsBuildIfNeeded(arguments, workingDirectory, variables);
136-
if (!execRun && !msbuildRun)
137-
{
138-
assemblyInfoUpdate.DoNotRestoreAssemblyInfo();
139-
//TODO Put warning back
140-
//if (!context.CurrentBuildServer.IsRunningInBuildAgent())
141-
//{
142-
// Console.WriteLine("WARNING: Not running in build server and /ProjectFile or /Exec arguments not passed");
143-
// Console.WriteLine();
144-
// Console.WriteLine("Run GitVersion.exe /? for help");
145-
//}
146-
}
147-
}
74+
ConfigureLogging(arguments);
75+
Logger.WriteInfo("Working directory: " + arguments.TargetPath);
14876

149-
if (gitPreparer.IsDynamicGitRepository)
150-
{
151-
DeleteHelper.DeleteGitRepository(gitPreparer.DynamicGitRepositoryPath);
152-
}
77+
SpecifiedArgumentRunner.Run(arguments, fileSystem);
15378
}
15479
catch (WarningException exception)
15580
{
@@ -167,11 +92,6 @@ static int Run()
16792
return 0;
16893
}
16994

170-
static IEnumerable<IBuildServer> GetApplicableBuildServers(Authentication authentication)
171-
{
172-
return BuildServerList.GetApplicableBuildServers(authentication);
173-
}
174-
17595
static void ConfigureLogging(Arguments arguments)
17696
{
17797
var writeActions = new List<Action<string>>
@@ -223,43 +143,5 @@ static List<string> GetArgumentsWithoutExeName()
223143
.Skip(1)
224144
.ToList();
225145
}
226-
227-
static bool RunMsBuildIfNeeded(Arguments args, string workingDirectory, VersionVariables variables)
228-
{
229-
if (string.IsNullOrEmpty(args.Proj)) return false;
230-
231-
Logger.WriteInfo(string.Format("Launching {0} \"{1}\" {2}", MsBuild, args.Proj, args.ProjArgs));
232-
var results = ProcessHelper.Run(
233-
Logger.WriteInfo, Logger.WriteError,
234-
null, MsBuild, string.Format("\"{0}\" {1}", args.Proj, args.ProjArgs), workingDirectory,
235-
GetEnvironmentalVariables(variables));
236-
237-
if (results != 0)
238-
throw new WarningException("MsBuild execution failed, non-zero return code");
239-
240-
return true;
241-
}
242-
243-
static bool RunExecCommandIfNeeded(Arguments args, string workingDirectory, VersionVariables variables)
244-
{
245-
if (string.IsNullOrEmpty(args.Exec)) return false;
246-
247-
Logger.WriteInfo(string.Format("Launching {0} {1}", args.Exec, args.ExecArgs));
248-
var results = ProcessHelper.Run(
249-
Logger.WriteInfo, Logger.WriteError,
250-
null, args.Exec, args.ExecArgs, workingDirectory,
251-
GetEnvironmentalVariables(variables));
252-
if (results != 0)
253-
throw new WarningException(string.Format("Execution of {0} failed, non-zero return code", args.Exec));
254-
255-
return true;
256-
}
257-
258-
static KeyValuePair<string, string>[] GetEnvironmentalVariables(VersionVariables variables)
259-
{
260-
return variables
261-
.Select(v => new KeyValuePair<string, string>("GitVersion_" + v.Key, v.Value))
262-
.ToArray();
263-
}
264146
}
265147
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
namespace GitVersion
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using GitVersion.Helpers;
7+
8+
class SpecifiedArgumentRunner
9+
{
10+
const string MsBuild = @"c:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe";
11+
12+
public static void Run(Arguments arguments, IFileSystem fileSystem)
13+
{
14+
var gitPreparer = new GitPreparer(arguments);
15+
var gitDirectory = gitPreparer.Prepare();
16+
if (string.IsNullOrEmpty(gitDirectory))
17+
{
18+
throw new Exception(string.Format("Failed to prepare or find the .git directory in path '{0}'", arguments.TargetPath));
19+
}
20+
var applicableBuildServers = GetApplicableBuildServers(arguments.Authentication).ToList();
21+
22+
foreach (var buildServer in applicableBuildServers)
23+
{
24+
buildServer.PerformPreProcessingSteps(gitDirectory);
25+
}
26+
VersionVariables variables;
27+
var versionFinder = new GitVersionFinder();
28+
var configuration = ConfigurationProvider.Provide(gitDirectory, fileSystem);
29+
30+
using (var repo = RepositoryLoader.GetRepo(gitDirectory))
31+
{
32+
var gitVersionContext = new GitVersionContext(repo, configuration, commitId: arguments.CommitId);
33+
var semanticVersion = versionFinder.FindVersion(gitVersionContext);
34+
var config = gitVersionContext.Configuration;
35+
variables = VariableProvider.GetVariablesFor(semanticVersion, config.AssemblyVersioningScheme, config.VersioningMode, config.ContinuousDeploymentFallbackTag, gitVersionContext.IsCurrentCommitTagged);
36+
}
37+
38+
if (arguments.Output == OutputType.BuildServer)
39+
{
40+
foreach (var buildServer in applicableBuildServers)
41+
{
42+
buildServer.WriteIntegration(Console.WriteLine, variables);
43+
}
44+
}
45+
46+
if (arguments.Output == OutputType.Json)
47+
{
48+
switch (arguments.ShowVariable)
49+
{
50+
case null:
51+
Console.WriteLine(JsonOutputFormatter.ToJson(variables));
52+
break;
53+
54+
default:
55+
string part;
56+
if (!variables.TryGetValue(arguments.ShowVariable, out part))
57+
{
58+
throw new WarningException(string.Format("'{0}' variable does not exist", arguments.ShowVariable));
59+
}
60+
Console.WriteLine(part);
61+
break;
62+
}
63+
}
64+
65+
using (var assemblyInfoUpdate = new AssemblyInfoFileUpdate(arguments, arguments.TargetPath, variables, fileSystem))
66+
{
67+
var execRun = RunExecCommandIfNeeded(arguments, arguments.TargetPath, variables);
68+
var msbuildRun = RunMsBuildIfNeeded(arguments, arguments.TargetPath, variables);
69+
if (!execRun && !msbuildRun)
70+
{
71+
assemblyInfoUpdate.DoNotRestoreAssemblyInfo();
72+
//TODO Put warning back
73+
//if (!context.CurrentBuildServer.IsRunningInBuildAgent())
74+
//{
75+
// Console.WriteLine("WARNING: Not running in build server and /ProjectFile or /Exec arguments not passed");
76+
// Console.WriteLine();
77+
// Console.WriteLine("Run GitVersion.exe /? for help");
78+
//}
79+
}
80+
}
81+
82+
if (gitPreparer.IsDynamicGitRepository)
83+
{
84+
DeleteHelper.DeleteGitRepository(gitPreparer.DynamicGitRepositoryPath);
85+
}
86+
}
87+
88+
static IEnumerable<IBuildServer> GetApplicableBuildServers(Authentication authentication)
89+
{
90+
return BuildServerList.GetApplicableBuildServers(authentication);
91+
}
92+
93+
static bool RunMsBuildIfNeeded(Arguments args, string workingDirectory, VersionVariables variables)
94+
{
95+
if (string.IsNullOrEmpty(args.Proj)) return false;
96+
97+
Logger.WriteInfo(string.Format("Launching {0} \"{1}\" {2}", MsBuild, args.Proj, args.ProjArgs));
98+
var results = ProcessHelper.Run(
99+
Logger.WriteInfo, Logger.WriteError,
100+
null, MsBuild, string.Format("\"{0}\" {1}", args.Proj, args.ProjArgs), workingDirectory,
101+
GetEnvironmentalVariables(variables));
102+
103+
if (results != 0)
104+
throw new WarningException("MsBuild execution failed, non-zero return code");
105+
106+
return true;
107+
}
108+
109+
static bool RunExecCommandIfNeeded(Arguments args, string workingDirectory, VersionVariables variables)
110+
{
111+
if (string.IsNullOrEmpty(args.Exec)) return false;
112+
113+
Logger.WriteInfo(string.Format("Launching {0} {1}", args.Exec, args.ExecArgs));
114+
var results = ProcessHelper.Run(
115+
Logger.WriteInfo, Logger.WriteError,
116+
null, args.Exec, args.ExecArgs, workingDirectory,
117+
GetEnvironmentalVariables(variables));
118+
if (results != 0)
119+
throw new WarningException(string.Format("Execution of {0} failed, non-zero return code", args.Exec));
120+
121+
return true;
122+
}
123+
124+
static KeyValuePair<string, string>[] GetEnvironmentalVariables(VersionVariables variables)
125+
{
126+
return variables
127+
.Select(v => new KeyValuePair<string, string>("GitVersion_" + v.Key, v.Value))
128+
.ToArray();
129+
}
130+
}
131+
}

0 commit comments

Comments
 (0)