Skip to content

Commit

Permalink
Analyze old style csproj. Fix bjorkstromm#3
Browse files Browse the repository at this point in the history
  • Loading branch information
Tradioyes committed Aug 15, 2019
1 parent df74837 commit 41c8b7a
Showing 1 changed file with 71 additions and 61 deletions.
132 changes: 71 additions & 61 deletions src/Depends.Core/DependencyAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,96 +183,106 @@ public DependencyGraph Analyze(string projectPath, string framework = null)
// Todo: Something went wrong, log and return better exception.
throw new InvalidOperationException("Unable to load project.");
}

if (!analyzerResult.IsNetSdkProject())
{
// Todo: Support "legacy" projects in the future.
throw new InvalidOperationException("Unable to load project.");
}
var projectNode = new ProjectReferenceNode(projectPath);
var builder = new DependencyGraph.Builder(projectNode);

var projectAssetsFilePath = analyzerResult.GetProjectAssetsFilePath();

if (!File.Exists(projectAssetsFilePath))
{
// Todo: Make sure this exists in future
throw new InvalidOperationException($"{projectAssetsFilePath} not found. Please run 'dotnet restore'");
}

var lockFile = new LockFileFormat().Read(projectAssetsFilePath);
// Old csproj

var targetFramework = analyzerResult.GetTargetFramework();
var runtimeIdentifier = analyzerResult.GetRuntimeIdentifier();

var libraries = lockFile.Targets.Single(
x => x.TargetFramework == targetFramework && x.RuntimeIdentifier == runtimeIdentifier)
.Libraries.Where(x => x.IsPackage()).ToList();
var oldStylePackageReferences = analyzerResult.GetItems("Reference").Where(x => x.ItemSpec.Contains("Version"));
foreach (var reference in oldStylePackageReferences)
{
var version = ExtractVersion(reference.ItemSpec);
var name = reference.ItemSpec.Split(',')[0];
var node = new PackageReferenceNode(name, version);
builder.WithNode(node);
builder.WithEdge(new Edge(projectNode, node, version));
}
}
else
{
// New csproj

var projectNode = new ProjectReferenceNode(projectPath);
var builder = new DependencyGraph.Builder(projectNode);
var lockFile = new LockFileFormat().Read(projectAssetsFilePath);

var libraryNodes = new Dictionary<string, PackageReferenceNode>(StringComparer.OrdinalIgnoreCase);
foreach (var library in libraries)
{
var libraryNode = library.ToNode();
builder.WithNode(libraryNode);
libraryNodes.Add(libraryNode.PackageId, libraryNode);
var targetFramework = analyzerResult.GetTargetFramework();
var runtimeIdentifier = analyzerResult.GetRuntimeIdentifier();

if (library.FrameworkAssemblies.Count > 0)
{
var assemblyNodes = library.FrameworkAssemblies
.Select(x => new AssemblyReferenceNode($"{x}.dll"));
builder.WithNodes(assemblyNodes);
builder.WithEdges(assemblyNodes
.Select(x => new Edge(libraryNode, x)));
}
var libraries = lockFile.Targets.Single(
x => x.TargetFramework == targetFramework && x.RuntimeIdentifier == runtimeIdentifier)
.Libraries.Where(x => x.IsPackage()).ToList();

if (library.RuntimeAssemblies.Count > 0)
var libraryNodes = new Dictionary<string, PackageReferenceNode>(StringComparer.OrdinalIgnoreCase);
foreach (var library in libraries)
{
var assemblyNodes = library.RuntimeAssemblies
.Select(x => new AssemblyReferenceNode(Path.GetFileName(x.Path)))
.Where(x => x.Id != "_._");
var libraryNode = library.ToNode();
builder.WithNode(libraryNode);
libraryNodes.Add(libraryNode.PackageId, libraryNode);

if (assemblyNodes.Any())
if (library.FrameworkAssemblies.Count > 0)
{
var assemblyNodes = library.FrameworkAssemblies
.Select(x => new AssemblyReferenceNode($"{x}.dll"));
builder.WithNodes(assemblyNodes);
builder.WithEdges(assemblyNodes
.Select(x => new Edge(libraryNode, x)));
}
}

//if (library.CompileTimeAssemblies.Count > 0)
//{
// var assemblyNodes = library.CompileTimeAssemblies
// .Select(x => new AssemblyReferenceNode(Path.GetFileName(x.Path)));
// builder.WithNodes(assemblyNodes);
// builder.WithEdges(assemblyNodes
// .Select(x => new Edge(libraryNode, x)));
//}
}
if (library.RuntimeAssemblies.Count > 0)
{
var assemblyNodes = library.RuntimeAssemblies
.Select(x => new AssemblyReferenceNode(Path.GetFileName(x.Path)))
.Where(x => x.Id != "_._");

if (assemblyNodes.Any())
{
builder.WithNodes(assemblyNodes);
builder.WithEdges(assemblyNodes
.Select(x => new Edge(libraryNode, x)));
}
}

foreach (var library in libraries)
{
var libraryNode = library.ToNode();
//if (library.CompileTimeAssemblies.Count > 0)
//{
// var assemblyNodes = library.CompileTimeAssemblies
// .Select(x => new AssemblyReferenceNode(Path.GetFileName(x.Path)));
// builder.WithNodes(assemblyNodes);
// builder.WithEdges(assemblyNodes
// .Select(x => new Edge(libraryNode, x)));
//}
}

if (library.Dependencies.Count > 0)
foreach (var library in libraries)
{
builder.WithEdges(library.Dependencies
.Select(x => new Edge(libraryNode, libraryNodes[x.Id], x.VersionRange.ToString())));
var libraryNode = library.ToNode();

if (library.Dependencies.Count > 0)
{
builder.WithEdges(library.Dependencies
.Select(x => new Edge(libraryNode, libraryNodes[x.Id], x.VersionRange.ToString())));
}
}
}

// Ignore unversioned references like implicit SDK packages
builder.WithEdges(analyzerResult.GetItems("PackageReference")
.Where(x => x.Metadata.ContainsKey("Version"))
.Select(x => new Edge(projectNode, libraryNodes[x.ItemSpec], x.Metadata["Version"])));
// Ignore unversioned references like implicit SDK packages
builder.WithEdges(analyzerResult.GetItems("PackageReference")
.Where(x => x.Metadata.ContainsKey("Version"))
.Select(x => new Edge(projectNode, libraryNodes[x.ItemSpec], x.Metadata["Version"])));
}

var references = analyzerResult.References//GetItems("Reference")
.Select(x => new AssemblyReferenceNode(Path.GetFileName(x)));
var references = analyzerResult.References.Select(x => new AssemblyReferenceNode(Path.GetFileName(x)));

builder.WithNodes(references);
builder.WithEdges(references.Select(x => new Edge(projectNode, x)));

return builder.Build();
}

private string ExtractVersion(string itemSpec)
{
return itemSpec.Split(',').Single(s => s.Contains("Version"))?.Split('=')[1];
}
}
}

0 comments on commit 41c8b7a

Please sign in to comment.