Skip to content
This repository has been archived by the owner on Dec 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #59 from FastReports/FixedPublishWinx64AndTrimmed
Browse files Browse the repository at this point in the history
Fixed publish self-contained win-x64. Fixed publish with Trimmed option
  • Loading branch information
KirillKornienko authored Aug 17, 2021
2 parents 58fc5ce + e3d8000 commit e0ecd67
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 15 deletions.
15 changes: 11 additions & 4 deletions src/shared/DotNetClasses/CSharpCodeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ public class CSharpCodeProvider : CodeDomProvider

public override CompilerResults CompileAssemblyFromSource(CompilerParameters cp, string code)
{
#if DEBUG
Console.WriteLine("FR.Compat: " +
#if NETSTANDARD
"NETSTANDARD"
#elif NETCOREAPP
"NETCOREAPP"
#endif
);
#endif

SyntaxTree codeTree = CSharpSyntaxTree.ParseText(code);
CSharpCompilationOptions options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary,
optimizationLevel: OptimizationLevel.Release,
Expand All @@ -27,11 +37,8 @@ public override CompilerResults CompileAssemblyFromSource(CompilerParameters cp,

List<MetadataReference> references = new List<MetadataReference>();

AddReferences(cp, references);

foreach (string reference in cp.ReferencedAssemblies)
references.Add(GetReference(reference));

AddExtraAssemblies(cp.ReferencedAssemblies, references);

Compilation compilation = CSharpCompilation.Create(
"_" + Guid.NewGuid().ToString("D"), new SyntaxTree[] { codeTree },
Expand Down
107 changes: 103 additions & 4 deletions src/shared/DotNetClasses/CodeDomProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,62 @@ public abstract class CodeDomProvider : IDisposable

public event EventHandler<CompilationEventArgs> BeforeEmitCompilation;

#if NETCOREAPP
/// <summary>
/// If these assemblies were not found when 'trimmed', then skip them
/// </summary>
protected static readonly string[] SkippedAssemblies = new string[] {
"System.dll",

"System.Drawing.dll",

//"System.Drawing.Primitives",

"System.Data.dll",

"System.Xml.dll",
};
#endif

protected void AddReferences(CompilerParameters cp, List<MetadataReference> references)
{
foreach (string reference in cp.ReferencedAssemblies)
{
#if DEBUG
Console.WriteLine($"TRY ADD {reference}.");
#endif
#if NETCOREAPP
try
{
#endif
references.Add(GetReference(reference));
#if NETCOREAPP
}
catch (FileNotFoundException e)
{
if (SkippedAssemblies.Contains(reference))
{
#if DEBUG
Console.WriteLine($"{reference} FileNotFound. SKIPPED");
#endif
continue;
}
else
throw e;
}
#endif

#if DEBUG
Console.WriteLine($"{reference} ADDED");
#endif
}
#if DEBUG
Console.WriteLine("AFTER ADDING ReferencedAssemblies");
#endif

AddExtraAssemblies(cp.ReferencedAssemblies, references);
}


protected void AddExtraAssemblies(StringCollection referencedAssemblies, List<MetadataReference> references)
{
Expand Down Expand Up @@ -52,7 +108,25 @@ protected void AddExtraAssemblies(StringCollection referencedAssemblies, List<Me
foreach(string assembly in assemblies)
{
if (!referencedAssemblies.Contains(assembly))
references.Add(GetReference(assembly));
{
#if NETCOREAPP
try
{
#endif
references.Add(GetReference(assembly));
#if NETCOREAPP
}
// If user run 'dotnet publish' with Trimmed - dotnet cut some extra assemblies.
// We skip this error, because some assemblies in 'assemblies' array may not be needed
catch (FileNotFoundException)
{
#if DEBUG
Console.WriteLine($"{assembly} FILENOTFOUND. SKIPPED");
#endif
continue;
}
#endif
}
}
}

Expand All @@ -76,7 +150,7 @@ public MetadataReference GetReference(string refDll)
return cache[refDll];

reference = refDll.EndsWith(".dll") || refDll.EndsWith(".exe") ?
refDll.Substring(0, refDll.Length - 4) : refDll;
refDll.Substring(0, refDll.Length - 4) : refDll;
if (!refDll.Contains(Path.DirectorySeparatorChar))
{
#if NETCOREAPP
Expand All @@ -87,6 +161,9 @@ public MetadataReference GetReference(string refDll)
{
if (loadedAssembly.GetName().Name == reference)
{
#if DEBUG
Console.WriteLine("FIND IN AssemblyLoadContext");
#endif
string location = loadedAssembly.Location;
if (string.IsNullOrEmpty(location))
{
Expand All @@ -107,6 +184,9 @@ public MetadataReference GetReference(string refDll)
{
if (string.Compare(currAssembly.GetName().Name, reference, true) == 0)
{
#if DEBUG
Console.WriteLine("FIND IN AppDomain");
#endif
// Found it, return the location as the full reference.
result = MetadataReference.CreateFromFile(currAssembly.Location);
cache[refDll] = result;
Expand All @@ -119,6 +199,9 @@ public MetadataReference GetReference(string refDll)
{
if (name.Name == reference)
{
#if DEBUG
Console.WriteLine("FIND IN ReferencedAssemblies");
#endif
#if NETCOREAPP
// try load Assembly in runtime (for user script with custom assembly)
var assembly = AssemblyLoadContext.Default.LoadFromAssemblyName(name);
Expand Down Expand Up @@ -170,16 +253,29 @@ public MetadataReference GetReference(string refDll)
catch
{
var assemblyName = new AssemblyName(reference);
#if DEBUG
Console.WriteLine("IN AssemblyName");
#endif

#if NETCOREAPP
// try load Assembly in runtime (for user script with custom assembly)
var assembly = AssemblyLoadContext.Default.LoadFromAssemblyName(assemblyName);
#else
var assembly = Assembly.Load(assemblyName);
#endif

string location = assembly.Location;
#if DEBUG
Console.WriteLine($"Location after LoadFromAssemblyName: {location}");
#endif

result = MetadataReference.CreateFromFile(location);
#if NETCOREAPP
if(string.IsNullOrEmpty(location))
{
result = GetMetadataReferenceInSingleFileApp(assembly);
}
else
#endif
result = MetadataReference.CreateFromFile(location);
cache[refDll] = result;
return result;
}
Expand All @@ -188,6 +284,9 @@ public MetadataReference GetReference(string refDll)
#if NETCOREAPP
private static unsafe MetadataReference GetMetadataReferenceInSingleFileApp(Assembly assembly)
{
#if DEBUG
Console.WriteLine($"TRY IN UNSAFE METHOD {assembly.GetName().Name}");
#endif
assembly.TryGetRawMetadata(out byte* blob, out int length);
var moduleMetadata = ModuleMetadata.CreateFromMetadata((IntPtr)blob, length);
var assemblyMetadata = AssemblyMetadata.Create(moduleMetadata);
Expand Down
8 changes: 1 addition & 7 deletions src/shared/DotNetClasses/VBCodeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,7 @@ public override CompilerResults CompileAssemblyFromSource(CompilerParameters cp,
List<MetadataReference> references = new List<MetadataReference>();


foreach (string reference in cp.ReferencedAssemblies)
references.Add(GetReference(reference));

AddExtraAssemblies(cp.ReferencedAssemblies, references);



AddReferences(cp, references);

Compilation compilation = VisualBasicCompilation.Create(
"_" + Guid.NewGuid().ToString("D"), new SyntaxTree[] { codeTree },
Expand Down

0 comments on commit e0ecd67

Please sign in to comment.