Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Csproj] Add support for conditional PackageReferences #258

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
33 changes: 33 additions & 0 deletions Sharpmake.UnitTests/PackageReferencesTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Ubisoft. All Rights Reserved.
// Licensed under the Apache 2.0 License. See LICENSE.md in the project root for license information.

using System;
using System.Linq;
using NUnit.Framework;

Expand Down Expand Up @@ -61,6 +62,27 @@ public void InheritedPrivatePackageReferencesToPublicBecomePublic()
Assert.True(configuration.ReferencesByNuGetPackage.SortedValues.Any(item => item.PrivateAssets == PackageReferences.DefaultPrivateAssets && item.Name == "NUnit"));
}
}

[Test]
public void PackageReferencesSupportConditions()
{
var project = GetProject<PackageReferencesTestProjects.ConditionalNuGetPackage>();
Assert.IsNotNull(project);
foreach (var configuration in project.Configurations)
{
Assert.AreEqual(2, configuration.ReferencesByNuGetPackage.Count);
CheckForPackageCondition("NUnit", "$(Configuration)='Release'");
CheckForPackageCondition("NUnit-debug", "$(Configuration)='Debug'");

void CheckForPackageCondition(string packageName, string expectedCondition)
{
Assert.AreEqual(
configuration.ReferencesByNuGetPackage.SortedValues
.First(v => string.Equals(v.Name, packageName, StringComparison.Ordinal)).Condition,
expectedCondition);
}
}
}
}

namespace PackageReferencesTestProjects
Expand Down Expand Up @@ -127,5 +149,16 @@ public override void ConfigureAll(Configuration conf, Target target)
conf.ReferencesByNuGetPackage.Add("NUnit", "3.4.1");
}
}

[Generate]
public class ConditionalNuGetPackage : CSharpUnitTestCommonProject
{
[Configure()]
public virtual void ConfigureAll(Configuration conf, Target target)
{
conf.ReferencesByNuGetPackage.Add("NUnit", "3.4.1", condition: "$(Configuration)='Release'" );
conf.ReferencesByNuGetPackage.Add("NUnit-debug", "3.4.1", condition: "$(Configuration)='Debug'" );
}
}
}
}
2 changes: 1 addition & 1 deletion Sharpmake/PackageReferences.Template.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public partial class PackageReferences
/// <remarks>
/// See : https://docs.microsoft.com/en-us/nuget/consume-packages/package-references-in-project-files#controlling-dependency-assets
/// </remarks>
private const string TemplateBeginPackageReference = " <PackageReference Include=\"[packageName]\" Version=\"[packageVersion]\"";
private const string TemplateBeginPackageReference = " <PackageReference Include=\"[packageName]\" Version=\"[packageVersion]\"[packageCondition]";
private const string TemplatePackagePrivateAssets = " <PrivateAssets>[privateAssets]</PrivateAssets>\n";
private const string TemplateEndPackageReference = " </PackageReference>\n";
}
Expand Down
26 changes: 21 additions & 5 deletions Sharpmake/PackageReferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,34 @@ public partial class PackageReferences
[DebuggerDisplay("{Name} {Version}")]
public class PackageReference : IResolverHelper, IComparable<PackageReference>
{
internal PackageReference(string name, string version, string dotNetHint, AssetsDependency privateAssets, string referenceType)
internal PackageReference(string name, string version, string dotNetHint, AssetsDependency privateAssets, string referenceType, string condition)
{
Name = name;
Version = version;
DotNetHint = dotNetHint;
PrivateAssets = privateAssets;
ReferenceType = referenceType;
Condition = condition;
}

internal PackageReference(string name, string version, string dotNetHint, AssetsDependency privateAssets)
: this(name, version, dotNetHint, privateAssets, null)
: this(name, version, dotNetHint, privateAssets, null, null)
{
}

public string Name { get; internal set; }
public string Version { get; internal set; }
public string DotNetHint { get; internal set; }
public string ReferenceType { get; internal set; }
public string Condition { get; internal set; }

public AssetsDependency PrivateAssets { get; internal set; }

public string Resolve(Resolver resolver)
{
using (resolver.NewScopedParameter("packageName", Name))
using (resolver.NewScopedParameter("packageVersion", Version))
using (resolver.NewScopedParameter("packageCondition", GetConditionParameter() ))
{
if (PrivateAssets == DefaultPrivateAssets)
{
Expand All @@ -60,6 +63,7 @@ public string Resolve(Resolver resolver, string customTemplate)
{
using (resolver.NewScopedParameter("packageName", Name))
using (resolver.NewScopedParameter("packageVersion", Version))
using (resolver.NewScopedParameter("packageCondition", GetConditionParameter()))
{
return resolver.Resolve(customTemplate);
}
Expand Down Expand Up @@ -137,17 +141,22 @@ internal static IEnumerable<string> GetFormatedAssetsDependency(AssetsDependency
yield return "buildTransitive";
}
}

private string GetConditionParameter()
{
return string.IsNullOrWhiteSpace(Condition) ? string.Empty : $" Condition=\"{Condition}\"";
}
}

private readonly UniqueList<PackageReference> _packageReferences = new UniqueList<PackageReference>();

public void Add(string packageName, string version, string dotNetHint = null, AssetsDependency privateAssets = DefaultPrivateAssets, string referenceType = null)
public void Add(string packageName, string version, string dotNetHint = null, AssetsDependency privateAssets = DefaultPrivateAssets, string referenceType = null, string condition = null)
{
// check package unicity
var existingPackage = _packageReferences.FirstOrDefault(pr => pr.Name == packageName);
if (existingPackage == null)
{
_packageReferences.Add(new PackageReference(packageName, version, null, privateAssets, referenceType));
_packageReferences.Add(new PackageReference(packageName, version, null, privateAssets, referenceType, condition ));
return;
}

Expand All @@ -162,11 +171,18 @@ public void Add(string packageName, string version, string dotNetHint = null, As
existingPackage.PrivateAssets &= privateAssets;
Builder.Instance.LogWarningLine($"Package {packageName} was added twice with different private assets. Kept assets are {string.Join(",", PackageReference.GetFormatedAssetsDependency(existingPackage.PrivateAssets))}.");
}

if (!string.Equals(condition, existingPackage.Condition, StringComparison.Ordinal))
{
existingPackage.Condition = condition;
Builder.Instance.LogWarningLine(
$"Package {packageName} was added twice with different conditions. Condition '{condition}' will be used.");
}
}

public void Add(string packageName, string version, string dotNetHint, AssetsDependency privateAssets)
{
Add(packageName, version, dotNetHint, privateAssets, null);
Add(packageName, version, dotNetHint, privateAssets, null, null);
}

public int Count => _packageReferences.Count;
Expand Down