NuGet/Home

Producing an MSBuild task package

Open

#5 063 ouverte le 19 avr. 2017

Voir sur GitHub
 (18 commentaires) (16 réactions) (0 assignés)HTML (292 forks)batch import
Functionality:PackPriority:3Type:DCRhelp wanted

Métriques du dépôt

Stars
 (1 459 stars)
Métriques de merge PR
 (Merge moyen 464j 23h) (1 PR mergée en 30 j)

Description

From @natemcmaster on April 14, 2017 18:41

Can we get better support for projects designed to produce MSBuild tasks?

Pain points

  • packing. You can get a task assembly and files into the package, but requires deep understanding of how to alter the default layout of the package in csproj.
    • TargetFrameworks. A package that only has task assemblies/targets is compiled for net46 and netcoreapp1.0, but the package itself can be compatible with any project, regardless of its TFM.
  • task references. When loading a task assembly, MSBuild does not use the NuGet cache to find dependencies. This means we have to package assemblies to sit side-by-side on disk so task loading works.
  • tasks with native dependencies. Never been able to make this work.

Workarounds Task assembly projects end up looking like this

  <PropertyGroup>
    <!--
      The netstandard1.0 and net45 TFMs don't actually compile tasks.
      Only here so the generated nuspec includes the TFM so it appears as "compatible" on NuGet.org
      and in VS NuGet GUI.

      netstandard1.6 => loaded on dotnet.exe
      net46 => loaded on MSBuild.exe
    -->
    <TargetFrameworks>netstandard1.6;net46;netstandard1.0;net45</TargetFrameworks>
    <!-- Be quiet NuGet. I don't want assemblies in lib/ and yes I'm sure this is right. -->
    <NoPackageAnalysis>true</NoPackageAnalysis>
    <!-- forces SDK to copy dependencies into build output to make packing easier -->
    <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
    <BuildOutputTargetFolder>tools</BuildOutputTargetFolder>
  </PropertyGroup>

  <ItemGroup>
    <!-- ensure nuspec doesn't contain any package references as we bundle their assemblies in our package -->
    <PackageReference Update="@(PackageReference)" PrivateAssets="All" />
  </ItemGroup>

  <!-- 
    Copy dependencies into the tools/$(TargetFramework)/ package folders.
    MSBuild does not resolve task runtime dependencies from PackageReference
  -->
  <Target Name="PackTaskDependencies" BeforeTargets="GenerateNuspec">
    <!--
    The include needs to happen after output has been copied to build output folder
    but before NuGet generates a nuspec.
    -->
    <ItemGroup>
      <_PackageFiles Include="bin\$(Configuration)\*\Newtonsoft.Json.dll;bin\$(Configuration)\*\NUglify.dll">
        <PackagePath>tools\%(RecursiveDir)</PackagePath>
        <Visible>false</Visible>
        <BuildAction>Content</BuildAction>
      </_PackageFiles>
    </ItemGroup>
  </Target>

Some MSBuild task projects in the wild:

madskristensen/BundlerMinifier aspnet/BuildTools natemcmaster/Yarn.MSBuild

Copied from original issue: dotnet/sdk#1125

Guide contributeur