Permalink
Browse files

adding preprocessor support for arbitrary content (continue #170)

  • Loading branch information...
1 parent eaef909 commit 4046ff4174839a073676bcdfab7c874d3c6065cc @AlexCuse committed Apr 11, 2012
@@ -155,7 +155,7 @@ protected string PreprocessFile(string file, IEnumerable<IPreprocessor> preproce
}
}
- string PreprocessContent(string file, IEnumerable<IPreprocessor> preprocessors, string content)
+ protected string PreprocessContent(string file, IEnumerable<IPreprocessor> preprocessors, string content)
{
if(preprocessors == null)
{
@@ -1,12 +1,10 @@
-using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using SquishIt.Framework.Base;
using SquishIt.Framework.Minifiers;
-using SquishIt.Framework.Minifiers.CSS;
using SquishIt.Framework.Resolvers;
using SquishIt.Framework.Files;
using SquishIt.Framework.Utilities;
@@ -109,11 +107,15 @@ public CSSBundle AppendHashForAssets()
protected override string BeforeMinify(string outputFile, List<string> filePaths, IEnumerable<ArbitraryContent> arbitraryContent)
{
+ //TODO: refactor so that this is common code and ProcessCSSFile/ProcessJavascriptFile are a single abstract method called here
var outputCss = new StringBuilder();
- //TODO: deal w/ arbitrary extensions
filePaths.Select(file => ProcessCssFile(file, outputFile))
- .Concat(arbitraryContent.Select(ac => ac.Content))
+ .Concat(arbitraryContent.Select(ac => {
+ var filename = "dummy." + ac.Extension;
+ var preprocessors = FindPreprocessors(filename);
+ return PreprocessContent(filename, preprocessors, ac.Content);
+ }))
.Aggregate(outputCss, (builder, val) => builder.Append(val + "\n"));
return outputCss.ToString();
@@ -28,7 +28,7 @@ protected override IEnumerable<string> allowedExtensions
get { return instanceAllowedExtensions.Union(Bundle.AllowedGlobalExtensions.Union(Bundle.AllowedScriptExtensions)); }
}
- protected override IEnumerable<string> disallowedExtensions
+ protected override IEnumerable<string> disallowedExtensions
{
get { return Bundle.AllowedStyleExtensions; }
}
@@ -64,16 +64,16 @@ protected override string CachePrefix
internal override void BeforeRenderDebug()
{
- foreach (var asset in bundleState.Assets)
+ foreach(var asset in bundleState.Assets)
{
var localPath = asset.LocalPath;
var preprocessors = FindPreprocessors(localPath);
- if (preprocessors != null && preprocessors.Count() > 0)
+ if(preprocessors != null && preprocessors.Count() > 0)
{
string outputFile = FileSystem.ResolveAppRelativePathToFileSystem(localPath);
string javascript = PreprocessJavascriptFile(outputFile, preprocessors);
outputFile += ".debug.js";
- using (var fileWriter = fileWriterFactory.GetFileWriter(outputFile))
+ using(var fileWriter = fileWriterFactory.GetFileWriter(outputFile))
{
fileWriter.Write(javascript);
}
@@ -85,25 +85,34 @@ internal override void BeforeRenderDebug()
protected override string BeforeMinify(string outputFile, List<string> files, IEnumerable<ArbitraryContent> arbitraryContent)
{
+ //TODO: refactor so that this is common code and ProcessCSSFile/ProcessJavascriptFile are a single abstract method called here
var sb = new StringBuilder();
- //TODO: deal w/ arbitrary extensions
+
files.Select(ProcessJavascriptFile)
- .Concat(arbitraryContent.Select(ac => ac.Content))
+ .Concat(arbitraryContent.Select(ac => {
+ var filename = "dummy." + ac.Extension;
+ var preprocessors = FindPreprocessors(filename);
+ return PreprocessContent(filename, preprocessors, ac.Content);
+ }))
.Aggregate(sb, (builder, val) => builder.Append(val + "\n"));
return sb.ToString();
}
- private string ProcessJavascriptFile(string file) {
+ private string ProcessJavascriptFile(string file)
+ {
var preprocessors = FindPreprocessors(file);
- if (preprocessors != null) {
+ if(preprocessors != null)
+ {
return PreprocessJavascriptFile(file, preprocessors);
}
return ReadFile(file);
}
- private string PreprocessJavascriptFile(string file, IEnumerable<IPreprocessor> preprocessors) {
- lock (typeof(JavaScriptBundle)) {
+ private string PreprocessJavascriptFile(string file, IEnumerable<IPreprocessor> preprocessors)
+ {
+ lock(typeof(JavaScriptBundle))
+ {
return PreprocessFile(file, preprocessors);
}
}
@@ -1,62 +0,0 @@
-using System;
-using NUnit.Framework;
-using SquishIt.CoffeeScript.Coffee;
-
-namespace SquishIt.Tests.Coffee
-{
- [TestFixture]
- public class CoffeescriptCompilerTests
- {
- [Test, Platform (Exclude = "Unix, Linux, Mono")]
- public void CompileWithSimpleAlertSucceeds ()
- {
- var compiler = new CoffeeScriptCompiler ();
-
- string result = compiler.Compile ("alert 'test' ");
-
- Assert.AreEqual ("(function() {\n alert('test');\n}).call(this);\n", result);
- }
-
- [Test, Platform (Exclude = "Unix, Linux, Mono")]
- public void CompileWithComplexScriptSucceeds ()
- {
- string source = @"# Assignment:
-number = 42
-opposite = true
-
-# Conditions:
-number = -42 if opposite
-
-# Functions:
-square = (x) -> x * x
-
-# Arrays:
-list = [1, 2, 3, 4, 5]
-
-# Objects:
-math =
- root: Math.sqrt
- square: square
- cube: (x) -> x * square x
-
-# Splats:
-race = (winner, runners...) ->
- print winner, runners
-
-# Existence:
-alert 'I knew it!' if elvis?";
-
- var compiler = new CoffeeScriptCompiler ();
- string result = compiler.Compile (source);
- }
-
- [Test, Platform (Include = "Unix, Linux, Mono")]
- public void CompileFailsGracefullyOnMono ()
- {
- var compiler = new CoffeeScriptCompiler();
- var exception = Assert.Throws(typeof (NotSupportedException), () => compiler.Compile(""));
- Assert.AreEqual("Coffeescript not yet supported for mono.", exception.Message);
- }
-
- }
-}
@@ -0,0 +1,101 @@
+using System;
+using NUnit.Framework;
+using SquishIt.CoffeeScript;
+using SquishIt.CoffeeScript.Coffee;
+using SquishIt.Framework;
+using SquishIt.Framework.Files;
+using SquishIt.Framework.Utilities;
+using SquishIt.Tests.Helpers;
+
+namespace SquishIt.Tests
+{
+ [TestFixture]
+ public class CoffeeScriptTests
+ {
+ //TODO: should probably have more tests here
+ private JavaScriptBundleFactory javaScriptBundleFactory;
+ private IHasher hasher;
+
+ [SetUp]
+ public void Setup()
+ {
+ javaScriptBundleFactory = new JavaScriptBundleFactory();
+ var retryableFileOpener = new RetryableFileOpener();
+ hasher = new Hasher(retryableFileOpener);
+ }
+
+ [Test]
+ public void CanBundleJavascriptWithArbitraryCoffeeScript()
+ {
+ var coffee = "alert 'test' ";
+
+ var tag = javaScriptBundleFactory
+ .WithDebuggingEnabled(false)
+ .Create()
+ .WithPreprocessor(new CoffeeScriptPreprocessor())
+ .AddString(coffee, ".coffee")
+ .Render("~/brewed.js");
+
+ var compiled = javaScriptBundleFactory.FileWriterFactory.Files[TestUtilities.PrepareRelativePath(@"brewed.js")];
+
+ Assert.AreEqual(@"(function(){alert(""test"")}).call(this)", compiled);
+ Assert.AreEqual(@"<script type=""text/javascript"" src=""brewed.js?r=hash""></script>", tag);
+ }
+ }
+
+ [TestFixture]
+ public class CoffeescriptCompilerTests
+ {
+ [Test, Platform(Exclude = "Unix, Linux, Mono")]
+ public void CompileWithSimpleAlertSucceeds()
+ {
+ var compiler = new CoffeeScriptCompiler();
+
+ string result = compiler.Compile("alert 'test' ");
+
+ Assert.AreEqual("(function() {\n alert('test');\n}).call(this);\n", result);
+ }
+
+ [Test, Platform(Exclude = "Unix, Linux, Mono")]
+ public void CompileWithComplexScriptSucceeds()
+ {
+ string source = @"# Assignment:
+number = 42
+opposite = true
+
+# Conditions:
+number = -42 if opposite
+
+# Functions:
+square = (x) -> x * x
+
+# Arrays:
+list = [1, 2, 3, 4, 5]
+
+# Objects:
+math =
+ root: Math.sqrt
+ square: square
+ cube: (x) -> x * square x
+
+# Splats:
+race = (winner, runners...) ->
+ print winner, runners
+
+# Existence:
+alert 'I knew it!' if elvis?";
+
+ var compiler = new CoffeeScriptCompiler();
+ string result = compiler.Compile(source);
+ }
+
+ [Test, Platform(Include = "Unix, Linux, Mono")]
+ public void CompileFailsGracefullyOnMono()
+ {
+ var compiler = new CoffeeScriptCompiler();
+ var exception = Assert.Throws(typeof(NotSupportedException), () => compiler.Compile(""));
+ Assert.AreEqual("Coffeescript not yet supported for mono.", exception.Message);
+ }
+
+ }
+}
@@ -55,6 +55,26 @@ public class LessTests
}
[Test]
+ public void CanBundleArbitraryLessContent ()
+ {
+ var tag = cssBundleFactory
+ .WithHasher(hasher)
+ .WithDebuggingEnabled(false)
+ .Create()
+ .WithPreprocessor(new LessPreprocessor())
+ .AddString(cssLess, ".less")
+ .Render("~/css/output.css");
+
+ var contents =
+ cssBundleFactory.FileWriterFactory.Files[TestUtilities.PrepareRelativePath(@"css\output.css")];
+
+
+ Assert.AreEqual("#header{color:#4d926f}h2{color:#4d926f}", contents);
+
+ Assert.AreEqual ("<link rel=\"stylesheet\" type=\"text/css\" href=\"css/output.css?r=15D3D9555DEFACE69D6AB9E7FD972638\" />", tag);
+ }
+
+ [Test]
public void CanBundleCssWithLessAndPathRewrites () {
using (new StylePreprocessorScope<LessPreprocessor> ()) {
string css =
@@ -69,6 +69,47 @@ public void CanBundleCssWithScss()
}
}
+
+ [Test]
+ public void CanBundleCssWithArbitraryScss()
+ {
+ using(new StylePreprocessorScope<SassPreprocessor>())
+ {
+ var original =
+ @"$blue: #3bbfce;
+ $margin: 16px;
+
+ .content-navigation {
+ border-color: $blue;
+ color:
+ darken($blue, 9%);
+ }
+
+ .border {
+ padding: $margin / 2;
+ margin: $margin / 2;
+ border-color: $blue;
+ }";
+
+ var expected =
+ @".content-navigation{border-color:#3bbfce;color:#2ca2af}.border{padding:8px;margin:8px;border-color:#3bbfce}";
+
+ var cssBundle = cssBundleFactory
+ .WithHasher(hasher)
+ .WithDebuggingEnabled(false)
+ .Create();
+
+ string tag = cssBundle
+ .AddString(original, ".scss")
+ .Render("~/css/output.css");
+
+ string contents = cssBundleFactory.FileWriterFactory.Files[TestUtilities.PrepareRelativePath(@"css\output.css")];
+
+ Assert.AreEqual(@"<link rel=""stylesheet"" type=""text/css"" href=""css/output.css?r=5C851B7837C923C399A44B1F5BF9F14A"" />", tag);
+ Assert.AreEqual(expected, contents);
+ }
+ }
+
[Test]
public void CanBundleCssWithSass()
{
@@ -111,6 +152,44 @@ public void CanBundleCssWithSass()
}
[Test]
+ public void CanBundleCssWithArbitrarySass()
+ {
+ using(new StylePreprocessorScope<SassPreprocessor>())
+ {
+ var original =
+@"$blue: #3bbfce
+$margin: 16px
+
+.content-navigation
+ border-color: $blue
+ color: darken($blue, 9%)
+
+.border
+ padding: $margin / 2
+ margin: $margin / 2
+ border-color: $blue";
+
+ var expected =
+ @".content-navigation{border-color:#3bbfce;color:#2ca2af}.border{padding:8px;margin:8px;border-color:#3bbfce}";
+
+ var cssBundle = cssBundleFactory
+ .WithHasher(hasher)
+ .WithDebuggingEnabled(false)
+ .Create();
+
+ string tag = cssBundle
+ .AddString(original, ".sass")
+ .Render("~/css/output.css");
+
+ string contents =
+ cssBundleFactory.FileWriterFactory.Files[TestUtilities.PrepareRelativePath(@"css\output.css")];
+
+ Assert.AreEqual(@"<link rel=""stylesheet"" type=""text/css"" href=""css/output.css?r=5C851B7837C923C399A44B1F5BF9F14A"" />", tag);
+ Assert.AreEqual(expected, contents);
+ }
+ }
+
+ [Test]
public void CanUseNesting()
{
using (new StylePreprocessorScope<SassPreprocessor>())
Oops, something went wrong.

0 comments on commit 4046ff4

Please sign in to comment.