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

Add 'dotnet new' templates for NoTargets and Traversal #529

Open
wants to merge 10 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
2 changes: 2 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
</ItemGroup>
<ItemGroup Condition="'$(IsTestProject)' == 'true'">
<PackageVersion Include="AssemblyShader" Version="1.0.3-preview" />
<PackageVersion Include="MartinCostello.Logging.XUnit" Version="0.3.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageVersion Include="Microsoft.TemplateEngine.Authoring.TemplateVerifier" Version="8.0.100" />
<PackageVersion Include="MSBuild.ProjectCreation" Version="10.0.0" />
<PackageVersion Include="Shouldly" Version="4.2.1" />
<PackageVersion Include="xunit" Version="2.6.4" />
Expand Down
12 changes: 12 additions & 0 deletions MSBuildSdks.sln
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Build.CopyOnWrite
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Build.CopyOnWrite.UnitTests", "src\CopyOnWrite.UnitTests\Microsoft.Build.CopyOnWrite.UnitTests.csproj", "{AF9F2AFE-04D4-40B3-B17F-54ABD3DE7E4E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Build.Traversal.Templates", "src\Traversal\Templates\Microsoft.Build.Traversal.Templates.csproj", "{1AEE3631-B92A-4A7F-974F-BAB420CE3913}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Build.NoTargets.Templates", "src\NoTargets\Templates\Microsoft.Build.NoTargets.Templates.csproj", "{03A56E49-A35E-4F9D-AEAE-B0257BF6F919}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -146,6 +150,14 @@ Global
{AF9F2AFE-04D4-40B3-B17F-54ABD3DE7E4E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AF9F2AFE-04D4-40B3-B17F-54ABD3DE7E4E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AF9F2AFE-04D4-40B3-B17F-54ABD3DE7E4E}.Release|Any CPU.Build.0 = Release|Any CPU
{1AEE3631-B92A-4A7F-974F-BAB420CE3913}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1AEE3631-B92A-4A7F-974F-BAB420CE3913}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1AEE3631-B92A-4A7F-974F-BAB420CE3913}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1AEE3631-B92A-4A7F-974F-BAB420CE3913}.Release|Any CPU.Build.0 = Release|Any CPU
{03A56E49-A35E-4F9D-AEAE-B0257BF6F919}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{03A56E49-A35E-4F9D-AEAE-B0257BF6F919}.Debug|Any CPU.Build.0 = Debug|Any CPU
{03A56E49-A35E-4F9D-AEAE-B0257BF6F919}.Release|Any CPU.ActiveCfg = Release|Any CPU
{03A56E49-A35E-4F9D-AEAE-B0257BF6F919}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AssemblyShader" />
<PackageReference Include="MartinCostello.Logging.XUnit"
Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))" />
<PackageReference Include="Microsoft.NET.Test.Sdk"
ShadeDependencies="NuGet.Frameworks" />
<PackageReference Include="Microsoft.TemplateEngine.Authoring.TemplateVerifier"
Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))" />
<PackageReference Include="MSBuild.ProjectCreation" />
<PackageReference Include="Newtonsoft.Json" />
<PackageReference Include="Shouldly" />
Expand All @@ -20,5 +24,8 @@
<None Include="..\NoTargets\Sdk\**\*"
Link="Sdk\%(RecursiveDir)%(Filename)%(Extension)"
CopyToOutputDirectory="PreserveNewest" />
<None Include="..\NoTargets\Templates\**"
Link="Templates\$(RelativeDir)%(RecursiveDir)%(Filename)%(Extension)"
CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
</Project>
3 changes: 3 additions & 0 deletions src/NoTargets.UnitTests/Snapshots/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This folder contains snapshots of the templates. These snapshots are used by the unit tests to validate template
instantiations. See https://github.com/dotnet/templating/blob/main/docs/authoring-tools/Templates-Testing-Tooling.md
for more information.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.Build.NoTargets">
<!--
To install the NoTargets MSBuild SDK, see https://github.com/microsoft/MSBuildSdks/tree/main?tab=readme-ov-file#how-can-i-use-these-sdks

For docs on customizing a NoTargets project, see https://github.com/microsoft/MSBuildSdks/blob/main/src/NoTargets/README.md
-->
<PropertyGroup>
<!--
Set the project type so tools can open the project regardless of file extension.
See https://github.com/microsoft/MSBuildSdks/issues/155.
-->
<ProjectTypeGuids>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>

<!--
Update as necessary; the project does not produce a binary, but the target framework may impact other project
dependencies or tools.
-->
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<!--
Replace with your custom targets and logic.

