Files

519 lines
53 KiB
XML

<Project DefaultTargets="CI">
<Import Project="$(MSBuildThisFileDirectory)BundledPlugins.props" />
<Import Project="$(MSBuildThisFileDirectory)RuntimeIdentifiers.props" />
<PropertyGroup>
<RepoRoot>$([System.IO.Path]::GetFullPath('$(MSBuildThisFileDirectory)..'))</RepoRoot>
<Solution>$(RepoRoot)/src/Nemera.slnx</Solution>
<Configuration Condition="'$(Configuration)' == ''">Debug</Configuration>
<ApiPublishDirectory>$(RepoRoot)/bin/api/publish</ApiPublishDirectory>
<WebPublishDirectory>$(RepoRoot)/bin/web/publish</WebPublishDirectory>
<!-- The per-runtime archives that are attached to a GitHub release. -->
<ReleaseArchivesDirectory>$(RepoRoot)/bin/release</ReleaseArchivesDirectory>
<!-- Archive verification extracts into its own folder, outside the uploaded release root, so an interrupted verification cannot leak scratch files into the release archives artifact. -->
<ReleaseArchiveVerificationDirectory>$(RepoRoot)/bin/release-verify</ReleaseArchiveVerificationDirectory>
<ReleaseVersion Condition="'$(ReleaseVersion)' == ''">$(Version)</ReleaseVersion>
<ReleaseVersion Condition="'$(ReleaseVersion)' == ''">0.0.0-dev</ReleaseVersion>
<!-- The version is embedded in archive names and tar commands, so only a three-part version or the development default is accepted. -->
<_ReleaseVersionPattern>^([0-9]+\.[0-9]+\.[0-9]+|0\.0\.0-dev)$</_ReleaseVersionPattern>
<!-- The Docker image tag, which defaults to latest for local images. -->
<_ImageVersion Condition="'$(Version)' != ''">$(Version)</_ImageVersion>
<_ImageVersion Condition="'$(Version)' == ''">latest</_ImageVersion>
<!-- The shared plugin staging folder is outside the release root, so it is never uploaded with the release. -->
<BundledPluginsPublishDirectory>$(RepoRoot)/bin/api/bundled-plugins</BundledPluginsPublishDirectory>
<!-- The runtime used for the Docker images and for the published manifest verification. -->
<ApiReleaseRuntimeIdentifier Condition="'$(ApiReleaseRuntimeIdentifier)' == ''">linux-x64</ApiReleaseRuntimeIdentifier>
<!-- Official images copy a published folder into a base image of the same architecture, so each glibc Linux runtime maps to its Docker platform. -->
<_DockerPlatform Condition="'$(ApiReleaseRuntimeIdentifier)' == 'linux-x64'">linux/amd64</_DockerPlatform>
<_DockerPlatform Condition="'$(ApiReleaseRuntimeIdentifier)' == 'linux-arm64'">linux/arm64</_DockerPlatform>
<BundledPluginsCurrentManifest Condition="'$(BundledPluginsCurrentManifest)' == ''">$(RepoRoot)/bin/api/BundledPlugins.current.manifest</BundledPluginsCurrentManifest>
<BundledPluginsSolution Condition="'$(BundledPluginsSolution)' == ''">$(MSBuildThisFileDirectory)obj/BundledPlugins.slnx</BundledPluginsSolution>
<_OfficialReleaseBuild Condition="'$(OfficialReleaseBuild)' == ''">false</_OfficialReleaseBuild>
<_OfficialReleaseBuild Condition="'$(OfficialReleaseBuild)' != ''">$(OfficialReleaseBuild)</_OfficialReleaseBuild>
<_DockerDependsOn>Build</_DockerDependsOn>
<_DockerDependsOn Condition="'$(_OfficialReleaseBuild)' == 'true'">ValidateDockerRelease;VerifyPublishedBundledPlugins</_DockerDependsOn>
<!-- A reused publish output was verified by the preceding publish step, so verifying it again only repeats the work. -->
<_DockerDependsOn Condition="'$(_OfficialReleaseBuild)' == 'true' And '$(ReusePublishedBundledPlugins)' == 'true'">ValidateDockerRelease</_DockerDependsOn>
<!-- Local images build for the host so docker compose keeps working; only release images pin the platform of the published folder they copy. -->
<_DockerPlatformOption Condition="'$(_OfficialReleaseBuild)' == 'true'">--platform &quot;$(_DockerPlatform)&quot; </_DockerPlatformOption>
</PropertyGroup>
<!-- Defined once because Publish and the verification targets run in the same project instance during RecordBundledPlugins. -->
<ItemGroup>
<_ReleaseRuntime Include="@(BundledPluginRuntime)" />
</ItemGroup>
<Target Name="Restore">
<Exec Command="dotnet restore &quot;$(Solution)&quot;" />
<PropertyGroup>
<BundledPluginsAlreadyRestored>true</BundledPluginsAlreadyRestored>
</PropertyGroup>
</Target>
<Target Name="Build" DependsOnTargets="Restore;CompileSolution;FinalizeOfficialBundledPluginManifest" />
<Target Name="CompileSolution" DependsOnTargets="PrepareOfficialBundledPluginManifest">
<Exec Condition="'$(_OfficialReleaseBuild)' == 'true'" Command="dotnet build &quot;$(Solution)&quot; -c $(Configuration) --no-restore -p:OfficialReleaseBuild=$(_OfficialReleaseBuild) -p:BundledPluginsCurrentManifest=&quot;$(BundledPluginsCurrentManifest)&quot;" />
<Exec Condition="'$(_OfficialReleaseBuild)' != 'true'" Command="dotnet build &quot;$(Solution)&quot; -c $(Configuration) --no-restore -p:OfficialReleaseBuild=$(_OfficialReleaseBuild)" />
</Target>
<Target Name="Test" DependsOnTargets="Build">
<Exec Command="dotnet test &quot;$(Solution)&quot; -c $(Configuration) --no-build --blame-hang --blame-hang-timeout 10m" />
</Target>
<!-- The generated solution lets dotnet restore and dotnet build handle the plugin projects as separate supported invocations. -->
<Target Name="PrepareBundledPluginsSolution">
<ItemGroup>
<_BundledPluginSolutionLine Include="&lt;Solution&gt;" />
<_BundledPluginSolutionLine Include="@(BundledPluginProject->' &lt;Project Path=&quot;%(FullPath)&quot; /&gt;')" />
<_BundledPluginSolutionLine Include="&lt;/Solution&gt;" />
</ItemGroup>
<Error Condition="'@(BundledPluginProject)' == ''" Text="The bundled plugin catalog is empty." />
<MakeDir Directories="$([System.IO.Path]::GetDirectoryName('$(BundledPluginsSolution)'))" />
<WriteLinesToFile File="$(BundledPluginsSolution)" Lines="@(_BundledPluginSolutionLine)" Overwrite="true" WriteOnlyWhenDifferent="true" />
</Target>
<Target Name="RestoreBundledPlugins" Condition="'$(BundledPluginsAlreadyRestored)' != 'true'" DependsOnTargets="PrepareBundledPluginsSolution">
<Exec Command="dotnet restore &quot;$(BundledPluginsSolution)&quot;" WorkingDirectory="$(RepoRoot)" />
</Target>
<Target Name="BuildBundledPlugins" DependsOnTargets="PrepareBundledPluginsSolution;RestoreBundledPlugins">
<Exec Command="dotnet build &quot;$(BundledPluginsSolution)&quot; -c $(Configuration) --no-restore -p:NemeraPluginsOutputDirectory=&quot;$(NemeraPluginsOutputDirectory)&quot; -p:OfficialReleaseBuild=$(_OfficialReleaseBuild)" WorkingDirectory="$(RepoRoot)" />
</Target>
<!-- The first official build needs a current manifest file to embed. The real manifest is generated from the deployed plugins after the solution build. -->
<Target Name="PrepareOfficialBundledPluginManifest" Condition="'$(_OfficialReleaseBuild)' == 'true'">
<MakeDir Directories="$([System.IO.Path]::GetDirectoryName('$(BundledPluginsCurrentManifest)'))" />
<WriteLinesToFile File="$(BundledPluginsCurrentManifest)" Lines="# The current manifest is generated after the solution build." Overwrite="true" WriteOnlyWhenDifferent="true" />
</Target>
<!-- The official manifest is generated from the plugins that the solution build actually deployed, so the embedded hashes and the deployed binaries cannot diverge. -->
<Target Name="FinalizeOfficialBundledPluginManifest" Condition="'$(_OfficialReleaseBuild)' == 'true'" DependsOnTargets="CompileSolution">
<MSBuild Projects="$(RepoRoot)/src/Nemera.Presentation.Api/Nemera.Presentation.Api.csproj" Targets="GetTargetPath" Properties="Configuration=$(Configuration)">
<Output TaskParameter="TargetOutputs" ItemName="_BundledPluginApiTargetPath" />
</MSBuild>
<Error Condition="'@(_BundledPluginApiTargetPath)' == ''" Text="The API target path could not be determined." />
<ItemGroup>
<_BundledPluginApiTargetDirectory Include="@(_BundledPluginApiTargetPath->'%(RootDir)%(Directory)')" />
</ItemGroup>
<PropertyGroup>
<_DeployedBundledPluginsDirectory>@(_BundledPluginApiTargetDirectory)plugins</_DeployedBundledPluginsDirectory>
</PropertyGroup>
<ItemGroup>
<_DeployedBundledPluginAssembly Include="$(_DeployedBundledPluginsDirectory)/Nemera.Plugins.*.dll" />
</ItemGroup>
<Error Condition="'@(_DeployedBundledPluginAssembly)' == ''" Text="No bundled plugin binaries were deployed to '$(_DeployedBundledPluginsDirectory)'." />
<GetFileHash Files="@(_DeployedBundledPluginAssembly)" Algorithm="SHA256">
<Output TaskParameter="Items" ItemName="_FinalizedBundledPluginHash" />
</GetFileHash>
<WriteLinesToFile File="$(BundledPluginsCurrentManifest)" Lines="@(_FinalizedBundledPluginHash->'%(Filename)%(Extension)|%(FileHash)')" Overwrite="true" WriteOnlyWhenDifferent="true" />
<Exec Command="dotnet build &quot;$(RepoRoot)/src/Nemera.Presentation.Api/Nemera.Presentation.Api.csproj&quot; -c $(Configuration) --no-restore -p:OfficialReleaseBuild=true -p:BundledPluginsCurrentManifest=&quot;$(BundledPluginsCurrentManifest)&quot;" />
<Exec Command="dotnet build &quot;$(RepoRoot)/tests/UnitTests/Nemera.Presentation.Api.UnitTests/Nemera.Presentation.Api.UnitTests.csproj&quot; -c $(Configuration) --no-restore -p:OfficialReleaseBuild=true -p:BundledPluginsCurrentManifest=&quot;$(BundledPluginsCurrentManifest)&quot;" />
<Exec Command="dotnet build &quot;$(RepoRoot)/tests/IntegrationTests/Nemera.Presentation.Api.IntegrationTests/Nemera.Presentation.Api.IntegrationTests.csproj&quot; -c $(Configuration) --no-restore -p:OfficialReleaseBuild=true -p:BundledPluginsCurrentManifest=&quot;$(BundledPluginsCurrentManifest)&quot;" />
</Target>
<Target Name="VerifyBundledPlugins" DependsOnTargets="Build">
<Exec Command="dotnet test &quot;$(Solution)&quot; -c $(Configuration) --no-build --filter &quot;FullyQualifiedName~BundledPluginManifest&quot;" />
</Target>
<!-- Ensures a fresh publish output exists unless the caller explicitly reuses the output of the preceding publish step. -->
<Target Name="EnsurePublishedBundledPlugins">
<MSBuild Projects="$(MSBuildThisFileFullPath)" Targets="Publish" Condition="'$(ReusePublishedBundledPlugins)' != 'true' And '$(_PublishedBundledPlugins)' != 'true'" />
</Target>
<!-- The catalog is the source of truth, so the staging folder must contain exactly the native runtime folders it declares. This fails the build when a dependency starts or stops deploying a folder. -->
<Target Name="VerifyStagingNativeRuntimeDirectories" DependsOnTargets="EnsurePublishedBundledPlugins">
<ItemGroup>
<_StagingNativeRuntimeDirectory Include="$([System.IO.Directory]::GetDirectories('$(BundledPluginsPublishDirectory)/runtimes'))" Condition="Exists('$(BundledPluginsPublishDirectory)/runtimes')" />
<_DeclaredStagingNativeRuntimeDirectory Include="@(BundledPluginRuntimeDirectory->'$(BundledPluginsPublishDirectory)/runtimes/%(Identity)')" />
</ItemGroup>
<!-- A shared fallback folder is declared once per runtime that uses it, so deduplicate the expected list before it is reported. -->
<RemoveDuplicates Inputs="@(_DeclaredStagingNativeRuntimeDirectory)">
<Output TaskParameter="Filtered" ItemName="_ExpectedStagingNativeRuntimeDirectory" />
</RemoveDuplicates>
<ItemGroup>
<_MissingStagingNativeRuntimeDirectory Include="@(_ExpectedStagingNativeRuntimeDirectory)" Condition="!Exists('%(FullPath)')" />
<_UnexpectedStagingNativeRuntimeDirectory Include="@(_StagingNativeRuntimeDirectory)" Exclude="@(_ExpectedStagingNativeRuntimeDirectory)" />
</ItemGroup>
<Error Condition="'@(_MissingStagingNativeRuntimeDirectory)' != ''" Text="The bundled plugin staging folder is missing native runtime folders that build/RuntimeIdentifiers.props declares: @(_MissingStagingNativeRuntimeDirectory)" />
<Error Condition="'@(_UnexpectedStagingNativeRuntimeDirectory)' != ''" Text="The bundled plugin staging folder contains native runtime folders that build/RuntimeIdentifiers.props does not declare: @(_UnexpectedStagingNativeRuntimeDirectory)" />
</Target>
<!-- Verifies every published runtime folder: assemblies, apphosts, plugins, native folders and files, the embedded plugin manifest, and the plugin binaries that the trust list records. Archive checks live in VerifyReleaseArchives. -->
<Target Name="VerifyPublishedBundledPlugins" DependsOnTargets="EnsurePublishedBundledPlugins;VerifyStagingNativeRuntimeDirectories">
<PropertyGroup>
<_ApiReleaseDirectory>$(ApiPublishDirectory)/$(ApiReleaseRuntimeIdentifier)</_ApiReleaseDirectory>
</PropertyGroup>
<Error Condition="!Exists('$(_ApiReleaseDirectory)/Nemera.Presentation.Api.dll')" Text="The published API assembly is missing from '$(_ApiReleaseDirectory)'." />
<Error Condition="!Exists('$(_ApiReleaseDirectory)/plugins')" Text="The published bundled plugins are missing from '$(_ApiReleaseDirectory)/plugins'." />
<ItemGroup>
<_WindowsReleaseRuntime Include="@(_ReleaseRuntime)" Condition="$([System.String]::Copy('%(Identity)').StartsWith('win-'))" />
<_UnixReleaseRuntime Include="@(_ReleaseRuntime)" Condition="$([System.String]::Copy('%(Identity)').StartsWith('win-')) == false" />
<_ReleaseApiPath Include="@(_ReleaseRuntime->'$(ApiPublishDirectory)/%(Identity)/Nemera.Presentation.Api.dll')" />
<_ReleaseWebPath Include="@(_ReleaseRuntime->'$(WebPublishDirectory)/%(Identity)/Nemera.Presentation.Web.dll')" />
<_ReleaseApiAppHostPath Include="@(_WindowsReleaseRuntime->'$(ApiPublishDirectory)/%(Identity)/Nemera.Presentation.Api.exe')" />
<_ReleaseApiAppHostPath Include="@(_UnixReleaseRuntime->'$(ApiPublishDirectory)/%(Identity)/Nemera.Presentation.Api')" />
<_ReleaseWebAppHostPath Include="@(_WindowsReleaseRuntime->'$(WebPublishDirectory)/%(Identity)/Nemera.Presentation.Web.exe')" />
<_ReleaseWebAppHostPath Include="@(_UnixReleaseRuntime->'$(WebPublishDirectory)/%(Identity)/Nemera.Presentation.Web')" />
<_MissingReleaseApi Include="@(_ReleaseApiPath)" Condition="!Exists('%(FullPath)')" />
<_MissingReleaseWeb Include="@(_ReleaseWebPath)" Condition="!Exists('%(FullPath)')" />
<_MissingReleaseApiAppHost Include="@(_ReleaseApiAppHostPath)" Condition="!Exists('%(FullPath)')" />
<_MissingReleaseWebAppHost Include="@(_ReleaseWebAppHostPath)" Condition="!Exists('%(FullPath)')" />
<_ReleaseNativeRuntimeMapping Include="@(BundledPluginRuntimeDirectory)" />
<_ReleasePdb Include="$(ApiPublishDirectory)/**/*.pdb;$(WebPublishDirectory)/**/*.pdb" />
</ItemGroup>
<Error Condition="'@(_MissingReleaseApi)' != ''" Text="The release is missing the API assembly for these runtimes: @(_MissingReleaseApi)" />
<Error Condition="'@(_MissingReleaseWeb)' != ''" Text="The release is missing the web assembly for these runtimes: @(_MissingReleaseWeb)" />
<Error Condition="'@(_MissingReleaseApiAppHost)' != ''" Text="The release is missing the API executable for these runtimes: @(_MissingReleaseApiAppHost)" />
<Error Condition="'@(_MissingReleaseWebAppHost)' != ''" Text="The release is missing the web executable for these runtimes: @(_MissingReleaseWebAppHost)" />
<Error Condition="'@(_ReleasePdb)' != ''" Text="The release still contains PDB files: @(_ReleasePdb)" />
<!-- Verify that every bundled plugin project has an assembly in every published runtime folder. -->
<MSBuild Projects="$(MSBuildThisFileFullPath)" Targets="VerifyReleasePluginAssemblies" Properties="ApiPublishDirectory=$(ApiPublishDirectory);BundledPluginReleaseRuntimeIdentifier=%(_ReleaseRuntime.Identity)" />
<!-- Verify the exact native runtime folders for every release, so a foreign OS or a foreign architecture of the same OS cannot pass. -->
<MSBuild Projects="$(MSBuildThisFileFullPath)" Targets="VerifyReleaseRuntimeDirectories" Properties="ApiPublishDirectory=$(ApiPublishDirectory);BundledPluginsPublishDirectory=$(BundledPluginsPublishDirectory);BundledPluginReleaseRuntimeIdentifier=%(_ReleaseRuntime.Identity)" />
<!-- Verify every declared native folder file by file, including the destination architecture and its fallback folders. -->
<MSBuild Projects="$(MSBuildThisFileFullPath)" Targets="VerifyReleaseRuntimeNativeFiles" Properties="ApiPublishDirectory=$(ApiPublishDirectory);BundledPluginsPublishDirectory=$(BundledPluginsPublishDirectory);BundledPluginReleaseRuntimeIdentifier=%(_ReleaseNativeRuntimeMapping.RuntimeIdentifier);BundledPluginRuntimeDirectoryName=%(_ReleaseNativeRuntimeMapping.Identity)" />
<!-- Verify the embedded manifest against every published API folder, not only the folder used by the Docker images. -->
<Exec Command="dotnet test &quot;$(RepoRoot)/tests/IntegrationTests/Nemera.Presentation.Api.IntegrationTests/Nemera.Presentation.Api.IntegrationTests.csproj&quot; -c $(Configuration) --no-build --filter &quot;FullyQualifiedName~PublishedBundledPluginManifest&quot;" EnvironmentVariables="NEMERA_API_PUBLISH_DIRECTORY=$(ApiPublishDirectory)/%(_ReleaseRuntime.Identity)" />
<!-- Every runtime folder must ship the same plugin binaries as the reference folder that the trust list records. -->
<MSBuild Projects="$(MSBuildThisFileFullPath)" Targets="VerifyReleasePluginFiles" Properties="BundledPluginReleaseRuntimeIdentifier=%(_ReleaseRuntime.Identity);ReferencePluginsDirectory=$(ApiPublishDirectory)/$(ApiReleaseRuntimeIdentifier)/plugins;ReleasePluginsDirectory=$(ApiPublishDirectory)/%(_ReleaseRuntime.Identity)/plugins" />
</Target>
<!-- The Linux and macOS archives keep their executable bit, so they must be created and verified on Linux or macOS. -->
<Target Name="ValidateReleaseArchivesPlatform">
<Error Condition="'$(OS)' == 'Windows_NT'" Text="Linux and macOS archives must be created and verified on Linux or macOS, because Windows cannot store the executable bit. Run this target on a non-Windows host." />
</Target>
<!-- Verifies every platform archive, independently of the publish-folder checks, so Docker reuse does not require bin/release to exist. -->
<Target Name="VerifyReleaseArchives" DependsOnTargets="ValidateReleaseArchivesPlatform;EnsurePublishedBundledPlugins;CreateReleaseArchives">
<ItemGroup>
<_ReleaseApiArchive Include="@(_ReleaseRuntime->'$(ReleaseArchivesDirectory)/Nemera-$(ReleaseVersion)-%(ReleaseArchiveStem)-api%(ReleaseArchiveExtension)')" />
<_ReleaseWebArchive Include="@(_ReleaseRuntime->'$(ReleaseArchivesDirectory)/Nemera-$(ReleaseVersion)-%(ReleaseArchiveStem)-web%(ReleaseArchiveExtension)')" />
<_MissingReleaseApiArchive Include="@(_ReleaseApiArchive)" Condition="!Exists('%(FullPath)')" />
<_MissingReleaseWebArchive Include="@(_ReleaseWebArchive)" Condition="!Exists('%(FullPath)')" />
</ItemGroup>
<Error Condition="'@(_MissingReleaseApiArchive)' != ''" Text="The release is missing these API archives: @(_MissingReleaseApiArchive)" />
<Error Condition="'@(_MissingReleaseWebArchive)' != ''" Text="The release is missing these web archives: @(_MissingReleaseWebArchive)" />
<!-- Extract every archive and compare its files and hashes with the publish folder it was built from. -->
<MSBuild Projects="$(MSBuildThisFileFullPath)" Targets="VerifyReleaseArchive" Properties="ReleaseArchivePath=$(ReleaseArchivesDirectory)/Nemera-$(ReleaseVersion)-%(_ReleaseRuntime.ReleaseArchiveStem)-api%(_ReleaseRuntime.ReleaseArchiveExtension);ReleaseArchiveSourceDirectory=$(ApiPublishDirectory)/%(_ReleaseRuntime.Identity);ReleaseArchiveExtension=%(_ReleaseRuntime.ReleaseArchiveExtension);ReleaseArchiveAppHostFileName=Nemera.Presentation.Api;ReleaseArchiveVerificationDirectory=$(ReleaseArchiveVerificationDirectory)/%(_ReleaseRuntime.Identity)-api" />
<MSBuild Projects="$(MSBuildThisFileFullPath)" Targets="VerifyReleaseArchive" Properties="ReleaseArchivePath=$(ReleaseArchivesDirectory)/Nemera-$(ReleaseVersion)-%(_ReleaseRuntime.ReleaseArchiveStem)-web%(_ReleaseRuntime.ReleaseArchiveExtension);ReleaseArchiveSourceDirectory=$(WebPublishDirectory)/%(_ReleaseRuntime.Identity);ReleaseArchiveExtension=%(_ReleaseRuntime.ReleaseArchiveExtension);ReleaseArchiveAppHostFileName=Nemera.Presentation.Web;ReleaseArchiveVerificationDirectory=$(ReleaseArchiveVerificationDirectory)/%(_ReleaseRuntime.Identity)-web" />
</Target>
<!-- Verifies that one release contains exactly the native runtime folders that the catalog declares for it, so a folder that never reached the release cannot pass. -->
<Target Name="VerifyReleaseRuntimeDirectories">
<PropertyGroup>
<_ReleaseRuntimeDirectoryPath>$(ApiPublishDirectory)/$(BundledPluginReleaseRuntimeIdentifier)/plugins/runtimes</_ReleaseRuntimeDirectoryPath>
</PropertyGroup>
<Error Condition="!Exists('$(_ReleaseRuntimeDirectoryPath)')" Text="The release for $(BundledPluginReleaseRuntimeIdentifier) is missing the native runtime folder '$(_ReleaseRuntimeDirectoryPath)'." />
<ItemGroup>
<_DeclaredRuntimeDirectory Include="@(BundledPluginRuntimeDirectory)" Condition="'%(BundledPluginRuntimeDirectory.RuntimeIdentifier)' == '$(BundledPluginReleaseRuntimeIdentifier)'" />
<_ExpectedRuntimeDirectory Include="@(_DeclaredRuntimeDirectory->'$(_ReleaseRuntimeDirectoryPath)/%(Identity)')" />
<_ActualRuntimeDirectory Include="$([System.IO.Directory]::GetDirectories('$(_ReleaseRuntimeDirectoryPath)'))" />
<_MissingRuntimeDirectory Include="@(_ExpectedRuntimeDirectory)" Condition="!Exists('%(FullPath)')" />
<_UnexpectedRuntimeDirectory Include="@(_ActualRuntimeDirectory)" Exclude="@(_ExpectedRuntimeDirectory)" />
</ItemGroup>
<Error Condition="'@(_DeclaredRuntimeDirectory)' == ''" Text="The runtime catalog declares no native runtime folders for $(BundledPluginReleaseRuntimeIdentifier)." />
<Error Condition="'@(_MissingRuntimeDirectory)' != ''" Text="The release for $(BundledPluginReleaseRuntimeIdentifier) is missing native runtime folders: @(_MissingRuntimeDirectory)" />
<Error Condition="'@(_UnexpectedRuntimeDirectory)' != ''" Text="The release for $(BundledPluginReleaseRuntimeIdentifier) contains unexpected native runtime folders: @(_UnexpectedRuntimeDirectory)" />
</Target>
<!-- Verifies that one declared native runtime folder in a release contains exactly the files from the bundled plugin staging folder, with identical hashes. -->
<Target Name="VerifyReleaseRuntimeNativeFiles">
<PropertyGroup>
<_NativeSourceDirectory>$(BundledPluginsPublishDirectory)/runtimes/$(BundledPluginRuntimeDirectoryName)</_NativeSourceDirectory>
<_NativeReleaseDirectory>$(ApiPublishDirectory)/$(BundledPluginReleaseRuntimeIdentifier)/plugins/runtimes/$(BundledPluginRuntimeDirectoryName)</_NativeReleaseDirectory>
</PropertyGroup>
<Error Condition="!Exists('$(_NativeSourceDirectory)')" Text="The bundled plugin staging folder is missing the native runtime directory '$(BundledPluginRuntimeDirectoryName)'." />
<ItemGroup>
<_NativeSourceFile Include="$(_NativeSourceDirectory)/**/*" />
<_NativeReleaseFile Include="@(_NativeSourceFile->'$(_NativeReleaseDirectory)/%(RecursiveDir)%(Filename)%(Extension)')" />
<_ActualNativeReleaseFile Include="$(_NativeReleaseDirectory)/**/*" />
<_MissingNativeReleaseFile Include="@(_NativeReleaseFile)" Condition="!Exists('%(FullPath)')" />
</ItemGroup>
<Error Condition="'@(_MissingNativeReleaseFile)' != ''" Text="The release for $(BundledPluginReleaseRuntimeIdentifier) is missing native files: @(_MissingNativeReleaseFile)" />
<GetFileHash Files="@(_NativeSourceFile)" Algorithm="SHA256">
<Output TaskParameter="Items" ItemName="_NativeSourceFileHash" />
</GetFileHash>
<GetFileHash Files="@(_NativeReleaseFile)" Algorithm="SHA256">
<Output TaskParameter="Items" ItemName="_NativeReleaseFileHash" />
</GetFileHash>
<PropertyGroup>
<_NativeSourceHashList>@(_NativeSourceFileHash->'%(FileHash)')</_NativeSourceHashList>
<_NativeReleaseHashList>@(_NativeReleaseFileHash->'%(FileHash)')</_NativeReleaseHashList>
<_NativeSourceFileCount>@(_NativeSourceFile->Count())</_NativeSourceFileCount>
<_ActualNativeFileCount>@(_ActualNativeReleaseFile->Count())</_ActualNativeFileCount>
</PropertyGroup>
<Error Condition="'$(_NativeSourceHashList)' != '$(_NativeReleaseHashList)'" Text="The native files in the release for $(BundledPluginReleaseRuntimeIdentifier) do not match the bundled plugin staging folder." />
<Error Condition="'$(_NativeSourceFileCount)' != '$(_ActualNativeFileCount)'" Text="The release for $(BundledPluginReleaseRuntimeIdentifier) contains an unexpected number of native files." />
</Target>
<!-- Verifies that every runtime folder contains an assembly for every bundled plugin project in the release catalog, so a plugin that never reached a published folder cannot pass. -->
<Target Name="VerifyReleasePluginAssemblies">
<ItemGroup>
<_ExpectedBundledPluginAssembly Include="@(BundledPluginProject->'$(ApiPublishDirectory)/$(BundledPluginReleaseRuntimeIdentifier)/plugins/%(Filename).dll')" />
<_MissingBundledPluginAssembly Include="@(_ExpectedBundledPluginAssembly)" Condition="!Exists('%(FullPath)')" />
</ItemGroup>
<Error Condition="'@(BundledPluginProject)' == ''" Text="The bundled plugin catalog is empty." />
<Error Condition="'@(_MissingBundledPluginAssembly)' != ''" Text="The release for $(BundledPluginReleaseRuntimeIdentifier) is missing bundled plugin assemblies: @(_MissingBundledPluginAssembly)" />
</Target>
<!-- Verifies that one release folder contains exactly the same plugin binaries as the reference published folder that the trust list records. -->
<Target Name="VerifyReleasePluginFiles">
<ItemGroup>
<_ReferencePluginAssembly Include="$(ReferencePluginsDirectory)/Nemera.Plugins.*.dll" />
<_ReleasePluginAssembly Include="@(_ReferencePluginAssembly->'$(ReleasePluginsDirectory)/%(Filename)%(Extension)')" />
<_ActualReleasePluginAssembly Include="$(ReleasePluginsDirectory)/Nemera.Plugins.*.dll" />
<_MissingReleasePluginAssembly Include="@(_ReleasePluginAssembly)" Condition="!Exists('%(FullPath)')" />
<_UnexpectedReleasePluginAssembly Include="@(_ActualReleasePluginAssembly)" Exclude="@(_ReleasePluginAssembly)" />
</ItemGroup>
<Error Condition="'@(_ReferencePluginAssembly)' == ''" Text="No plugin binaries were found in the reference published folder '$(ReferencePluginsDirectory)'." />
<Error Condition="'@(_MissingReleasePluginAssembly)' != ''" Text="The release for $(BundledPluginReleaseRuntimeIdentifier) is missing plugin binaries: @(_MissingReleasePluginAssembly)" />
<Error Condition="'@(_UnexpectedReleasePluginAssembly)' != ''" Text="The release for $(BundledPluginReleaseRuntimeIdentifier) contains unexpected plugin binaries: @(_UnexpectedReleasePluginAssembly)" />
<GetFileHash Files="@(_ReferencePluginAssembly)" Algorithm="SHA256">
<Output TaskParameter="Items" ItemName="_ReferencePluginAssemblyHash" />
</GetFileHash>
<GetFileHash Files="@(_ReleasePluginAssembly)" Algorithm="SHA256">
<Output TaskParameter="Items" ItemName="_ReleasePluginAssemblyHash" />
</GetFileHash>
<PropertyGroup>
<_ReferencePluginAssemblyCount>@(_ReferencePluginAssembly->Count())</_ReferencePluginAssemblyCount>
<_ActualReleasePluginAssemblyCount>@(_ActualReleasePluginAssembly->Count())</_ActualReleasePluginAssemblyCount>
<_ReferencePluginAssemblyHashList>@(_ReferencePluginAssemblyHash->'%(FileHash)')</_ReferencePluginAssemblyHashList>
<_ReleasePluginAssemblyHashList>@(_ReleasePluginAssemblyHash->'%(FileHash)')</_ReleasePluginAssemblyHashList>
</PropertyGroup>
<Error Condition="'$(_ReferencePluginAssemblyCount)' != '$(_ActualReleasePluginAssemblyCount)'" Text="The release for $(BundledPluginReleaseRuntimeIdentifier) contains an unexpected number of plugin binaries." />
<Error Condition="'$(_ReferencePluginAssemblyHashList)' != '$(_ReleasePluginAssemblyHashList)'" Text="The plugin binaries in the release for $(BundledPluginReleaseRuntimeIdentifier) do not match the reference published folder." />
</Target>
<!-- Extracts one release archive and verifies that it contains exactly the files from its publish folder, including their hashes. -->
<Target Name="VerifyReleaseArchive">
<RemoveDir Directories="$(ReleaseArchiveVerificationDirectory)" Condition="Exists('$(ReleaseArchiveVerificationDirectory)')" />
<MakeDir Directories="$(ReleaseArchiveVerificationDirectory)" />
<Exec Condition="'$(ReleaseArchiveExtension)' == '.zip' And '$(OS)' == 'Windows_NT'" Command="tar -xf &quot;$(ReleaseArchivePath)&quot; -C &quot;$(ReleaseArchiveVerificationDirectory)&quot;" />
<Exec Condition="'$(ReleaseArchiveExtension)' == '.zip' And '$(OS)' != 'Windows_NT'" Command="unzip -q &quot;$(ReleaseArchivePath)&quot; -d &quot;$(ReleaseArchiveVerificationDirectory)&quot;" />
<Exec Condition="'$(ReleaseArchiveExtension)' == '.tar.gz'" Command="tar -xzf &quot;$(ReleaseArchivePath)&quot; -C &quot;$(ReleaseArchiveVerificationDirectory)&quot;" />
<!-- Linux and macOS archives must keep the app host executable, which Windows cannot store when it creates the archive. -->
<Exec Condition="'$(ReleaseArchiveExtension)' == '.tar.gz'" Command="tar -tvzf &quot;$(ReleaseArchivePath)&quot;" ConsoleToMSBuild="true" StandardOutputImportance="low">
<Output TaskParameter="ConsoleOutput" ItemName="_TarReleaseArchiveEntry" />
</Exec>
<ItemGroup>
<_ExecutableTarReleaseArchiveEntry Include="@(_TarReleaseArchiveEntry)" Condition="'$(ReleaseArchiveExtension)' == '.tar.gz' And $([System.Text.RegularExpressions.Regex]::IsMatch('%(Identity)', '^-..x.*[ ./]$(ReleaseArchiveAppHostFileName)\s*$'))" />
</ItemGroup>
<Error Condition="'$(ReleaseArchiveExtension)' == '.tar.gz' And '$(ReleaseArchiveAppHostFileName)' == ''" Text="The release archive verification requires the app host file name of the archive." />
<Error Condition="'$(ReleaseArchiveExtension)' == '.tar.gz' And '$(ReleaseArchiveAppHostFileName)' != '' And '@(_ExecutableTarReleaseArchiveEntry)' == ''" Text="The release archive '$(ReleaseArchivePath)' does not store '$(ReleaseArchiveAppHostFileName)' with the executable bit, so it cannot be started after extraction. Create Linux and macOS archives on Linux or macOS." />
<ItemGroup>
<_ArchiveSourceFile Include="$(ReleaseArchiveSourceDirectory)/**/*" />
<_ArchiveExpectedFile Include="@(_ArchiveSourceFile->'$(ReleaseArchiveVerificationDirectory)/%(RecursiveDir)%(Filename)%(Extension)')" />
<_ArchiveExtractedFile Include="$(ReleaseArchiveVerificationDirectory)/**/*" />
<_MissingArchiveFile Include="@(_ArchiveExpectedFile)" Condition="!Exists('%(FullPath)')" />
</ItemGroup>
<Error Condition="'@(_MissingArchiveFile)' != ''" Text="The release archive '$(ReleaseArchivePath)' is missing files: @(_MissingArchiveFile)" />
<GetFileHash Files="@(_ArchiveSourceFile)" Algorithm="SHA256">
<Output TaskParameter="Items" ItemName="_ArchiveSourceFileHash" />
</GetFileHash>
<GetFileHash Files="@(_ArchiveExpectedFile)" Algorithm="SHA256">
<Output TaskParameter="Items" ItemName="_ArchiveVerificationFileHash" />
</GetFileHash>
<PropertyGroup>
<_ArchiveSourceHashList>@(_ArchiveSourceFileHash->'%(FileHash)')</_ArchiveSourceHashList>
<_ArchiveExtractedHashList>@(_ArchiveVerificationFileHash->'%(FileHash)')</_ArchiveExtractedHashList>
<_ArchiveSourceFileCount>@(_ArchiveSourceFile->Count())</_ArchiveSourceFileCount>
<_ArchiveExtractedFileCount>@(_ArchiveExtractedFile->Count())</_ArchiveExtractedFileCount>
</PropertyGroup>
<Error Condition="'$(_ArchiveSourceHashList)' != '$(_ArchiveExtractedHashList)'" Text="The release archive '$(ReleaseArchivePath)' does not match its publish folder." />
<Error Condition="'$(_ArchiveSourceFileCount)' != '$(_ArchiveExtractedFileCount)'" Text="The release archive '$(ReleaseArchivePath)' contains an unexpected number of files." />
<RemoveDir Directories="$(ReleaseArchiveVerificationDirectory)" Condition="Exists('$(ReleaseArchiveVerificationDirectory)')" />
</Target>
<!-- ApiReleaseRuntimeIdentifier selects the reference publish folder, the recorded hashes and the Docker context, so it must be one of the release catalog runtimes. -->
<Target Name="ValidateReleaseRuntimeIdentifier">
<ItemGroup>
<_UnknownReleaseRuntime Include="$(ApiReleaseRuntimeIdentifier)" Exclude="@(BundledPluginRuntime)" />
</ItemGroup>
<Error Condition="'@(_UnknownReleaseRuntime)' != ''" Text="ApiReleaseRuntimeIdentifier '$(ApiReleaseRuntimeIdentifier)' is not in the release runtime catalog. Use one of: @(BundledPluginRuntime)." />
</Target>
<!-- Official release configuration must be validated before any build or test work starts. -->
<Target Name="ValidateOfficialRelease" DependsOnTargets="ValidateReleaseRuntimeIdentifier">
<Error Condition="'$(OfficialReleaseBuild)' != 'true'" Text="The Publish target is reserved for official release builds. Pass -p:OfficialReleaseBuild=true." />
<Error Condition="'$(Configuration)' != 'Release'" Text="Official releases must be published in the Release configuration." />
</Target>
<!-- Publishes one framework-dependent folder per supported runtime, so every platform gets its native apphost and the bundled plugins, and then packages each folder into its own API and web archive. -->
<Target Name="Publish" DependsOnTargets="ValidateOfficialRelease;ValidateReleaseVersion;VerifyBundledPlugins">
<RemoveDir Directories="$(ApiPublishDirectory)" Condition="Exists('$(ApiPublishDirectory)')" />
<RemoveDir Directories="$(WebPublishDirectory)" Condition="Exists('$(WebPublishDirectory)')" />
<RemoveDir Directories="$(BundledPluginsPublishDirectory)" Condition="Exists('$(BundledPluginsPublishDirectory)')" />
<RemoveDir Directories="$(ReleaseArchivesDirectory)" Condition="Exists('$(ReleaseArchivesDirectory)')" />
<Error Condition="!Exists('$(BundledPluginsCurrentManifest)')" Text="The current bundled plugin manifest '$(BundledPluginsCurrentManifest)' is missing. Build the official plugins before publishing." />
<MakeDir Directories="$(BundledPluginsPublishDirectory)" />
<MSBuild Projects="@(BundledPluginProject)" Targets="Build" Properties="Configuration=$(Configuration);NemeraPluginsOutputDirectory=$(BundledPluginsPublishDirectory)/;OfficialReleaseBuild=$(_OfficialReleaseBuild)" />
<ItemGroup>
<_PublishedBundledPluginAssembly Include="$(BundledPluginsPublishDirectory)/Nemera.Plugins.*.dll" />
</ItemGroup>
<Error Condition="'@(_PublishedBundledPluginAssembly)' == ''" Text="No bundled plugin binaries were deployed to '$(BundledPluginsPublishDirectory)'." />
<GetFileHash Files="@(_PublishedBundledPluginAssembly)" Algorithm="SHA256">
<Output TaskParameter="Items" ItemName="_PublishedBundledPluginHash" />
</GetFileHash>
<WriteLinesToFile File="$(BundledPluginsCurrentManifest)" Lines="@(_PublishedBundledPluginHash->'%(Filename)%(Extension)|%(FileHash)')" Overwrite="true" WriteOnlyWhenDifferent="true" />
<!-- Restore once for every runtime, so the publishes reuse a multi-RID assets file instead of leaving the last single-RID restore behind. The environment variable avoids passing semicolons as a global MSBuild property. -->
<Exec Condition="'$(OS)' == 'Windows_NT'" Command="set &quot;NEMERA_RELEASE_RUNTIME_IDENTIFIERS=@(_ReleaseRuntime)&quot; &amp;&amp; dotnet restore &quot;$(RepoRoot)/src/Nemera.Presentation.Api/Nemera.Presentation.Api.csproj&quot;" />
<Exec Condition="'$(OS)' != 'Windows_NT'" Command="NEMERA_RELEASE_RUNTIME_IDENTIFIERS='@(_ReleaseRuntime)' dotnet restore &quot;$(RepoRoot)/src/Nemera.Presentation.Api/Nemera.Presentation.Api.csproj&quot;" />
<Exec Condition="'$(OS)' == 'Windows_NT'" Command="set &quot;NEMERA_RELEASE_RUNTIME_IDENTIFIERS=@(_ReleaseRuntime)&quot; &amp;&amp; dotnet restore &quot;$(RepoRoot)/src/Nemera.Presentation.Web/Nemera.Presentation.Web.csproj&quot;" />
<Exec Condition="'$(OS)' != 'Windows_NT'" Command="NEMERA_RELEASE_RUNTIME_IDENTIFIERS='@(_ReleaseRuntime)' dotnet restore &quot;$(RepoRoot)/src/Nemera.Presentation.Web/Nemera.Presentation.Web.csproj&quot;" />
<!-- Each publish produces the native launcher for that runtime: .exe on Windows, extensionless on Linux and macOS. -->
<Exec Command="dotnet publish &quot;$(RepoRoot)/src/Nemera.Presentation.Api/Nemera.Presentation.Api.csproj&quot; -c $(Configuration) --no-restore -r %(_ReleaseRuntime.Identity) --self-contained false -o &quot;$(ApiPublishDirectory)/%(_ReleaseRuntime.Identity)&quot; -p:OfficialReleaseBuild=true -p:BundledPluginsCurrentManifest=&quot;$(BundledPluginsCurrentManifest)&quot; -p:BundledPluginsPredeployed=true" />
<Exec Command="dotnet publish &quot;$(RepoRoot)/src/Nemera.Presentation.Web/Nemera.Presentation.Web.csproj&quot; -c $(Configuration) --no-restore -r %(_ReleaseRuntime.Identity) --self-contained false -o &quot;$(WebPublishDirectory)/%(_ReleaseRuntime.Identity)&quot;" />
<MSBuild Projects="$(MSBuildThisFileFullPath)" Targets="CopyBundledPluginsToReleaseRuntime" Properties="Configuration=$(Configuration);ApiPublishDirectory=$(ApiPublishDirectory);BundledPluginsPublishDirectory=$(BundledPluginsPublishDirectory);BundledPluginReleaseRuntimeIdentifier=%(_ReleaseRuntime.Identity)" />
<ItemGroup>
<!-- Release downloads do not need debug symbols. -->
<_PublishedPdb Include="$(ApiPublishDirectory)/**/*.pdb;$(WebPublishDirectory)/**/*.pdb" />
</ItemGroup>
<Delete Files="@(_PublishedPdb)" />
<PropertyGroup>
<_PublishedBundledPlugins>true</_PublishedBundledPlugins>
</PropertyGroup>
</Target>
<!-- Copies only the destination runtime and its declared native folders into one release folder, so a Windows release never contains Linux or macOS native libraries. -->
<Target Name="CopyBundledPluginsToReleaseRuntime">
<ItemGroup>
<!-- Everything outside runtimes is shared by all plugins and is always copied. -->
<_RuntimeBundledPluginSharedFile Include="$(BundledPluginsPublishDirectory)/**/*" Exclude="$(BundledPluginsPublishDirectory)/runtimes/**" />
<!-- The runtime catalog declares the native folders that belong to each release, including family fallbacks such as osx. -->
<_ReleaseNativeRuntimeDirectory Include="@(BundledPluginRuntimeDirectory)" Condition="'%(BundledPluginRuntimeDirectory.RuntimeIdentifier)' == '$(BundledPluginReleaseRuntimeIdentifier)'" />
</ItemGroup>
<Copy SourceFiles="@(_RuntimeBundledPluginSharedFile)" DestinationFiles="@(_RuntimeBundledPluginSharedFile->'$(ApiPublishDirectory)/$(BundledPluginReleaseRuntimeIdentifier)/plugins/%(RecursiveDir)%(Filename)%(Extension)')" />
<MSBuild Projects="$(MSBuildThisFileFullPath)" Targets="CopyBundledPluginRuntimeDirectory" Properties="Configuration=$(Configuration);ApiPublishDirectory=$(ApiPublishDirectory);BundledPluginsPublishDirectory=$(BundledPluginsPublishDirectory);BundledPluginReleaseRuntimeIdentifier=$(BundledPluginReleaseRuntimeIdentifier);BundledPluginRuntimeDirectoryName=%(_ReleaseNativeRuntimeDirectory.Identity)" />
</Target>
<!-- Copies one declared native runtime folder, for example osx, into the release folder of the runtime that needs it. -->
<Target Name="CopyBundledPluginRuntimeDirectory">
<ItemGroup>
<_RuntimeBundledPluginNativeFile Include="$(BundledPluginsPublishDirectory)/runtimes/$(BundledPluginRuntimeDirectoryName)/**/*" />
</ItemGroup>
<Copy SourceFiles="@(_RuntimeBundledPluginNativeFile)" DestinationFiles="@(_RuntimeBundledPluginNativeFile->'$(ApiPublishDirectory)/$(BundledPluginReleaseRuntimeIdentifier)/plugins/runtimes/$(BundledPluginRuntimeDirectoryName)/%(RecursiveDir)%(Filename)%(Extension)')" />
</Target>
<!-- ReleaseVersion becomes the archive file name, so it is validated before any archive path or tar command uses it. -->
<Target Name="ValidateReleaseVersion">
<PropertyGroup>
<_ReleaseVersionIsValid>false</_ReleaseVersionIsValid>
<_ReleaseVersionIsValid Condition="$([System.Text.RegularExpressions.Regex]::IsMatch('$(ReleaseVersion)', '$(_ReleaseVersionPattern)'))">true</_ReleaseVersionIsValid>
</PropertyGroup>
<Error Condition="'$(_ReleaseVersionIsValid)' != 'true'" Text="The release version '$(ReleaseVersion)' is invalid. Pass a three-part version such as 1.2.3, for example -p:Version=1.2.3." />
</Target>
<!-- Builds one API archive and one web archive per runtime from the publish output, so each download contains only that platform's files. It can run directly after a publish. -->
<Target Name="CreateReleaseArchives" AfterTargets="Publish" DependsOnTargets="ValidateReleaseVersion">
<ItemGroup>
<_ArchiveRuntime Include="@(BundledPluginRuntime)" />
<_ArchivePublishOutput Include="@(_ArchiveRuntime->'$(ApiPublishDirectory)/%(Identity)/Nemera.Presentation.Api.dll')" />
<_ArchivePublishOutput Include="@(_ArchiveRuntime->'$(WebPublishDirectory)/%(Identity)/Nemera.Presentation.Web.dll')" />
<_MissingArchivePublishOutput Include="@(_ArchivePublishOutput)" Condition="!Exists('%(FullPath)')" />
</ItemGroup>
<Error Condition="'@(_MissingArchivePublishOutput)' != ''" Text="The release archives cannot be created because the publish output is missing. Run the Publish target first, which also creates the archives: @(_MissingArchivePublishOutput)" />
<RemoveDir Directories="$(ReleaseArchivesDirectory)" Condition="Exists('$(ReleaseArchivesDirectory)')" />
<MakeDir Directories="$(ReleaseArchivesDirectory)" />
<!-- Windows cannot store the executable bit in a tar archive, so it must not create the Linux and macOS archives. -->
<Warning Condition="'$(OS)' == 'Windows_NT'" Text="Windows cannot store the executable bit in a tar archive, so the Linux and macOS archives are not created. Run the archive targets on Linux or macOS to create them." />
<!-- Windows archives are zip files. Linux and macOS archives are tar.gz so the executable permission survives the download. -->
<ZipDirectory SourceDirectory="$(ApiPublishDirectory)/%(_ArchiveRuntime.Identity)" DestinationFile="$(ReleaseArchivesDirectory)/Nemera-$(ReleaseVersion)-%(_ArchiveRuntime.ReleaseArchiveStem)-api%(_ArchiveRuntime.ReleaseArchiveExtension)" Condition="'%(_ArchiveRuntime.ReleaseArchiveExtension)' == '.zip'" />
<ZipDirectory SourceDirectory="$(WebPublishDirectory)/%(_ArchiveRuntime.Identity)" DestinationFile="$(ReleaseArchivesDirectory)/Nemera-$(ReleaseVersion)-%(_ArchiveRuntime.ReleaseArchiveStem)-web%(_ArchiveRuntime.ReleaseArchiveExtension)" Condition="'%(_ArchiveRuntime.ReleaseArchiveExtension)' == '.zip'" />
<Exec Command="tar -czf &quot;$(ReleaseArchivesDirectory)/Nemera-$(ReleaseVersion)-%(_ArchiveRuntime.ReleaseArchiveStem)-api%(_ArchiveRuntime.ReleaseArchiveExtension)&quot; -C &quot;$(ApiPublishDirectory)/%(_ArchiveRuntime.Identity)&quot; ." Condition="'%(_ArchiveRuntime.ReleaseArchiveExtension)' == '.tar.gz' And '$(OS)' != 'Windows_NT'" />
<Exec Command="tar -czf &quot;$(ReleaseArchivesDirectory)/Nemera-$(ReleaseVersion)-%(_ArchiveRuntime.ReleaseArchiveStem)-web%(_ArchiveRuntime.ReleaseArchiveExtension)&quot; -C &quot;$(WebPublishDirectory)/%(_ArchiveRuntime.Identity)&quot; ." Condition="'%(_ArchiveRuntime.ReleaseArchiveExtension)' == '.tar.gz' And '$(OS)' != 'Windows_NT'" />
</Target>
<!-- Official images must be Release builds and must use a glibc Linux runtime, because the image base is a glibc distribution and copies the published folder of that architecture. -->
<Target Name="ValidateDockerRelease" Condition="'$(_OfficialReleaseBuild)' == 'true'" DependsOnTargets="ValidateReleaseRuntimeIdentifier">
<Error Condition="'$(Configuration)' != 'Release'" Text="Official Docker images must be built in the Release configuration." />
<ItemGroup>
<_DockerReleaseRuntime Include="linux-x64;linux-arm64" />
<_NonGlibcDockerReleaseRuntime Include="$(ApiReleaseRuntimeIdentifier)" Exclude="@(_DockerReleaseRuntime)" />
</ItemGroup>
<Error Condition="'@(_NonGlibcDockerReleaseRuntime)' != ''" Text="Official Docker images require a glibc Linux runtime identifier, because the image base is a glibc distribution. Use one of: @(_DockerReleaseRuntime). The linux-musl-x64 runtime cannot be used with these images." />
<Error Condition="'$(_DockerPlatform)' == ''" Text="No Docker platform is defined for ApiReleaseRuntimeIdentifier '$(ApiReleaseRuntimeIdentifier)'." />
<!-- A reused publish output skips the publish-folder verification, so at least require the folders the images are built from. -->
<Error Condition="'$(ReusePublishedBundledPlugins)' == 'true' And !Exists('$(ApiPublishDirectory)/$(ApiReleaseRuntimeIdentifier)/Nemera.Presentation.Api.dll')" Text="ReusePublishedBundledPlugins=true requires a published API folder at '$(ApiPublishDirectory)/$(ApiReleaseRuntimeIdentifier)'. Publish the release first." />
<Error Condition="'$(ReusePublishedBundledPlugins)' == 'true' And !Exists('$(WebPublishDirectory)/$(ApiReleaseRuntimeIdentifier)/Nemera.Presentation.Web.dll')" Text="ReusePublishedBundledPlugins=true requires a published web folder at '$(WebPublishDirectory)/$(ApiReleaseRuntimeIdentifier)'. Publish the release first." />
</Target>
<!-- The image tag comes from Version and is passed to docker build, so it must be a plain release version or the local latest default, never a shell fragment. -->
<Target Name="ValidateDockerImageVersion">
<PropertyGroup>
<_DockerImageVersionIsValid>false</_DockerImageVersionIsValid>
<_DockerImageVersionIsValid Condition="'$(_ImageVersion)' == 'latest'">true</_DockerImageVersionIsValid>
<_DockerImageVersionIsValid Condition="$([System.Text.RegularExpressions.Regex]::IsMatch('$(_ImageVersion)', '$(_ReleaseVersionPattern)'))">true</_DockerImageVersionIsValid>
</PropertyGroup>
<Error Condition="'$(_DockerImageVersionIsValid)' != 'true'" Text="The Docker image version '$(Version)' is invalid. Pass a three-part version such as 1.2.3, for example -p:Version=1.2.3." />
</Target>
<!-- Official images build from a publish folder that the preceding step verified, and local images build from source so docker compose keeps working without a prior publish. -->
<Target Name="Docker" DependsOnTargets="ValidateDockerImageVersion;$(_DockerDependsOn)">
<PropertyGroup>
<_DockerApiDockerfile Condition="'$(_OfficialReleaseBuild)' == 'true'">$(RepoRoot)/src/Nemera.Presentation.Api/Dockerfile.release</_DockerApiDockerfile>
<_DockerApiDockerfile Condition="'$(_OfficialReleaseBuild)' != 'true'">$(RepoRoot)/src/Nemera.Presentation.Api/Dockerfile</_DockerApiDockerfile>
<_DockerApiContext Condition="'$(_OfficialReleaseBuild)' == 'true'">$(ApiPublishDirectory)/$(ApiReleaseRuntimeIdentifier)</_DockerApiContext>
<_DockerApiContext Condition="'$(_OfficialReleaseBuild)' != 'true'">$(RepoRoot)</_DockerApiContext>
<_DockerWebDockerfile Condition="'$(_OfficialReleaseBuild)' == 'true'">$(RepoRoot)/src/Nemera.Presentation.Web/Dockerfile.release</_DockerWebDockerfile>
<_DockerWebDockerfile Condition="'$(_OfficialReleaseBuild)' != 'true'">$(RepoRoot)/src/Nemera.Presentation.Web/Dockerfile</_DockerWebDockerfile>
<_DockerWebContext Condition="'$(_OfficialReleaseBuild)' == 'true'">$(WebPublishDirectory)/$(ApiReleaseRuntimeIdentifier)</_DockerWebContext>
<_DockerWebContext Condition="'$(_OfficialReleaseBuild)' != 'true'">$(RepoRoot)</_DockerWebContext>
</PropertyGroup>
<Exec Command="docker build $(_DockerPlatformOption)-t &quot;nemera-api:$(_ImageVersion)&quot; -f &quot;$(_DockerApiDockerfile)&quot; &quot;$(_DockerApiContext)&quot;" />
<Exec Command="docker build $(_DockerPlatformOption)-t &quot;nemera-web:$(_ImageVersion)&quot; -f &quot;$(_DockerWebDockerfile)&quot; &quot;$(_DockerWebContext)&quot;" />
</Target>
<!-- Recording is only allowed from a clean GitHub Actions checkout whose commit is the current origin/master. -->
<Target Name="ValidateBundledPluginsRecording">
<Error Condition="'$(AllowBundledPluginsRecording)' != 'true'" Text="Recording bundled plugin hashes is reserved for the release workflow. Pass -p:AllowBundledPluginsRecording=true only from CI." />
<Error Condition="'$(GITHUB_ACTIONS)' != 'true'" Text="Recording bundled plugin hashes is only allowed in GitHub Actions." />
<PropertyGroup>
<_ReleaseBranchIsValid>false</_ReleaseBranchIsValid>
<_ReleaseBranchIsValid Condition="'$(ReleaseBranch)' != '' And $(ReleaseBranch.StartsWith('release/'))">true</_ReleaseBranchIsValid>
</PropertyGroup>
<Error Condition="'$(_ReleaseBranchIsValid)' != 'true'" Text="Recording bundled plugin hashes requires a release/x.y.z branch." />
<Exec Command="git status --porcelain" WorkingDirectory="$(RepoRoot)" ConsoleToMSBuild="true">
<Output TaskParameter="ConsoleOutput" ItemName="_BundledPluginsGitStatus" />
</Exec>
<Error Condition="'@(_BundledPluginsGitStatus)' != ''" Text="Recording bundled plugin hashes requires a clean worktree. Commit or stash the changes first." />
<Exec Command="git rev-parse HEAD" WorkingDirectory="$(RepoRoot)" ConsoleToMSBuild="true">
<Output TaskParameter="ConsoleOutput" ItemName="_BundledPluginsHead" />
</Exec>
<Exec Command="git rev-parse origin/master" WorkingDirectory="$(RepoRoot)" ConsoleToMSBuild="true">
<Output TaskParameter="ConsoleOutput" ItemName="_BundledPluginsMasterHead" />
</Exec>
<Error Condition="'@(_BundledPluginsHead)' != '@(_BundledPluginsMasterHead)'" Text="Recording bundled plugin hashes requires the release branch to point at origin/master. Merge the previous trust-list pull request and recreate the release branch from master." />
</Target>
<Target Name="RecordBundledPlugins" DependsOnTargets="ValidateBundledPluginsRecording;Publish;VerifyPublishedBundledPlugins;VerifyReleaseArchives;RecordBundledPluginsFromPublish" />
<!-- VerifyPublishedBundledPlugins has already checked the embedded manifest against every published API folder and compared every runtime folder's plugin binaries with the reference folder, so the reference folder is representative of every shipped platform. -->
<Target Name="RecordBundledPluginsFromPublish" DependsOnTargets="ValidateOfficialRelease;ValidateBundledPluginsRecording;VerifyPublishedBundledPlugins">
<ItemGroup>
<_BundledPluginPublishedAssembly Include="$(ApiPublishDirectory)/$(ApiReleaseRuntimeIdentifier)/plugins/Nemera.Plugins.*.dll" />
</ItemGroup>
<GetFileHash Files="@(_BundledPluginPublishedAssembly)" Algorithm="SHA256">
<Output TaskParameter="Items" ItemName="_RecordedBundledPluginHash" />
</GetFileHash>
<Error Condition="'@(_RecordedBundledPluginHash)' == ''" Text="No bundled plugin binaries were found in '$(ApiPublishDirectory)/$(ApiReleaseRuntimeIdentifier)/plugins'." />
<Error Condition="!Exists('$(BundledPluginsManifest)')" Text="The bundled plugin trust manifest '$(BundledPluginsManifest)' is missing." />
<ReadLinesFromFile File="$(BundledPluginsManifest)">
<Output TaskParameter="Lines" ItemName="_ExistingBundledPluginManifestLine" />
</ReadLinesFromFile>
<ItemGroup>
<_NewBundledPluginManifestLine Include="@(_RecordedBundledPluginHash->'%(Filename)%(Extension)|%(FileHash)')" />
<_CombinedBundledPluginManifestLine Include="@(_ExistingBundledPluginManifestLine);@(_NewBundledPluginManifestLine)" />
</ItemGroup>
<!-- Rewrite the manifest as a deduplicated merge so a repeated run cannot append the same hash twice. -->
<RemoveDuplicates Inputs="@(_CombinedBundledPluginManifestLine)">
<Output TaskParameter="Filtered" ItemName="_DeduplicatedBundledPluginManifestLine" />
</RemoveDuplicates>
<WriteLinesToFile File="$(BundledPluginsManifest)" Lines="@(_DeduplicatedBundledPluginManifestLine)" Overwrite="true" />
</Target>
<Target Name="CI" DependsOnTargets="Test">
<Message Text="CI build complete." Importance="high" />
</Target>
</Project>