Skip to content

Commit 61b4fbc

Browse files
committed
code cleanup for GitVersionTask
1 parent c3ca29e commit 61b4fbc

File tree

5 files changed

+52
-145
lines changed

5 files changed

+52
-145
lines changed

src/GitVersionTask/GenerateGitVersionInformation.cs

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,6 @@ namespace GitVersionTask
88

99
public class GenerateGitVersionInformation : GitVersionTaskBase
1010
{
11-
public GenerateGitVersionInformation()
12-
{
13-
}
14-
15-
[Required]
16-
public string SolutionDirectory { get; set; }
17-
1811
[Required]
1912
public string ProjectFile { get; set; }
2013

@@ -27,31 +20,9 @@ public GenerateGitVersionInformation()
2720
[Output]
2821
public string GitVersionInformationFilePath { get; set; }
2922

30-
public bool NoFetch { get; set; }
31-
32-
public override bool Execute()
33-
{
34-
try
35-
{
36-
InnerExecute();
37-
return true;
38-
}
39-
catch (WarningException errorException)
40-
{
41-
this.LogWarning(errorException.Message);
42-
return true;
43-
}
44-
catch (Exception exception)
45-
{
46-
this.LogError("Error occurred: " + exception);
47-
return false;
48-
}
49-
}
50-
51-
void InnerExecute()
23+
protected override void InnerExecute()
5224
{
53-
VersionVariables versionVariables;
54-
if (!ExecuteCore.TryGetVersion(SolutionDirectory, out versionVariables, NoFetch, new Authentication()))
25+
if (!ExecuteCore.TryGetVersion(SolutionDirectory, out var versionVariables, NoFetch, new Authentication()))
5526
{
5627
return;
5728
}

src/GitVersionTask/GetVersion.cs

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
11
namespace GitVersionTask
22
{
3-
using System;
43
using GitVersion;
54

65
using Microsoft.Build.Framework;
76

87
public class GetVersion : GitVersionTaskBase
98
{
10-
public GetVersion()
11-
{
12-
}
13-
14-
[Required]
15-
public string SolutionDirectory { get; set; }
16-
17-
public bool NoFetch { get; set; }
18-
199
[Output]
2010
public string Major { get; set; }
2111

@@ -106,31 +96,15 @@ public GetVersion()
10696
[Output]
10797
public string CommitsSinceVersionSourcePadded { get; set; }
10898

109-
public override bool Execute()
99+
protected override void InnerExecute()
110100
{
111-
try
101+
if (ExecuteCore.TryGetVersion(SolutionDirectory, out var versionVariables, NoFetch, new Authentication()))
112102
{
113-
VersionVariables variables;
114-
115-
if (ExecuteCore.TryGetVersion(SolutionDirectory, out variables, NoFetch, new Authentication()))
103+
var thisType = typeof(GetVersion);
104+
foreach (var variable in versionVariables)
116105
{
117-
var thisType = typeof(GetVersion);
118-
foreach (var variable in variables)
119-
{
120-
thisType.GetProperty(variable.Key).SetValue(this, variable.Value, null);
121-
}
106+
thisType.GetProperty(variable.Key).SetValue(this, variable.Value, null);
122107
}
123-
return true;
124-
}
125-
catch (WarningException errorException)
126-
{
127-
this.LogWarning(errorException.Message);
128-
return true;
129-
}
130-
catch (Exception exception)
131-
{
132-
this.LogError("Error occurred: " + exception);
133-
return false;
134108
}
135109
}
136110
}
Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
namespace GitVersionTask
1+
namespace GitVersionTask
22
{
3+
using System;
34
using GitVersion;
45
using GitVersion.Helpers;
56

@@ -8,38 +9,59 @@
89

910
public abstract class GitVersionTaskBase : Task
1011
{
11-
readonly ExecuteCore executeCore;
12-
1312
protected GitVersionTaskBase()
1413
{
1514
var fileSystem = new FileSystem();
16-
executeCore = new ExecuteCore(fileSystem);
17-
GitVersion.Logger.SetLoggers(this.LogDebug, this.LogInfo, this.LogWarning, s => this.LogError(s));
15+
ExecuteCore = new ExecuteCore(fileSystem);
16+
GitVersion.Logger.SetLoggers(LogDebug, LogInfo, LogWarning, s => LogError(s));
1817
}
1918

20-
protected ExecuteCore ExecuteCore
19+
public override bool Execute()
2120
{
22-
get { return executeCore; }
21+
try
22+
{
23+
InnerExecute();
24+
return true;
25+
}
26+
catch (WarningException errorException)
27+
{
28+
LogWarning(errorException.Message);
29+
return true;
30+
}
31+
catch (Exception exception)
32+
{
33+
LogError("Error occurred: " + exception);
34+
return false;
35+
}
2336
}
2437

38+
protected abstract void InnerExecute();
39+
40+
protected ExecuteCore ExecuteCore { get; }
41+
42+
[Required]
43+
public string SolutionDirectory { get; set; }
44+
45+
public bool NoFetch { get; set; }
46+
2547
public void LogDebug(string message)
2648
{
27-
this.BuildEngine.LogMessageEvent(new BuildMessageEventArgs(message, string.Empty, "GitVersionTask", MessageImportance.Low));
49+
BuildEngine.LogMessageEvent(new BuildMessageEventArgs(message, string.Empty, "GitVersionTask", MessageImportance.Low));
2850
}
2951

3052
public void LogWarning(string message)
3153
{
32-
this.BuildEngine.LogWarningEvent(new BuildWarningEventArgs(string.Empty, string.Empty, null, 0, 0, 0, 0, message, string.Empty, "GitVersionTask"));
54+
BuildEngine.LogWarningEvent(new BuildWarningEventArgs(string.Empty, string.Empty, null, 0, 0, 0, 0, message, string.Empty, "GitVersionTask"));
3355
}
3456

3557
public void LogInfo(string message)
3658
{
37-
this.BuildEngine.LogMessageEvent(new BuildMessageEventArgs(message, string.Empty, "GitVersionTask", MessageImportance.Normal));
59+
BuildEngine.LogMessageEvent(new BuildMessageEventArgs(message, string.Empty, "GitVersionTask", MessageImportance.Normal));
3860
}
3961

4062
public void LogError(string message, string file = null)
4163
{
42-
this.BuildEngine.LogErrorEvent(new BuildErrorEventArgs(string.Empty, string.Empty, file, 0, 0, 0, 0, message, string.Empty, "GitVersionTask"));
64+
BuildEngine.LogErrorEvent(new BuildErrorEventArgs(string.Empty, string.Empty, file, 0, 0, 0, 0, message, string.Empty, "GitVersionTask"));
4365
}
4466
}
45-
}
67+
}

src/GitVersionTask/UpdateAssemblyInfo.cs

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,6 @@ namespace GitVersionTask
99

1010
public class UpdateAssemblyInfo : GitVersionTaskBase
1111
{
12-
public UpdateAssemblyInfo()
13-
{
14-
}
15-
16-
[Required]
17-
public string SolutionDirectory { get; set; }
18-
1912
[Required]
2013
public string ProjectFile { get; set; }
2114

@@ -31,35 +24,13 @@ public UpdateAssemblyInfo()
3124
[Output]
3225
public string AssemblyInfoTempFilePath { get; set; }
3326

34-
public bool NoFetch { get; set; }
35-
36-
public override bool Execute()
37-
{
38-
try
39-
{
40-
InnerExecute();
41-
return true;
42-
}
43-
catch (WarningException errorException)
44-
{
45-
this.LogWarning(errorException.Message);
46-
return true;
47-
}
48-
catch (Exception exception)
49-
{
50-
this.LogError("Error occurred: " + exception);
51-
return false;
52-
}
53-
}
54-
55-
void InnerExecute()
27+
protected override void InnerExecute()
5628
{
5729
TempFileTracker.DeleteTempFiles();
5830

5931
InvalidFileChecker.CheckForInvalidFiles(CompileFiles, ProjectFile);
6032

61-
VersionVariables versionVariables;
62-
if (!ExecuteCore.TryGetVersion(SolutionDirectory, out versionVariables, NoFetch, new Authentication()))
33+
if (!ExecuteCore.TryGetVersion(SolutionDirectory, out var versionVariables, NoFetch, new Authentication()))
6334
{
6435
return;
6536
}

src/GitVersionTask/WriteVersionInfoToBuildLog.cs

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,30 @@
11
namespace GitVersionTask
22
{
3-
using System;
43
using System.Collections.Generic;
54
using GitVersion;
6-
using Microsoft.Build.Framework;
75

86
public class WriteVersionInfoToBuildLog : GitVersionTaskBase
97
{
10-
public WriteVersionInfoToBuildLog()
8+
protected override void InnerExecute()
119
{
12-
}
13-
14-
[Required]
15-
public string SolutionDirectory { get; set; }
16-
17-
public bool NoFetch { get; set; }
18-
19-
public override bool Execute()
20-
{
21-
try
22-
{
23-
InnerExecute();
24-
return true;
25-
}
26-
catch (WarningException errorException)
27-
{
28-
this.LogWarning(errorException.Message);
29-
return true;
30-
}
31-
catch (Exception exception)
32-
{
33-
this.LogError("Error occurred: " + exception);
34-
return false;
35-
}
36-
}
37-
38-
void InnerExecute()
39-
{
40-
VersionVariables result;
41-
if (!ExecuteCore.TryGetVersion(SolutionDirectory, out result, NoFetch, new Authentication()))
10+
if (!ExecuteCore.TryGetVersion(SolutionDirectory, out var versionVariables, NoFetch, new Authentication()))
4211
{
4312
return;
4413
}
4514

46-
WriteIntegrationParameters(BuildServerList.GetApplicableBuildServers(), result);
15+
WriteIntegrationParameters(BuildServerList.GetApplicableBuildServers(), versionVariables);
4716
}
4817

49-
void WriteIntegrationParameters(IEnumerable<IBuildServer> applicableBuildServers, VersionVariables variables)
18+
void WriteIntegrationParameters(IEnumerable<IBuildServer> applicableBuildServers, VersionVariables versionVariables)
5019
{
5120
foreach (var buildServer in applicableBuildServers)
5221
{
53-
this.LogInfo(string.Format("Executing GenerateSetVersionMessage for '{0}'.", buildServer.GetType().Name));
54-
this.LogInfo(buildServer.GenerateSetVersionMessage(variables));
55-
this.LogInfo(string.Format("Executing GenerateBuildLogOutput for '{0}'.", buildServer.GetType().Name));
56-
foreach (var buildParameter in BuildOutputFormatter.GenerateBuildLogOutput(buildServer, variables))
22+
LogInfo($"Executing GenerateSetVersionMessage for '{buildServer.GetType().Name}'.");
23+
LogInfo(buildServer.GenerateSetVersionMessage(versionVariables));
24+
LogInfo($"Executing GenerateBuildLogOutput for '{buildServer.GetType().Name}'.");
25+
foreach (var buildParameter in BuildOutputFormatter.GenerateBuildLogOutput(buildServer, versionVariables))
5726
{
58-
this.LogInfo(buildParameter);
27+
LogInfo(buildParameter);
5928
}
6029
}
6130
}

0 commit comments

Comments
 (0)