Skip to content

Commit

Permalink
re-implemented logic of CreateTempDirectory and processing intermed…
Browse files Browse the repository at this point in the history
…iate files when RunIlAsm

related issue: #73
  • Loading branch information
3F committed Jun 9, 2020
1 parent 8f56f66 commit 91f2445
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 116 deletions.
72 changes: 46 additions & 26 deletions RGiesecke.DllExport/DllExportWeaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Permissions;
using System.Text;
using RGiesecke.DllExport.Extensions;
using RGiesecke.DllExport.Parsing;
using RGiesecke.DllExport.Properties;
using TempDir = RGiesecke.DllExport.Utilities.TempDir;

namespace RGiesecke.DllExport
{
Expand Down Expand Up @@ -70,38 +71,47 @@ public void Run()
}
}
}
if(this.Exports.Count == 0)

if(Exports.Count == 0)
{
return;
}
using(this.GetNotifier().CreateContextName((object)this, Resources.CreateTempDirectoryContextName))

using(GetNotifier().CreateContextName(this, Resources.CreateTempDirectoryContextName))
using(var dir = new TempDir())
{
using(ValueDisposable<string> tempDirectory = Utilities.CreateTempDirectory())
RunIlDasm(dir.FullPath);

void _ilasm() => RunIlAsm(dir.FullPath);

if(InputValues.LeaveIntermediateFiles.IsTrue())
{
this.RunIlDasm(tempDirectory.Value);
bool flag = ((IEnumerable<string>)new string[2] { "true", "yes" }).Any<string>((Func<string, bool>)(t => t.Equals(this.InputValues.LeaveIntermediateFiles, StringComparison.InvariantCultureIgnoreCase)));
if(flag)
{
using(this.GetNotifier().CreateContextName((object)this, Resources.CopyBeforeContextName))
DllExportWeaver.CopyDirectory(tempDirectory.Value, Path.Combine(Path.GetDirectoryName(this.InputValues.OutputFileName), "Before"), true);
}
using(IlAsm ilAsm = this.PrepareIlAsm(tempDirectory.Value))
this.RunIlAsm(ilAsm);
if(!flag)
{
return;
}
using(this.GetNotifier().CreateContextName((object)this, Resources.CopyAfterContextName))
DllExportWeaver.CopyDirectory(tempDirectory.Value, Path.Combine(Path.GetDirectoryName(this.InputValues.OutputFileName), "After"), true);
MakeWithIntermediateFiles(dir, _ilasm);
return;
}

_ilasm();
}
}

private IDllExportNotifier GetNotifier()
private void MakeWithIntermediateFiles(TempDir dir, Action ilasm)
{
return this.ServiceProvider.GetService<IDllExportNotifier>();
const string _PRE = "Before";
const string _POST = "After";

string fout = Path.GetDirectoryName(InputValues.OutputFileName);

using(GetNotifier().CreateContextName(this, Resources.CopyBeforeContextName))
CopyDirectory(dir.FullPath, Path.Combine(fout, _PRE), true);

ilasm?.Invoke();

using(GetNotifier().CreateContextName(this, Resources.CopyAfterContextName))
CopyDirectory(dir.FullPath, Path.Combine(fout, _POST), true);
}

private IDllExportNotifier GetNotifier() => ServiceProvider.GetService<IDllExportNotifier>();

private static string GetCleanedDirectoryPath(string path)
{
if(path == null)
Expand Down Expand Up @@ -144,16 +154,24 @@ private static void CopyDirectory(string sourceDirectory, string destinationDire
}
}

private void RunIlAsm(IlAsm ilAsm)
private void RunIlAsm(string tempDir)
{
using(GetNotifier().CreateContextName(this, "RunIlAsm"))
using(IlAsm ilAsm = PrepareIlAsm(tempDir ?? throw new ArgumentNullException(nameof(tempDir))))
{
RunIlAsm(ilAsm);
}
}

