Permalink
Browse files

Creating domain and applying rules when site is compiled.

  • Loading branch information...
1 parent cdd20ca commit b9af97787a054b4f8502d237c2f2e35196fd9457 @dragan committed May 15, 2012
View
@@ -6,4 +6,5 @@ _ReSharper*
packages
bin
obj
-artifacts
+artifacts
+test-results
@@ -0,0 +1,121 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+
+using Mulder.Base.Domain;
+using Mulder.Base.Extensions;
+using Mulder.Base.Filters;
+using Mulder.Base.IO;
+using Mulder.Base.Logging;
+
+namespace Mulder.Base.Compilation
+{
+ public class Compiler : ICompiler
+ {
+ readonly ILog log;
+ readonly IFileSystem fileSystem;
+ readonly IFilterFactory filterFactory;
+
+ public Compiler(ILog log, IFileSystem fileSystem, IFilterFactory filterFactory)
+ {
+ this.log = log;
+ this.fileSystem = fileSystem;
+ this.filterFactory = filterFactory;
+ }
+
+ public void Compile(Site site)
+ {
+ string outputDirectory = site.Configuration["OutputDirectory"].ToString();
+ fileSystem.CreateDirectory(outputDirectory);
+
+ CreateStaticFiles(site);
+
+ IEnumerable<StaticFile> staticFiles = site.Items.SelectMany(item => item.StaticFiles);
+
+ ApplyRules(staticFiles, site, outputDirectory);
+
+ CompileStaticFiles(staticFiles, site);
+ }
+
+ void CreateStaticFiles(Site site)
+ {
+ foreach (Item item in site.Items) {
+ CompileRule compileRule = site.GetCompilationRuleFor(item);
+
+ if (compileRule != null)
+ item.AddStaticFile(new StaticFile(item));
+ }
+ }
+
+ void ApplyRules(IEnumerable<StaticFile> staticFiles, Site site, string outputDirectory)
+ {
+ foreach (StaticFile staticFile in staticFiles) {
+ ApplyRouteRule(staticFile, site, outputDirectory);
+ ApplyCompileRule(staticFile, site);
+ }
+ }
+
+ void ApplyRouteRule(StaticFile staticFile, Site site, string outputDirectory)
+ {
+ RouteRule routeRule = site.GetRoutingRuleFor(staticFile.Item);
+ string route = routeRule.ApplyTo(staticFile, site);
+ staticFile.Path = outputDirectory + route;
+ }
+
+ void ApplyCompileRule(StaticFile staticFile, Site site)
+ {
+ CompileRule compileRule = site.GetCompilationRuleFor(staticFile.Item);
+ compileRule.ApplyTo(staticFile, site);
+ }
+
+ void CompileStaticFiles(IEnumerable<StaticFile> staticFiles, Site site)
+ {
+ foreach (StaticFile staticFile in staticFiles) {
+
+ ExecuteFilters(staticFile);
+ ExecuteLayoutFilter(staticFile, site);
+
+ fileSystem.WriteStringToFile(staticFile.Path, staticFile.GetLastSnapShot());
+
+ log.InfoMessage("\tcreate {0}", staticFile.Path);
+ }
+ }
+
+ void ExecuteFilters(StaticFile staticFile)
+ {
+ foreach (var filterName in staticFile.FilterNameQueue) {
+ IFilter filter = filterFactory.CreateFilter(filterName);
+
+ string source = staticFile.GetLastSnapShot();
+ var arguments = new Dictionary<string, object> {
+ {"item", staticFile.Item.Meta }
+ };
+
+ string result = filter.Execute(source, arguments);
+
+ staticFile.CreateSnapShot(result);
+ }
+ }
+
+ void ExecuteLayoutFilter(StaticFile staticFile, Site site)
+ {
+ var layoutRule = site.GetLayoutRuleFor(staticFile.Layout);
+
+ IFilter filter = filterFactory.CreateFilter(layoutRule.FilterName);
+ var arguments = new Dictionary<string, object> {
+ { "layout", staticFile.Layout.Meta },
+ { "item", staticFile.Item.Meta },
+ { "content", staticFile.GetLastSnapShot() }
+ };
+
+ string result = filter.Execute(staticFile.Layout.Content, arguments);
+
+ staticFile.CreateSnapShot(result);
+ }
+ }
+
+ public interface ICompiler
+ {
+ void Compile(Site site);
+ }
+}
@@ -0,0 +1,24 @@
+using System;
+
+using Mulder.Base.Domain;
+using Mulder.Base.Filters;
+
+namespace Mulder.Base.Compilation
+{
+ public class FilterFactory : IFilterFactory
+ {
+ public FilterFactory()
+ {
+ }
+
+ public IFilter CreateFilter(string filterName)
+ {
+ return null;
+ }
+ }
+
+ public interface IFilterFactory
+ {
+ IFilter CreateFilter(string filterName);
+ }
+}
@@ -0,0 +1,43 @@
+using System;
+
+namespace Mulder.Base.Domain
+{
+ public class CompileRule : Rule, IEquatable<CompileRule>
+ {
+ readonly Action<RuleContext> block;
+
+ public CompileRule(string identifierPattern, Action<RuleContext> block) : base(identifierPattern)
+ {
+ this.block = block;
+ }
+
+ public void ApplyTo(StaticFile staticFile, Site site)
+ {
+ var ruleContext = new RuleContext(staticFile, site);
+
+ block.Invoke(ruleContext);
+ }
+
+ public override int GetHashCode()
+ {
+ int hash = 17;
+ hash = hash * 23 + block.GetHashCode();
+ return hash + base.GetHashCode();
+ }
+
+ public override bool Equals(object obj)
+ {
+ if (obj is CompileRule) return Equals((CompileRule)obj);
+
+ return false;
+ }
+
+ public bool Equals(CompileRule other)
+ {
+ if (other == null)
+ return false;
+
+ return block == other.block && base.Equals(other);
+ }
+ }
+}
@@ -0,0 +1,13 @@
+using System;
+using System.Collections.Generic;
+
+namespace Mulder.Base.Domain
+{
+ public interface ISourceFile
+ {
+ string Identifier { get; }
+ string Content { get; }
+ IDictionary<string, object> Meta { get; }
+ DateTime ModificationTime { get; }
+ }
+}
@@ -0,0 +1,70 @@
+using System;
+using System.Collections.Generic;
+
+namespace Mulder.Base.Domain
+{
+ public class Item : ISourceFile, IEquatable<Item>
+ {
+ readonly string identifier;
+ readonly bool isBinary;
+ readonly string content;
+ readonly IDictionary<string, object> meta;
+ readonly DateTime modificationTime;
+ readonly List<StaticFile> staticFiles;
+
+ public string Identifier { get { return identifier; } }
+ public bool IsBinary { get { return isBinary; } }
+ public string Content { get { return content; } }
+ public IDictionary<string, object> Meta { get { return meta; } }
+ public DateTime ModificationTime { get { return modificationTime; } }
+ public IEnumerable<StaticFile> StaticFiles { get { return staticFiles; } }
+
+ public Item(string identifier, bool isBinary, string content, IDictionary<string, object> meta, DateTime modificationTime)
+ {
+ this.identifier = identifier;
+ this.isBinary = isBinary;
+ this.content = content;
+ this.meta = meta;
+ this.modificationTime = modificationTime;
+
+ staticFiles = new List<StaticFile>();
+ }
+
+ public void AddStaticFile(StaticFile staticFile)
+ {
+ staticFiles.Add(staticFile);
+ }
+
+ public override int GetHashCode()
+ {
+ int hash = 17;
+ hash = hash * 23 + identifier.GetHashCode();
+ hash = hash * 23 + isBinary.GetHashCode();
+ hash = hash * 23 + content.GetHashCode();
+ hash = hash * 23 + meta.GetHashCode();
+ hash = hash * 23 + modificationTime.GetHashCode();
+ hash = hash * 23 + staticFiles.GetHashCode();
+ return hash;
+ }
+
+ public override bool Equals(object obj)
+ {
+ if (obj is Item) return Equals((Item)obj);
+
+ return false;
+ }
+
+ public bool Equals(Item other)
+ {
+ if (other == null)
+ return false;
+
+ return identifier == other.identifier
+ && isBinary == other.isBinary
+ && content == other.content
+ && meta == other.meta
+ && modificationTime == other.modificationTime
+ && staticFiles == other.staticFiles;
+ }
+ }
+}
@@ -0,0 +1,54 @@
+using System;
+using System.Collections.Generic;
+
+namespace Mulder.Base.Domain
+{
+ public class Layout : ISourceFile, IEquatable<Layout>
+ {
+ readonly string identifier;
+ readonly string content;
+ readonly IDictionary<string, object> meta;
+ readonly DateTime modificationTime;
+
+ public string Identifier { get { return identifier; } }
+ public string Content { get { return content; } }
+ public IDictionary<string, object> Meta { get { return meta; } }
+ public DateTime ModificationTime { get { return modificationTime; } }
+
+ public Layout(string identifier, string content, IDictionary<string, object> meta, DateTime modificationTime)
+ {
+ this.identifier = identifier;
+ this.content = content;
+ this.meta = meta;
+ this.modificationTime = modificationTime;
+ }
+
+ public override int GetHashCode()
+ {
+ int hash = 17;
+ hash = hash * 23 + identifier.GetHashCode();
+ hash = hash * 23 + content.GetHashCode();
+ hash = hash * 23 + meta.GetHashCode();
+ hash = hash * 23 + modificationTime.GetHashCode();
+ return hash;
+ }
+
+ public override bool Equals(object obj)
+ {
+ if (obj is Layout) return Equals((Layout)obj);
+
+ return false;
+ }
+
+ public bool Equals(Layout other)
+ {
+ if (other == null)
+ return false;
+
+ return identifier == other.identifier
+ && content == other.content
+ && meta == other.meta
+ && modificationTime == other.modificationTime;
+ }
+ }
+}
@@ -0,0 +1,38 @@
+using System;
+
+namespace Mulder.Base.Domain
+{
+ public class LayoutRule : Rule, IEquatable<LayoutRule>
+ {
+ readonly string filterName;
+
+ public string FilterName { get { return filterName; } }
+
+ public LayoutRule(string identifierPattern, string filterName) : base(identifierPattern)
+ {
+ this.filterName = filterName;
+ }
+
+ public override int GetHashCode()
+ {
+ int hash = 17;
+ hash = hash * 23 + filterName.GetHashCode();
+ return hash + base.GetHashCode();
+ }
+
+ public override bool Equals(object obj)
+ {
+ if (obj is LayoutRule) return Equals((LayoutRule)obj);
+
+ return false;
+ }
+
+ public bool Equals(LayoutRule other)
+ {
+ if (other == null)
+ return false;
+
+ return filterName == other.filterName && base.Equals(other);
+ }
+ }
+}
Oops, something went wrong.

0 comments on commit b9af977

Please sign in to comment.