Skip to content

Commit

Permalink
Support .NET 5 SDK by importing its props and targets (#213)
Browse files Browse the repository at this point in the history
Previous versions of the .NET SDK did not require you to import them to get some of their build logic.  More and more of the logic is being moved out of what ships with MSBuild and into what ships in the SDK.  To that end, Traversal should import the Microsoft.NET.Sdk and just extend it.

Get all unit tests working on all target frameworks (net472, netcoreapp3.1, net5.0)
  • Loading branch information
jeffkl authored Nov 11, 2020
1 parent 5933d25 commit 6d33372
Show file tree
Hide file tree
Showing 20 changed files with 151 additions and 53 deletions.
5 changes: 5 additions & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
Visible="false" />
</ItemGroup>

<ItemGroup Condition="'$(IsTestProject)' == 'true'">
<Content Include="$(MSBuildThisFileDirectory)xunit.runner.json"
CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Update="@(InternalsVisibleTo)" Condition="'$(SignType)' == 'Test' Or '$(SignType)' == 'Real'" Key="002400000480000094000000060200000024000052534131000400000100010015c01ae1f50e8cc09ba9eac9147cf8fd9fce2cfe9f8dce4f7301c4132ca9fb50ce8cbf1df4dc18dd4d210e4345c744ecb3365ed327efdbc52603faa5e21daa11234c8c4a73e51f03bf192544581ebe107adee3a34928e39d04e524a9ce729d5090bfd7dad9d10c722c0def9ccc08ff0a03790e48bcd1f9b6c476063e1966a1c4" />
</ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions MSBuildSdks.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{DC479DC1
Packages.props = Packages.props
stylecop.json = stylecop.json
version.json = version.json
xunit.runner.json = xunit.runner.json
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Build.NoTargets", "src\NoTargets\Microsoft.Build.NoTargets.csproj", "{86A02D27-6A67-461B-931C-96051F363CAD}"
Expand Down
20 changes: 17 additions & 3 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,25 @@ jobs:
arguments: '$(MSBuildArgs)'

- task: DotNetCoreCLI@2
displayName: 'Run Unit Tests'
displayName: 'Run Unit Tests (.NET 5)'
inputs:
command: 'test'
arguments: '--no-restore --no-build "/restore:false"'
testRunTitle: 'Unit Tests'
arguments: '--no-restore --no-build --framework net5.0 "/restore:false"'
testRunTitle: '.NET v5.0'

- task: DotNetCoreCLI@2
displayName: 'Run Unit Tests (.NET Framework)'
inputs:
command: 'test'
arguments: '--no-restore --no-build --framework net472 "/restore:false"'
testRunTitle: '.NET Framework v4.7.2'

- task: DotNetCoreCLI@2
displayName: 'Run Unit Tests (.NET Core)'
inputs:
command: 'test'
arguments: '--no-restore --no-build --framework netcoreapp3.1 "/restore:false"'
testRunTitle: '.NET Core v3.1'

- task: PublishBuildArtifacts@1
displayName: 'Publish Artifacts'
Expand Down
24 changes: 13 additions & 11 deletions src/Artifacts.UnitTests/CustomProjectCreatorTemplates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//
// Licensed under the MIT license.

using Microsoft.Build.Artifacts.Tasks;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Utilities.ProjectCreation;
using System;
Expand All @@ -12,8 +13,9 @@ namespace Microsoft.Build.Artifacts.UnitTests
{
internal static class CustomProjectCreatorTemplates
{
private static readonly string ArtifactsTaskAssembly = Path.Combine(Environment.CurrentDirectory, "Microsoft.Build.Artifacts.dll");
private static readonly string CurrentDirectory = Environment.CurrentDirectory;
private static readonly string ArtifactsTaskAssembly = typeof(Robocopy).Assembly.Location;

private static readonly string ThisAssemblyDirectory = Path.GetDirectoryName(ArtifactsTaskAssembly);

public static ProjectCreator MultiTargetingProjectWithArtifacts(
this ProjectCreatorTemplates templates,
Expand All @@ -28,12 +30,12 @@ public static ProjectCreator MultiTargetingProjectWithArtifacts(
path: path,
projectCreator: creator => creator
.Property("ArtifactsTaskAssembly", ArtifactsTaskAssembly)
.Import(Path.Combine(CurrentDirectory, "build", "Microsoft.Build.Artifacts.props"), condition: "'$(TargetFramework)' != ''")
.Import(Path.Combine(CurrentDirectory, "buildMultiTargeting", "Microsoft.Build.Artifacts.props"), condition: "'$(TargetFramework)' == ''")
.Property("ArtifactsPath", artifactsPath.FullName)
.Import(Path.Combine(ThisAssemblyDirectory, "build", "Microsoft.Build.Artifacts.props"), condition: "'$(TargetFramework)' != ''")
.Import(Path.Combine(ThisAssemblyDirectory, "buildMultiTargeting", "Microsoft.Build.Artifacts.props"), condition: "'$(TargetFramework)' == ''")
.Property("ArtifactsPath", artifactsPath?.FullName)
.CustomAction(customAction)
.Import(Path.Combine(CurrentDirectory, "build", "Microsoft.Build.Artifacts.targets"), condition: "'$(TargetFramework)' != ''")
.Import(Path.Combine(CurrentDirectory, "buildMultiTargeting", "Microsoft.Build.Artifacts.targets"), condition: "'$(TargetFramework)' == ''"));
.Import(Path.Combine(ThisAssemblyDirectory, "build", "Microsoft.Build.Artifacts.targets"), condition: "'$(TargetFramework)' != ''")
.Import(Path.Combine(ThisAssemblyDirectory, "buildMultiTargeting", "Microsoft.Build.Artifacts.targets"), condition: "'$(TargetFramework)' == ''"));
}

public static ProjectCreator ProjectWithArtifacts(
Expand Down Expand Up @@ -62,7 +64,7 @@ public static ProjectCreator ProjectWithArtifacts(
projectCollection,
projectFileOptions)
.Property("ArtifactsTaskAssembly", ArtifactsTaskAssembly)
.Import(Path.Combine(CurrentDirectory, "build", "Microsoft.Build.Artifacts.props"))
.Import(Path.Combine(ThisAssemblyDirectory, "build", "Microsoft.Build.Artifacts.props"))
.Property("TargetFramework", targetFramework)
.Property("OutputPath", outputPath == null ? null : $"{outputPath.TrimEnd('\\')}\\")
.Property("AppendTargetFrameworkToOutputPath", appendTargetFrameworkToOutputPath.HasValue ? appendTargetFrameworkToOutputPath.ToString() : null)
Expand All @@ -71,7 +73,7 @@ public static ProjectCreator ProjectWithArtifacts(
.CustomAction(customAction)
.Target("Build")
.Target("AfterBuild", afterTargets: "Build")
.Import(Path.Combine(CurrentDirectory, "build", "Microsoft.Build.Artifacts.targets"));
.Import(Path.Combine(ThisAssemblyDirectory, "build", "Microsoft.Build.Artifacts.targets"));
}

public static ProjectCreator SdkProjectWithArtifacts(
Expand Down Expand Up @@ -100,7 +102,7 @@ public static ProjectCreator SdkProjectWithArtifacts(
projectCollection,
projectFileOptions)
.Property("ArtifactsTaskAssembly", ArtifactsTaskAssembly)
.Import(Path.Combine(CurrentDirectory, "Sdk", "Sdk.props"))
.Import(Path.Combine(ThisAssemblyDirectory, "Sdk", "Sdk.props"))
.Property("TargetFramework", targetFramework)
.Property("OutputPath", $"{outputPath.TrimEnd('\\')}\\")
.Property("AppendTargetFrameworkToOutputPath", appendTargetFrameworkToOutputPath.HasValue ? appendTargetFrameworkToOutputPath.ToString() : null)
Expand All @@ -109,7 +111,7 @@ public static ProjectCreator SdkProjectWithArtifacts(
.CustomAction(customAction)
.Target("Build")
.Target("AfterBuild", afterTargets: "Build")
.Import(Path.Combine(CurrentDirectory, "Sdk", "Sdk.targets"));
.Import(Path.Combine(ThisAssemblyDirectory, "Sdk", "Sdk.targets"));
}
}
}
36 changes: 19 additions & 17 deletions src/CentralPackageVersions.UnitTests/CentralPackageVersionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ namespace Microsoft.Build.CentralPackageVersions.UnitTests
{
public class CentralPackageVersionsTests : MSBuildSdkTestBase
{
private static readonly string ThisAssemblyDirectory = Path.GetDirectoryName(typeof(CustomProjectCreatorTemplates).Assembly.Location);

[Theory]
[InlineData(true, ".csproj")]
[InlineData(true, ".sfproj")]
Expand All @@ -40,7 +42,7 @@ public void CanBeExplicitlyEnabled(bool createPackagesConfig, string extension)
}),
projectCreator: creator => creator
.Property("EnableCentralPackageVersions", "true")
.Import(Path.Combine(Environment.CurrentDirectory, @"Sdk\Sdk.targets")))
.Import(Path.Combine(ThisAssemblyDirectory, @"Sdk\Sdk.targets")))
.TryGetPropertyValue("EnableCentralPackageVersions", out string enableCentralPackageVersions);

enableCentralPackageVersions.ShouldBe("true");
Expand All @@ -64,7 +66,7 @@ public void CanDisableCentralPackageVersions(string projectFileExtension)
}),
projectCreator: creator => creator
.ItemPackageReference("Foo", "10.0.0")
.Import(Path.Combine(Environment.CurrentDirectory, @"Sdk\Sdk.targets")))
.Import(Path.Combine(ThisAssemblyDirectory, @"Sdk\Sdk.targets")))
.TryBuild("CheckPackageReferences", out bool result, out BuildOutput buildOutput)
.Project
.GetItems("PackageReference")
Expand Down Expand Up @@ -96,7 +98,7 @@ public void CanDisableGlobalPackageReferences(string projectFileExtension)
}),
projectCreator: creator => creator
.ItemPackageReference("Foo")
.Import(Path.Combine(Environment.CurrentDirectory, @"Sdk\Sdk.targets")))
.Import(Path.Combine(ThisAssemblyDirectory, @"Sdk\Sdk.targets")))
.TryBuild("CheckPackageReferences", out bool result, out BuildOutput buildOutput)
.Project
.GetItems("PackageReference")
Expand Down Expand Up @@ -134,7 +136,7 @@ public void CanOverridePackageVersion(string projectFileExtension)
{
["VersionOverride"] = "9.0.1",
})
.Import(Path.Combine(Environment.CurrentDirectory, @"Sdk\Sdk.targets")))
.Import(Path.Combine(ThisAssemblyDirectory, @"Sdk\Sdk.targets")))
.TryBuild("CheckPackageReferences", out bool result, out BuildOutput buildOutput)
.Project
.GetItems("PackageReference")
Expand Down Expand Up @@ -165,7 +167,7 @@ public void FSharpCorePackageReferenceCanBeDisabled()
}),
targetFramework: "net46",
projectCreator: creator => creator
.Import(Path.Combine(Environment.CurrentDirectory, @"Sdk\Sdk.targets")))
.Import(Path.Combine(ThisAssemblyDirectory, @"Sdk\Sdk.targets")))
.TryGetItems("PackageReference", out IReadOnlyCollection<ProjectItem> items);

items.Where(i => i.EvaluatedInclude.Equals("FSharp.Core"))
Expand All @@ -183,7 +185,7 @@ public void FSharpCorePackageReferenceNoSystemValueTupleForNetStandardProjects()
.SdkCsproj(
path: Path.Combine(TestRootPath, "test.fsproj"),
projectCreator: creator => creator
.Import(Path.Combine(Environment.CurrentDirectory, @"Sdk\Sdk.targets")))
.Import(Path.Combine(ThisAssemblyDirectory, @"Sdk\Sdk.targets")))
.TryGetItems("PackageReference", out IReadOnlyCollection<ProjectItem> items);

items.Where(i => i.EvaluatedInclude.Equals("FSharp.Core"))
Expand All @@ -204,7 +206,7 @@ public void FSharpCorePackageReferenceUpdated()
path: Path.Combine(TestRootPath, "test.fsproj"),
targetFramework: "net46",
projectCreator: creator => creator
.Import(Path.Combine(Environment.CurrentDirectory, @"Sdk\Sdk.targets")))
.Import(Path.Combine(ThisAssemblyDirectory, @"Sdk\Sdk.targets")))
.TryGetItems("PackageReference", out IReadOnlyCollection<ProjectItem> items);

items.Where(i => i.EvaluatedInclude.Equals("FSharp.Core"))
Expand Down Expand Up @@ -241,7 +243,7 @@ public void IsDisabledForProjectsWithPackagesConfigOrDoNotSupportPackageReferenc
["DisableImplicitFrameworkReferences"] = "true",
}),
projectCreator: creator => creator
.Import(Path.Combine(Environment.CurrentDirectory, @"Sdk\Sdk.targets")))
.Import(Path.Combine(ThisAssemblyDirectory, @"Sdk\Sdk.targets")))
.TryGetPropertyValue("EnableCentralPackageVersions", out string enableCentralPackageVersions);

enableCentralPackageVersions.ShouldBe("false");
Expand All @@ -261,7 +263,7 @@ public void LogErrorIfProjectSpecifiesGlobalPackageReference(string projectFileE
projectCreator: creator => creator
.ItemPackageReference("Foo")
.ItemPackageReference("Global1")
.Import(Path.Combine(Environment.CurrentDirectory, @"Sdk\Sdk.targets")))
.Import(Path.Combine(ThisAssemblyDirectory, @"Sdk\Sdk.targets")))
.TryBuild("CheckPackageReferences", out bool result, out BuildOutput buildOutput);

result.ShouldBeFalse(() => buildOutput.GetConsoleLog());
Expand All @@ -283,7 +285,7 @@ public void LogErrorIfProjectSpecifiesUnknownPackage(string projectFileExtension
projectCreator: creator => creator
.ItemPackageReference("Foo")
.ItemPackageReference("Baz")
.Import(Path.Combine(Environment.CurrentDirectory, @"Sdk\Sdk.targets")))
.Import(Path.Combine(ThisAssemblyDirectory, @"Sdk\Sdk.targets")))
.TryBuild("CheckPackageReferences", out bool result, out BuildOutput buildOutput);

result.ShouldBeFalse(() => buildOutput.GetConsoleLog());
Expand All @@ -304,7 +306,7 @@ public void LogErrorIfProjectSpecifiesVersion(string projectFileExtension)
path: Path.Combine(TestRootPath, $"test.{projectFileExtension}"),
projectCreator: creator => creator
.ItemPackageReference("Foo", "10.0.0")
.Import(Path.Combine(Environment.CurrentDirectory, @"Sdk\Sdk.targets")))
.Import(Path.Combine(ThisAssemblyDirectory, @"Sdk\Sdk.targets")))
.TryBuild("CheckPackageReferences", out bool result, out BuildOutput buildOutput);