See https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-targets for general information for writing custom
targets.
-->
<Target Name="HelloWorld" BeforeTargets="AfterBuild">
<Message Text="Hello, World!" Importance="High" />
</Target>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.Build.NoTargets">
<!--
To install the NoTargets MSBuild SDK, see https://github.com/microsoft/MSBuildSdks/tree/main?tab=readme-ov-file#how-can-i-use-these-sdks

For docs on customizing a NoTargets project, see https://github.com/microsoft/MSBuildSdks/blob/main/src/NoTargets/README.md
-->
<PropertyGroup>
<!--
Set the project type so tools can open the project regardless of file extension.
See https://github.com/microsoft/MSBuildSdks/issues/155.
-->
<ProjectTypeGuids>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>

<!--
Update as necessary; the project does not produce a binary, but the target framework may impact other project
dependencies or tools.
-->
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<!--
Replace with your custom targets and logic.

See https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-targets for general information for writing custom
targets.
-->
<Target Name="HelloWorld" BeforeTargets="AfterBuild">
<Message Text="Hello, World!" Importance="High" />
</Target>
</Project>
54 changes: 54 additions & 0 deletions src/NoTargets.UnitTests/TemplateTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the MIT license.

#if NET8_0_OR_GREATER
using Microsoft.Extensions.Logging;
using Microsoft.TemplateEngine.Authoring.TemplateVerifier;
using System.IO;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;

namespace Microsoft.Build.NoTargets.UnitTests
{
public class TemplateTests
{
private static readonly string RootPath = Path.GetDirectoryName(typeof(TemplateTests).Assembly.Location);
private readonly ILoggerFactory _loggerFactory;

public TemplateTests(ITestOutputHelper xunitOutputHelper)
{
_loggerFactory = LoggerFactory.Create(config =>
{
config.AddXUnit(xunitOutputHelper);
});
}

[Fact]
public async Task UsesDirectoryAsDefaultName()
{
TemplateVerifierOptions options = new (templateName: "notargets")
{
TemplatePath = Path.Combine(RootPath, "Templates", "notargets"),
};

VerificationEngine engine = new (_loggerFactory);
await engine.Execute(options);
}

[Fact]
public async Task RespectsExplicitName()
{
TemplateVerifierOptions options = new (templateName: "notargets")
{
TemplateSpecificArgs = new[] { "--name", "asdf" },
TemplatePath = Path.Combine(RootPath, "Templates", "notargets"),
};

VerificationEngine engine = new (_loggerFactory);
await engine.Execute(options);
}
}
}
#endif
5 changes: 2 additions & 3 deletions src/NoTargets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ The `Microsoft.Build.NoTargets` MSBuild project SDK allows project tree owners t
To have a project that just copies a file:
```xml
<Project Sdk="Microsoft.Build.NoTargets">

<PropertyGroup>
<TargetFramework>net46</TargetFramework>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down Expand Up @@ -40,7 +39,7 @@ Or a project that runs a tool:
```xml
<Project Sdk="Microsoft.Build.NoTargets">
<PropertyGroup>
<TargetFramework>net46</TargetFramework>
<TargetFramework>netstandard2.0</TargetFramework>
<MyTool>mytool.exe</MyTool>
</PropertyGroup>

Expand Down
32 changes: 32 additions & 0 deletions src/NoTargets/Templates/Microsoft.Build.NoTargets.Templates.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Title>MSBuild NoTargets templates</Title>
<Description>Templates for common MSBuild NoTargets projects</Description>
<PackageTags>MSBuild NoTargets project template</PackageTags>
<ArtifactsPath>$(BaseArtifactsPath)$(MSBuildProjectName)\</ArtifactsPath>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageType>Template</PackageType>
<TargetFramework>net8.0</TargetFramework>
<IncludeContentInPack>true</IncludeContentInPack>
<IncludeBuildOutput>false</IncludeBuildOutput>
<ContentTargetFolders>content</ContentTargetFolders>
<NoWarn>$(NoWarn);NU5128</NoWarn>
<NoDefaultExcludes>true</NoDefaultExcludes>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

<PropertyGroup>
<LocalizeTemplates>false</LocalizeTemplates>
</PropertyGroup>

<ItemGroup>
<Content Include="notargets\**\*" Exclude="notargets\**\bin\**;notargets\**\obj\**" />
<Compile Remove="**\*" />
</ItemGroup>

<ItemGroup>
<None Include="README.md" Pack="true" PackagePath="" />
</ItemGroup>

</Project>
24 changes: 24 additions & 0 deletions src/NoTargets/Templates/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Microsoft.Build.Traversal.Templates

## Installing the templates

