dotnet / sdk

Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI
https://dot.net/core
MIT License
2.65k stars 1.05k forks source link

Better way of hooking into "Before" Publish #3196

Open NinoFloris opened 5 years ago

NinoFloris commented 5 years ago

So I was looking around to see if I could redefine some defaults to always be true iff we run a Publish action.

I could only seem to get there by chaining msbuild gotchas, essentially having a project like:

<Project>
  <Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk"  />
  <Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />

  <Target Name="OldPublish" DependsOnTargets="_PublishBuildAlternative;_PublishNoBuildAlternative">
    <!-- Ensure there is minimal verbosity output pointing to the publish directory and not just the
         build step's minimal output. Otherwise there is no indication at minimal verbosity of where
         the published assets were copied. -->
    <Message Importance="High" Text="$(MSBuildProjectName) -> $([System.IO.Path]::GetFullPath('$(PublishDir)'))" />
  </Target>

  <Target Name="NewDefaults">
    <PropertyGroup>
       ... Override defaults here
    </PropertyGroup>
  </Target>

  <Target Name="Publish"
          DependsOnTargets="NewDefaults"
          Condition="$(IsPublishable) == 'true'">

    // We actually need NewDefaults as a separate target to set the props
    // due to a long standing CallTarget bug https://github.com/Microsoft/msbuild/issues/1006
    <CallTarget Targets="NewPublish" />
  </Target>
</Project>

The pain here is mainly that you cannot get any custom evalution in before /t: Publish — without doing crazy stuff — there's only the indifferent InitialTargets.

Problem there obviously is if you really need a Target to only run before Publish, not always.

Is there anything we can do better here?

It looks I'm not the first either https://github.com/dotnet/sdk/issues/1039, although there it seems BeforePublish was still a thing.

dasMulli commented 5 years ago

Which defaults do you need to change? If they are for building and not directly related to publishing, then it is this difficult.

dasMulli commented 5 years ago

If you used publish profiles, you could change some property groups or add targets (e.g. before build targets) in the publish profile..