Skip to content

Commit

Permalink
(cake-contribGH-79) suggest using the same cake versions
Browse files Browse the repository at this point in the history
for cake.core, cake.common and cake.testing
  • Loading branch information
nils-a committed Mar 19, 2021
1 parent 7309971 commit 8fce2f5
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 13 deletions.
24 changes: 23 additions & 1 deletion src/Tasks.Tests/RecommendedCakeVersionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace CakeContrib.Guidelines.Tasks.Tests
{
public class RequiredCakeVersionTests
public class RecommendedCakeVersionTests
{
[Fact]
public void Should_Warn_If_One_Version_Is_Not_Correct()
Expand Down Expand Up @@ -142,5 +142,27 @@ public void Should_Log_Error_If_WarnAsError_Is_Set()
// then
fixture.BuildEngine.ErrorEvents.Should().HaveCount(1);
}

[Fact]
public void Should_Suggest_Consolidation_Versions()
{
// given
var fixture = new RecommendedCakeVersionFixture();
fixture.WithReference("cake.core", "0.38.5");
fixture.WithReference("cake.common", "0.33.0");
fixture.WithReferencesToCheck("cake.core", "cake.common");
fixture.WithRecommendedVersion("1.0.0");
fixture.WithOmittedReferences("cake.core", "cake.common");

// when
fixture.Execute();

// then
fixture.BuildEngine.MessageEvents.Should()
.Contain(x =>
x.Code != null
&& x.Code.Equals("CCG0009", StringComparison.OrdinalIgnoreCase)
&& x.Message.StartsWith("2 different"));
}
}
}
26 changes: 26 additions & 0 deletions src/Tasks/Extensions/LogExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,32 @@ internal static void CcgTrace(
log.LogMessage(importance, message);
}

/// <summary>
/// Writes a suggestion. (Shows up as "message" in Visual Studio)
/// This is a convenience-Method that wraps some always identical parameters.
/// </summary>
/// <param name="log">The <see cref="TaskLoggingHelper"/> instance.</param>
/// <param name="ccgRuleId">The CCG Rule.</param>
/// <param name="projectFile">The project file. May be null.</param>
/// <param name="message">The message to show.</param>
internal static void CcgSuggestion(this TaskLoggingHelper log, int ccgRuleId, string projectFile, string message)
{
var ccgRule = GetRule(ccgRuleId);
var helpLink = GetHelpLink(ccgRuleId);
message = $"{message} (see {helpLink})";

log.LogCriticalMessage(
null,
ccgRule,
string.Empty, // not usable anyway. See https://github.com/MicrosoftDocs/visualstudio-docs/issues/5894
projectFile ?? string.Empty,
0,
0,
0,
0,
message);
}

/// <summary>
/// Writes a warning.
/// This is a convenience-Method that wraps some always identical parameters.
Expand Down
26 changes: 14 additions & 12 deletions src/Tasks/RecommendedCakeVersion.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;

using CakeContrib.Guidelines.Tasks.Extensions;
Expand All @@ -13,12 +14,6 @@ namespace CakeContrib.Guidelines.Tasks
/// </summary>
public class RecommendedCakeVersion : Task
{
#if DEBUG
private const MessageImportance LogLevel = MessageImportance.High;
#else
private const MessageImportance LogLevel = MessageImportance.Low;
#endif

/// <summary>
/// Gets or sets the Recommended Version.
/// </summary>
Expand Down Expand Up @@ -64,20 +59,18 @@ public class RecommendedCakeVersion : Task
/// </summary>
public string[] WarningsAsErrors { get; set; }


/// <inheritdoc />
public override bool Execute()
{
if (!CakeProjectType.IsOneOf(ProjectType, CakeProjectType.Addin, CakeProjectType.Module))
{
Log.LogMessage(
LogLevel,
$"No Cake reference required for {ProjectType} projects.");
Log.CcgTrace($"No Cake reference required for {ProjectType} projects.");
return true;
}

var omitted = Omitted.Select(x => x.ToString()).ToArray();
var toCheck = ReferencesToCheck.Select(x => x.ToString()).ToArray();
var referencedVersions = new HashSet<string>();
foreach (var r in References)
{
var package = r.ToString();
Expand All @@ -88,13 +81,14 @@ public override bool Execute()
continue;
}

referencedVersions.Add(version);
if (omitted.Any(x => x.Equals(package, StringComparison.OrdinalIgnoreCase)))
{
Log.LogMessage(LogLevel, $"Package '{package}' is set to omit.");
Log.CcgTrace($"Package '{package}' is set to omit.");
continue;
}

Log.LogMessage(LogLevel, $"Checking {package} version {version} against recommended version of {RecommendedVersion}");
Log.CcgTrace($"Checking {package} version {version} against recommended version of {RecommendedVersion}");

if (!RecommendedVersion.Equals(version, StringComparison.OrdinalIgnoreCase))
{
Expand All @@ -107,6 +101,14 @@ public override bool Execute()
}
}

if (referencedVersions.Count > 1)
{
Log.CcgSuggestion(
9,
ProjectFile,
$"{referencedVersions.Count} different versions of Cake were referenced. It is suggested to reference the same version for all Cake references.");
}

return true;
}
}
Expand Down

0 comments on commit 8fce2f5

Please sign in to comment.