Skip to content

Commit

Permalink
Enabed content transformations on pass through debugging mode. Otherw…
Browse files Browse the repository at this point in the history
…ise CSS transforms are not performed.
  • Loading branch information
skroonenburg committed Sep 25, 2011
1 parent fcb99c8 commit ab82e5c
Show file tree
Hide file tree
Showing 18 changed files with 132 additions and 67 deletions.
Binary file modified Lib/Rejuicer.dll
Binary file not shown.
2 changes: 1 addition & 1 deletion Nuspec/Rejuicer.nuspec
Expand Up @@ -2,7 +2,7 @@
<package>
<metadata>
<id>Rejuicer</id>
<version>1.1.3</version>
<version>1.2.0</version>
<authors>Sam Kroonenburg</authors>
<description>Minify your CSS/JS. Simple fluent configuration. Wildcard file matching. Works on Azure. Easy script debugging.</description>
<language>en-US</language>
Expand Down
3 changes: 2 additions & 1 deletion Source/Rejuicer/Rejuicer-test/DefaultCssTransformerTest.cs
Expand Up @@ -6,6 +6,7 @@
using NUnit.Framework;
using Rejuicer;
using Rejuicer.Engine;
using Rejuicer.Model;

namespace Rejuicer_test
{
Expand All @@ -14,7 +15,7 @@ public class DefaultCssTransformerTest
{
private string TransformCss(string css, Func<string, string> replacement)
{
return new DefaultCssTransformer(new StubVirtualPathResolver(replacement)).TransformFile(css);
return new DefaultCssTransformer(new StubVirtualPathResolver(replacement)).TransformFile(new PhysicalFileSource(ResourceType.Css, Mode.Minify, "~/test.css", "test.css"), css.AsStream()).ReadString();
}

[Test]
Expand Down
9 changes: 8 additions & 1 deletion Source/Rejuicer/Rejuicer/Engine/DefaultCssTransformer.cs
Expand Up @@ -41,6 +41,11 @@ public Stream TransformFile(PhysicalFileSource source, Stream inputContent)
var url = x.Groups["capturedUrl"];
if (url != null)
{
if (!url.Value.StartsWith("~"))
{
return x.Value;
}
var quotation = x.Groups["quotation"];
var quoteWrapper = quotation != null ? quotation.Value : "";
var virtualPath = url.Value;
Expand All @@ -59,7 +64,9 @@ public Stream TransformFile(PhysicalFileSource source, Stream inputContent)
}
// get the timestamp and write it out into the URL...
virtualPath = RejuicerEngine.GetConfigFor(virtualPath).GetTimestampedUrl(_cacheProvider);
var config = RejuicerEngine.GetConfigFor(virtualPath);
// don't timestamp the URL in pass through mode
virtualPath = RejuicerEngine.IsPassThroughEnabled ? config.GetNonTimestampedUrl(_cacheProvider) : config.GetTimestampedUrl(_cacheProvider);
// Need to add a dependency of this CSS file to the now linked image.
source.AddDependency(RejuicerEngine.GetConfigFor(url.Value));
Expand Down
2 changes: 1 addition & 1 deletion Source/Rejuicer/Rejuicer/Engine/FileResolver.cs
Expand Up @@ -19,7 +19,7 @@ static FileResolver()

public static IEnumerable<string> VirtualPathsFor(RejuicerConfigurationSource config)
{
return config == null ? Enumerable.Empty<string>() : config.GetDependencies().Select(f => VirtualPathResolver.GetVirtualPathFor(f));
return config == null ? Enumerable.Empty<string>() : config.GetDependencies(config.ResourceType).Select(f => VirtualPathResolver.GetVirtualPathFor(f));
}

private class FileComparer : IEqualityComparer<FileInfo>
Expand Down
48 changes: 48 additions & 0 deletions Source/Rejuicer/Rejuicer/Engine/PhysicalFileRegister.cs
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Rejuicer.Model;

namespace Rejuicer.Engine
{
public static class PhysicalFileRegister
{
private static readonly IDictionary<string, PhysicalFileSource> fileSourceRegister = new Dictionary<string, PhysicalFileSource>();
internal static IVirtualPathResolver VirtualPathResolver { get; set; }

static PhysicalFileRegister()
{
VirtualPathResolver = new VirtualPathResolver();
}

public static void Set(PhysicalFileSource fileSource)
{
fileSourceRegister[fileSource.VirtualPath] = fileSource;
}

public static PhysicalFileSource For(string virtualPath)
{
return fileSourceRegister.ContainsKey(virtualPath) ? fileSourceRegister[virtualPath] : null;
}

public static PhysicalFileSource For(FileInfo file, ResourceType resourceType, Mode mode)
{
var virtualPathFor = VirtualPathResolver.GetVirtualPathFor(file);

var physicalFileSource = For(virtualPathFor);

if (physicalFileSource == null)
{
physicalFileSource = new PhysicalFileSource(resourceType,
virtualPathFor,
file.FullName);

Set(physicalFileSource);
}

return physicalFileSource;
}
}
}
34 changes: 29 additions & 5 deletions Source/Rejuicer/Rejuicer/Engine/RejuicerEngine.cs
Expand Up @@ -19,6 +19,7 @@ internal static class RejuicerEngine
internal static Dictionary<string, RejuicerConfigurationSource> _configurations = new Dictionary<string,RejuicerConfigurationSource>();
private static ICacheProvider cacheProvider = new CacheProvider();
private static bool _configurationContainsPlaceholder = false;
internal static IVirtualPathResolver _virtualPathResolver = new VirtualPathResolver();

internal static void AddConfiguration(RejuicerConfigurationSource config)
{
Expand Down Expand Up @@ -71,6 +72,7 @@ public static bool HasConfigurationFor(string virtualPath)

internal static IEnumerable<string> GetVirtualPathsFor(string requestedFilename)
{
// Get all dependencies of the matching content type of the parent.
return FileResolver.VirtualPathsFor(GetConfigFor(requestedFilename));
}

Expand All @@ -79,16 +81,30 @@ public static IEnumerable<FileInfo> GetDependenciesFor(string requestedFilename)
return GetConfigFor(requestedFilename).GetDependencies();
}

private static PhysicalFileSource GetPhysicalFileFor(string requestedFilename)
{
requestedFilename = MakeVirtualPathFromRequestUrl(requestedFilename);

return PhysicalFileRegister.For(requestedFilename);
}

public static bool PassThroughDebuggingFor(Uri requestedUrl)
{
return IsPassThroughEnabled && GetPhysicalFileFor(VirtualPathUtility.ToAppRelative(requestedUrl.AbsolutePath)) != null;
}

private static string MakeVirtualPathFromRequestUrl(string requestUrl)
{
return !requestUrl.StartsWith("~") ? string.Format("~{0}", requestUrl) : requestUrl;
}

internal static RejuicerConfigurationSource GetConfigFor(string requestedFilename)
{
try
{
_configurationLock.EnterReadLock();

if (!requestedFilename.StartsWith("~"))
{
requestedFilename = string.Format("~{0}", requestedFilename);
}
requestedFilename = MakeVirtualPathFromRequestUrl(requestedFilename);

if (!_configurationContainsPlaceholder)
{
Expand Down Expand Up @@ -148,7 +164,15 @@ public static OutputContent GetContentFor(Uri uri)

public static OutputContent GetContentFor(string requestedFilename)
{
return GetConfigFor(requestedFilename).GetContent(cacheProvider);
return ((IContentSource)GetConfigFor(requestedFilename) ?? (IsPassThroughEnabled ? GetPhysicalFileFor(requestedFilename) : null)).GetContent(cacheProvider);
}

public static bool IsPassThroughEnabled
{
get
{
return HttpContext.Current.IsDebuggingEnabled && (Configuration == null || ((!Configuration.PreventPassThroughOnDebug.HasValue || !Configuration.PreventPassThroughOnDebug.Value)));
}
}
}
}
34 changes: 0 additions & 34 deletions Source/Rejuicer/Rejuicer/Engine/RejuicerHandler.cs

This file was deleted.

2 changes: 1 addition & 1 deletion Source/Rejuicer/Rejuicer/Engine/RejuicerModule.cs
Expand Up @@ -28,7 +28,7 @@ private static void context_BeginRequest(object sender, EventArgs e)
var request = HttpContext.Current.Request;
var requested = request.Url;

if (RejuicerEngine.HasConfigurationFor(requested))
if (RejuicerEngine.HasConfigurationFor(requested) || RejuicerEngine.PassThroughDebuggingFor(requested))
{
var result = RejuicerEngine.GetContentFor(requested);

Expand Down
5 changes: 1 addition & 4 deletions Source/Rejuicer/Rejuicer/Engine/StreamUtilities.cs
Expand Up @@ -42,10 +42,7 @@ public static Stream AsStream(this string input)

public static string ReadString(this Stream input)
{
using (var streamReader = new StreamReader(input))
{
return streamReader.ReadToEnd();
}
return new StreamReader(input).ReadToEnd();
}
}
}
18 changes: 8 additions & 10 deletions Source/Rejuicer/Rejuicer/HtmlHelpers/Rejuicer.cs
Expand Up @@ -37,6 +37,12 @@ public static IHtmlString IncludeRejuicedJsFor(this HtmlHelper instance, string

var toInclude = GetIncludesFor(filename);
var config = RejuicerEngine.GetConfigFor(filename);

if (config == null)
{
return new HtmlString("");
}

var dependencies = config.GetDependencies();

var scripts = new HtmlString(string.Join("\n", toInclude.Select(f =>
Expand Down Expand Up @@ -86,19 +92,11 @@ public static IHtmlString IncludeRejuicedCssFor(this HtmlHelper instance, string
return cachedIncludes.RenderHtml();
}

private static bool PassThroughOnDebugging
{
get
{
return HttpContext.Current.IsDebuggingEnabled && !(RejuicerConfiguration.Current != null && RejuicerConfiguration.Current.PreventPassThroughOnDebug.HasValue && RejuicerConfiguration.Current.PreventPassThroughOnDebug.Value);
}
}

private static IEnumerable<string> GetIncludesFor(string filename)
{
var toInclude = new List<string>();

if (PassThroughOnDebugging)
if (RejuicerEngine.IsPassThroughEnabled)
{
toInclude.AddRange(RejuicerEngine.GetVirtualPathsFor(filename));
}
Expand All @@ -118,7 +116,7 @@ private static IncludesCacheModel GetCachedIncludesFor(string filename)
private static void SetCachedIncludesFor(string filename, IncludesCacheModel value, IEnumerable<FileInfo> files)
{
// Create a depdency on all of the files
var dependencies = PassThroughOnDebugging ? null :
var dependencies = RejuicerEngine.IsPassThroughEnabled ? null :
files.Where(f => f != null);

cacheProvider.Add(GetCacheKeyFor(filename), value, dependencies);
Expand Down
1 change: 1 addition & 0 deletions Source/Rejuicer/Rejuicer/Model/IContentSource.cs
Expand Up @@ -10,6 +10,7 @@ namespace Rejuicer.Model
public interface IContentSource
{
ResourceType ResourceType { get; }
IEnumerable<FileInfo> GetDependencies(ResourceType? resourceType);
IEnumerable<FileInfo> GetDependencies();
OutputContent GetContent(ICacheProvider cacheProvider);
}
Expand Down
17 changes: 14 additions & 3 deletions Source/Rejuicer/Rejuicer/Model/PhysicalFileSource.cs
Expand Up @@ -13,24 +13,35 @@ public class PhysicalFileSource : IContentSource, IPhysicalFileDependency
{
private ReaderWriterLockSlim _lock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);

public PhysicalFileSource(ResourceType resourceType, Mode mode, string virtualPath, string physicalPath)
public PhysicalFileSource(ResourceType resourceType, string virtualPath, string physicalPath)
{
VirtualPath = virtualPath;
ResourceType = resourceType;
PhysicalPath = physicalPath;
Mode = mode;
_dependencies = new List<IContentSource>();
}

public ResourceType ResourceType { get; private set; }
public Mode Mode { get; private set; }

public IEnumerable<FileInfo> GetDependencies()
{
return GetDependencies(null);
}

public IEnumerable<FileInfo> GetDependencies(ResourceType? resourceType)
{
_lock.EnterReadLock();
try
{
return _dependencies.SelectMany(x => x.GetDependencies()).Union(new[] {new FileInfo(PhysicalPath)});
var dependencies = _dependencies.SelectMany(x => x.GetDependencies(resourceType));

if (resourceType.HasValue && ResourceType == resourceType.Value)
{
dependencies = dependencies.Union(new[] { new FileInfo(PhysicalPath) });
}

return dependencies;
}
finally
{
Expand Down
12 changes: 11 additions & 1 deletion Source/Rejuicer/Rejuicer/Model/RejuicerConfigurationSource.cs
Expand Up @@ -79,11 +79,16 @@ public void ReevaluateRules()
}

public IEnumerable<FileInfo> GetDependencies()
{
return GetDependencies(null);
}

public IEnumerable<FileInfo> GetDependencies(ResourceType? resourceType)
{
_lock.EnterReadLock();
try
{
return this.SelectMany(x => x.GetDependencies()).Distinct();
return this.SelectMany(x => x.GetDependencies(resourceType)).Distinct();
}
finally
{
Expand All @@ -102,6 +107,11 @@ public string GetTimestampedUrl(ICacheProvider cacheProvider)
(GetLastModifiedDate(cacheProvider) - DateTime.MinValue).Ticks.ToString());
}

public string GetNonTimestampedUrl(ICacheProvider cacheProvider)
{
return RequestFor.Replace(FilenameUniquePlaceholder, "");
}

public OutputContent GetContent(ICacheProvider cacheProvider)
{
var upgraded = false;
Expand Down
4 changes: 2 additions & 2 deletions Source/Rejuicer/Rejuicer/Properties/AssemblyInfo.cs
Expand Up @@ -32,7 +32,7 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.1.1.0")]
[assembly: AssemblyFileVersion("1.1.1.0")]
[assembly: AssemblyVersion("1.2.0.0")]
[assembly: AssemblyFileVersion("1.2.0.0")]
[assembly: InternalsVisibleTo("Rejuicer-test")]
[assembly: InternalsVisibleTo("RejuicerConfiguration-test")]
2 changes: 1 addition & 1 deletion Source/Rejuicer/Rejuicer/Rejuicer.csproj
Expand Up @@ -62,6 +62,7 @@
<Compile Include="Engine\BaseStringMinificationProvider.cs" />
<Compile Include="Engine\CacheProvider.cs" />
<Compile Include="Engine\ImageMinificationProvider.cs" />
<Compile Include="Engine\PhysicalFileRegister.cs" />
<Compile Include="Model\IPhysicalFileDependency.cs" />
<Compile Include="Engine\JsMinificationProvider.cs" />
<Compile Include="Engine\CssMinificationProvider.cs" />
Expand Down Expand Up @@ -103,7 +104,6 @@
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<Compile Include="Engine\RejuicerHandler.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
3 changes: 2 additions & 1 deletion Source/Rejuicer/Rejuicer/Rules/SingleFileRule.cs
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rejuicer.Engine;

namespace Rejuicer.Model
{
Expand All @@ -26,7 +27,7 @@ public SingleFileRule(string virtualPath, Mode mode, IVirtualPathResolver virtua

public IOrderedEnumerable<IContentSource> Evaluate(ResourceType resourceType)
{
return new IContentSource[] { new PhysicalFileSource(resourceType, Mode, VirtualPath, VirtualPathResolver.ResolveVirtualPathToFile(VirtualPath).FullName) }.OrderBy(x => x);
return new IContentSource[] { PhysicalFileRegister.For(VirtualPathResolver.ResolveVirtualPathToFile(VirtualPath), resourceType, Mode) }.OrderBy(x => x);
}
}
}

0 comments on commit ab82e5c

Please sign in to comment.