Browse Source

Improved loading native assets for .NET Framework 4 and mysteriously failing Core. Honestly, I'm loosing my mind due to dotnet csproj inconsistencies

MarcinZiabek 1 year ago
parent
commit
e59336b7a1

+ 14 - 0
Source/QuestPDF/Build/net4/QuestPDF.targets

@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+    <ItemGroup>
+        <None Include="$(MSBuildThisFileDirectory)\..\..\runtimes\**\*.*">
+            <Link>runtimes\%(RecursiveDir)%(Filename)%(Extension)</Link>
+            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+        </None>
+
+        <None Include="$(MSBuildThisFileDirectory)\..\..\runtimes\any\native\LatoFont\*.*">
+            <Link>LatoFont\%(Filename)%(Extension)</Link>
+            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+        </None>
+    </ItemGroup>
+</Project>

+ 5 - 3
Source/QuestPDF/QuestPDF.csproj

@@ -3,7 +3,7 @@
         <Authors>MarcinZiabek</Authors>
         <Company>CodeFlint</Company>
         <PackageId>QuestPDF</PackageId>
-        <Version>2024.3.0-internal91</Version>
+        <Version>2024.3.0-betaint16</Version>
         <PackageDescription>QuestPDF is an open-source, modern and battle-tested library that can help you with generating PDF documents by offering friendly, discoverable and predictable C# fluent API. Easily generate PDF reports, invoices, exports, etc.</PackageDescription>
         <PackageReleaseNotes>$([System.IO.File]::ReadAllText("$(MSBuildProjectDirectory)/Resources/ReleaseNotes.txt"))</PackageReleaseNotes>
         <LangVersion>10</LangVersion>
@@ -53,9 +53,11 @@
             <PackagePath>runtimes\%(RecursiveDir)%(Filename)%(Extension)</PackagePath>
         </None>
 
-        <None Include="QuestPDF.targets">
-            <PackagePath>build\</PackagePath>
+        <None Include="Build\**\*.*">
+            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+            <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
             <Pack>true</Pack>
+            <PackagePath>build\%(RecursiveDir)%(Filename)%(Extension)</PackagePath>
         </None>
     </ItemGroup>
 </Project>

+ 0 - 19
Source/QuestPDF/QuestPDF.targets

@@ -1,19 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
-    <ItemGroup>
-        <None Include="$(MSBuildThisFileDirectory)\..\runtimes\win-x64\native\QuestPdfSkia.dll" Condition="$(TargetFramework.StartsWith('net4'))">
-            <Link>QuestPdfSkia.dll</Link>
-            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
-        </None>
-
-        <None Include="$(MSBuildThisFileDirectory)\..\runtimes\win-x64\native\icudtl.dat" Condition="$(TargetFramework.StartsWith('net4'))">
-            <Link>icudtl.dat</Link>
-            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
-        </None>
-
-        <None Include="$(MSBuildThisFileDirectory)\..\runtimes\any\native\LatoFont\*.*" Condition="$(TargetFramework.StartsWith('net4'))">
-            <Link>LatoFont\%(Filename)%(Extension)</Link>
-            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
-        </None>
-    </ItemGroup>
-</Project>

+ 18 - 6
Source/QuestPDF/Skia/SkNativeDependencyCompatibilityChecker.cs

@@ -10,6 +10,10 @@ public static class SkNativeDependencyCompatibilityChecker
         
     public static void Test()
     {
+        const string exceptionBaseMessage = "The QuestPDF library has encountered an issue while loading one of its dependencies.";
+        var newLine = Environment.NewLine;
+        var paragraph = newLine + newLine;
+        
         if (IsCompatibilityChecked)
             return;
             
@@ -22,9 +26,19 @@ public static class SkNativeDependencyCompatibilityChecker
             IsCompatibilityChecked = true;
             return;
         }
-        
-        if (SkNativeDependencyProvider.IsCurrentPlatformSupported())
-            throw new InitializationException($"Your runtime is currently not supported by QuestPDF.");
+
+        if (!SkNativeDependencyProvider.IsCurrentPlatformSupported())
+        {
+            var message = 
+                $"{exceptionBaseMessage}{paragraph}" +
+                "Your runtime is currently not supported by QuestPDF. " +
+                "Currently supported runtimes are: win-x64, linux-x64, osx-x64, osx-arm64.";
+            
+            if (RuntimeInformation.ProcessArchitecture == Architecture.X64)
+                message += $"{paragraph}Please consider setting the 'Platform target' property to 'x64' in your project settings.";
+            
+            throw new InitializationException(message, innerException);
+        }
         
         // detect platform, copy appropriate native files and test compatibility again
         SkNativeDependencyProvider.EnsureNativeFileAvailability();
@@ -37,9 +51,7 @@ public static class SkNativeDependencyCompatibilityChecker
             return;
         }
 
-        var initializationExceptionMessage = $"The QuestPDF library has encountered an issue while loading one of its dependencies.";
-        
-        throw new Exception(initializationExceptionMessage, innerException);
+        throw new Exception(exceptionBaseMessage, innerException);
     }
     
     private static Exception? CheckIfExceptionIsThrownWhenLoadingNativeDependencies()

+ 2 - 2
Source/QuestPDF/Skia/SkNativeDependencyProvider.cs

@@ -24,7 +24,7 @@ internal static class SkNativeDependencyProvider
     {
         try
         {
-            EnsureNativeFileAvailability();
+            GetRuntimePlatform();
             return true;
         }
         catch
@@ -60,7 +60,7 @@ internal static class SkNativeDependencyProvider
                 return "osx-arm64";
         }
 
-        throw new InitializationException("Your runtime is currently not supported by QuestPDF");
+        throw new InitializationException("Your runtime is currently not supported by QuestPDF.");
     }
         
     static string GetNativeFileRuntimePath(string fileName)