Skip to content

Instantly share code, notes, and snippets.

@ahjohannessen
Created April 19, 2011 22:24
Show Gist options
  • Save ahjohannessen/929869 to your computer and use it in GitHub Desktop.
Save ahjohannessen/929869 to your computer and use it in GitHub Desktop.
SparkFinder.cs
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