Skip to content

Commit 5580d48

Browse files
authored
Fix NoTargets not loading properly in Visual Studio (#23)
Recent versions of Visual Studio require imports and properties otherwise it will migrate the project to a legacy project. I'm adding imports to the Microsoft.NET.Sdk and users will have to specify a TargetFramework in their projects. I also updated the README to reflect this. * Added common library for unit tests * Add unit tests for NoTargets Fixes #18 Fixes #20
1 parent 75c2553 commit 5580d48

File tree

11 files changed

+142
-14
lines changed

11 files changed

+142
-14
lines changed

MSBuildSdks.sln

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Build.CentralPack
2222
EndProject
2323
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Build.Traversal.UnitTests", "src\Traversal.UnitTests\Microsoft.Build.Traversal.UnitTests.csproj", "{48AEFD7C-4F21-4855-9EB0-75B1EB58955C}"
2424
EndProject
25+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTest.Common", "src\UnitTest.Common\UnitTest.Common.csproj", "{8B1D3EDD-EFF1-4D1A-98D5-58B799784A99}"
26+
EndProject
27+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.Build.NoTargets.UnitTests", "src\NoTargets.UnitTests\Microsoft.Build.NoTargets.UnitTests.csproj", "{FE997E79-94D9-4663-9727-ABF40B67E1CF}"
28+
EndProject
2529
Global
2630
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2731
Debug|Any CPU = Debug|Any CPU
@@ -44,6 +48,14 @@ Global
4448
{48AEFD7C-4F21-4855-9EB0-75B1EB58955C}.Debug|Any CPU.Build.0 = Debug|Any CPU
4549
{48AEFD7C-4F21-4855-9EB0-75B1EB58955C}.Release|Any CPU.ActiveCfg = Release|Any CPU
4650
{48AEFD7C-4F21-4855-9EB0-75B1EB58955C}.Release|Any CPU.Build.0 = Release|Any CPU
51+
{8B1D3EDD-EFF1-4D1A-98D5-58B799784A99}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
52+
{8B1D3EDD-EFF1-4D1A-98D5-58B799784A99}.Debug|Any CPU.Build.0 = Debug|Any CPU
53+
{8B1D3EDD-EFF1-4D1A-98D5-58B799784A99}.Release|Any CPU.ActiveCfg = Release|Any CPU
54+
{8B1D3EDD-EFF1-4D1A-98D5-58B799784A99}.Release|Any CPU.Build.0 = Release|Any CPU
55+
{FE997E79-94D9-4663-9727-ABF40B67E1CF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
56+
{FE997E79-94D9-4663-9727-ABF40B67E1CF}.Debug|Any CPU.Build.0 = Debug|Any CPU
57+
{FE997E79-94D9-4663-9727-ABF40B67E1CF}.Release|Any CPU.ActiveCfg = Release|Any CPU
58+
{FE997E79-94D9-4663-9727-ABF40B67E1CF}.Release|Any CPU.Build.0 = Release|Any CPU
4759
EndGlobalSection
4860
GlobalSection(SolutionProperties) = preSolution
4961
HideSolutionNode = FALSE
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net46</TargetFramework>
5+
<IsPackable>false</IsPackable>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.6.2" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<ProjectReference Include="..\NoTargets\Microsoft.Build.NoTargets.csproj">
14+
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
15+
</ProjectReference>
16+
<ProjectReference Include="..\UnitTest.Common\UnitTest.Common.csproj" />
17+
</ItemGroup>
18+
19+
<ItemGroup>
20+
<None Include="..\NoTargets\Sdk\Sdk.props" Link="Sdk\Sdk.props" CopyToOutputDirectory="PreserveNewest" />
21+
<None Include="..\NoTargets\Sdk\Sdk.targets" Link="Sdk\Sdk.targets" CopyToOutputDirectory="PreserveNewest" />
22+
</ItemGroup>
23+
24+
</Project>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Linq;
2+
using Microsoft.Build.Framework;
3+
using Microsoft.Build.Traversal.UnitTests;
4+
using Microsoft.Build.Utilities.ProjectCreation;
5+
using Shouldly;
6+
using Xunit;
7+
8+
namespace Microsoft.Build.NoTargets.UnitTests
9+
{
10+
public class NoTargetsTests : MSBuildSdkTestBase
11+
{
12+
[Fact]
13+
public void SimpleBuild()
14+
{
15+
ProjectCreator.Templates.NoTargetsProject(
16+
path: GetTempFileWithExtension(".csproj"),
17+
customAction: creator =>
18+
{
19+
creator.Target("TakeAction", afterTargets: "Build")
20+
.TaskMessage("86F00AF59170450E9D687652D74A6394", MessageImportance.High);
21+
})
22+
.Property("GenerateDependencyFile", "false")
23+
.Save()
24+
.TryBuild("Build", out bool result, out BuildOutput buildOutput);
25+
26+
result.ShouldBeTrue(() => buildOutput.GetConsoleLog());
27+
28+
buildOutput
29+
.MessagesHighImportance
30+
.Select(i => i.Message)
31+
.ToList()
32+
.ShouldContain("86F00AF59170450E9D687652D74A6394");
33+
}
34+
}
35+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Microsoft.Build.Evaluation;
2+
using Microsoft.Build.Utilities.ProjectCreation;
3+
using System;
4+
using System.IO;
5+
6+
namespace Microsoft.Build.NoTargets.UnitTests
7+
{
8+
public static class CustomProjectCreatorTemplates
9+
{
10+
public static ProjectCreator NoTargetsProject(
11+
this ProjectCreatorTemplates templates,
12+
Action<ProjectCreator> customAction = null,
13+
string path = null,
14+
string defaultTargets = null,
15+
string initialTargets = null,
16+
string sdk = null,
17+
string toolsVersion = null,
18+
string treatAsLocalProperty = null,
19+
ProjectCollection projectCollection = null,
20+
NewProjectFileOptions? projectFileOptions = NewProjectFileOptions.None)
21+
{
22+
string currentDirectory = Environment.CurrentDirectory;
23+
24+
return ProjectCreator.Create(
25+
path,
26+
defaultTargets,
27+
initialTargets,
28+
sdk,
29+
toolsVersion,
30+
treatAsLocalProperty,
31+
projectCollection,
32+
projectFileOptions)
33+
.Import(Path.Combine(currentDirectory, "Sdk", "Sdk.props"))
34+
.Property("TargetFramework", "netstandard1.0")
35+
.CustomAction(customAction)
36+
.Import(Path.Combine(currentDirectory, "Sdk", "Sdk.targets"));
37+
}
38+
}
39+
}

src/NoTargets/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ The `Microsoft.Build.NoTargets` MSBuild project SDK allows project tree owners t
99
To have a project that just copies a file:
1010
```xml
1111
<Project Sdk="Microsoft.Build.NoTargets">
12+
13+
<PropertyGroup>
14+
<TargetFramework>net46</TargetFramework>
15+
</PropertyGroup>
16+
1217
<ItemGroup>
1318
<FilesToCopy Include="files\**" />
1419
</ItemGroup>
@@ -34,6 +39,7 @@ Or a project that runs a tool:
3439
```xml
3540
<Project Sdk="Microsoft.Build.NoTargets">
3641
<PropertyGroup>
42+
<TargetFramework>net46</TargetFramework>
3743
<MyTool>mytool.exe</MyTool>
3844
</PropertyGroup>
3945

src/NoTargets/Sdk/Sdk.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<Import Project="$(CustomBeforeNoTargetsProps)" Condition=" '$(CustomBeforeNoTargetsProps)' != '' And Exists('$(CustomBeforeNoTargetsProps)') " />
1010

11-
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition=" '$(MicrosoftCommonPropsHasBeenImported)' != 'true' And Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props') "/>
11+
<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" Condition=" '$(MicrosoftCommonPropsHasBeenImported)' != 'true' "/>
1212

1313
<Import Project="$(CustomAfterNoTargetsProps)" Condition=" '$(CustomAfterNoTargetsProps)' != '' And Exists('$(CustomAfterNoTargetsProps)') " />
1414
</Project>

src/NoTargets/Sdk/Sdk.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<Import Project="$(CustomBeforeNoTargets)" Condition="'$(CustomBeforeNoTargets)' != '' and Exists('$(CustomBeforeNoTargets)')" />
1010

11-
<Import Project="$(MSBuildToolsPath)\Microsoft.Common.targets" Condition=" Exists('$(MSBuildToolsPath)\Microsoft.Common.targets') " />
11+
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" Condition=" '$(CommonTargetsPath)' == '' " />
1212

1313
<ItemGroup>
1414
<IntermediateAssembly Remove="@(IntermediateAssembly)" />

src/Traversal.UnitTests/Microsoft.Build.Traversal.UnitTests.csproj

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,19 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<None Include="..\Traversal\Sdk\Sdk.props" Link="Sdk\Sdk.props" CopyToOutputDirectory="PreserveNewest" />
10-
<None Include="..\Traversal\Sdk\Sdk.targets" Link="Sdk\Sdk.targets" CopyToOutputDirectory="PreserveNewest" />
11-
</ItemGroup>
12-
13-
<ItemGroup>
14-
<PackageReference Include="Microsoft.Build.Locator" Version="1.0.7-preview-ge60d679b53" />
15-
<PackageReference Include="MSBuild.ProjectCreation" Version="1.0.6" />
16-
<PackageReference Include="Microsoft.Build" Version="15.6.82" ExcludeAssets="runtime" />
17-
<PackageReference Include="Microsoft.Build.Framework" Version="15.6.82" ExcludeAssets="runtime" />
189
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.6.2" />
19-
<PackageReference Include="Shouldly" Version="3.0.0" />
20-
<PackageReference Include="xunit" Version="2.3.1" />
21-
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
2210
</ItemGroup>
2311

2412
<ItemGroup>
2513
<ProjectReference Include="..\Traversal\Microsoft.Build.Traversal.csproj">
2614
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
2715
</ProjectReference>
16+
<ProjectReference Include="..\UnitTest.Common\UnitTest.Common.csproj" />
17+
</ItemGroup>
18+
19+
<ItemGroup>
20+
<None Include="..\Traversal\Sdk\Sdk.props" Link="Sdk\Sdk.props" CopyToOutputDirectory="PreserveNewest" />
21+
<None Include="..\Traversal\Sdk\Sdk.targets" Link="Sdk\Sdk.targets" CopyToOutputDirectory="PreserveNewest" />
2822
</ItemGroup>
2923

3024
</Project>
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)