因此,简单的答案是按照本文档中的说明执行报告代码差异方式。这个基本答案的问题在于,它预先假设两个 NDepend 项目总是引用两个相同的程序集。
当然,程序集的数量和名称在您的上下文中会有所不同,因此我们需要动态构建两个项目(旧/新)并通过NDepend.API分析它们。
这是 NDepend.API 源代码。对于It-Just-Works 体验,在 PowerTools 源代码 (in $NDependInstallDir$\NDepend.PowerTools.SourceCode\NDepend.PowerTools.sln
) 中只需 在注册调用FoldersDiff.Main();
后调用方法, in 。AssemblyResolve
Program.cs
...
AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolverHelper.AssemblyResolveHandler;
FoldersDiff.Main();
...
这是利用 NDepend.API 的源代码。
codeBase
请注意,通过两个对象和对象可以做更多事情compareContext
。除了显示添加/删除/codeWasChanges 的 3 个程序集列表之外,您还可以显示 API 中断更改、添加的新方法和类型、修改的类和方法、代码质量回归……为此,只需查看有关 diff的默认代码规则,基于相同的NDepend.CodeModel API。
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using NDepend;
using NDepend.Analysis;
using NDepend.CodeModel;
using NDepend.Path;
using NDepend.Project;
class FoldersDiff {
private static readonly NDependServicesProvider s_NDependServicesProvider = new NDependServicesProvider();
internal static void Main() {
var dirOld = @"C:\MyProduct\OldAssembliesDir".ToAbsoluteDirectoryPath();
var dirNew = @"C:\MyProduct\NewAssembliesDir".ToAbsoluteDirectoryPath();
Console.WriteLine("Analyzing assemblies in " + dirOld.ToString());
var codeBaseOld = GetCodeBaseFromAsmInDir(dirOld, TemporaryProjectMode.TemporaryOlder);
Console.WriteLine("Analyzing assemblies in " + dirNew.ToString());
var codeBaseNew = GetCodeBaseFromAsmInDir(dirNew, TemporaryProjectMode.TemporaryNewer);
var compareContext = codeBaseNew.CreateCompareContextWithOlder(codeBaseOld);
// So much more can be done by exploring fine-grained diff in codeBases and compareContext
Dump("Added assemblies", codeBaseNew.Assemblies.Where(compareContext.WasAdded));
Dump("Removed assemblies", codeBaseOld.Assemblies.Where(compareContext.WasRemoved));
Dump("Assemblies with modified code", codeBaseNew.Assemblies.Where(compareContext.CodeWasChanged));
Console.Read();
}
internal static ICodeBase GetCodeBaseFromAsmInDir(IAbsoluteDirectoryPath dir, TemporaryProjectMode temporaryProjectMode) {
Debug.Assert(dir.Exists);
var dotNetManager = s_NDependServicesProvider.DotNetManager;
var assembliesPath = dir.ChildrenFilesPath.Where(dotNetManager.IsAssembly).ToArray();
Debug.Assert(assembliesPath.Length > 0); // Make sure we found assemblies
var projectManager = s_NDependServicesProvider.ProjectManager;
IProject project = projectManager.CreateTemporaryProject(assembliesPath, temporaryProjectMode);
// In PowerTool context, better call:
// var analysisResult = ProjectAnalysisUtils.RunAnalysisShowProgressOnConsole(project);
var analysisResult = project.RunAnalysis();
return analysisResult.CodeBase;
}
internal static void Dump(string title, IEnumerable<IAssembly> assemblies) {
Debug.Assert(!string.IsNullOrEmpty(title));
Debug.Assert(assemblies != null);
Console.WriteLine(title);
foreach (var @assembly in assemblies) {
Console.WriteLine(" " + @assembly.Name);
}
}
}