Skip to content

Commit

Permalink
Enable path-based use of AssemblyDiffer to close files (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
idg10 authored Aug 18, 2020
1 parent f37fa9c commit 474f223
Showing 1 changed file with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,40 @@ namespace Endjin.ApiChange.Api.Diff
using Query;
using Mono.Cecil;

public class AssemblyDiffer
public class AssemblyDiffer : IDisposable
{
private readonly AssemblyDiffCollection myDiff = new AssemblyDiffCollection();

private readonly AssemblyDefinition myV1;

private readonly AssemblyDefinition myV2;

private readonly bool ownAssemblyDefinitions;

/// <summary>
/// Creates an <see cref="AssemblyDiffer" /> from existing <see cref="AssemblyDefinition"/>
/// objects.
/// </summary>
/// <param name="v1">The assembly file v1.</param>
/// <param name="v2">The assembly file v2.</param>
/// <remarks>
/// When you use this constructor, the resulting instance will NOT dispose either of the
/// <see cref="AssemblyDefinition"/> objects. The assumption is that when the caller
/// supplies these objects, the caller owns them. (If you use path-based overload,
/// <see cref="AssemblyDiffer(string,string)"/>, this object will dispose the
/// <see cref="AssemblyDefinition"/> objects because in that case, this object created them
/// so only this object is able to dipose of them.)
/// </remarks>
public AssemblyDiffer(AssemblyDefinition v1, AssemblyDefinition v2)
{
this.myV1 = v1 ?? throw new ArgumentNullException(nameof(v1));
this.myV2 = v2 ?? throw new ArgumentNullException(nameof(v2));

ownAssemblyDefinitions = false;
}

/// <summary>
/// Initializes a new instance of the <see cref="AssemblyDiffer" /> class.
/// Creates an <see cref="AssemblyDiffer" />.
/// </summary>
/// <param name="assemblyFileV1">The assembly file v1.</param>
/// <param name="assemblyFileV2">The assembly file v2.</param>
Expand All @@ -52,6 +70,8 @@ public AssemblyDiffer(string assemblyFileV1, string assemblyFileV2)
{
throw new ArgumentException($"Could not load assemblyV2 {assemblyFileV2}");
}

ownAssemblyDefinitions = true;
}

private void OnAddedType(TypeDefinition type)
Expand All @@ -66,6 +86,16 @@ private void OnRemovedType(TypeDefinition type)
this.myDiff.AddedRemovedTypes.Add(diff);
}

/// <inheritdoc/>
public void Dispose()
{
if (this.ownAssemblyDefinitions)
{
this.myV1.Dispose();
this.myV2.Dispose();
}
}

public AssemblyDiffCollection GenerateTypeDiff(QueryAggregator queries)
{
if (queries == null || queries.TypeQueries.Count == 0)
Expand Down

0 comments on commit 474f223

Please sign in to comment.