result.ShouldBeFalse(() => buildOutput.GetConsoleLog());
Expand All @@ -330,7 +332,7 @@ public void LogErrorIfProjectSpecifiesVersionAndVersionOverrideIsDisabled(string
}),
projectCreator: creator => creator
.ItemPackageReference("Foo", "10.0.0")
.Import(Path.Combine(Environment.CurrentDirectory, @"Sdk\Sdk.targets")))
.Import(Path.Combine(ThisAssemblyDirectory, @"Sdk\Sdk.targets")))
.TryBuild("CheckPackageReferences", out bool result, out BuildOutput buildOutput);

result.ShouldBeFalse(() => buildOutput.GetConsoleLog());
Expand All @@ -350,7 +352,7 @@ public void MicrosoftAspNetCoreAllUpdated()
sdk: "Microsoft.NET.Sdk.Web",
projectCreator: creator => creator
.ItemPackageReference("Microsoft.AspNetCore.All")
.Import(Path.Combine(Environment.CurrentDirectory, @"Sdk\Sdk.targets")))
.Import(Path.Combine(ThisAssemblyDirectory, @"Sdk\Sdk.targets")))
.TryGetItems("PackageReference", out IReadOnlyCollection<ProjectItem> items);

items.Where(i => i.EvaluatedInclude.Equals("Microsoft.AspNetCore.All"))
Expand All @@ -371,7 +373,7 @@ public void MicrosoftAspNetCoreAppUpdated()
sdk: "Microsoft.NET.Sdk.Web",
projectCreator: creator => creator
.ItemPackageReference("Microsoft.AspNetCore.App")
.Import(Path.Combine(Environment.CurrentDirectory, @"Sdk\Sdk.targets")))
.Import(Path.Combine(ThisAssemblyDirectory, @"Sdk\Sdk.targets")))
.TryGetItems("PackageReference", out IReadOnlyCollection<ProjectItem> items);

