Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #37 from Mercell/master
Browse files Browse the repository at this point in the history
Look for references in repositoryPath specified in NuGet.config file
  • Loading branch information
RicoSuter committed Dec 5, 2017
2 parents 25584c8 + 14b92b1 commit 8765f26
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 8 deletions.
80 changes: 74 additions & 6 deletions src/NuGetReferenceSwitcher.Presentation/Models/ProjectModel.cs
Expand Up @@ -6,11 +6,12 @@
// <author>Rico Suter, mail@rsuter.com</author>
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Threading;
using System.Xml.Linq;
using System.Xml;
using EnvDTE;
using MyToolkit.Collections;
using VSLangProj;
Expand All @@ -21,13 +22,17 @@ public class ProjectModel
{
private readonly VSProject _vsProject;

public FileInfo SolutionFile { get; set; }

/// <summary>Initializes a new instance of the <see cref="ProjectModel"/> class. </summary>
/// <param name="project">The native project object. </param>
public ProjectModel(VSProject project)
/// <param name="application">The native application object. </param>
public ProjectModel(VSProject project, DTE application)
{
_vsProject = project;

Name = project.Project.Name;

Name = project.Project.Name;
SolutionFile = new FileInfo(application.Solution.FileName);
LoadReferences();
}

Expand Down Expand Up @@ -158,13 +163,76 @@ private void LoadReferences()
References = new ExtendedObservableCollection<ReferenceModel>();
NuGetReferences = new ExtendedObservableCollection<ReferenceModel>();

List<string> packageDirs = new List<string>();
packageDirs.Add("/packages/");
packageDirs.Add("\\packages\\");

if (SolutionFile.Exists)
{
string nuGetRepositoryPath = GetNuGetRepositoryPath(SolutionFile.Directory);
if (!string.IsNullOrEmpty(nuGetRepositoryPath))
{
packageDirs.Add(nuGetRepositoryPath);
}
}

foreach (var vsReference in _vsProject.References.OfType<Reference>())
{
var reference = new ReferenceModel(vsReference);
References.Add(reference);
if (vsReference.Path.ToLower().Contains("/packages/") || vsReference.Path.ToLower().Contains("\\packages\\"))
NuGetReferences.Add(reference);
string vsReferencePath = vsReference.Path.ToLower();
foreach (string packageDir in packageDirs)
{
if (vsReferencePath.Contains(packageDir))
{
NuGetReferences.Add(reference);
break;
}
}
}
}

/// <summary>
/// Looks for NuGet.Config files in every parent directories and returns the first repositoryPath found
/// </summary>
/// <param name="dir">The starting dir to look for a NuGet.config file</param>
/// <returns>repositoryPath if a path is found and exists</returns>
private string GetNuGetRepositoryPath(DirectoryInfo dir)
{
FileInfo nuGetConfigFile = dir.GetFiles("NuGet.Config").FirstOrDefault();

if (nuGetConfigFile != null)
{
XmlDocument nuGetConfig = new XmlDocument();
nuGetConfig.Load(nuGetConfigFile.FullName);
var pathNode = nuGetConfig.SelectSingleNode("//config//add[contains(@key,'repositoryPath')]");

if (pathNode?.Attributes?["value"] != null)
{
string repositoryPath = pathNode.Attributes["value"].Value;
repositoryPath = Environment.ExpandEnvironmentVariables(repositoryPath);

if (System.IO.Path.IsPathRooted(repositoryPath))
{
return repositoryPath;
}
else
{
DirectoryInfo repositoryDirectory = new DirectoryInfo(System.IO.Path.Combine(nuGetConfigFile.DirectoryName, repositoryPath));
if (repositoryDirectory.Exists)
{
return repositoryDirectory.FullName.ToLower();
}
}
}
}

if (dir.Parent != null)
{
return GetNuGetRepositoryPath(dir.Parent);
}

return null;
}
}
}
Expand Up @@ -219,7 +219,7 @@ private List<ProjectModel> GetAllProjects(IEnumerable<Project> objects)
{
try
{
projects.Add(new ProjectModel((VSProject)project.Object));
projects.Add(new ProjectModel((VSProject)project.Object, Application));
}
catch (Exception e)
{
Expand All @@ -240,7 +240,7 @@ private void AddProjectToSolutionIfNeeded(FromNuGetToProjectTransformation fromN
if (!string.IsNullOrEmpty(fromNuGetToProjectTransformation.ToProjectPath) && File.Exists(fromNuGetToProjectTransformation.ToProjectPath))
{
var project = Application.Solution.AddFromFile(fromNuGetToProjectTransformation.ToProjectPath);
var myProject = new ProjectModel((VSProject)project.Object);
var myProject = new ProjectModel((VSProject)project.Object, Application);
fromNuGetToProjectTransformation.ToProject = myProject;
}
else
Expand Down

0 comments on commit 8765f26

Please sign in to comment.