Skip to content

Commit

Permalink
(cake-contrib#387) Added support for Central Package Mangagement
Browse files Browse the repository at this point in the history
  • Loading branch information
nils-a committed Jan 3, 2025
1 parent f4e34a0 commit d9cca6e
Show file tree
Hide file tree
Showing 14 changed files with 275 additions and 50 deletions.
1 change: 1 addition & 0 deletions src/Guidelines/build/CakeInternalReferences.targets
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
ProjectType="$(CakeContribGuidelinesProjectType)"
CakeVersion="$(CakeContribGuidelinesOverrideTargetFrameworkCakeVersion)"
References="@(PackageReference)"
PackageVersions="@(PackageVersion)"
NoWarn="$(NoWarn)"
WarningsAsErrors="$(WarningsAsErrors)"
/>
Expand Down
1 change: 1 addition & 0 deletions src/Guidelines/build/RecommendedCakeVersion.targets
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
ProjectFile="$(MSBuildProjectFullPath)"
RecommendedVersion="$(RecommendedCakeVersion)"
References="@(PackageReference)"
PackageVersions="@(PackageVersion)"
Omitted="@(CakeContribGuidelinesOmitRecommendedCakeVersion)"
ReferencesToCheck="@(CakeReferenceToCheck)"
ProjectType="$(CakeContribGuidelinesProjectType)"
Expand Down
1 change: 1 addition & 0 deletions src/Guidelines/build/TargetFrameworkVersions.targets
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<TargetFrameworkVersions
ProjectFile="$(MSBuildProjectFullPath)"
References="@(PackageReference)"
PackageVersions="@(PackageVersion)"
TargetFramework="$(TargetFramework)"
TargetFrameworks="$(TargetFrameworks)"
Omitted="@(CakeContribGuidelinesOmitTargetFramework)"
Expand Down
68 changes: 37 additions & 31 deletions src/Tasks.IntegrationTests/E2eTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void Referencing_Cake_Core_with_PrivateAssets_results_in_no_error()
{
// given
fixture.WithoutDefaultCakeReference();
fixture.WithPackageReference("Cake.Core", "0.38.5", "all");
fixture.WithPackageReference("Cake.Core", fixture.DefaultCakeVersion, "all");

// when
var result = fixture.Run();
Expand All @@ -136,7 +136,7 @@ public void Referencing_Cake_Core_without_PrivateAssets_results_in_CCG0004_error
{
// given
fixture.WithoutDefaultCakeReference();
fixture.WithPackageReference("Cake.Core", "0.38.5");
fixture.WithPackageReference("Cake.Core", fixture.DefaultCakeVersion);

// when
var result = fixture.Run();
Expand Down Expand Up @@ -221,22 +221,27 @@ public void Missing_Required_Target_results_in_CCG0007_error()
// then
result.IsErrorExitCode.ShouldBeTrue();
result.ErrorLines.ShouldContain(l => l.IndexOf("CCG0007", StringComparison.Ordinal) > -1);
result.ErrorLines.ShouldContain(l => l.IndexOf("netstandard2.0", StringComparison.Ordinal) > -1);
result.ErrorLines.ShouldContain(l => l.IndexOf(fixture.DefaultTargetFrameworkForModules, StringComparison.Ordinal) > -1);
}

[Fact]
public void Missing_Suggested_Target_results_in_CCG0007_warning()
{
var missingTfm = fixture.DefaultTargetFrameworksForAddins
.Split(";", StringSplitOptions.RemoveEmptyEntries)

Check failure on line 231 in src/Tasks.IntegrationTests/E2eTests.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022)

Argument 1: cannot convert from 'string' to 'char'

Check failure on line 231 in src/Tasks.IntegrationTests/E2eTests.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022)

Argument 2: cannot convert from 'System.StringSplitOptions' to 'char'
.First();
var allTfmButMissing = fixture.DefaultTargetFrameworksForAddins.Replace(missingTfm, string.Empty);

// given
fixture.WithTargetFrameworks("netstandard2.0");
fixture.WithTargetFrameworks(allTfmButMissing);

// when
var result = fixture.Run();

// then
result.IsErrorExitCode.ShouldBeFalse();
result.WarningLines.ShouldContain(l => l.IndexOf("CCG0007", StringComparison.Ordinal) > -1);
result.WarningLines.ShouldContain(l => l.IndexOf("net461", StringComparison.Ordinal) > -1);
result.IsErrorExitCode.ShouldBeTrue();
result.ErrorLines.ShouldContain(l => l.IndexOf("CCG0007", StringComparison.Ordinal) > -1);
result.ErrorLines.ShouldContain(l => l.IndexOf(missingTfm, StringComparison.Ordinal) > -1);
}

