Skip to content

example of how to do multi-tfm publishing without SDK support#35

Open
baronfel wants to merge 1 commit intoDamianEdwards:mainfrom
baronfel:parallel-publish
Open

example of how to do multi-tfm publishing without SDK support#35
baronfel wants to merge 1 commit intoDamianEdwards:mainfrom
baronfel:parallel-publish

Conversation

@baronfel
Copy link
Copy Markdown

@baronfel baronfel commented Sep 8, 2023

No description provided.

Copy link
Copy Markdown
Author

@baronfel baronfel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some notes on what's going on here.

Comment thread Directory.Build.targets
@@ -0,0 +1,31 @@
<Project>
<Target
Name="PublishSamples"
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

putting this target in every project (via D.B.targets) means that calling the target at the solution-level Just Works (thanks to solution file's traversal targets mechanism).

Comment thread Directory.Build.targets
<Project>
<Target
Name="PublishSamples"
Condition="'$(IsSample)' == 'true'">
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but we don't want this to fire for the actual library (which is why the Samples now have a D.B.props that adds this property).

Comment thread Directory.Build.targets
Comment on lines +7 to +9
<_IsMultiTargetTfm Condition="'$(TargetFrameworks)' != ''and '$(TargetFramework)' == ''">true</_IsMultiTargetTfm>
<_IsSingleTargetTfmNoRids Condition="'$(TargetFramework)' != '' and '$(RuntimeIdentifiers)' == ''">true</_IsSingleTargetTfmNoRids>
<_IsSingleTargetAndMultiRid Condition="'$(TargetFramework)' != '' and '$(RuntimeIdentifiers)' != ''">true</_IsSingleTargetAndMultiRid>
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SDK (or MSBuild) could provide helper properties like this to make Conditions easier for authors.

Comment thread Directory.Build.targets
Comment on lines +12 to +17
<ItemGroup Condition="'$(_IsMultiTargetTfm)' == 'true'">
<_TFMItems Include="$(TargetFrameworks)" />
<_SingleSamplePublish
Include="$(MSBuildProjectFullPath)"
AdditionalProperties="TargetFramework=%(_TFMItems.Identity);PublishDir=$(ArtifactsRoot)$(MSBuildProjectName)/%(_TFMItems.Identity)" />
</ItemGroup>
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we have multiple TFMs, then we want to Publish the project multiple times - once for each TFM. And since we need to deduplicate the published outputs, we add the TFM onto the outputs directory here. This logic is something very much like what the SDK might do - but for some publish targets like Containers, more customization might be needed (as well as some kind of post-TFMs-published hook).

Comment thread Directory.Build.targets
Comment on lines +20 to +24
<ItemGroup Condition="'$(_IsSingleTargetTfm)' == 'true'">
<_SingleSamplePublish
Include="$(MSBuildProjectFullPath)"
AdditionalProperties="PublishDir=$(ArtifactsRoot)$(MSBuildProjectName)/$(TargetFramework)" />
</ItemGroup>
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A single TFM is similarly simple - just build this project once, specifying only the property that's needed.

Comment thread Directory.Build.targets
Comment on lines +26 to +29
<MSBuild
Projects="@(_SingleSamplePublish)"
Targets="Publish"
BuildInParallel="true" />
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we've arranged our work to be done, fire it all off in parallel.

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

Successfully merging this pull request may close these issues.

1 participant