Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
code cleanup for GitVersionTask
  • Loading branch information
arturcic committed May 16, 2019
commit ccd343d3ee9c1e2633b225c524577927464b685d
1 change: 1 addition & 0 deletions src/GitVersionTask.Tests/InvalidFileCheckerTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using GitVersion;
using GitVersionTask;
using GitVersionTask.Tests.Mocks;
using Microsoft.Build.Framework;
using NUnit.Framework;
Expand Down
23 changes: 1 addition & 22 deletions src/GitVersionTask/GenerateGitVersionInformation.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
namespace GitVersionTask
{
using System.IO;
using GitVersion;
using GitVersion.Helpers;
using Microsoft.Build.Framework;

public class GenerateGitVersionInformation : GitVersionTaskBase
Expand All @@ -19,24 +16,6 @@ public class GenerateGitVersionInformation : GitVersionTaskBase
[Output]
public string GitVersionInformationFilePath { get; set; }

protected override void InnerExecute()
{
if (GetVersionVariables(out var versionVariables)) return;

var fileExtension = TaskUtils.GetFileExtension(Language);
var fileName = $"GitVersionInformation.g.{fileExtension}";

if (IntermediateOutputPath == null)
{
fileName = $"GitVersionInformation_{Path.GetFileNameWithoutExtension(ProjectFile)}_{Path.GetRandomFileName()}.g.{fileExtension}";
}

var workingDirectory = IntermediateOutputPath ?? TempFileTracker.TempPath;

GitVersionInformationFilePath = Path.Combine(workingDirectory, fileName);

var generator = new GitVersionInformationGenerator(fileName, workingDirectory, versionVariables, new FileSystem());
generator.Generate();
}
public override bool Execute() => GitVersionTasks.GenerateGitVersionInformation(this);
}
}
11 changes: 1 addition & 10 deletions src/GitVersionTask/GetVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,6 @@ public class GetVersion : GitVersionTaskBase
[Output]
public string CommitsSinceVersionSourcePadded { get; set; }

protected override void InnerExecute()
{
if (GetVersionVariables(out var versionVariables)) return;

var thisType = typeof(GetVersion);
foreach (var variable in versionVariables)
{
thisType.GetProperty(variable.Key)?.SetValue(this, variable.Value, null);
}
}
public override bool Execute() => GitVersionTasks.GetVersion(this);
}
}
26 changes: 0 additions & 26 deletions src/GitVersionTask/GitVersionTask.v2.ncrunchproject

This file was deleted.

