Created
April 6, 2011 18:51
-
-
Save emiaj/906259 to your computer and use it in GitHub Desktop.
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
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Web.Hosting; | |
using FubuCore.Util; | |
namespace FubuMVC.Spark | |
{ | |
public interface ISparkScanner | |
{ | |
IEnumerable<ISparkItem> Scan(); | |
} | |
public class SparkScannerSettings | |
{ | |
public ISparkScanner Scanner { get; set; } | |
} | |
// NOTE: TBD | |
public interface ISparkItem | |
{ | |
} | |
public class ViewSparkItem : ISparkItem | |
{ | |
} | |
public class PartialSparkItem : ISparkItem | |
{ | |
} | |
public class MacroSparkItem : ISparkItem | |
{ | |
} | |
public class BindingSparkItem : ISparkItem | |
{ | |
} | |
public class CombinedSparkScanner : ISparkScanner | |
{ | |
private readonly ISparkScanner _first; | |
private readonly ISparkScanner _second; | |
public CombinedSparkScanner(ISparkScanner first, ISparkScanner second) | |
{ | |
_first = first; | |
_second = second; | |
} | |
public IEnumerable<ISparkItem> Scan() | |
{ | |
var first = _first.Scan(); | |
var second = _second.Scan(); | |
return first.Union(second); | |
} | |
} | |
public class VirtualPathSparkScanner : ISparkScanner | |
{ | |
private readonly string _root; | |
private readonly CompositeFilter<VirtualFile> _filter; | |
private readonly Builder<VirtualFile, ISparkItem> _builder; | |
public VirtualPathSparkScanner(string root) | |
{ | |
_root = root; | |
_filter = new CompositeFilter<VirtualFile>(); | |
_filter.Excludes += x => !Path.HasExtension(x.Name); | |
_filter.Includes += x => Path.GetExtension(x.Name) == ".spark"; | |
_filter.Includes += x => Path.GetFileName(x.Name) == "bindings.xml"; | |
_builder = new Builder<VirtualFile, ISparkItem>(x => new ViewSparkItem()); | |
_builder.Register(x => Path.GetFileName(x.Name) == "bindings.xml", x => new BindingSparkItem()); | |
_builder.Register(x => Path.GetFileName(x.Name) == "_global.spark", x => new MacroSparkItem()); | |
_builder.Register(x => Path.GetFileName(x.Name).StartsWith("_"), x => new PartialSparkItem()); | |
} | |
public IEnumerable<ISparkItem> Scan() | |
{ | |
var provider = HostingEnvironment.VirtualPathProvider; | |
var rootDirectory = provider.GetDirectory(_root); | |
return scan(rootDirectory); | |
} | |
private IEnumerable<ISparkItem> scan(VirtualDirectory directory) | |
{ | |
var files = directory.Files.OfType<VirtualFile>().Where(_filter.Matches); | |
var items = files.Select(file => _builder.Build(file)); | |
var children = directory.Directories.OfType<VirtualDirectory>(); | |
return children.Aggregate(items, (current, child) => current.Union(scan(child))); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment