diff --git a/SquishIt.Framework/Base/BundleBase.cs b/SquishIt.Framework/Base/BundleBase.cs index e67e141..3e082d2 100644 --- a/SquishIt.Framework/Base/BundleBase.cs +++ b/SquishIt.Framework/Base/BundleBase.cs @@ -155,7 +155,7 @@ protected string PreprocessFile(string file, IEnumerable preproce } } - string PreprocessContent(string file, IEnumerable preprocessors, string content) + protected string PreprocessContent(string file, IEnumerable preprocessors, string content) { if(preprocessors == null) { diff --git a/SquishIt.Framework/Css/CssBundle.cs b/SquishIt.Framework/Css/CssBundle.cs index df8afce..274604c 100644 --- a/SquishIt.Framework/Css/CssBundle.cs +++ b/SquishIt.Framework/Css/CssBundle.cs @@ -1,4 +1,3 @@ -using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -6,7 +5,6 @@ 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 filePaths, IEnumerable 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(); diff --git a/SquishIt.Framework/JavaScript/JavaScriptBundle.cs b/SquishIt.Framework/JavaScript/JavaScriptBundle.cs index c88764b..c8b7236 100644 --- a/SquishIt.Framework/JavaScript/JavaScriptBundle.cs +++ b/SquishIt.Framework/JavaScript/JavaScriptBundle.cs @@ -28,7 +28,7 @@ protected override IEnumerable allowedExtensions get { return instanceAllowedExtensions.Union(Bundle.AllowedGlobalExtensions.Union(Bundle.AllowedScriptExtensions)); } } - protected override IEnumerable disallowedExtensions + protected override IEnumerable 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 files, IEnumerable 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 preprocessors) { - lock (typeof(JavaScriptBundle)) { + private string PreprocessJavascriptFile(string file, IEnumerable preprocessors) + { + lock(typeof(JavaScriptBundle)) + { return PreprocessFile(file, preprocessors); } } diff --git a/SquishIt.Tests/Coffee/CoffeescriptCompilerTests.cs b/SquishIt.Tests/Coffee/CoffeescriptCompilerTests.cs deleted file mode 100644 index 9de069e..0000000 --- a/SquishIt.Tests/Coffee/CoffeescriptCompilerTests.cs +++ /dev/null @@ -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); - } - - } -} diff --git a/SquishIt.Tests/CoffeeScriptTests.cs b/SquishIt.Tests/CoffeeScriptTests.cs new file mode 100644 index 0000000..4660503 --- /dev/null +++ b/SquishIt.Tests/CoffeeScriptTests.cs @@ -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(@"", 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); + } + + } +} diff --git a/SquishIt.Tests/LessTests.cs b/SquishIt.Tests/LessTests.cs index de8316c..43019f7 100644 --- a/SquishIt.Tests/LessTests.cs +++ b/SquishIt.Tests/LessTests.cs @@ -54,6 +54,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 ("", tag); + } + [Test] public void CanBundleCssWithLessAndPathRewrites () { using (new StylePreprocessorScope ()) { diff --git a/SquishIt.Tests/SassTests.cs b/SquishIt.Tests/SassTests.cs index deffc04..fabb0f9 100644 --- a/SquishIt.Tests/SassTests.cs +++ b/SquishIt.Tests/SassTests.cs @@ -69,6 +69,47 @@ public void CanBundleCssWithScss() } } + + [Test] + public void CanBundleCssWithArbitraryScss() + { + using(new StylePreprocessorScope()) + { + 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(@"", tag); + Assert.AreEqual(expected, contents); + } + } + [Test] public void CanBundleCssWithSass() { @@ -110,6 +151,44 @@ public void CanBundleCssWithSass() } } + [Test] + public void CanBundleCssWithArbitrarySass() + { + using(new StylePreprocessorScope()) + { + 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(@"", tag); + Assert.AreEqual(expected, contents); + } + } + [Test] public void CanUseNesting() { diff --git a/SquishIt.Tests/SquishIt.Tests.csproj b/SquishIt.Tests/SquishIt.Tests.csproj index 110c2fb..9d51b4f 100644 --- a/SquishIt.Tests/SquishIt.Tests.csproj +++ b/SquishIt.Tests/SquishIt.Tests.csproj @@ -87,7 +87,7 @@ - +