Skip to content

Commit

Permalink
Pass in path explicitly for Cecil
Browse files Browse the repository at this point in the history
  • Loading branch information
LPeter1997 committed Sep 19, 2024
1 parent 1c042d4 commit aedd093
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 19 deletions.
18 changes: 1 addition & 17 deletions src/Draco.Coverage.MSBuild/CoverageWeaveTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,7 @@ public override bool Execute()
return true;
}

private void Weave(string inputPath, string outputPath)
{
if (inputPath != outputPath)
{
using var readerStream = File.OpenRead(inputPath);
using var writerStream = File.OpenWrite(outputPath);
InstrumentedAssembly.Weave(readerStream, writerStream);
}
else
{
// We use memory streams to avoid locking the file
using var readerStream = new MemoryStream(File.ReadAllBytes(inputPath));
using var writerStream = new MemoryStream();
InstrumentedAssembly.Weave(readerStream, writerStream);
File.WriteAllBytes(outputPath, writerStream.ToArray());
}
}
private void Weave(string inputPath, string outputPath) => InstrumentedAssembly.Weave(inputPath, outputPath);

// MSBuild passes in the path to all binaries copied, concatenated with a semocilon
// We need to split them and filter for .dll files
Expand Down
34 changes: 32 additions & 2 deletions src/Draco.Coverage/InstrumentationWeaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,28 @@ namespace Draco.Coverage;
/// </summary>
internal sealed class InstrumentationWeaver
{
/// <summary>
/// Weaves the instrumentation code into the specified assembly.
/// </summary>
/// <param name="sourcePath">The path to the assembly that should be weaved.</param>
/// <param name="targetPath">The path to write the weaved assembly to.</param>
/// <param name="settings">The settings for the weaver.</param>
public static void WeaveInstrumentationCode(
string sourcePath,
string targetPath,
InstrumentationWeaverSettings? settings = null)
{
var readerParameters = new ReaderParameters { ReadSymbols = true };
using var targetStream = new MemoryStream();
// NOTE: We don't fall-back to the stream-based method here, because Cecil needs the path of the assembly to read the symbols
using (var assemblyDefinition = AssemblyDefinition.ReadAssembly(sourcePath, readerParameters))
{
WeaveInstrumentationCode(assemblyDefinition, targetStream, settings);
}

File.WriteAllBytes(targetPath, targetStream.ToArray());
}

/// <summary>
/// Weaves the instrumentation code into the specified assembly.
/// </summary>
Expand All @@ -25,11 +47,19 @@ public static void WeaveInstrumentationCode(
Stream targetStream,
InstrumentationWeaverSettings? settings = null)
{
settings ??= InstrumentationWeaverSettings.Default;

var readerParameters = new ReaderParameters { ReadSymbols = true };
using var assemblyDefinition = AssemblyDefinition.ReadAssembly(sourceStream, readerParameters);

WeaveInstrumentationCode(assemblyDefinition, targetStream, settings);
}

private static void WeaveInstrumentationCode(
AssemblyDefinition assemblyDefinition,
Stream targetStream,
InstrumentationWeaverSettings? settings = null)
{
settings ??= InstrumentationWeaverSettings.Default;

var weaver = new InstrumentationWeaver(assemblyDefinition.MainModule, settings);
weaver.WeaveModule();

Expand Down
9 changes: 9 additions & 0 deletions src/Draco.Coverage/InstrumentedAssembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ namespace Draco.Coverage;
/// </summary>
public sealed class InstrumentedAssembly : IDisposable
{
/// <summary>
/// Weaves instrumentation code into the given assembly.
/// </summary>
/// <param name="sourcePath">The path to the assembly to weave.</param>
/// <param name="targetPath">The path to write the weaved assembly to.</param>
/// <param name="settings">The settings for the weaver.</param>
public static void Weave(string sourcePath, string targetPath, InstrumentationWeaverSettings? settings = null) =>
InstrumentationWeaver.WeaveInstrumentationCode(sourcePath, targetPath, settings);

/// <summary>
/// Weaves instrumentation code into the given assembly.
/// </summary>
Expand Down

0 comments on commit aedd093

Please sign in to comment.