Skip to content

Commit 23cb86f

Browse files
committed
Compacted and modified code as per discussion on pull request item in GitHub.
1 parent ae32f96 commit 23cb86f

File tree

7 files changed

+111
-136
lines changed

7 files changed

+111
-136
lines changed

src/GitVersionTask/GenerateGitVersionInformation.cs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,21 @@ namespace GitVersionTask
77

88
public static class GenerateGitVersionInformation
99
{
10-
public static Output Execute(
11-
Input input
12-
)
10+
// This method is entrypoint for the task declared in .props file
11+
public static Output Execute(Input input)
1312
{
14-
return GitVersionTaskBase.ExecuteGitVersionTask(
15-
input,
16-
InnerExecute
17-
);
13+
return GitVersionTaskCommonFunctionality.ExecuteGitVersionTask(input, InnerExecute);
1814
}
1915

20-
private static Output InnerExecute(
21-
Input input,
22-
TaskLogger logger
23-
)
16+
private static Output InnerExecute(Input input, TaskLogger logger)
2417
{
25-
var execute = GitVersionTaskBase.CreateExecuteCore();
18+
var execute = GitVersionTaskCommonFunctionality.CreateExecuteCore();
2619
if (!execute.TryGetVersion(input.SolutionDirectory, out var versionVariables, input.NoFetch, new Authentication()))
2720
{
2821
return null;
2922
}
3023

31-
var fileWriteInfo = input.IntermediateOutputPath.GetWorkingDirectoryAndFileNameAndExtension(
24+
var fileWriteInfo = input.IntermediateOutputPath.GetFileWriteInfo(
3225
input.Language,
3326
input.ProjectFile,
3427
(pf, ext) => $"GitVersionInformation.g.{ext}",
@@ -46,7 +39,7 @@ TaskLogger logger
4639
}
4740

4841

49-
public sealed class Input : GitVersionTaskBase.InputWithCommonAdditionalProperties
42+
public sealed class Input : InputWithCommonAdditionalProperties
5043
{
5144
}
5245

src/GitVersionTask/GetVersion.cs

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,14 @@ namespace GitVersionTask
55
public static class GetVersion
66
{
77

8-
public static Output Execute(
9-
Input input
10-
)
8+
public static Output Execute(Input input)
119
{
12-
return GitVersionTaskBase.ExecuteGitVersionTask(
13-
input,
14-
InnerExecute
15-
);
10+
return GitVersionTaskCommonFunctionality.ExecuteGitVersionTask(input, InnerExecute);
1611
}
1712

18-
private static Output InnerExecute(
19-
Input input,
20-
TaskLogger logger
21-
)
13+
private static Output InnerExecute(Input input, TaskLogger logger)
2214
{
23-
if (!GitVersionTaskBase.CreateExecuteCore().TryGetVersion(input.SolutionDirectory, out var versionVariables, input.NoFetch, new GitVersion.Authentication()))
15+
if (!GitVersionTaskCommonFunctionality.CreateExecuteCore().TryGetVersion(input.SolutionDirectory, out var versionVariables, input.NoFetch, new GitVersion.Authentication()))
2416
{
2517
return null;
2618
}
@@ -35,72 +27,72 @@ TaskLogger logger
3527
return output;
3628
}
3729

38-
public sealed class Input : GitVersionTaskBase.AbstractInput
30+
public sealed class Input : InputBase
3931
{
4032

4133
}
4234

4335
public sealed class Output
4436
{
45-
public String Major { get; set; }
37+
public string Major { get; set; }
4638

47-
public String Minor { get; set; }
39+
public string Minor { get; set; }
4840

49-
public String Patch { get; set; }
41+
public string Patch { get; set; }
5042

51-
public String PreReleaseTag { get; set; }
43+
public string PreReleaseTag { get; set; }
5244

53-
public String PreReleaseTagWithDash { get; set; }
45+
public string PreReleaseTagWithDash { get; set; }
5446

55-
public String PreReleaseLabel { get; set; }
47+
public string PreReleaseLabel { get; set; }
5648

57-
public String PreReleaseNumber { get; set; }
49+
public string PreReleaseNumber { get; set; }
5850

59-
public String WeightedPreReleaseNumber { get; set; }
51+
public string WeightedPreReleaseNumber { get; set; }
6052

61-
public String BuildMetaData { get; set; }
53+
public string BuildMetaData { get; set; }
6254

63-
public String BuildMetaDataPadded { get; set; }
55+
public string BuildMetaDataPadded { get; set; }
6456

65-
public String FullBuildMetaData { get; set; }
57+
public string FullBuildMetaData { get; set; }
6658

67-
public String MajorMinorPatch { get; set; }
59+
public string MajorMinorPatch { get; set; }
6860

69-
public String SemVer { get; set; }
61+
public string SemVer { get; set; }
7062

71-
public String LegacySemVer { get; set; }
63+
public string LegacySemVer { get; set; }
7264

73-
public String LegacySemVerPadded { get; set; }
65+
public string LegacySemVerPadded { get; set; }
7466

75-
public String AssemblySemVer { get; set; }
67+
public string AssemblySemVer { get; set; }
7668

77-
public String AssemblySemFileVer { get; set; }
69+
public string AssemblySemFileVer { get; set; }
7870

79-
public String FullSemVer { get; set; }
71+
public string FullSemVer { get; set; }
8072

81-
public String InformationalVersion { get; set; }
73+
public string InformationalVersion { get; set; }
8274

83-
public String BranchName { get; set; }
75+
public string BranchName { get; set; }
8476

85-
public String Sha { get; set; }
77+
public string Sha { get; set; }
8678

87-
public String ShortSha { get; set; }
79+
public string ShortSha { get; set; }
8880

89-
public String NuGetVersionV2 { get; set; }
81+
public string NuGetVersionV2 { get; set; }
9082

91-
public String NuGetVersion { get; set; }
83+
public string NuGetVersion { get; set; }
9284

93-
public String NuGetPreReleaseTagV2 { get; set; }
85+
public string NuGetPreReleaseTagV2 { get; set; }
9486

95-
public String NuGetPreReleaseTag { get; set; }
87+
public string NuGetPreReleaseTag { get; set; }
9688

97-
public String CommitDate { get; set; }
89+
public string CommitDate { get; set; }
9890

99-
public String VersionSourceSha { get; set; }
91+
public string VersionSourceSha { get; set; }
10092

101-
public String CommitsSinceVersionSource { get; set; }
93+
public string CommitsSinceVersionSource { get; set; }
10294

103-
public String CommitsSinceVersionSourcePadded { get; set; }
95+
public string CommitsSinceVersionSourcePadded { get; set; }
10496
}
10597

10698
}

src/GitVersionTask/GitVersionTaskBase.cs renamed to src/GitVersionTask/GitVersionTaskCommonFunctionality.cs

Lines changed: 52 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,17 @@ namespace GitVersionTask
55
using System;
66
using System.IO;
77

8-
public static class GitVersionTaskBase
8+
public static class GitVersionTaskCommonFunctionality
99
{
1010
internal static TOutput ExecuteGitVersionTask<TInput, TOutput>(
1111
TInput input,
1212
Func<TInput, TaskLogger, TOutput> execute
1313
)
14-
where TInput : AbstractInput
14+
where TInput : InputBase
1515
where TOutput : class, new()
1616
{
17-
if (!input.ValidateInput())
18-
{
19-
throw new Exception("Invalid input.");
20-
}
17+
18+
input.ValidateInputOrThrowException();
2119

2220
var logger = new TaskLogger();
2321
Logger.SetLoggers(logger.LogInfo, logger.LogInfo, logger.LogWarning, s => logger.LogError(s));
@@ -50,7 +48,7 @@ Func<TInput, TaskLogger, TOutput> execute
5048
public static ExecuteCore CreateExecuteCore()
5149
=> new ExecuteCore(new FileSystem());
5250

53-
private static string GetFileExtension(this String language)
51+
private static string GetFileExtension(this string language)
5452
{
5553
switch (language)
5654
{
@@ -68,16 +66,16 @@ private static string GetFileExtension(this String language)
6866
}
6967
}
7068

71-
public static FileWriteInfo GetWorkingDirectoryAndFileNameAndExtension(
72-
this String intermediateOutputPath,
73-
String language,
74-
String projectFile,
75-
Func<String, String, String> fileNameWithIntermediatePath,
76-
Func<String, String, String> fileNameNoIntermediatePath
69+
public static FileWriteInfo GetFileWriteInfo(
70+
this string intermediateOutputPath,
71+
string language,
72+
string projectFile,
73+
Func<string, string, string> fileNameWithIntermediatePath,
74+
Func<string, string, string> fileNameNoIntermediatePath
7775
)
7876
{
7977
var fileExtension = language.GetFileExtension();
80-
String workingDirectory, fileName;
78+
string workingDirectory, fileName;
8179
if (intermediateOutputPath == null)
8280
{
8381
fileName = fileNameWithIntermediatePath(projectFile, fileExtension);
@@ -91,51 +89,66 @@ Func<String, String, String> fileNameNoIntermediatePath
9189
return new FileWriteInfo(workingDirectory, fileName, fileExtension);
9290
}
9391

94-
public abstract class AbstractInput
95-
{
96-
public String SolutionDirectory { get; set; }
92+
}
93+
94+
public abstract class InputBase
95+
{
96+
public string SolutionDirectory { get; set; }
9797

98-
public Boolean NoFetch { get; set; }
98+
public Boolean NoFetch { get; set; }
9999

100-
public virtual Boolean ValidateInput()
100+
public void ValidateInputOrThrowException()
101+
{
102+
if (!this.ValidateInput())
101103
{
102-
return !String.IsNullOrEmpty(this.SolutionDirectory);
104+
throw new InputValidationException($"Invalid input for {this.GetType()}.");
103105
}
104106
}
105107

106-
public abstract class InputWithCommonAdditionalProperties : AbstractInput
108+
protected virtual Boolean ValidateInput()
107109
{
108-
public String ProjectFile { get; set; }
110+
return !String.IsNullOrEmpty(this.SolutionDirectory);
111+
}
112+
}
109113

110-
public String IntermediateOutputPath { get; set; }
114+
public abstract class InputWithCommonAdditionalProperties : InputBase
115+
{
116+
public string ProjectFile { get; set; }
111117

112-
public String Language { get; set; }
118+
public string IntermediateOutputPath { get; set; }
119+
120+
public string Language { get; set; }
121+
122+
protected override Boolean ValidateInput()
123+
{
124+
return base.ValidateInput()
125+
&& !String.IsNullOrEmpty(this.ProjectFile)
126+
&& !String.IsNullOrEmpty(this.IntermediateOutputPath)
127+
&& !String.IsNullOrEmpty(this.Language);
128+
}
129+
}
130+
131+
public sealed class InputValidationException : Exception
132+
{
133+
public InputValidationException(string msg, Exception inner = null)
134+
: base(msg, inner)
135+
{
113136

114-
public override Boolean ValidateInput()
115-
{
116-
return base.ValidateInput()
117-
&& !String.IsNullOrEmpty(this.ProjectFile)
118-
&& !String.IsNullOrEmpty(this.IntermediateOutputPath)
119-
&& !String.IsNullOrEmpty(this.Language);
120-
}
121137
}
122138
}
123139

124140
public sealed class FileWriteInfo
125141
{
126-
public FileWriteInfo(
127-
String workingDirectory,
128-
String fileName,
129-
String fileExtension
130-
)
142+
public FileWriteInfo(string workingDirectory, string fileName, string fileExtension)
131143
{
132144
this.WorkingDirectory = workingDirectory;
133145
this.FileName = fileName;
134146
this.FileExtension = fileExtension;
135147
}
136148

137-
public String WorkingDirectory { get; }
138-
public String FileName { get; }
139-
public String FileExtension { get; }
149+
public string WorkingDirectory { get; }
150+
public string FileName { get; }
151+
public string FileExtension { get; }
140152
}
153+
141154
}

src/GitVersionTask/InvalidFileChecker.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static class InvalidFileChecker
1313
{ ".vb", VisualBasicFileContainsVersionAttribute }
1414
};
1515

16-
public static void CheckForInvalidFiles(IEnumerable<String> compileFiles, string projectFile)
16+
public static void CheckForInvalidFiles(IEnumerable<string> compileFiles, string projectFile)
1717
{
1818
foreach (var compileFile in GetInvalidFiles(compileFiles, projectFile))
1919
{
@@ -97,7 +97,7 @@ static bool VisualBasicFileContainsVersionAttribute(string compileFile, string p
9797
\s*\(\s*\)\s*\> # End brackets ()>");
9898
}
9999

100-
static IEnumerable<string> GetInvalidFiles(IEnumerable<String> compileFiles, string projectFile)
100+
static IEnumerable<string> GetInvalidFiles(IEnumerable<string> compileFiles, string projectFile)
101101
{
102102
return compileFiles
103103
.Where(compileFile => compileFile.Contains("AssemblyInfo"))

src/GitVersionTask/TaskLogger.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ class TaskLogger
66
private readonly TextWriter stdout;
77
private readonly TextWriter stderr;
88

9-
public TaskLogger(
10-
TextWriter paramStdout = null,
11-
TextWriter paramStderr = null
12-
)
9+
public TaskLogger(TextWriter paramStdout = null, TextWriter paramStderr = null)
1310
{
1411
this.stdout = paramStdout ?? Console.Out;
1512
this.stderr = paramStderr ?? Console.Error;

0 commit comments

Comments
 (0)