Created
April 19, 2011 22:24
-
-
Save ahjohannessen/929869 to your computer and use it in GitHub Desktop.
SparkFinder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SparkFinder | |
{ | |
private readonly SparkItems _items; | |
private readonly string[] _sharedFolderNames; | |
public SparkFinder(SparkItems items, string[] sharedFolderNames) | |
{ | |
_items = items; | |
_sharedFolderNames = sharedFolderNames; | |
} | |
public SparkItem FindSpark(string sparkName, SparkItem item) | |
{ | |
var spark = findSpark(sparkName, item.FilePath, item.RootPath); | |
// Meh. | |
if (spark == null && item.Origin != Constants.HostOrigin) | |
{ | |
// ** | |
spark = findSpark(sparkName, string.Empty, "~/".ToPhysicalPath()); | |
} | |
return spark; | |
} | |
private SparkItem findSpark(string sparkName, string startPath, string stopPath) | |
{ | |
var reachables = reachableLocations(startPath, stopPath); | |
return _items | |
.Where(x => x.Name() == sparkName) | |
.Where(x => reachables.Contains(x.DirectoryPath())) | |
.FirstOrDefault(); | |
} | |
private IEnumerable<string> reachableLocations(string path, string root) | |
{ | |
do | |
{ | |
path = Path.GetDirectoryName(path); | |
if (path == null) break; | |
// TODO: Consider yield return path if ancestor dirs should be included; | |
foreach (var sharedFolder in _sharedFolderNames) | |
{ | |
yield return Path.Combine(path, sharedFolder); | |
} | |
} while (path.IsNotEmpty() && path.PathRelativeTo(root).IsNotEmpty()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment