Browse Source

use relative path if reference and target project are in the same root path

pull/2186/head
文煌 5 years ago committed by Siegfried Pammer
parent
commit
a5fdb05c6f
  1. 5
      ICSharpCode.Decompiler/CSharp/ProjectDecompiler/IProjectInfoProvider.cs
  2. 3
      ICSharpCode.Decompiler/CSharp/ProjectDecompiler/ProjectFileWriterSdkStyle.cs
  3. 36
      ICSharpCode.Decompiler/CSharp/ProjectDecompiler/WholeProjectDecompiler.cs

5
ICSharpCode.Decompiler/CSharp/ProjectDecompiler/IProjectInfoProvider.cs

@ -42,6 +42,11 @@ namespace ICSharpCode.Decompiler.CSharp.ProjectDecompiler
/// </summary> /// </summary>
Guid ProjectGuid { get; } Guid ProjectGuid { get; }
/// <summary>
/// Gets the target directory of the project
/// </summary>
string TargetDirectory { get; }
/// <summary> /// <summary>
/// Gets the name of the key file being used for strong name signing. Can be null if no file is available. /// Gets the name of the key file being used for strong name signing. Can be null if no file is available.
/// </summary> /// </summary>

3
ICSharpCode.Decompiler/CSharp/ProjectDecompiler/ProjectFileWriterSdkStyle.cs