private IlAsm RunIlAsm(IlAsm ilAsm)
{
using(GetNotifier().CreateContextName(this, nameof(RunIlAsm)))
{
if(InputValues.Cpu != CpuPlatform.AnyCpu) {
reassembleFile(ilAsm, InputValues.OutputFileName, "", InputValues.Cpu);
return;
reassembleFile(ilAsm, InputValues.OutputFileName, string.Empty, InputValues.Cpu);
return ilAsm;
}

string dir = Path.GetDirectoryName(InputValues.OutputFileName) ?? "";
string dir = Path.GetDirectoryName(InputValues.OutputFileName) ?? string.Empty;
string fileName = Path.GetFileName(InputValues.OutputFileName);

if(!Directory.Exists(dir)) {
Expand All @@ -163,6 +181,8 @@ private void RunIlAsm(IlAsm ilAsm)
reassembleFile(ilAsm, Path.Combine(Path.Combine(dir, "x86"), fileName), ".x86", CpuPlatform.X86);
reassembleFile(ilAsm, Path.Combine(Path.Combine(dir, "x64"), fileName), ".x64", CpuPlatform.X64);
}

return ilAsm;
}

private IlAsm PrepareIlAsm(string tempDirectory)
Expand Down
27 changes: 27 additions & 0 deletions RGiesecke.DllExport/Extensions/StringExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//# Author of original code ([Decompiled] MIT-License): Copyright (c) 2009-2015 Robert Giesecke
//# Use Readme & LICENSE files for details.

//# Modifications: Copyright (c) 2016-2020 Denis Kuzmin < [email protected] > GitHub/3F
//$ Distributed under the MIT License (MIT)

namespace RGiesecke.DllExport.Extensions
{
internal static class StringExtension
{
internal static bool IsTrue(this string str)
{
if(string.IsNullOrEmpty(str))
{
return false;
}

switch(str)
{
case "1": case "true": case "True": case "TRUE": return true;
/* legacy */ case "yes": case "Yes": case "YES": return true;
}

return false;
}
}
}
44 changes: 0 additions & 44 deletions RGiesecke.DllExport/GenericDisposable.cs

This file was deleted.

3 changes: 1 addition & 2 deletions RGiesecke.DllExport/RGiesecke.DllExport.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@
</ItemGroup>
<ItemGroup>
<Compile Include="DisabledAssemblyResolver.cs" />
<Compile Include="Extensions\StringExtension.cs" />
<Compile Include="PatchesType.cs" />
<Compile Include="PeCheckType.cs" />
<Compile Include="DllExportLogginCodes.cs" />
<Compile Include="DllExportServiceProviderExtensions.cs" />
<Compile Include="GenericDisposable.cs" />
<Compile Include="IProblemSolver.cs" />
<Compile Include="NotificationContext.cs" />
<Compile Include="AssemblyBinaryProperties.cs" />
Expand All @@ -74,7 +74,6 @@
<Compile Include="SourceCodePosition.cs" />
<Compile Include="SourceCodeRange.cs" />
<Compile Include="Utilities.cs" />
<Compile Include="ValueDisposable.cs" />
<Compile Include="Parsing\HasServiceProvider.cs" />
<Compile Include="Parsing\IlParser.cs" />
<Compile Include="Parsing\DllExportNotifierWrapper.cs" />
Expand Down
41 changes: 22 additions & 19 deletions RGiesecke.DllExport/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,33 +95,36 @@ public static T TryInitialize<T>(this T instance, Action<T> call) where T : IDis
}
}

public static ValueDisposable<string> CreateTempDirectory()
internal sealed class TempDir: IDisposable
{
return new ValueDisposable<string>(Utilities.CreateTempDirectoryCore(), (Action<string>)(dir => Directory.Delete(dir, true)));
}
public string FullPath { get; }

private static string CreateTempDirectoryCore()
{
string path1 = (string)null;
try
public TempDir()
{
string tempFileName = Path.GetTempFileName();
if(!string.IsNullOrEmpty(tempFileName) && File.Exists(tempFileName))
{
File.Delete(tempFileName);
}
string path2 = Path.Combine(Path.GetFullPath(Path.GetDirectoryName(tempFileName)), Path.GetFileNameWithoutExtension(tempFileName));
Directory.CreateDirectory(path2);
return path2;
string path = Path.GetTempPath();
string name = Guid.NewGuid().ToString();

FullPath = Path.Combine(path, $"dxp-{name}");
Directory.CreateDirectory(FullPath);
}
catch

#region IDisposable

private bool disposed;

private void Dispose(bool _)
{
if(!string.IsNullOrEmpty(path1) && Directory.Exists(path1))
if(!disposed)
{
Directory.Delete(path1, true);
if(Directory.Exists(FullPath)) Directory.Delete(FullPath, true);

disposed = true;
}
throw;
}

public void Dispose() => Dispose(true);

#endregion
}
}
}
25 changes: 0 additions & 25 deletions RGiesecke.DllExport/ValueDisposable.cs

This file was deleted.

0 comments on commit 91f2445

Please sign in to comment.