Skip to content
Browse files

Fix the support for different bundle types that share the same path.

  • Loading branch information...
1 parent 54cbc5b commit 137493a0f76099f74e8fa57498be2c6008df9b55 @andrewdavey committed Jan 3, 2012
View
6 src/Cassette.IntegrationTests/Cassette.IntegrationTests.csproj
@@ -82,6 +82,12 @@
<Content Include="assets\templates\asset-2.htm">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
+ <Content Include="overlap\widget.css">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </Content>
+ <Content Include="overlap\widget.js">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </Content>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Cassette.Web\Cassette.Web.csproj">
View
31 src/Cassette.IntegrationTests/CassetteApplication.cs
@@ -8,6 +8,7 @@
using Cassette.HtmlTemplates;
using Cassette.IO;
using Cassette.Scripts;
+using Cassette.Stylesheets;
using Cassette.Web;
using Moq;
using Should;
@@ -111,16 +112,40 @@ public void HtmlTemplatesCanBeRenderedInlineInPage()
}
}
- CassetteApplication CreateApplication(Action<BundleCollection> configure)
+ [Fact]
+ public void GivenReferenceToBundlesWithSharedPath_WhenRenderEachType_ThenHtmlIsReturnedForEach()
+ {
+ Action<BundleCollection> config = bundles =>
+ {
+ bundles.Add<StylesheetBundle>("~/");
+ bundles.Add<ScriptBundle>("~/");
+ };
+
+ using (var app = CreateApplication(config, "overlap"))
+ {
+ app.OnPostMapRequestHandler();
+ var builder = app.GetReferenceBuilder();
+
+ builder.Reference("~");
+ var scriptHtml = builder.Render<ScriptBundle>(null);
+ var stylesheetHtml = builder.Render<StylesheetBundle>(null);
+
+ stylesheetHtml.ShouldContain("<link");
+ scriptHtml.ShouldContain("<script");
+ }
+ }
+
+ CassetteApplication CreateApplication(Action<BundleCollection> configure, string sourceDirectory = "assets")
{
+ IBundleContainer bundleContainer = null;
var settings = new CassetteSettings("")
{
CacheDirectory = new IsolatedStorageDirectory(storage),
- SourceDirectory = new FileSystemDirectory(Path.GetFullPath("assets"))
+ SourceDirectory = new FileSystemDirectory(Path.GetFullPath(sourceDirectory)),
+ UrlGenerator = new CassetteRouting(new VirtualDirectoryPrepender("/"), () => bundleContainer)
};
var bundles = new BundleCollection(settings);
configure(bundles);
- IBundleContainer bundleContainer = null;
var application = new CassetteApplication(
bundles,
settings,
View
1 src/Cassette.IntegrationTests/overlap/widget.css
@@ -0,0 +1 @@
+/* File Created: January 3, 2012 */
View
1 src/Cassette.IntegrationTests/overlap/widget.js
@@ -0,0 +1 @@
+
View
8 src/Cassette.UnitTests/BundleContainer.cs
@@ -50,7 +50,7 @@ public void FindBundleContainingPathOfBundleReturnsTheBundle()
var container = new BundleContainer(new[] {
expectedBundle
});
- var actualBundle = container.FindBundleContainingPath<Bundle>("~/test");
+ var actualBundle = container.FindBundlesContainingPath("~/test").First();
actualBundle.ShouldBeSameAs(expectedBundle);
}
@@ -60,8 +60,8 @@ public void FindBundleContainingPathWithWrongPathReturnsNull()
var container = new BundleContainer(new[] {
new TestableBundle("~/test")
});
- var actualBundle = container.FindBundleContainingPath<Bundle>("~/WRONG");
- actualBundle.ShouldBeNull();
+ var actualBundle = container.FindBundlesContainingPath("~/WRONG");
+ actualBundle.ShouldBeEmpty();
}
[Fact]
@@ -76,7 +76,7 @@ public void FindBundleContainingPathOfAssetReturnsTheBundle()
var container = new BundleContainer(new[] {
expectedBundle
});
- var actualBundle = container.FindBundleContainingPath<Bundle>("~/test/test.js");
+ var actualBundle = container.FindBundlesContainingPath("~/test/test.js").First();
actualBundle.ShouldBeSameAs(expectedBundle);
}
View
10 src/Cassette.UnitTests/Configuration/BundleContainerFactoryTestSuite.cs
@@ -22,8 +22,8 @@ public void WhenCreateWithBundles_ThenItReturnsContainerWithBundles()
var builder = CreateFactory(new Dictionary<Type, IBundleFactory<Bundle>>());
var container = builder.Create(bundles, CreateSettings());
- container.FindBundleContainingPath<Bundle>("~/test1").ShouldBeSameAs(bundle1);
- container.FindBundleContainingPath<Bundle>("~/test2").ShouldBeSameAs(bundle2);
+ container.FindBundlesContainingPath("~/test1").First().ShouldBeSameAs(bundle1);
+ container.FindBundlesContainingPath("~/test2").First().ShouldBeSameAs(bundle2);
}
[Fact]
@@ -54,7 +54,7 @@ public void WhenCreateWithBundleHavingExternalReference_ThenAnExternalBundleIsAl
var builder = CreateFactory(factories);
var container = builder.Create(new[] { bundle }, CreateSettings());
- container.FindBundleContainingPath<Bundle>("http://external.com/api.js").ShouldBeSameAs(externalBundle);
+ container.FindBundlesContainingPath("http://external.com/api.js").First().ShouldBeSameAs(externalBundle);
}
[Fact]
@@ -99,7 +99,7 @@ public void GivenAssetWithUrlReference_WhenCreate_ThenExternalBundleInContainer(
var settings = CreateSettings();
var container = containerFactory.Create(new[] { bundle }, settings);
- container.FindBundleContainingPath<Bundle>("http://test.com/").ShouldBeSameAs(externalBundle);
+ container.FindBundlesContainingPath("http://test.com/").First().ShouldBeSameAs(externalBundle);
}
[Fact]
@@ -123,7 +123,7 @@ public void GivenAssetWithUrlReferenceAndSameBundleLevelUrlReference_WhenCreate_
var settings = CreateSettings();
var container = containerFactory.Create(new[] { bundle }, settings);
- container.FindBundleContainingPath<Bundle>("http://test.com/").ShouldBeSameAs(externalBundle);
+ container.FindBundlesContainingPath("http://test.com/").First().ShouldBeSameAs(externalBundle);
}
[Fact]
View
3 src/Cassette.UnitTests/Configuration/CachedBundleContainerFactory.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using System.Linq;
using Cassette.Persistence;
using Moq;
using Should;
@@ -20,7 +21,7 @@ public void GivenCacheIsUpToDate_WhenCreateWithBundle_ThenContainerCreatedWithTh
var factory = CreateFactory();
var container = factory.Create(bundles, CreateSettings());
- container.FindBundleContainingPath<Bundle>("~/test").ShouldBeSameAs(bundles[0]);
+ container.FindBundlesContainingPath("~/test").SequenceEqual(bundles).ShouldBeTrue();
}
[Fact]
View
73 src/Cassette.UnitTests/ReferenceBuilder.cs
@@ -15,7 +15,7 @@ public class ReferenceBuilder_Reference_Tests
{
public ReferenceBuilder_Reference_Tests()
{
- application = new Mock<ICassetteApplication>();
+ new Mock<ICassetteApplication>();
bundleFactories = new Dictionary<Type, IBundleFactory<Bundle>>();
bundleContainer = new Mock<IBundleContainer>();
bundleContainer.Setup(c => c.IncludeReferencesAndSortBundles(It.IsAny<IEnumerable<Bundle>>()))
@@ -26,15 +26,14 @@ public ReferenceBuilder_Reference_Tests()
ReferenceBuilder builder;
readonly Mock<IBundleContainer> bundleContainer;
- readonly Mock<ICassetteApplication> application;
readonly Dictionary<Type, IBundleFactory<Bundle>> bundleFactories;
[Fact]
public void WhenAddReferenceToBundleDirectory_ThenGetBundlesReturnTheBundle()
{
var bundle = new ScriptBundle("~/test");
- bundleContainer.Setup(c => c.FindBundleContainingPath<Bundle>("~/test"))
- .Returns(bundle);
+ bundleContainer.Setup(c => c.FindBundlesContainingPath("~/test"))
+ .Returns(new[] { bundle });
bundleContainer.Setup(c => c.IncludeReferencesAndSortBundles(It.IsAny<IEnumerable<Bundle>>()))
.Returns(new[] { bundle })
.Verifiable();
@@ -50,8 +49,8 @@ public void WhenAddReferenceToBundleDirectory_ThenGetBundlesReturnTheBundle()
public void WhenAddReferenceToSameBundleTwice_ThenGetBundlesReturnsOnlyOneBundle()
{
var bundle = new ScriptBundle("~/test");
- bundleContainer.Setup(c => c.FindBundleContainingPath<Bundle>("~/test"))
- .Returns(bundle);
+ bundleContainer.Setup(c => c.FindBundlesContainingPath("~/test"))
+ .Returns(new[] { bundle });
bundleContainer.Setup(c => c.IncludeReferencesAndSortBundles(It.IsAny<IEnumerable<Bundle>>()))
.Returns(new[] { bundle })
.Verifiable();
@@ -68,8 +67,8 @@ public void WhenAddReferenceToBundleDirectoryWithLocation_ThenGetBundlesThatLoca
{
var bundle = new ScriptBundle("~/test");
bundle.PageLocation = "body";
- bundleContainer.Setup(c => c.FindBundleContainingPath<Bundle>("~/test"))
- .Returns(bundle);
+ bundleContainer.Setup(c => c.FindBundlesContainingPath("~/test"))
+ .Returns(new[] { bundle });
bundleContainer.Setup(c => c.IncludeReferencesAndSortBundles(It.IsAny<IEnumerable<Bundle>>()))
.Returns(new[] { bundle })
.Verifiable();
@@ -87,10 +86,10 @@ public void OnlyBundlesMatchingLocationAreReturnedByGetBundles()
var bundle1 = new ScriptBundle("~/test1");
var bundle2 = new ScriptBundle("~/test2");
bundle1.PageLocation = "body";
- bundleContainer.Setup(c => c.FindBundleContainingPath<Bundle>("~/test1"))
- .Returns(bundle1);
- bundleContainer.Setup(c => c.FindBundleContainingPath<Bundle>("~/test2"))
- .Returns(bundle2);
+ bundleContainer.Setup(c => c.FindBundlesContainingPath("~/test1"))
+ .Returns(new[] { bundle1 });
+ bundleContainer.Setup(c => c.FindBundlesContainingPath("~/test2"))
+ .Returns(new[] { bundle2 });
bundleContainer.Setup(c => c.IncludeReferencesAndSortBundles(It.IsAny<IEnumerable<Bundle>>()))
.Returns(new[] { bundle1 });
builder.Reference("test1", null);
@@ -104,7 +103,7 @@ public void OnlyBundlesMatchingLocationAreReturnedByGetBundles()
[Fact]
public void WhenAddReferenceToNonExistentBundle_ThenThrowException()
{
- bundleContainer.Setup(c => c.FindBundleContainingPath<Bundle>("~\\test")).Returns((ScriptBundle)null);
+ bundleContainer.Setup(c => c.FindBundlesContainingPath("~\\test")).Returns(new ScriptBundle[0]);
Assert.Throws<ArgumentException>(delegate
{
@@ -118,8 +117,8 @@ public void GivenBundleAReferencesBundleB_WhenAddReferenceToBundleA_ThenGetBundl
var bundleA = new ScriptBundle("~/a");
var bundleB = new ScriptBundle("~/b");
- bundleContainer.Setup(c => c.FindBundleContainingPath<Bundle>("~/a"))
- .Returns(bundleA);
+ bundleContainer.Setup(c => c.FindBundlesContainingPath("~/a"))
+ .Returns(new[] { bundleA });
bundleContainer.Setup(c => c.IncludeReferencesAndSortBundles(It.IsAny<IEnumerable<Bundle>>()))
.Returns(new[] { bundleB, bundleA });
@@ -222,8 +221,8 @@ public void WhenAddReferenceToUnknownUrlWithBundleTypeAndUnexpectedExtension_The
public void WhenAddReferenceWithLocation_ThenGetBundlesForThatLocationReturnsTheBundle()
{
var bundle = new ScriptBundle("~/test");
- bundleContainer.Setup(c => c.FindBundleContainingPath<Bundle>("~/test"))
- .Returns(bundle);
+ bundleContainer.Setup(c => c.FindBundlesContainingPath("~/test"))
+ .Returns(new[] { bundle });
bundleContainer.Setup(c => c.IncludeReferencesAndSortBundles(It.IsAny<IEnumerable<Bundle>>()))
.Returns(new[] { bundle });
builder.Reference("test", "body");
@@ -235,8 +234,8 @@ public void WhenAddReferenceWithLocation_ThenGetBundlesForThatLocationReturnsThe
public void GivenNullLocationAlreadyRendered_WhenAddReferenceToNullLocation_ThenExceptionThrown()
{
var bundle = new ScriptBundle("~/test");
- bundleContainer.Setup(c => c.FindBundleContainingPath<Bundle>("~/test"))
- .Returns(bundle);
+ bundleContainer.Setup(c => c.FindBundlesContainingPath("~/test"))
+ .Returns(new[] { bundle });
builder.Render<ScriptBundle>();
@@ -249,8 +248,8 @@ public void GivenNullLocationAlreadyRendered_WhenAddReferenceToNullLocation_Then
public void GivenLocationAlreadyRendered_WhenAddReferenceToThatLocation_ThenExceptionThrown()
{
var bundle = new ScriptBundle("~/test");
- bundleContainer.Setup(c => c.FindBundleContainingPath<Bundle>("~/test"))
- .Returns(bundle);
+ bundleContainer.Setup(c => c.FindBundlesContainingPath("~/test"))
+ .Returns(new[] { bundle });
builder.Render<ScriptBundle>("location");
@@ -268,14 +267,26 @@ public void GivenLocationAlreadyRenderedButHtmlRewrittingEnabled_WhenAddReferenc
new CassetteSettings("") { IsHtmlRewritingEnabled = true }
);
var bundle = new ScriptBundle("~/test");
- bundleContainer.Setup(c => c.FindBundleContainingPath<Bundle>("~/test"))
- .Returns(bundle);
+ bundleContainer.Setup(c => c.FindBundlesContainingPath("~/test"))
+ .Returns(new[] { bundle });
builder.Render<ScriptBundle>("test");
builder.Reference("~/test", "test");
builder.GetBundles("test").First().ShouldBeSameAs(bundle);
}
+
+ [Fact]
+ public void GivenTwoBundlesWithSamePathButDifferentType_WhenReferenceThePath_ThenBothBundlesAreReferenced()
+ {
+ var bundle1 = new ScriptBundle("~/test");
+ var bundle2 = new StylesheetBundle("~/test");
+ bundleContainer.Setup(c => c.FindBundlesContainingPath("~/test"))
+ .Returns(new Bundle[] { bundle1, bundle2 });
+
+ builder.Reference("~/test");
+ builder.GetBundles(null).Count().ShouldEqual(2);
+ }
}
public class ReferenceBuilder_Render_Tests
@@ -306,8 +317,8 @@ public ReferenceBuilder_Render_Tests()
public void GivenAddReferenceToPath_WhenRender_ThenBundleRenderOutputReturned()
{
var bundle = new TestableBundle("~/stub");
- bundleContainer.Setup(c => c.FindBundleContainingPath<Bundle>(It.IsAny<string>()))
- .Returns(bundle);
+ bundleContainer.Setup(c => c.FindBundlesContainingPath(It.IsAny<string>()))
+ .Returns(new[] { bundle });
referenceBuilder.Reference("test");
@@ -320,8 +331,8 @@ public void GivenAddReferenceToPath_WhenRender_ThenBundleRenderOutputReturned()
public void GivenAddReferenceToPath_WhenRenderWithLocation_ThenBundleRenderOutputReturned()
{
var bundle = new TestableBundle("~/stub") { RenderResult = "output" };
- bundleContainer.Setup(c => c.FindBundleContainingPath<Bundle>(It.IsAny<string>()))
- .Returns(bundle);
+ bundleContainer.Setup(c => c.FindBundlesContainingPath(It.IsAny<string>()))
+ .Returns(new[] { bundle });
referenceBuilder.Reference("test");
var html = referenceBuilder.Render<TestableBundle>("body");
@@ -334,10 +345,10 @@ public void GivenAddReferenceToTwoPaths_WhenRender_ThenBundleRenderOutputsSepara
{
var bundle1 = new TestableBundle("~/stub1") { RenderResult = "output1" };
var bundle2 = new TestableBundle("~/stub2") { RenderResult = "output2" };
- bundleContainer.Setup(c => c.FindBundleContainingPath<Bundle>("~/stub1"))
- .Returns(bundle1);
- bundleContainer.Setup(c => c.FindBundleContainingPath<Bundle>("~/stub2"))
- .Returns(bundle2);
+ bundleContainer.Setup(c => c.FindBundlesContainingPath("~/stub1"))
+ .Returns(new[] { bundle1 });
+ bundleContainer.Setup(c => c.FindBundlesContainingPath("~/stub2"))
+ .Returns(new[] { bundle2 });
referenceBuilder.Reference("~/stub1");
referenceBuilder.Reference("~/stub2");
View
11 src/Cassette.UnitTests/Web/AssetRequestHandler.cs
@@ -37,9 +37,9 @@ public AssetRequestHandler_Tests()
response.SetupGet(r => r.Cache).Returns(cache.Object);
request.SetupGet(r => r.Headers).Returns(requestHeaders);
- var bundleContainer = new Mock<IBundleContainer>();
- bundleContainer.Setup(c => c.FindBundleContainingPath<Bundle>(It.IsAny<string>()))
- .Returns(() => bundle);
+ bundleContainer = new Mock<IBundleContainer>();
+ bundleContainer.Setup(c => c.FindBundlesContainingPath(It.IsAny<string>()))
+ .Returns(() => new[] { bundle });
handler = new AssetRequestHandler(requestContext.Object, () => bundleContainer.Object);
}
@@ -48,6 +48,7 @@ public AssetRequestHandler_Tests()
readonly Mock<HttpResponseBase> response;
readonly Mock<HttpCachePolicyBase> cache;
readonly NameValueCollection requestHeaders;
+ readonly Mock<IBundleContainer> bundleContainer;
Bundle bundle;
MemoryStream outputStream;
@@ -58,8 +59,10 @@ public void IsReusableIsFalse()
}
[Fact]
- public void GivenBundleIsNull_WhenProcessRequest_ThenNotFoundResponse()
+ public void GivenBundleNotFound_WhenProcessRequest_ThenNotFoundResponse()
{
+ bundleContainer.Setup(c => c.FindBundlesContainingPath(It.IsAny<string>()))
+ .Returns(() => new Bundle[0]);
handler.ProcessRequest(null);
response.VerifySet(r => r.StatusCode = 404);
}
View
8 src/Cassette.UnitTests/Web/BundleRequestHandler.cs
@@ -64,8 +64,8 @@ protected void SetupTestBundle()
.Returns(() => "asset-content".AsStream());
asset.SetupGet(a => a.Hash).Returns(new byte[] { 1, 2, 3 });
bundle.Assets.Add(asset.Object);
- container.Setup(c => c.FindBundleContainingPath<TestableBundle>("~/test"))
- .Returns(bundle);
+ container.Setup(c => c.FindBundlesContainingPath("~/test"))
+ .Returns(new[] { bundle });
}
void IDisposable.Dispose()
@@ -84,8 +84,8 @@ public GivenBundleExists_WhenProcessRequest()
.Returns(() => "asset-content".AsStream());
asset.SetupGet(a => a.Hash).Returns(new byte[] { 1, 2, 3 });
bundle.Assets.Add(asset.Object);
- container.Setup(c => c.FindBundleContainingPath<TestableBundle>("~/test"))
- .Returns(bundle);
+ container.Setup(c => c.FindBundlesContainingPath("~/test"))
+ .Returns(new[] { bundle });
var handler = CreateRequestHandler("test_010203");
handler.ProcessRequest();
View
13 src/Cassette.UnitTests/Web/Bundles.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using System.Linq;
using Cassette.Configuration;
using Cassette.HtmlTemplates;
using Cassette.IO;
@@ -135,8 +136,8 @@ public void UrlUsesGetBundleUrlDelegate()
{
var bundle = new TestableBundle("~/test");
bundleContainer
- .Setup(c => c.FindBundleContainingPath<Bundle>("~/test"))
- .Returns(bundle);
+ .Setup(c => c.FindBundlesContainingPath("~/test"))
+ .Returns(new[] { bundle });
urlGenerator
.Setup(g => g.CreateBundleUrl(bundle))
.Returns("URL");
@@ -151,8 +152,8 @@ public void TypedUrlUsesGetBundleUrlDelegate()
{
var bundle = new TestableBundle("~/test");
bundleContainer
- .Setup(c => c.FindBundleContainingPath<TestableBundle>("~/test"))
- .Returns(bundle);
+ .Setup(c => c.FindBundlesContainingPath("~/test"))
+ .Returns(new[] { bundle });
urlGenerator
.Setup(g => g.CreateBundleUrl(bundle))
.Returns("URL");
@@ -181,9 +182,9 @@ public TestableApplication(IUrlGenerator urlGenerator, IReferenceBuilder referen
public override T FindBundleContainingPath<T>(string path)
{
- return bundleContainer.FindBundleContainingPath<T>(path);
+ return bundleContainer.FindBundlesContainingPath(path).OfType<T>().FirstOrDefault();
}
-
+
protected override IReferenceBuilder GetOrCreateReferenceBuilder(Func<IReferenceBuilder> create)
{
return referenceBuilder;
View
3 src/Cassette.Web/BundleRequestHandler.cs
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.IO.Compression;
+using System.Linq;
using System.Web;
using System.Web.Routing;
using Cassette.Utilities;
@@ -66,7 +67,7 @@ Bundle FindBundle()
var path = "~/" + routeData.GetRequiredString("path");
Trace.Source.TraceInformation("Handling bundle request for \"{0}\".", path);
path = RemoveTrailingHashFromPath(path);
- return getBundleContainer().FindBundleContainingPath<T>(path);
+ return getBundleContainer().FindBundlesContainingPath(path).OfType<T>().FirstOrDefault();
}
/// <summary>
View
7 src/Cassette/BundleContainer.cs
@@ -102,10 +102,9 @@ void AddBundlesReferencedBy(Bundle bundle, ISet<Bundle> all)
}
}
- public T FindBundleContainingPath<T>(string path)
- where T : Bundle
+ public IEnumerable<Bundle> FindBundlesContainingPath(string path)
{
- return bundles.OfType<T>().FirstOrDefault(bundle => bundle.ContainsPath(path));
+ return bundles.Where(bundle => bundle.ContainsPath(path));
}
void ValidateBundleReferences()
@@ -153,7 +152,7 @@ select new
|| r.Type == AssetReferenceType.Url)
.Select(r => r.Path)
.Concat(bundle.References)
- .Select(FindBundleContainingPath<Bundle>)
+ .SelectMany(FindBundlesContainingPath)
)
}
).ToDictionary(x => x.bundle, x => x.references);
View
24 src/Cassette/BundleContainerExtensions.cs
@@ -1,19 +1,29 @@
-namespace Cassette
+using System.Linq;
+
+namespace Cassette
{
static class BundleContainerExtensions
{
public static bool TryGetAssetByPath(this IBundleContainer bundleContainer, string path, out IAsset asset, out Bundle bundle)
{
- bundle = bundleContainer.FindBundleContainingPath<Bundle>(path);
- if (bundle == null)
+ var results =
+ from b in bundleContainer.FindBundlesContainingPath(path)
+ let a = b.FindAssetByPath(path)
+ where a != null
+ select new { Bundle = b, Asset = a };
+
+ var result = results.FirstOrDefault();
+ if (result != null)
{
- asset = null;
- return false;
+ asset = result.Asset;
+ bundle = result.Bundle;
+ return true;
}
else
{
- asset = bundle.FindAssetByPath(path);
- return asset != null;
+ asset = null;
+ bundle = null;
+ return false;
}
}
}
View
3 src/Cassette/CassetteApplicationBase.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using System.Linq;
using Cassette.Configuration;
namespace Cassette
@@ -28,7 +29,7 @@ protected internal IBundleContainer BundleContainer
public virtual T FindBundleContainingPath<T>(string path)
where T : Bundle
{
- return bundleContainer.FindBundleContainingPath<T>(path);
+ return bundleContainer.FindBundlesContainingPath(path).OfType<T>().FirstOrDefault();
}
public IReferenceBuilder GetReferenceBuilder()
View
2 src/Cassette/IBundleContainer.cs
@@ -6,7 +6,7 @@ namespace Cassette
interface IBundleContainer : IDisposable
{
IEnumerable<Bundle> Bundles { get; }
- T FindBundleContainingPath<T>(string path) where T : Bundle;
+ IEnumerable<Bundle> FindBundlesContainingPath(string path);
IEnumerable<Bundle> IncludeReferencesAndSortBundles(IEnumerable<Bundle> bundles);
}
}
View
17 src/Cassette/Persistence/BundleCache.cs
@@ -249,13 +249,16 @@ select r.Path
IEnumerable<XElement> CreateReferenceElements(Bundle bundle, IBundleContainer bundleContainer)
{
- var references = (
- from asset in bundle.Assets
- from r in asset.References
- where r.Type == AssetReferenceType.DifferentBundle || r.Type == AssetReferenceType.Url
- select bundleContainer.FindBundleContainingPath<Bundle>(r.Path).Path
- ).Concat(bundle.References)
- .Distinct(StringComparer.OrdinalIgnoreCase);
+ var paths = from asset in bundle.Assets
+ from r in asset.References
+ where r.Type == AssetReferenceType.DifferentBundle || r.Type == AssetReferenceType.Url
+ select r.Path;
+
+ var references = paths
+ .SelectMany(bundleContainer.FindBundlesContainingPath)
+ .Select(b => b.Path)
+ .Concat(bundle.References)
+ .Distinct(StringComparer.OrdinalIgnoreCase);
return from reference in references
select new XElement("Reference", new XAttribute("Path", reference));
View
45 src/Cassette/ReferenceBuilder.cs
@@ -26,13 +26,13 @@ public ReferenceBuilder(IBundleContainer bundleContainer, IDictionary<Type, IBun
public void Reference<T>(string path, string location = null)
where T : Bundle
{
- var bundle = GetBundle(path, () => bundleFactories[typeof(T)].CreateExternalBundle(path));
- Reference(bundle, location);
+ var bundles = GetBundles(path, () => bundleFactories[typeof(T)].CreateExternalBundle(path));
+ Reference(bundles, location);
}
public void Reference(string path, string location = null)
{
- var bundle = GetBundle(path, () =>
+ var bundles = GetBundles(path, () =>
{
if (path.EndsWith(".js", StringComparison.OrdinalIgnoreCase))
{
@@ -53,45 +53,54 @@ public void Reference(string path, string location = null)
}
});
- Reference(bundle, location);
+ Reference(bundles, location);
}
- Bundle GetBundle(string path, Func<Bundle> createExternalBundle)
+ IEnumerable<Bundle> GetBundles(string path, Func<Bundle> createExternalBundle)
{
path = PathUtilities.AppRelative(path);
- var bundle = bundleContainer.FindBundleContainingPath<Bundle>(path);
- if (bundle == null && path.IsUrl())
+ var bundles = bundleContainer.FindBundlesContainingPath(path).ToArray();
+ if (bundles.Length == 0 && path.IsUrl())
{
- bundle = createExternalBundle();
+ var bundle = createExternalBundle();
bundle.Process(settings);
+ bundles = new[] { bundle };
}
- if (bundle == null)
+ if (bundles.Length == 0)
{
throw new ArgumentException("Cannot find an asset bundle containing the path \"" + path + "\".");
}
- return bundle;
+ return bundles;
}
public void Reference(Bundle bundle, string location = null)
{
+ Reference(new[] { bundle }, location);
+ }
+
+ void Reference(IEnumerable<Bundle> bundles, string location = null)
+ {
if (!settings.IsHtmlRewritingEnabled && HasRenderedLocation(location))
{
ThrowRewritingRequiredException(location);
}
- // Bundle can define it's own prefered location. Use this when we aren't given
- // an explicit location argument i.e. null.
- if (location == null)
+ foreach (var bundle in bundles)
{
- location = bundle.PageLocation;
- }
+ // Bundle can define it's own prefered location. Use this when we aren't given
+ // an explicit location argument i.e. null.
+ if (location == null)
+ {
+ location = bundle.PageLocation;
+ }
- var bundles = GetOrCreateBundleSet(location);
- if (bundles.Contains(bundle)) return;
- bundles.Add(bundle);
+ var bundlesForLocation = GetOrCreateBundleSet(location);
+ if (bundlesForLocation.Contains(bundle)) return;
+ bundlesForLocation.Add(bundle);
+ }
}
bool HasRenderedLocation(string location)

0 comments on commit 137493a

Please sign in to comment.
Something went wrong with that request. Please try again.