[Fact]
Expand All @@ -246,6 +251,7 @@ public void Missing_Suggested_Target_net5_for_Cake_v100_results_in_CCG0007_warni
fixture.WithoutDefaultCakeReference();
fixture.WithPackageReference("Cake.Core", "1.0.0", "all");
fixture.WithTargetFrameworks("netstandard2.0;net461");
fixture.OmitRecommendedCakeVersion();

// when
var result = fixture.Run();
Expand Down Expand Up @@ -351,7 +357,7 @@ public void ProjectType_When_Assembly_Is_Module_Is_Module()
{
// given
fixture.WithAssemblyName("Cake.Buildsystems.Module");
fixture.WithTargetFrameworks("netstandard2.0");
fixture.WithTargetFrameworks(fixture.DefaultTargetFrameworkForModules);
fixture.WithCustomContent(@"
<Target Name=""ForTest""
AfterTargets=""BeforeBuild""
Expand All @@ -376,7 +382,7 @@ public void ProjectType_When_Assembly_Is_Module_Is_Module()
public void ProjectType_When_PackageId_Is_Module_Is_Module()
{
// given
fixture.WithTargetFrameworks("netstandard2.0");
fixture.WithTargetFrameworks(fixture.DefaultTargetFrameworkForModules);
fixture.WithCustomContent(@"
<PropertyGroup>
<PackageId>Cake.Buildsystems.Module</PackageId>
Expand Down Expand Up @@ -498,7 +504,7 @@ public void Packaging_Different_Types_Should_Add_The_Correct_Icon(string assembl
fixture.WithAssemblyName(assemblyName);
if (isModule)
{
fixture.WithTargetFrameworks("netstandard2.0");
fixture.WithTargetFrameworks(fixture.DefaultTargetFrameworkForModules);
}
fixture.WithCustomContent(@"
<PropertyGroup>
Expand Down Expand Up @@ -537,7 +543,7 @@ public void Packaging_Different_Types_Should_Add_The_Correct_IconUrl_To_Properti
fixture.WithAssemblyName(assemblyName);
if (isModule)
{
fixture.WithTargetFrameworks("netstandard2.0");
fixture.WithTargetFrameworks(fixture.DefaultTargetFrameworkForModules);
}
fixture.WithCustomContent(@"
<PropertyGroup>
Expand Down Expand Up @@ -589,7 +595,7 @@ public void Missing_Module_Tag_Should_Raise_CCG0008_For_Modules()
<PackageId>Cake.Buildsystems.Module</PackageId>
</PropertyGroup>");
fixture.WithTags("cake build cake-build script");
fixture.WithTargetFrameworks("netstandard2.0");
fixture.WithTargetFrameworks(fixture.DefaultTargetFrameworkForModules);

// when
var result = fixture.Run();
Expand Down Expand Up @@ -651,6 +657,7 @@ public void Incorrect_Cake_Reference_Should_Raise_CCG0009()
// given
fixture.WithoutDefaultCakeReference();
fixture.WithPackageReference("cake.core", "0.38.5", "All");
fixture.WithTargetFrameworks("netstandard2.0;net461;net5.0");

// when
var result = fixture.Run();
Expand All @@ -667,6 +674,7 @@ public void Incorrect_But_Omitted_Cake_Reference_Should_Not_Raise_CCG0009()
// given
fixture.WithoutDefaultCakeReference();
fixture.WithPackageReference("cake.core", "0.38.5", "All");
fixture.WithTargetFrameworks("netstandard2.0;net461;net5.0");
fixture.WithCustomContent(@"
<ItemGroup>
<CakeContribGuidelinesOmitRecommendedCakeVersion Include=""Cake.Core"" />
Expand All @@ -680,29 +688,11 @@ public void Incorrect_But_Omitted_Cake_Reference_Should_Not_Raise_CCG0009()
result.WarningLines.ShouldBeEmpty();
}

[Fact]
public void Missing_Suggested_Target_results_not_in_CCG0007_warning_if_NoWarn_is_set()
{
// given
fixture.WithTargetFrameworks("netstandard2.0");
fixture.WithCustomContent(@"
<PropertyGroup>
<NoWarn>1701;1702;ccg0007</NoWarn>
</PropertyGroup>");

// when
var result = fixture.Run();

// then
result.IsErrorExitCode.ShouldBeFalse();
result.WarningLines.ShouldNotContain(l => l.IndexOf("CCG0007", StringComparison.Ordinal) > -1);
}

[Fact]
public void Missing_Suggested_Target_results_in_CCG0007_error_if_WarningsAsErrors_is_set()
{
// given
fixture.WithTargetFrameworks("netstandard2.0");
fixture.WithTargetFrameworks(fixture.DefaultTargetFrameworkForModules);
fixture.WithCustomContent(@"
<PropertyGroup>
<WarningsAsErrors>NU1605;ccg0007</WarningsAsErrors >
Expand Down Expand Up @@ -738,5 +728,21 @@ public void Missing_Required_Target_results_in_CCG0007_error_if_Cake_Version_Is_
err.ShouldNotBeNull();
err.ShouldContain("netstandard2.0");
}

[Fact]
public void Central_Package_Management_Correct_Cake_Reference_Should_Not_Raise_CCG0009()
{
// given
fixture.WithoutDefaultCakeReference();
fixture.WithPackageReference("cake.core", privateAssets: "All");
fixture.WithCpmPackageVersion("Cake.Core", fixture.DefaultCakeVersion);

// when
var result = fixture.Run();

// then
result.IsErrorExitCode.ShouldBeFalse();
result.WarningLines.ShouldBeEmpty();
}
}
}
67 changes: 59 additions & 8 deletions src/Tasks.IntegrationTests/Fixtures/E2eTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@ public class E2eTestFixture : IDisposable
private bool hasEditorConfig = true;
private bool omitRecommendedCakeVersion = false;
private bool hasDefaultCakeReference = true;
private readonly List<string> customContent = new List<string>();
private string targetFrameworks = "netstandard2.0;net461;net5.0";
private readonly List<string> references = new List<string>();
private readonly List<string> customContent = new();

Check failure on line 25 in src/Tasks.IntegrationTests/Fixtures/E2eTestFixture.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022)

Feature 'target-typed object creation' is not available in C# 7.3. Please use language version 9.0 or greater.

Check failure on line 25 in src/Tasks.IntegrationTests/Fixtures/E2eTestFixture.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022)

Feature 'target-typed object creation' is not available in C# 7.3. Please use language version 9.0 or greater.
private string targetFrameworks = "net8.0;net9.0";
private readonly List<string> references = new();

Check failure on line 27 in src/Tasks.IntegrationTests/Fixtures/E2eTestFixture.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022)

Feature 'target-typed object creation' is not available in C# 7.3. Please use language version 9.0 or greater.

Check failure on line 27 in src/Tasks.IntegrationTests/Fixtures/E2eTestFixture.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022)

Feature 'target-typed object creation' is not available in C# 7.3. Please use language version 9.0 or greater.
private readonly Dictionary<string, string> cpmPackageVersions = new();

Check failure on line 28 in src/Tasks.IntegrationTests/Fixtures/E2eTestFixture.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022)

Feature 'target-typed object creation' is not available in C# 7.3. Please use language version 9.0 or greater.

Check failure on line 28 in src/Tasks.IntegrationTests/Fixtures/E2eTestFixture.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022)

Feature 'target-typed object creation' is not available in C# 7.3. Please use language version 9.0 or greater.
private string tags = "cake;cake-build;build;script;addin;cake-addin;module;cake-module;recipe;cake-recipe";

public string DefaultCakeVersion => "5.0.0";
public string DefaultTargetFrameworkForModules => "net8.0";
public string DefaultTargetFrameworksForAddins => "net8.0;net9.0";

public E2eTestFixture(string tempFolder, ITestOutputHelper logger)
{
this.tempFolder = tempFolder;
Expand Down Expand Up @@ -78,6 +83,7 @@ private string WriteProject()
{
properties.Add($"<PackageTags>{tags}</PackageTags>");
}

if (hasStylecopJson)
{
var stylecopJson = Path.Combine(tempFolder, "stylecop.json");
Expand All @@ -95,16 +101,22 @@ private string WriteProject()
}
if (hasStylecopReference)
{
items.Add(@"
<PackageReference Include=""StyleCop.Analyzers"" Version=""1.1.118"">
var version = "Version=\"1.1.118\"";
if (cpmPackageVersions.Any())
{
version = string.Empty;
}

items.Add($@"
<PackageReference Include=""StyleCop.Analyzers"" {version}>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>");
}

if (hasDefaultCakeReference)
{
WithPackageReference("cake.core","1.0.0", "all");
WithPackageReference("cake.core", DefaultCakeVersion, "all");
}

if (omitRecommendedCakeVersion)
Expand All @@ -116,6 +128,32 @@ private string WriteProject()
</ItemGroup>");
}

if (cpmPackageVersions.Any())
{
if (hasStylecopReference)
{
cpmPackageVersions.Add("StyleCop.Analyzers", "1.1.118");
}

var cpmFile = Path.Combine(tempFolder, "Directory.Packages.props");
File.WriteAllText(cpmFile,
$"""

Check failure on line 140 in src/Tasks.IntegrationTests/Fixtures/E2eTestFixture.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022)

Feature 'raw string literals' is not available in C# 7.3. Please use language version 11.0 or greater.
<Project>
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
{
string.Join(
Environment.NewLine,
cpmPackageVersions.Select(kvp => $"<PackageVersion Include=\"{kvp.Key}\" Version=\"{kvp.Value}\" />").ToArray()
)
}

Check failure on line 151 in src/Tasks.IntegrationTests/Fixtures/E2eTestFixture.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022)

Newlines inside a non-verbatim interpolated string are not supported in C# 7.3. Please use language version 11.0 or greater.
</ItemGroup>
</Project>
""");
}

items.AddRange(references);

File.WriteAllText(csproj, string.Format(template,
Expand Down Expand Up @@ -157,12 +195,18 @@ internal void OmitRecommendedCakeVersion()

internal void WithPackageReference(
string packageName,
string version,
string version = null,
string privateAssets = null,
params Tuple<string, string>[] additionalAttributes)
{
var reference = new StringBuilder();
reference.Append($@"<PackageReference Include=""{packageName}"" Version=""{version}""");
reference.Append($@"<PackageReference Include=""{packageName}""");

if (version != null)
{
reference.Append($@" Version=""{version}""");
}

if (privateAssets != null)
{
reference.Append($@" PrivateAssets=""{privateAssets}""");
Expand All @@ -177,6 +221,13 @@ internal void WithPackageReference(
references.Add(reference.ToString());
}

internal void WithCpmPackageVersion(
string packageName,
string version)
{
this.cpmPackageVersions.Add(packageName, version);
}

internal void WithoutStylecopReference()
{
hasStylecopReference = false;
Expand Down
2 changes: 1 addition & 1 deletion src/Tasks.IntegrationTests/Tasks.IntegrationTests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;</TargetFrameworks>
<TargetFrameworks>net8.0;net9.0;</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)'!='Unix'">$(TargetFrameworks);net472</TargetFrameworks>
<AssemblyName>CakeContrib.Guidelines.Tasks.IntegrationTests</AssemblyName>
<RootNamespace>CakeContrib.Guidelines.Tasks.IntegrationTests</RootNamespace>
Expand Down
30 changes: 29 additions & 1 deletion src/Tasks.Tests/Fixtures/RecommendedCakeVersionFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace CakeContrib.Guidelines.Tasks.Tests.Fixtures
public class RecommendedCakeVersionFixture : BaseBuildFixture<RecommendedCakeVersion>
{
private readonly List<ITaskItem> references;
private readonly List<ITaskItem> cpmVersions;
private readonly List<ITaskItem> omitted;
private readonly List<ITaskItem> referencesToCheck;

Expand All @@ -16,6 +17,7 @@ public RecommendedCakeVersionFixture()
references = new List<ITaskItem>();
omitted = new List<ITaskItem>();
referencesToCheck = new List<ITaskItem>();
cpmVersions = new List<ITaskItem>();
Task.ProjectType = CakeProjectType.Addin.ToString();
}

Expand All @@ -24,6 +26,7 @@ public override bool Execute()
Task.Omitted = omitted.ToArray();
Task.References = references.ToArray();
Task.ReferencesToCheck = referencesToCheck.ToArray();
Task.PackageVersions = cpmVersions.ToArray();
return base.Execute();
}

Expand Down Expand Up @@ -67,7 +70,7 @@ public void WithProjectTypeRecipe()
{
Task.ProjectType = "Recipe";
}

public void WithNoWarn(params string[] rules)
{
Task.NoWarn = rules;
Expand All @@ -77,5 +80,30 @@ public void WithWarningsAsErrors(params string[] rules)
{
Task.WarningsAsErrors = rules;
}

public void WithCpmReference(string referenceName, string versionOverride = null)
{
var attributes = new Dictionary<string, string>();
if (versionOverride != null)
{
attributes.Add("VersionOverride", versionOverride);
}

var reference = GetMockTaskItem(
referenceName,
attributes);
references.Add(reference.Object);
}

public void WithCpmPackageVersion(string referenceName, string version)
{
var reference = GetMockTaskItem(
referenceName,
new Dictionary<string, string>
{
{ "version", version }
});
cpmVersions.Add(reference.Object);
}
}
}
Loading

0 comments on commit d9cca6e

Please sign in to comment.