59 changes: 0 additions & 59 deletions src/GitVersionTask/GitVersionTaskBase.cs
Original file line number Diff line number Diff line change
@@ -1,72 +1,13 @@
namespace GitVersionTask
{
using System;
using GitVersion;
using GitVersion.Helpers;

using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

public abstract class GitVersionTaskBase : Task
{
protected GitVersionTaskBase()
{
var fileSystem = new FileSystem();
ExecuteCore = new ExecuteCore(fileSystem);
GitVersion.Logger.SetLoggers(LogDebug, LogInfo, LogWarning, s => LogError(s));
}

public override bool Execute()
{
try
{
InnerExecute();
return true;
}
catch (WarningException errorException)
{
LogWarning(errorException.Message);
return true;
}
catch (Exception exception)
{
LogError("Error occurred: " + exception);
return false;
}
}

protected abstract void InnerExecute();

protected ExecuteCore ExecuteCore { get; }

[Required]
public string SolutionDirectory { get; set; }

public bool NoFetch { get; set; }

public void LogDebug(string message)
{
BuildEngine.LogMessageEvent(new BuildMessageEventArgs(message, string.Empty, "GitVersionTask", MessageImportance.Low));
}

public void LogWarning(string message)
{
BuildEngine.LogWarningEvent(new BuildWarningEventArgs(string.Empty, string.Empty, null, 0, 0, 0, 0, message, string.Empty, "GitVersionTask"));
}

public void LogInfo(string message)
{
BuildEngine.LogMessageEvent(new BuildMessageEventArgs(message, string.Empty, "GitVersionTask", MessageImportance.Normal));
}

public void LogError(string message, string file = null)
{
BuildEngine.LogErrorEvent(new BuildErrorEventArgs(string.Empty, string.Empty, file, 0, 0, 0, 0, message, string.Empty, "GitVersionTask"));
}

protected bool GetVersionVariables(out VersionVariables versionVariables)
{
return !ExecuteCore.TryGetVersion(SolutionDirectory, out versionVariables, NoFetch, new Authentication());
}
}
}
154 changes: 154 additions & 0 deletions src/GitVersionTask/GitVersionTasks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
namespace GitVersionTask
{
using System;
using System.IO;
using GitVersion;
using GitVersion.Helpers;
using Microsoft.Build.Framework;

public static class GitVersionTasks
{
public static ExecuteCore ExecuteCore { get; }

static GitVersionTasks()
{
var fileSystem = new FileSystem();
ExecuteCore = new ExecuteCore(fileSystem);
}

public static bool GetVersion(GetVersion task)
{
return Execute(task, t =>
{
if (!GetVersionVariables(task, out var versionVariables)) return;

var thisType = typeof(GetVersion);
foreach (var variable in versionVariables)
{
thisType.GetProperty(variable.Key)?.SetValue(task, variable.Value, null);
}
});
}

public static bool GenerateGitVersionInformation(GenerateGitVersionInformation task)
{
return Execute(task, t =>
{
if (!GetVersionVariables(task, out var versionVariables)) return;

var fileExtension = GetFileExtension(task.Language);
var fileName = $"GitVersionInformation.g.{fileExtension}";

if (task.IntermediateOutputPath == null)
{
fileName = $"GitVersionInformation_{Path.GetFileNameWithoutExtension(task.ProjectFile)}_{Path.GetRandomFileName()}.g.{fileExtension}";
}

var workingDirectory = task.IntermediateOutputPath ?? TempFileTracker.TempPath;

task.GitVersionInformationFilePath = Path.Combine(workingDirectory, fileName);

var generator = new GitVersionInformationGenerator(fileName, workingDirectory, versionVariables, new FileSystem());
generator.Generate();
});
}

public static bool UpdateAssemblyInfo(UpdateAssemblyInfo task)
{
return Execute(task, t =>
{
TempFileTracker.DeleteTempFiles();
InvalidFileChecker.CheckForInvalidFiles(task.CompileFiles, task.ProjectFile);

if (!GetVersionVariables(task, out var versionVariables)) return;

var fileExtension = GetFileExtension(task.Language);
var assemblyInfoFileName = $"GitVersionTaskAssemblyInfo.g.{fileExtension}";

if (task.IntermediateOutputPath == null)
{
assemblyInfoFileName = $"AssemblyInfo_{Path.GetFileNameWithoutExtension(task.ProjectFile)}_{Path.GetRandomFileName()}.g.{fileExtension}";
}

var workingDirectory = task.IntermediateOutputPath ?? TempFileTracker.TempPath;

task.AssemblyInfoTempFilePath = Path.Combine(workingDirectory, assemblyInfoFileName);

using (var assemblyInfoFileUpdater = new AssemblyInfoFileUpdater(assemblyInfoFileName, workingDirectory, versionVariables, new FileSystem(), true))
{
assemblyInfoFileUpdater.Update();
assemblyInfoFileUpdater.CommitChanges();
}
});
}

public static bool WriteVersionInfoToBuildLog(WriteVersionInfoToBuildLog task)
{
return Execute(task, t =>
{
if (!GetVersionVariables(task, out var versionVariables)) return;
var log = task.Log;

foreach (var buildServer in BuildServerList.GetApplicableBuildServers())
{
log.LogMessage($"Executing GenerateSetVersionMessage for '{buildServer.GetType().Name}'.");
log.LogMessage(buildServer.GenerateSetVersionMessage(versionVariables));
log.LogMessage($"Executing GenerateBuildLogOutput for '{buildServer.GetType().Name}'.");
foreach (var buildParameter in BuildOutputFormatter.GenerateBuildLogOutput(buildServer, versionVariables))
{
log.LogMessage(buildParameter);
}
}
});
}

private static bool Execute<T>(T task, Action<T> action)
where T : GitVersionTaskBase
{
void LogDebug(string message) => task.Log.LogMessage(MessageImportance.Low, message);
void LogInfo(string message) => task.Log.LogMessage(MessageImportance.Normal, message);
void LogWarning(string message) => task.Log.LogWarning(message);
void LogError(string message) => task.Log.LogError(message);

Logger.SetLoggers(LogDebug, LogInfo, LogWarning, LogError);
var log = task.Log;
try
{
action(task);
}
catch (WarningException errorException)
{
log.LogWarningFromException(errorException);
return true;
}
catch (Exception exception)
{
log.LogErrorFromException(exception);
return false;
}

return !log.HasLoggedErrors;
}

private static bool GetVersionVariables(GitVersionTaskBase task, out VersionVariables versionVariables)
=> ExecuteCore.TryGetVersion(task.SolutionDirectory, out versionVariables, task.NoFetch, new Authentication());

private static string GetFileExtension(string language)
{
switch(language)
{
case "C#":
return "cs";

case "F#":
return "fs";

case "VB":
return "vb";

default:
throw new Exception($"Unknown language detected: '{language}'");
}
}
}
}
Loading