diff --git a/Directory.Packages.props b/Directory.Packages.props index 9d9dcb1b..7d8fcade 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -33,7 +33,9 @@ + + diff --git a/MSBuildSdks.sln b/MSBuildSdks.sln index edc0f6e7..25e27bae 100644 --- a/MSBuildSdks.sln +++ b/MSBuildSdks.sln @@ -90,6 +90,9 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.Build.Cargo", "sr EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.Build.Cargo.UnitTests", "src\Cargo.UnitTests\Microsoft.Build.Cargo.UnitTests.csproj", "{D6EF1644-D06C-4877-A8F7-3543E5D3175B}" 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}" Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -164,6 +167,14 @@ Global {D6EF1644-D06C-4877-A8F7-3543E5D3175B}.Debug|Any CPU.Build.0 = Debug|Any CPU {D6EF1644-D06C-4877-A8F7-3543E5D3175B}.Release|Any CPU.ActiveCfg = Release|Any CPU {D6EF1644-D06C-4877-A8F7-3543E5D3175B}.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 diff --git a/src/NoTargets.UnitTests/Microsoft.Build.NoTargets.UnitTests.csproj b/src/NoTargets.UnitTests/Microsoft.Build.NoTargets.UnitTests.csproj index 6ff89f99..3781fa44 100644 --- a/src/NoTargets.UnitTests/Microsoft.Build.NoTargets.UnitTests.csproj +++ b/src/NoTargets.UnitTests/Microsoft.Build.NoTargets.UnitTests.csproj @@ -4,8 +4,12 @@ + + @@ -21,5 +25,8 @@ + diff --git a/src/NoTargets.UnitTests/Snapshots/README.md b/src/NoTargets.UnitTests/Snapshots/README.md new file mode 100644 index 00000000..e59fcf69 --- /dev/null +++ b/src/NoTargets.UnitTests/Snapshots/README.md @@ -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. \ No newline at end of file diff --git a/src/NoTargets.UnitTests/Snapshots/RespectsExplicitName.notargets.--name#asdf.verified/notargets/asdf.csproj b/src/NoTargets.UnitTests/Snapshots/RespectsExplicitName.notargets.--name#asdf.verified/notargets/asdf.csproj new file mode 100644 index 00000000..76fc4a51 --- /dev/null +++ b/src/NoTargets.UnitTests/Snapshots/RespectsExplicitName.notargets.--name#asdf.verified/notargets/asdf.csproj @@ -0,0 +1,24 @@ + + + + + netstandard2.0 + + + + + + + diff --git a/src/NoTargets.UnitTests/Snapshots/UsesDirectoryAsDefaultName.notargets._.verified/notargets/notargets.csproj b/src/NoTargets.UnitTests/Snapshots/UsesDirectoryAsDefaultName.notargets._.verified/notargets/notargets.csproj new file mode 100644 index 00000000..76fc4a51 --- /dev/null +++ b/src/NoTargets.UnitTests/Snapshots/UsesDirectoryAsDefaultName.notargets._.verified/notargets/notargets.csproj @@ -0,0 +1,24 @@ + + + + + netstandard2.0 + + + + + + + diff --git a/src/NoTargets.UnitTests/TemplateTests.cs b/src/NoTargets.UnitTests/TemplateTests.cs new file mode 100644 index 00000000..df66f9cd --- /dev/null +++ b/src/NoTargets.UnitTests/TemplateTests.cs @@ -0,0 +1,54 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// +// Licensed under the MIT license. + +#if NET9_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 diff --git a/src/NoTargets/README.md b/src/NoTargets/README.md index f772a68e..e49ad356 100644 --- a/src/NoTargets/README.md +++ b/src/NoTargets/README.md @@ -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 - - net46 + netstandard2.0 @@ -40,7 +39,7 @@ Or a project that runs a tool: ```xml - net46 + netstandard2.0 mytool.exe diff --git a/src/NoTargets/Templates/Microsoft.Build.NoTargets.Templates.csproj b/src/NoTargets/Templates/Microsoft.Build.NoTargets.Templates.csproj new file mode 100644 index 00000000..c17309f9 --- /dev/null +++ b/src/NoTargets/Templates/Microsoft.Build.NoTargets.Templates.csproj @@ -0,0 +1,31 @@ + + + + MSBuild NoTargets templates + Templates for common MSBuild NoTargets projects + MSBuild NoTargets project template + README.md + false + Template + + + $(BaseArtifactsPath)$(MSBuildProjectName)\ + true + netstandard2.0 + true + false + content + $(NoWarn);NU5128 + true + false + + + + + + + + + + + diff --git a/src/NoTargets/Templates/README.md b/src/NoTargets/Templates/README.md new file mode 100644 index 00000000..cf75930b --- /dev/null +++ b/src/NoTargets/Templates/README.md @@ -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" +``` diff --git a/src/NoTargets/Templates/notargets/.template.config/template.json b/src/NoTargets/Templates/notargets/.template.config/template.json new file mode 100644 index 00000000..93b12978 --- /dev/null +++ b/src/NoTargets/Templates/notargets/.template.config/template.json @@ -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" +} \ No newline at end of file diff --git a/src/NoTargets/Templates/notargets/NoTargets.Template.csproj b/src/NoTargets/Templates/notargets/NoTargets.Template.csproj new file mode 100644 index 00000000..8aaa29a1 --- /dev/null +++ b/src/NoTargets/Templates/notargets/NoTargets.Template.csproj @@ -0,0 +1,24 @@ + + + + + netstandard2.0 + + + + + + + diff --git a/src/Traversal.UnitTests/Microsoft.Build.Traversal.UnitTests.csproj b/src/Traversal.UnitTests/Microsoft.Build.Traversal.UnitTests.csproj index a4fe61fc..66454c59 100644 --- a/src/Traversal.UnitTests/Microsoft.Build.Traversal.UnitTests.csproj +++ b/src/Traversal.UnitTests/Microsoft.Build.Traversal.UnitTests.csproj @@ -4,8 +4,12 @@ + + @@ -21,5 +25,8 @@ + diff --git a/src/Traversal.UnitTests/Snapshots/README.md b/src/Traversal.UnitTests/Snapshots/README.md new file mode 100644 index 00000000..e59fcf69 --- /dev/null +++ b/src/Traversal.UnitTests/Snapshots/README.md @@ -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. \ No newline at end of file diff --git a/src/Traversal.UnitTests/Snapshots/RespectsExplicitName.traversal.--name#asdf.verified/traversal/asdf.proj b/src/Traversal.UnitTests/Snapshots/RespectsExplicitName.traversal.--name#asdf.verified/traversal/asdf.proj new file mode 100644 index 00000000..1b5f1ce4 --- /dev/null +++ b/src/Traversal.UnitTests/Snapshots/RespectsExplicitName.traversal.--name#asdf.verified/traversal/asdf.proj @@ -0,0 +1,21 @@ + + + + + + + + + + diff --git a/src/Traversal.UnitTests/Snapshots/UsesDirsAsDefaultName.traversal.--name#.verified/traversal/dirs.proj b/src/Traversal.UnitTests/Snapshots/UsesDirsAsDefaultName.traversal.--name#.verified/traversal/dirs.proj new file mode 100644 index 00000000..1b5f1ce4 --- /dev/null +++ b/src/Traversal.UnitTests/Snapshots/UsesDirsAsDefaultName.traversal.--name#.verified/traversal/dirs.proj @@ -0,0 +1,21 @@ + + + + + + + + + + diff --git a/src/Traversal.UnitTests/TemplateTests.cs b/src/Traversal.UnitTests/TemplateTests.cs new file mode 100644 index 00000000..aed78b69 --- /dev/null +++ b/src/Traversal.UnitTests/TemplateTests.cs @@ -0,0 +1,55 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// +// Licensed under the MIT license. + +#if NET9_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.Traversal.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 UsesDirsAsDefaultName() + { + TemplateVerifierOptions options = new (templateName: "traversal") + { + TemplateSpecificArgs = new[] { "--name", string.Empty }, // BUG: Workaround for https://github.com/dotnet/templating/issues/7394 + TemplatePath = Path.Combine(RootPath, "Templates", "traversal"), + }; + + VerificationEngine engine = new (_loggerFactory); + await engine.Execute(options); + } + + [Fact] + public async Task RespectsExplicitName() + { + TemplateVerifierOptions options = new (templateName: "traversal") + { + TemplateSpecificArgs = new[] { "--name", "asdf" }, + TemplatePath = Path.Combine(RootPath, "Templates", "traversal"), + }; + + VerificationEngine engine = new (_loggerFactory); + await engine.Execute(options); + } + } +} +#endif diff --git a/src/Traversal/Templates/Microsoft.Build.Traversal.Templates.csproj b/src/Traversal/Templates/Microsoft.Build.Traversal.Templates.csproj new file mode 100644 index 00000000..0cf1f929 --- /dev/null +++ b/src/Traversal/Templates/Microsoft.Build.Traversal.Templates.csproj @@ -0,0 +1,31 @@ + + + + MSBuild Traversal templates + Templates for common MSBuild traversal projects + MSBuild traversal dirs template + README.md + false + Template + + + $(BaseArtifactsPath)$(MSBuildProjectName)\ + true + netstandard2.0 + true + false + content + $(NoWarn);NU5128 + true + false + + + + + + + + + + + diff --git a/src/Traversal/Templates/README.md b/src/Traversal/Templates/README.md new file mode 100644 index 00000000..227c625b --- /dev/null +++ b/src/Traversal/Templates/README.md @@ -0,0 +1,24 @@ +# Microsoft.Build.Traversal.Templates + +## Installing the templates + +The templates are available on [NuGet](https://www.nuget.org/packages/Microsoft.Build.Traversal.Templates/). +To install the templates, run the following command: + +```bash +dotnet new -i Microsoft.Build.Traversal.Templates +``` + +## Using the templates + +Creating a new project with the default name "dirs.proj" + +```bash +dotnet new traversal +``` + +Creating a new project "AdventureWorks" (`-n` or `--name`): + +```bash +dotnet new travesal -n "AdventureWorks" +``` diff --git a/src/Traversal/Templates/traversal/.template.config/template.json b/src/Traversal/Templates/traversal/.template.config/template.json new file mode 100644 index 00000000..7b17f86a --- /dev/null +++ b/src/Traversal/Templates/traversal/.template.config/template.json @@ -0,0 +1,17 @@ +{ + "$schema": "http://json.schemastore.org/template", + "author": "Microsoft", + "classifications": [ + "MSBuild" + ], + "identity": "Microsoft.Build.Traversal", + "name": "MSBuild Traversal Project", + "description": "A starter project file for MSBuild Traversal projects", + "shortName": "traversal", + "tags": { + "type": "item" + }, + "sourceName": "dirs", + "preferDefaultName": true, + "defaultName": "dirs" +} \ No newline at end of file diff --git a/src/Traversal/Templates/traversal/dirs.proj b/src/Traversal/Templates/traversal/dirs.proj new file mode 100644 index 00000000..1b5f1ce4 --- /dev/null +++ b/src/Traversal/Templates/traversal/dirs.proj @@ -0,0 +1,21 @@ + + + + + + + + + +