items.Where(i => i.EvaluatedInclude.Equals("Microsoft.AspNetCore.App"))
Expand All @@ -398,7 +400,7 @@ public void PackageVersionsAreApplied(string projectFileExtension)
projectCreator: creator => creator
.ItemPackageReference("Foo")
.ItemPackageReference("Bar")
.Import(Path.Combine(Environment.CurrentDirectory, @"Sdk\Sdk.targets")))
.Import(Path.Combine(ThisAssemblyDirectory, @"Sdk\Sdk.targets")))
.Project
.GetItems("PackageReference")
.Where(i => !i.EvaluatedInclude.Equals("FSharp.Core"))
Expand Down Expand Up @@ -434,7 +436,7 @@ public void VersionOverridesWithoutCentralVersionsAreAllowed(string projectFileE
{
["VersionOverride"] = "1.0.0",
})
.Import(Path.Combine(Environment.CurrentDirectory, @"Sdk\Sdk.targets")))
.Import(Path.Combine(ThisAssemblyDirectory, @"Sdk\Sdk.targets")))
.TryBuild("CheckPackageReferences", out bool result, out BuildOutput buildOutput);

result.ShouldBeTrue(() => buildOutput.GetConsoleLog());
Expand Down Expand Up @@ -463,7 +465,7 @@ public void VersionOverridesWithoutCentralVersionsCanBeDisabled(string projectFi
{
["VersionOverride"] = "1.0.0",
})
.Import(Path.Combine(Environment.CurrentDirectory, @"Sdk\Sdk.targets")))
.Import(Path.Combine(ThisAssemblyDirectory, @"Sdk\Sdk.targets")))
.TryBuild("CheckPackageReferences", out bool result, out BuildOutput buildOutput);

result.ShouldBeFalse(() => buildOutput.GetConsoleLog());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<TargetFrameworks>net472;netcoreapp3.1;net5.0</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.Build.NoTargets">
<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<TargetFrameworks>net472;netcoreapp3.1;net5.0</TargetFrameworks>
<Description>Provides the ability to centrally manage your NuGet package versions when using PackageReference.</Description>
<PackageTags>MSBuild MSBuildSdk</PackageTags>
<ArtifactsPath>$(BaseArtifactsPath)\$(MSBuildProjectName)\</ArtifactsPath>
Expand Down
8 changes: 4 additions & 4 deletions src/NoTargets.UnitTests/CustomProjectCreatorTemplates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace Microsoft.Build.NoTargets.UnitTests
{
public static class CustomProjectCreatorTemplates
{
private static readonly string ThisAssemblyDirectory = Path.GetDirectoryName(typeof(CustomProjectCreatorTemplates).Assembly.Location);

public static ProjectCreator NoTargetsProject(
this ProjectCreatorTemplates templates,
Action<ProjectCreator> customAction = null,
Expand All @@ -26,8 +28,6 @@ public static ProjectCreator NoTargetsProject(
IDictionary<string, string> globalProperties = null,
NewProjectFileOptions? projectFileOptions = NewProjectFileOptions.None)
{
string currentDirectory = Environment.CurrentDirectory;

return ProjectCreator.Create(
path,
defaultTargets,
Expand All @@ -38,10 +38,10 @@ public static ProjectCreator NoTargetsProject(
projectCollection,
projectFileOptions,
globalProperties)
.Import(Path.Combine(currentDirectory, "Sdk", "Sdk.props"))
.Import(Path.Combine(ThisAssemblyDirectory, "Sdk", "Sdk.props"))
.Property("TargetFramework", targetFramework)
.CustomAction(customAction)
.Import(Path.Combine(currentDirectory, "Sdk", "Sdk.targets"));
.Import(Path.Combine(ThisAssemblyDirectory, "Sdk", "Sdk.targets"));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<TargetFrameworks>net472;netcoreapp3.1;net5.0</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
Expand Down
Loading

0 comments on commit 6d33372

Please sign in to comment.