@ -24,6 +24,7 @@ using System.Reflection.PortableExecutable;
using System.Xml; using System.Xml;
using ICSharpCode.Decompiler.Metadata; using ICSharpCode.Decompiler.Metadata;
using ICSharpCode.Decompiler.Util;
namespace ICSharpCode.Decompiler.CSharp.ProjectDecompiler namespace ICSharpCode.Decompiler.CSharp.ProjectDecompiler
{ {
@ -186,7 +187,7 @@ namespace ICSharpCode.Decompiler.CSharp.ProjectDecompiler
var asembly = project.AssemblyResolver.Resolve(reference); var asembly = project.AssemblyResolver.Resolve(reference);
if (asembly != null && !project.AssemblyResolver.IsGacAssembly(reference)) if (asembly != null && !project.AssemblyResolver.IsGacAssembly(reference))
{ {
xml.WriteElementString("HintPath", asembly.FileName); xml.WriteElementString("HintPath", FileUtility.GetRelativePath(project.TargetDirectory, asembly.FileName));
} }
xml.WriteEndElement(); xml.WriteEndElement();

36
ICSharpCode.Decompiler/CSharp/ProjectDecompiler/WholeProjectDecompiler.cs

@ -72,6 +72,15 @@ namespace ICSharpCode.Decompiler.CSharp.ProjectDecompiler
/// </summary> /// </summary>
public Guid ProjectGuid { get; } public Guid ProjectGuid { get; }
/// <summary>
/// The target directory that the decompiled files are written to.
/// </summary>
/// <remarks>
/// This property is set by DecompileProject() and protected so that overridden protected members
/// can access it.
/// </remarks>
public string TargetDirectory { get; protected set; }
/// <summary> /// <summary>
/// Path to the snk file to use for signing. /// Path to the snk file to use for signing.
/// <c>null</c> to not sign. /// <c>null</c> to not sign.
@ -112,15 +121,6 @@ namespace ICSharpCode.Decompiler.CSharp.ProjectDecompiler
// per-run members // per-run members
HashSet<string> directories = new HashSet<string>(Platform.FileNameComparer); HashSet<string> directories = new HashSet<string>(Platform.FileNameComparer);
/// <summary>
/// The target directory that the decompiled files are written to.
/// </summary>
/// <remarks>
/// This field is set by DecompileProject() and protected so that overridden protected members
/// can access it.
/// </remarks>
protected string targetDirectory;
readonly IProjectFileWriter projectWriter; readonly IProjectFileWriter projectWriter;
public void DecompileProject(PEFile moduleDefinition, string targetDirectory, CancellationToken cancellationToken = default(CancellationToken)) public void DecompileProject(PEFile moduleDefinition, string targetDirectory, CancellationToken cancellationToken = default(CancellationToken))
@ -138,7 +138,7 @@ namespace ICSharpCode.Decompiler.CSharp.ProjectDecompiler
{ {
throw new InvalidOperationException("Must set TargetDirectory"); throw new InvalidOperationException("Must set TargetDirectory");
} }
this.targetDirectory = targetDirectory; TargetDirectory = targetDirectory;
directories.Clear(); directories.Clear();
var files = WriteCodeFilesInProject(moduleDefinition, cancellationToken).ToList(); var files = WriteCodeFilesInProject(moduleDefinition, cancellationToken).ToList();
files.AddRange(WriteResourceFilesInProject(moduleDefinition)); files.AddRange(WriteResourceFilesInProject(moduleDefinition));
@ -183,9 +183,9 @@ namespace ICSharpCode.Decompiler.CSharp.ProjectDecompiler
const string prop = "Properties"; const string prop = "Properties";
if (directories.Add(prop)) if (directories.Add(prop))
Directory.CreateDirectory(Path.Combine(targetDirectory, prop)); Directory.CreateDirectory(Path.Combine(TargetDirectory, prop));
string assemblyInfo = Path.Combine(prop, "AssemblyInfo.cs"); string assemblyInfo = Path.Combine(prop, "AssemblyInfo.cs");
using (StreamWriter w = new StreamWriter(Path.Combine(targetDirectory, assemblyInfo))) using (StreamWriter w = new StreamWriter(Path.Combine(TargetDirectory, assemblyInfo)))
{ {
syntaxTree.AcceptVisitor(new CSharpOutputVisitor(w, Settings.CSharpFormattingOptions)); syntaxTree.AcceptVisitor(new CSharpOutputVisitor(w, Settings.CSharpFormattingOptions));
} }
@ -207,7 +207,7 @@ namespace ICSharpCode.Decompiler.CSharp.ProjectDecompiler
{ {
string dir = CleanUpFileName(metadata.GetString(type.Namespace)); string dir = CleanUpFileName(metadata.GetString(type.Namespace));
if (directories.Add(dir)) if (directories.Add(dir))
Directory.CreateDirectory(Path.Combine(targetDirectory, dir)); Directory.CreateDirectory(Path.Combine(TargetDirectory, dir));
return Path.Combine(dir, file); return Path.Combine(dir, file);
} }
}, StringComparer.OrdinalIgnoreCase).ToList(); }, StringComparer.OrdinalIgnoreCase).ToList();
@ -221,7 +221,7 @@ namespace ICSharpCode.Decompiler.CSharp.ProjectDecompiler
CancellationToken = cancellationToken CancellationToken = cancellationToken
}, },
delegate (IGrouping<string, TypeDefinitionHandle> file) { delegate (IGrouping<string, TypeDefinitionHandle> file) {
using (StreamWriter w = new StreamWriter(Path.Combine(targetDirectory, file.Key))) using (StreamWriter w = new StreamWriter(Path.Combine(TargetDirectory, file.Key)))
{ {
try try
{ {
@ -264,7 +264,7 @@ namespace ICSharpCode.Decompiler.CSharp.ProjectDecompiler
string dirName = Path.GetDirectoryName(fileName); string dirName = Path.GetDirectoryName(fileName);
if (!string.IsNullOrEmpty(dirName) && directories.Add(dirName)) if (!string.IsNullOrEmpty(dirName) && directories.Add(dirName))
{ {
Directory.CreateDirectory(Path.Combine(targetDirectory, dirName)); Directory.CreateDirectory(Path.Combine(TargetDirectory, dirName));
} }
Stream entryStream = (Stream)value; Stream entryStream = (Stream)value;
entryStream.Position = 0; entryStream.Position = 0;
@ -306,7 +306,7 @@ namespace ICSharpCode.Decompiler.CSharp.ProjectDecompiler
else else
{ {
string fileName = GetFileNameForResource(r.Name); string fileName = GetFileNameForResource(r.Name);
using (FileStream fs = new FileStream(Path.Combine(targetDirectory, fileName), FileMode.Create, FileAccess.Write)) using (FileStream fs = new FileStream(Path.Combine(TargetDirectory, fileName), FileMode.Create, FileAccess.Write))
{ {
stream.Position = 0; stream.Position = 0;
stream.CopyTo(fs); stream.CopyTo(fs);
@ -323,7 +323,7 @@ namespace ICSharpCode.Decompiler.CSharp.ProjectDecompiler
string resx = Path.ChangeExtension(fileName, ".resx"); string resx = Path.ChangeExtension(fileName, ".resx");
try try
{ {
using (FileStream fs = new FileStream(Path.Combine(targetDirectory, resx), FileMode.Create, FileAccess.Write)) using (FileStream fs = new FileStream(Path.Combine(TargetDirectory, resx), FileMode.Create, FileAccess.Write))
using (ResXResourceWriter writer = new ResXResourceWriter(fs)) using (ResXResourceWriter writer = new ResXResourceWriter(fs))
{ {
foreach (var entry in new ResourcesFile(entryStream)) foreach (var entry in new ResourcesFile(entryStream))
@ -342,7 +342,7 @@ namespace ICSharpCode.Decompiler.CSharp.ProjectDecompiler
// if the .resources can't be decoded, just save them as-is // if the .resources can't be decoded, just save them as-is
} }
} }
using (FileStream fs = new FileStream(Path.Combine(targetDirectory, fileName), FileMode.Create, FileAccess.Write)) using (FileStream fs = new FileStream(Path.Combine(TargetDirectory, fileName), FileMode.Create, FileAccess.Write))
{ {
entryStream.CopyTo(fs); entryStream.CopyTo(fs);
} }

Loading…
Cancel
Save