microsoft / MSBuildSdks

MSBuild project SDKs
MIT License
442 stars 81 forks source link

[Feature Request] New MSBuildSdk to simplify creating an MSBuild task package #551

Open MattKotsenas opened 4 months ago

MattKotsenas commented 4 months ago

The Problem

The general steps to create a NuGet package that contains MSBuild tasks is outlined here: https://learn.microsoft.com/en-us/visualstudio/msbuild/tutorial-custom-task-code-generation?view=vs-2022#package-the-task-for-distribution

This process requires 8 steps:

  1. Set CopyLocalLockFileAssemblies
  2. Mark all PackageReferences with PrivateAssets="All"
  3. Add the CopyProjectReferencesToPackage target
  4. Set the BuildOutputTargetFolder
  5. Suppress NU5100 and NU5128
  6. Mark Microsoft.Build.* assemblies with ExcludeAssets="runtime"
  7. Set GenerateDependencyFile and add target to copy to output directory
  8. Add the build/, buildTransitive/, buildMultiTargeting/ folders to the package

and I always forget at least one when creating a new project.

The Proposal

Create a new SDK to centralize these steps, and ideally update the docs to point to the SDK instead. In addition to lowering the barrier to authoring MSBuild tasks, centralizing the process for creating an MSBuild task package will make it easier for authors to follow best practices as they evolve (similar to the DotNet.ReproducibleBuilds packages).

I'm happy to do this work myself, but wanted to get approval before starting. Thoughts?

baronfel commented 4 months ago

I'm torn about where the line for an MSBuild SDK and a .NET Template might be - I could see pushing several of those steps into an MSBuild SDK, and then shipping a .NET SDK Template that could get your started on a new Task package - project file using the new SDK, placeholder props/targets, a class library for a Task with all of the organization/project file changes to make it all work, etc.

To be clear, I'd love to see something like a base SDK exist! I love this suggestion and would be happy to help push it forward.

MattKotsenas commented 4 months ago

OK, happy to get started here. Two asks:

  1. If you have initial thoughts on which pieces belong in which part (SDK vs template) post them here
  2. Since we're considering making another template, weigh in on #529 as well since a. It's directionally aligned b. I made a bunch of directory layout assumptions that'll impact the work here
MeikTranel commented 4 months ago

Happy to give input - interested to see how y'all gonna approach this - supporting VS desktop and dotnet cli - supporting testing...

If you need some inspiration i'll throw my package in the ring:

https://github.com/MeikTranel/NXPorts/blob/master/src/NXPorts/NXPorts.csproj

I use MSBuild sdk/task packages much more extensively but unfortunately i cant post these here.

ericstj commented 2 months ago

I wonder if there's a broader generalization to this proposal which might be "Plugin SDK". Similar constraints might apply to roslyn analyzers, MSBuild tasks, powershell commandlets, Azure functions, etc.