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

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions .github/workflows/_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,7 @@ jobs:

- name: Publish samples
run: |
for projectPath in ./samples/**/*.csproj ; do
for tfm in "net6.0" "net7.0"; do
projectFileName=${projectPath##*/}
projectName=${projectFileName%.*}
dotnet publish "$projectPath" --output "./artifacts/$projectName" --framework $tfm --configuration Release --no-build --verbosity normal
done;
done;
dotnet build -c Release /t:PublishSamples --no-build --verbosity normal

- name: Pack (ci)
run: dotnet pack --configuration Release --output ./artifacts/ci --verbosity normal -p:BuildNumber=$BUILD_NUMBER -p:SourceRevisionId=$GITHUB_SHA -p:ContinuousIntegrationBuild=true
Expand Down
31 changes: 31 additions & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project>
<Target
Name="PublishSamples"
Copy link
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).

Condition="'$(IsSample)' == 'true'">
Copy link
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).

<PropertyGroup>
<ArtifactsRoot>$(MSBuildThisFileDirectory)/artifacts/</ArtifactsRoot>
<_IsMultiTargetTfm Condition="'$(TargetFrameworks)' != ''and '$(TargetFramework)' == ''">true</_IsMultiTargetTfm>
<_IsSingleTargetTfmNoRids Condition="'$(TargetFramework)' != '' and '$(RuntimeIdentifiers)' == ''">true</_IsSingleTargetTfmNoRids>
<_IsSingleTargetAndMultiRid Condition="'$(TargetFramework)' != '' and '$(RuntimeIdentifiers)' != ''">true</_IsSingleTargetAndMultiRid>
Comment on lines +7 to +9
Copy link
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.

</PropertyGroup>
<!-- TFMs but no TF -> multitarget, making samples for each TFM -->
<ItemGroup Condition="'$(_IsMultiTargetTfm)' == 'true'">
<_TFMItems Include="$(TargetFrameworks)" />
<_SingleSamplePublish
Include="$(MSBuildProjectFullPath)"
AdditionalProperties="TargetFramework=%(_TFMItems.Identity);PublishDir=$(ArtifactsRoot)$(MSBuildProjectName)/%(_TFMItems.Identity)" />
</ItemGroup>
Comment on lines +12 to +17
Copy link
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).


<!-- TF but no TFMs -> single sample (aka the default pathway) up until now -->
<ItemGroup Condition="'$(_IsSingleTargetTfm)' == 'true'">
<_SingleSamplePublish
Include="$(MSBuildProjectFullPath)"
AdditionalProperties="PublishDir=$(ArtifactsRoot)$(MSBuildProjectName)/$(TargetFramework)" />
</ItemGroup>
Comment on lines +20 to +24
Copy link
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.


<MSBuild
Projects="@(_SingleSamplePublish)"
Targets="Publish"
BuildInParallel="true" />
Comment on lines +26 to +29
Copy link
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.

</Target>
</Project>
8 changes: 8 additions & 0 deletions samples/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project>

<PropertyGroup>
<IsSample>true</IsSample>
</PropertyGroup>

<Import Project="../Directory.Build.props" />
</Project>