Permalink
Browse files

Moved back to .NET 3.5. Added error handling logic for minification.

  • Loading branch information...
1 parent 8c1abe5 commit e882bbf002b3bf2a501e78bb49bf1cd1c0f9fb19 @skroonenburg skroonenburg committed Jan 21, 2012
@@ -1,7 +1,7 @@
-<?xml version="1.0" encoding="utf-8" ?>
+<?xml version="1.0"?>
<configuration>
<configSections>
<section name="compactor" type="Rejuicer.Configuration.CompactorConfiguration, Rejuicer"/>
</configSections>
- <compactor Cache="true" Compact="true" Combine="true" />
-</configuration>
+ <compactor Cache="true" Compact="true" Combine="true"/>
+<startup><supportedRuntime version="v2.0.50727"/></startup></configuration>
@@ -10,8 +10,9 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Rejuicer_test</RootNamespace>
<AssemblyName>Rejuicer-test</AssemblyName>
- <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
+ <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
+ <TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
@@ -39,7 +40,6 @@
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
- <Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
@@ -50,14 +50,14 @@
<Compile Include="RejuicerEngineTests.cs" />
</ItemGroup>
<ItemGroup>
+ <None Include="App.config" />
+ </ItemGroup>
+ <ItemGroup>
<ProjectReference Include="..\Rejuicer\Rejuicer.csproj">
<Project>{084F7A79-1D16-438B-8FE8-14FB389CD274}</Project>
<Name>Rejuicer</Name>
</ProjectReference>
</ItemGroup>
- <ItemGroup>
- <None Include="App.config" />
- </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
@@ -16,20 +16,8 @@ public byte[] Minify(byte[] data)
{
// Read the data into a string
var stringValue = data.ReadString();
- var culture = Thread.CurrentThread.CurrentCulture;
- try
- {
- Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
-
- var minifiedValue = MinifyStringValue(stringValue);
-
- return minifiedValue.AsBytes();
- }
- finally
- {
- Thread.CurrentThread.CurrentCulture = culture;
- }
+ return MinifyStringValue(stringValue).AsBytes();
}
public byte[] Combine(IEnumerable<byte[]> data)
@@ -1,40 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Globalization;
-using System.IO;
-using System.Linq;
-using System.Text;
-using System.Threading;
-
-namespace Rejuicer.Engine
-{
- public class ImageMinificationProvider : IMinificationProvider
- {
- public byte[] Minify(byte[] data)
- {
- // Perform no minification on image data
- return data;
- }
-
- public byte[] Combine(IEnumerable<byte[]> data)
- {
- return data.FirstOrDefault();
- }
-
- public string GetContentType(string filename)
- {
- switch (Path.GetExtension(filename).ToUpperInvariant())
- {
- case "PNG":
- return "image/png";
- case "GIF":
- return "image/gif";
- case "JPG":
- case "JPEG":
- return "image/jpg";
- default:
- return "image";
- }
- }
- }
-}
@@ -12,22 +12,13 @@ public class JsMinificationProvider : BaseStringMinificationProvider
{
public override string MinifyStringValue(string data)
{
- try
+ if (string.IsNullOrEmpty(data))
{
-
- if (string.IsNullOrEmpty(data))
- {
- return "";
- }
-
- // Uses the same default values as Yahoo YUI Compressor
- return Yahoo.Yui.Compressor.JavaScriptCompressor.Compress(data, true, true, false, false, -1, Encoding.UTF8, CultureInfo.InvariantCulture);
+ return "";
}
- catch (InvalidOperationException e)
- {
- throw new InvalidOperationException("Encountered exception trying minify invalid JavaScript.", e);
- }
-
+
+ // Uses the same default values as Yahoo YUI Compressor
+ return Yahoo.Yui.Compressor.JavaScriptCompressor.Compress(data, true, true, false, false, -1, Encoding.UTF8, CultureInfo.InvariantCulture);
}
public override string GetContentType(string filename)
@@ -3,18 +3,19 @@
using System.Linq;
using System.Text;
using System.Web;
+using System.Web.Mvc;
using Rejuicer.Model;
namespace Rejuicer.HtmlHelpers
{
public class IncludesCacheModel
{
- public IHtmlString IncludesHtml { get; set; }
+ public MvcHtmlString IncludesHtml { get; set; }
public string HashValue { get; set; }
- public IHtmlString RenderHtml()
+ public MvcHtmlString RenderHtml()
{
- return new HtmlString(IncludesHtml.ToString().Replace(RejuicerConfigurationSource.FilenameUniquePlaceholder, HashValue));
+ return MvcHtmlString.Create(IncludesHtml.ToString().Replace(RejuicerConfigurationSource.FilenameUniquePlaceholder, HashValue));
}
}
}
@@ -14,20 +14,20 @@ namespace Rejuicer
{
public static class Rejuiced
{
- public static IHtmlString CssFor(string filename)
+ public static MvcHtmlString CssFor(string filename)
{
return IncludeRejuicedCssFor(null, filename);
}
- public static IHtmlString JsFor(string filename)
+ public static MvcHtmlString JsFor(string filename)
{
return IncludeRejuicedJsFor(null, filename);
}
private static IVirtualPathResolver virtualPathResolver = new VirtualPathResolver();
private static ICacheProvider cacheProvider = new CacheProvider();
- public static IHtmlString IncludeRejuicedJsFor(this HtmlHelper instance, string filename)
+ public static MvcHtmlString IncludeRejuicedJsFor(HtmlHelper instance, string filename)
{
var cachedValue = GetCachedIncludesFor(filename);
if (cachedValue != null)
@@ -40,20 +40,20 @@ public static IHtmlString IncludeRejuicedJsFor(this HtmlHelper instance, string
if (config == null)
{
- return new HtmlString("");
+ return MvcHtmlString.Create("");
}
var dependencies = config.GetDependencies();
- var scripts = new HtmlString(string.Join("\n", toInclude.Select(f =>
+ var scripts = MvcHtmlString.Create(string.Join("\n", toInclude.Select(f =>
{
// Output <script src='' type=''>
var script = new TagBuilder("script");
- script.Attributes.Add("src", UrlHelper.GenerateContentUrl(f, HttpContext.Current.Request.RequestContext.HttpContext));
+ script.Attributes.Add("src", UrlHelper.GenerateContentUrl(f, new HttpContextWrapper(HttpContext.Current)));
script.Attributes.Add("type", "text/javascript");
return script.ToString(TagRenderMode.Normal);
- })));
+ }).ToArray()));
var cachedIncludes = new IncludesCacheModel { IncludesHtml = scripts, HashValue = config.GetHashValue(cacheProvider) };
@@ -62,7 +62,7 @@ public static IHtmlString IncludeRejuicedJsFor(this HtmlHelper instance, string
return cachedIncludes.RenderHtml();
}
- public static IHtmlString IncludeRejuicedCssFor(this HtmlHelper instance, string filename)
+ public static MvcHtmlString IncludeRejuicedCssFor(this HtmlHelper instance, string filename)
{
var cachedValue = GetCachedIncludesFor(filename);
if (cachedValue != null)
@@ -74,12 +74,12 @@ public static IHtmlString IncludeRejuicedCssFor(this HtmlHelper instance, string
var config = RejuicerEngine.GetConfigFor(filename);
if (config == null)
{
- return new HtmlString("");
+ return MvcHtmlString.Create("");
}
var dependencies = config.GetDependencies();
- var links = new HtmlString(string.Join("\n", toInclude.Select(f =>
+ var links = MvcHtmlString.Create(string.Join("\n", toInclude.Select(f =>
{
// Output <script src='' type=''>
var link = new TagBuilder("link");
@@ -88,7 +88,7 @@ public static IHtmlString IncludeRejuicedCssFor(this HtmlHelper instance, string
link.Attributes.Add("type", "text/css");
return link.ToString(TagRenderMode.SelfClosing);
- })));
+ }).ToArray()));
var cachedIncludes = new IncludesCacheModel { IncludesHtml = links, HashValue = config.GetHashValue(cacheProvider) };
@@ -112,7 +112,19 @@ public OutputContent GetContent(ICacheProvider cacheProvider, Mode mode)
Log.WriteLine("Minifying Content For '{0}'", VirtualPath);
// Minified value
- var minifiedContent = minificationProvider.Minify(rejuicedValue);
+ byte[] minifiedContent = null;
+ try
+ {
+ minifiedContent = minificationProvider.Minify(rejuicedValue);
+ }
+ catch (Exception e)
+ {
+ // Yes, catching Exception is bad. However, anyone can plug in their own minification provider
+ // and throw any exception they want. We want to make sure that exceptions thrown by rejuicer
+ // have the filename inside them. So we just wrap the exception here & throw the wrapped exception.
+ throw new InvalidOperationException(string.Format("Encountered exception trying minify invalid JavaScript for file '{0}'.", VirtualPath), e);
+ }
+
var minifiedValue = new OutputContent
{
Content = minifiedContent,
@@ -1,10 +1,5 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Web;
+using System.Web;
using System.IO;
-using System.Web.Mvc;
namespace Rejuicer
{
@@ -10,7 +10,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Rejuicer</RootNamespace>
<AssemblyName>Rejuicer</AssemblyName>
- <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
+ <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<SccProjectName>
</SccProjectName>
@@ -20,6 +20,7 @@
</SccAuxPath>
<SccProvider>
</SccProvider>
+ <TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
@@ -51,7 +52,6 @@
<Reference Include="System.Web.Routing" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
- <Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="Yahoo.Yui.Compressor, Version=1.6.0.2, Culture=neutral, PublicKeyToken=f8b4b81ec75097e2, processorArchitecture=MSIL">
@@ -63,7 +63,7 @@
<Compile Include="Engine\BaseStringMinificationProvider.cs" />
<Compile Include="Engine\CacheProvider.cs" />
<Compile Include="Engine\HashUtilities.cs" />
- <Compile Include="Engine\ImageMinificationProvider.cs" />
+ <Compile Include="Engine\MinificationProviders\ImageMinificationProvider.cs" />
<Compile Include="Engine\Log.cs" />
<Compile Include="Engine\PhysicalFileRegister.cs" />
<Compile Include="Model\IPhysicalFileDependency.cs" />
@@ -48,7 +48,7 @@ public IOrderedEnumerable<IContentSource> Evaluate(ResourceType resourceType)
if (physicalPath == null)
return Enumerable.Empty<IContentSource>().OrderBy(x => x);
- return Directory.EnumerateFiles(physicalPath.FullName, SearchPattern ?? "*", IsRecursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)
+ return Directory.GetFiles(physicalPath.FullName, SearchPattern ?? "*", IsRecursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)
.Select(f => new FileInfo(f))
.Select(file => PhysicalFileRegister.For(file, resourceType, Mode))
.OfType<IContentSource>()
@@ -1,7 +1,7 @@
-<?xml version="1.0" encoding="utf-8" ?>
+<?xml version="1.0"?>
<configuration>
<configSections>
<section name="rejuicer" type="Rejuicer.Configuration.RejuicerConfiguration, Rejuicer"/>
</configSections>
- <rejuicer PreventPassThroughOnDebug="true" />
-</configuration>
+ <rejuicer PreventPassThroughOnDebug="true"/>
+<startup><supportedRuntime version="v2.0.50727"/></startup></configuration>
@@ -10,8 +10,9 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>RejuicerConfiguration_test</RootNamespace>
<AssemblyName>RejuicerConfiguration-test</AssemblyName>
- <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
+ <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
+ <TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
@@ -40,7 +41,6 @@
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
- <Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>

0 comments on commit e882bbf

Please sign in to comment.