Description
I'm trying to run a task that only runs on publish and that runs before Build. The goal of this task is that it will setup some properties before build gets called in the context of publish that might trigger a rebuild.
I have run into this for two scenarios so far:
- In the first scenario, I'm doing IL Linking on the build assemblies + BCL using the monolinker and I want to enable linking as part of the build process when in the context of publish.
- In the second scenario I'm writing an assembly metadata under certain conditions that can vary between build and publish. (Off on regular builds/On on publish builds). For a similar case you could imagine that I'm running csc without optimizations on build and with optimizations on publish (so I want to be able to setup the optimizations for publish when I'm in the context of publish).
I've tried to create a task with BeforeTargets="Publish" but runs after Build. I've looked at the Task and I haven't been able to figure out how to plug before build in the context of publish:
I've also tried to redefine the publish task in my csproj file but that doesn't work given the way the SDK gets imported.
If we were to have a property PublishDependsOn would I be able to add my targets there before the Build target?
Is there a way to know the original target that was invoked (Build/Publish) so that I can distinguish that in my target?
Is there any other way to achieve this that I'm missing? (Hook up before the Build target only when publish is running to select potentially different build settings).
This is a sample of what I'm trying to accomplish (just writes an assembly metadata attribute to the assembly on publish/build.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<WriteMetadataOnBuild>false</WriteMetadataOnBuild>
<WriteMetadataOnPublish>true</WriteMetadataOnPublish>
</PropertyGroup>
<Target Name="PreparePublishBuildConfiguration" BeforeTargets="Publish">
<PropertyGroup>
<_ShouldIncludeWriteMetadata>$(WriteMetadataOnPublish)</_ShouldIncludeWriteMetadata>
</PropertyGroup>
</Target>
<Target Name="GetDefaultBuildConfiguration" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<_ShouldIncludeWriteMetadata Condition="$(_ShouldIncludeWriteMetadata) == ''">$(WriteMetadataOnBuild)</_ShouldIncludeWriteMetadata>
</PropertyGroup>
</Target>
<Target Name="AddAssemblyAttribute" BeforeTargets="GetAssemblyAttributes" Condition="'$(_ShouldIncludeWriteMetadata)' == 'true'">
<ItemGroup>
<AssemblyAttribute Include="System.Reflection.AssemblyMetadataAttribute">
<_parameter1>Foo</_parameter1>
<_parameter2>Bar</_parameter2>
</AssemblyAttribute>
</ItemGroup>
</Target>
</Project>
This is what I imagine would work if there's a property for it
<PublishDependsOn>
PreparePublishBuildConfiguration;
$(PublishDependsOn)
</PublishDependsOn>
</PropertyGroup>
<Target Name="PreparePublishBuildConfiguration">
<PropertyGroup>
<_ShouldIncludeWriteMetadata>$(WriteMetadataOnPublish)</_ShouldIncludeWriteMetadata>
</PropertyGroup>
</Target>
Any help/guidance will be appreciated.