The templates are available on [NuGet](https://www.nuget.org/packages/Microsoft.Build.NoTargets.Templates/).
To install the templates, run the following command:

```bash
dotnet new -i Microsoft.Build.NoTargets.Templates
```

## Using the templates

Creating a new project with the default name

```bash
dotnet new notargets
```

Creating a new project "AdventureWorks" (`-n` or `--name`):

```bash
dotnet new notargets -n "AdventureWorks"
```
15 changes: 15 additions & 0 deletions src/NoTargets/Templates/notargets/.template.config/template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "http://json.schemastore.org/template",
"author": "Microsoft",
"classifications": [
"MSBuild"
],
"identity": "Microsoft.Build.NoTargets",
"name": "MSBuild NoTargets Project",
"description": "A starter project file for MSBuild NoTargets projects",
"shortName": "notargets",
"tags": {
"type": "project"
},
"sourceName": "NoTargets.Template"
}
30 changes: 30 additions & 0 deletions src/NoTargets/Templates/notargets/NoTargets.Template.msbuildproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.Build.NoTargets">
<!--
To install the NoTargets MSBuild SDK, see https://github.com/microsoft/MSBuildSdks/tree/main?tab=readme-ov-file#how-can-i-use-these-sdks

For docs on customizing a NoTargets project, see https://github.com/microsoft/MSBuildSdks/blob/main/src/NoTargets/README.md
-->
<PropertyGroup>
<!--
Set the project type so tools can open the project regardless of file extension.
See https://github.com/microsoft/MSBuildSdks/issues/155.
-->
<ProjectTypeGuids>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>

<!--
Update as necessary; the project does not produce a binary, but the target framework may impact other project
dependencies or tools.
-->
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<!--
Replace with your custom targets and logic.

See https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-targets for general information for writing custom
targets.
-->
<Target Name="HelloWorld" BeforeTargets="AfterBuild">
<Message Text="Hello, World!" Importance="High" />
</Target>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AssemblyShader" />
<PackageReference Include="MartinCostello.Logging.XUnit"
Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))" />
<PackageReference Include="Microsoft.NET.Test.Sdk"
ShadeDependencies="NuGet.Frameworks" />
<PackageReference Include="Microsoft.TemplateEngine.Authoring.TemplateVerifier"
Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))" />
<PackageReference Include="MSBuild.ProjectCreation" />
<PackageReference Include="Newtonsoft.Json" />
<PackageReference Include="Shouldly" />
Expand All @@ -20,5 +24,8 @@
<None Include="..\Traversal\Sdk\**"
Link="Sdk\$(RelativeDir)%(Filename)%(Extension)"
CopyToOutputDirectory="PreserveNewest" />
<None Include="..\Traversal\Templates\**"
Link="Templates\$(RelativeDir)%(RecursiveDir)%(Filename)%(Extension)"
CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
</Project>
3 changes: 3 additions & 0 deletions src/Traversal.UnitTests/Snapshots/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This folder contains snapshots of the templates. These snapshots are used by the unit tests to validate template
instantiations. See https://github.com/dotnet/templating/blob/main/docs/authoring-tools/Templates-Testing-Tooling.md
for more information.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.Build.Traversal">
<!--
To install the Traversal MSBuild SDK, see https://github.com/microsoft/MSBuildSdks/tree/main?tab=readme-ov-file#how-can-i-use-these-sdks

For docs on customizing a Traversal project, see https://github.com/microsoft/MSBuildSdks/blob/main/src/Traversal/README.md
-->

<!--
Uncomment the following lines if you use the SlnGen tool (https://microsoft.github.io/slngen/) and want to customize how solutions are generated.

<PropertyGroup>
<SlnGenProjectName>MyCustomProject</SlnGenProjectName>
<SlnGenFolders>true</SlnGenFolders>
</PropertyGroup>
-->

<ItemGroup>
<!-- Build all projects recursively under the "src" folder -->
<ProjectReference Include="src\**\*.*proj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.Build.Traversal">
<!--
To install the Traversal MSBuild SDK, see https://github.com/microsoft/MSBuildSdks/tree/main?tab=readme-ov-file#how-can-i-use-these-sdks

For docs on customizing a Traversal project, see https://github.com/microsoft/MSBuildSdks/blob/main/src/Traversal/README.md
-->

<!--
Uncomment the following lines if you use the SlnGen tool (https://microsoft.github.io/slngen/) and want to customize how solutions are generated.

<PropertyGroup>
<SlnGenProjectName>MyCustomProject</SlnGenProjectName>
<SlnGenFolders>true</SlnGenFolders>
</PropertyGroup>
-->

<ItemGroup>
<!-- Build all projects recursively under the "src" folder -->
<ProjectReference Include="src\**\*.*proj" />
</ItemGroup>
</Project>
Loading