Skip to content

Instantly share code, notes, and snippets.

@mattjohnsonpint
Last active October 19, 2023 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattjohnsonpint/37d3ac06a563c41618fc933bf7282d66 to your computer and use it in GitHub Desktop.
Save mattjohnsonpint/37d3ac06a563c41618fc933bf7282d66 to your computer and use it in GitHub Desktop.
Workaround for Visual Studio Hot Restart issue
<!--
This target ensures that iOS Frameworks and XCFrameworks are collected from NuGet packages when using Hot Restart.
_UnpackHotRestartFrameworkAssemblies doesn't work for NuGet packages, because the frameworks aren't stored as embedded resources, but just files in the package.
-->
<Target Name="_CollectHotRestartFrameworksFromPackages" BeforeTargets="_CollectHotRestartFrameworks" Condition="'$(IsHotRestartBuild)' == 'true'">
<!--
Find all resources within NuGet packages.
For example, a package with assembly Foo.dll will have an adjacent Foo.resources folder or a Foo.resources.zip file.
-->
<ItemGroup>
<_PackageResourcesDirs Include="@(AssembliesWithFrameworks -> '%(RootDir)%(Directory)%(FileName).resources')"
Condition="Exists('%(RootDir)%(Directory)%(FileName).resources')" />
<_PackageResourcesZips Include="@(AssembliesWithFrameworks -> '%(RootDir)%(Directory)%(FileName).resources.zip')"
Condition="Exists('%(RootDir)%(Directory)%(FileName).resources.zip')" />
</ItemGroup>
<!--
For zipped resources, we'll need to unpack them somewhere.
The DeviceSpecificIntermediateOutputPath can get too long and hit max path, so we'll work from TEMP instead.
-->
<PropertyGroup>
<HotRestartPackageResourcesDir>$(TEMP)\Xamarin\HotRestart\Resources\</HotRestartPackageResourcesDir>
</PropertyGroup>
<Unzip ZipFilePath="@(_PackageResourcesZips)"
ExtractionPath="$(HotRestartPackageResourcesDir)%(NuGetPackageId)\%(NuGetPackageVersion)"
Condition="'@(_PackageResourcesZips)' != '' And !Exists('$(HotRestartPackageResourcesDir)%(NuGetPackageId)\%(NuGetPackageVersion)')" />
<!-- Add the folders where we unzipped each file to _PackageResourcesDirs -->
<ItemGroup Condition="'@(_PackageResourcesZips)' != ''">
<_PackageResourcesDirs Include="@(_PackageResourcesZips -> '$(HotRestartPackageResourcesDir)%(NuGetPackageId)\%(NuGetPackageVersion)')" />
</ItemGroup>
<!-- If we have any _PackageResourcesDirs (from either originally zipped or unzipped), scan them for frameworks. -->
<ItemGroup Condition="'@(_PackageResourcesDirs)' != ''">
<!-- Regular frameworks will be found immediately in the root of the resources folder. -->
<_PackageResourcesFrameworkFiles Include="%(_PackageResourcesDirs.Identity)\*.framework\*" />
<!-- XCFrameworks will have multiple targets. We need only the framework from the ios-arm64 target. -->
<_PackageResourcesFrameworkFiles Include="%(_PackageResourcesDirs.Identity)\**\*.xcframework\ios-arm64\*.framework\*" />
<!-- Condense the file list to the exact list of frameworks we're adding. -->
<_HotRestartFrameworksFromPackages Include="@(_PackageResourcesFrameworkFiles -> '%(RootDir)%(Directory)')" KeepDuplicates="false" />
<!-- Finally, add them to _HotRestartFrameworks, which is used by the existing _CollectHotRestartFrameworks target. -->
<_HotRestartFrameworks Include="@(_HotRestartFrameworksFromPackages -> TrimEnd('\'))" />
</ItemGroup>
</Target>
@ozanyasindogan
Copy link

Thank's a lot, that helped with DevExpress MAUI controls.

@Yahya0007
Copy link

Thanks. Worked for DX MAUI

@SamuelBachar
Copy link

Is there way how to do it on Windows 10 and Visual Studio Version 17.7.5 Microsoft Visual Studio Community 2022 (64-bit) ?

I get and error Given file is not a Mach-O file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment