Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify build settings before build on the context of publish? #2078

Open
javiercn opened this issue Mar 22, 2018 · 3 comments
Open

Modify build settings before build on the context of publish? #2078

javiercn opened this issue Mar 22, 2018 · 3 comments
Milestone

Comments

@javiercn
Copy link
Member

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:

https://github.com/dotnet/sdk/blob/master/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Publish.targets#L41

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.

@livarcocc livarcocc added this to the Discussion milestone Mar 22, 2018
JL03-Yue pushed a commit that referenced this issue Mar 19, 2024
* Update dependencies from https://github.com/dotnet/source-build-externals build 20240116.1

Microsoft.SourceBuild.Intermediate.source-build-externals
 From Version 9.0.0-alpha.1.24059.3 -> To Version 9.0.0-alpha.1.24066.1

* Update dependencies from https://github.com/dotnet/source-build-externals build 20240126.1

Microsoft.SourceBuild.Intermediate.source-build-externals
 From Version 9.0.0-alpha.1.24059.3 -> To Version 9.0.0-alpha.1.24076.1

* Update dependencies from https://github.com/dotnet/source-build-externals build 20240201.1

Microsoft.SourceBuild.Intermediate.source-build-externals
 From Version 9.0.0-alpha.1.24059.3 -> To Version 9.0.0-alpha.1.24101.1

---------

Co-authored-by: dotnet-maestro[bot] <dotnet-maestro[bot]@users.noreply.github.com>
Co-authored-by: Jan Jones <janjones@microsoft.com>
Copy link
Contributor

Due to lack of recent activity, this issue has been labeled as 'stale'. It will be closed if no further activity occurs within 30 more days. Any new comment will remove the label.

@github-actions github-actions bot added the stale label Apr 25, 2024
@KalleOlaviNiemitalo
Copy link

The _IsPublishing property may be usable for this. See #26324

@github-actions github-actions bot removed the stale label Apr 26, 2024
@baronfel
Copy link
Member

Please do not encourage the use of _IsPublishing - like all _-prefixed properties it is an internal implementation detail and subject to change at any time. The fact that some folks are using it in ways that we (SDK team) would rather that not is unfortunate - we should limit the use of this and solve the problems raised in that issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants