public static IHtmlString Render(string bundleName) { var orchestrator = new BundleResolver(); var scripts = orchestrator.BuildScripts(bundleName); return new HtmlString(scripts); }
static void AddBundle(BundleCollection bundles, string bundlePath, string scriptPath) { bool isJavascript = scriptPath.EndsWith(".js"); var resolver = new BundleResolver(bundles); if (bundles.Any(b => b.Path == bundlePath)) { var bundle = bundles.First(b => b.Path == bundlePath); var contents = resolver.GetBundleContents(bundle.Path); if (!contents.Any(s => s == scriptPath)) { //if bundle already exists, include the script path bundle.Include(scriptPath); } } else { //if bundle not exists, create the bundle and include the script path if (isJavascript) { bundles.Add(new ScriptBundle(bundlePath).Include(scriptPath)); } else { bundles.Add(new StyleBundle(bundlePath).Include(scriptPath, new CssRewriteUrlTransform())); } } }
public void SingleControlInBundleTest() { var resolver = new BundleResolver(_moqCache.Object); var results = resolver.GetControlTypesInBundles(new[] { "SingleBundle" }, TestConfigPath); // Assert all controls in SingleBundle group AssertResults(results, new[] { "TextBoxWatermarkExtender" }); }
public void EnsureNonVirtualPathsDoNotThrowTest() { BundleCollection col = new BundleCollection(); BundleResolver resolver = new BundleResolver(col); Assert.IsFalse(resolver.IsBundleVirtualPath("missingTilde")); Assert.IsNull(resolver.GetBundleContents("missingTilde")); Assert.IsNull(resolver.GetBundleUrl("missingTilde")); }
public void ValidBundleUrlTest() { BundleCollection col = new BundleCollection(); col.Add(new Bundle("~/js")); BundleResolver resolver = new BundleResolver(col); Assert.IsTrue(resolver.IsBundleVirtualPath("~/js")); }
public void Caching_SetMethodIsCalledIfCacheIsEmpty() { _moqCache.Setup(c => c.Get <string>(CacheConfigName)).Returns(""); var resolver = new BundleResolver(_moqCache.Object); resolver.GetControlTypesInBundles(new[] { "SingleBundle" }, TestConfigPath); _moqCache.Verify(c => c.Set(CacheConfigName, It.IsAny <string>(), It.IsAny <string>()), Times.Once()); }
public void Caching_SetMethodIsNotCalledIfCacheIsFull() { _moqCache.Setup(c => c.Get <string>(CacheConfigName)).Returns(File.ReadAllText(TestConfigPath)); var resolver = new BundleResolver(_moqCache.Object); resolver.GetControlTypesInBundles(null, TestConfigPath); _moqCache.Verify(c => c.Set(CacheConfigName, It.IsAny <string>(), It.IsAny <string>()), Times.Never()); }
public void EnsureEmptyVirtualPathsDoNotThrowTest() { BundleCollection col = new BundleCollection(); BundleResolver resolver = new BundleResolver(col); Assert.IsFalse(resolver.IsBundleVirtualPath(String.Empty)); Assert.IsNull(resolver.GetBundleContents(String.Empty)); Assert.IsNull(resolver.GetBundleUrl(String.Empty)); }
public void NonBundleValidUrlTest() { BundleCollection col = new BundleCollection(); col.Add(new Bundle("~/js")); BundleResolver resolver = new BundleResolver(col); Assert.IsFalse(resolver.IsBundleVirtualPath("~/nope")); Assert.IsNull(resolver.GetBundleContents("~/nope")); Assert.IsNull(resolver.GetBundleUrl("~/nope")); }
public void BuildScriptsShouldGetDebugScripts() { var manager = new Mock<IAppManager>(); var markupBuilder = new Mock<IMarkupBuilder>(); manager.Setup(x => x.IsDebugging).Returns(true); var resolver = new BundleResolver(manager.Object, markupBuilder.Object); resolver.BuildScripts(""); markupBuilder.Verify(x => x.GetScriptsForDebugging(""), Times.Once); }
private BundleCollection SetupBundleCollection(bool optimizationEnabled) { AssetManager manager = SetupAssetManager(); BundleCollection col = new BundleCollection(); manager.Bundles = col; BundleResolver resolver = new BundleResolver(col, manager.Context); manager.Resolver = resolver; manager.OptimizationEnabled = optimizationEnabled; return(col); }
public void MultipleControlsInBundleTest() { var resolver = new BundleResolver(_moqCache.Object); var results = resolver.GetControlTypesInBundles(new[] { "MultiBundle" }, TestConfigPath); // Assert all controls in MultiBundle group AssertResults(results, new[] { "TextBoxWatermarkExtender", "RoundedCornersExtender", "DropShadowExtender" }); }
public static IHtmlString Render(string path) { if (BundleTable.EnableOptimizations) { return(Styles.Render(path)); } var resolver = new BundleResolver(BundleTable.Bundles); var url = resolver.GetBundleUrl(path); var styles = string.Format("<link href=\"{0}\" rel=\"Stylesheet\"/>", url); return(new MvcHtmlString(styles)); }
private static IHtmlString GenerateVersion(string html, string[] paths) { BundleResolver bundle = new BundleResolver(BundleTable.Bundles); System.Text.StringBuilder sb = new System.Text.StringBuilder(); var context = new HttpContextWrapper(HttpContext.Current); foreach (var p in paths) { var list = new List <string>(); string parm = "", path = p; if (p.Contains("?")) { parm = p.Substring(p.LastIndexOf("?")); path = p.Replace(parm, ""); } if (BundleTable.EnableOptimizations) { var b = bundle.GetBundleUrl(path);//取压缩 if (b == null) { b = path; } else if (!string.IsNullOrWhiteSpace(parm)) { parm = parm.Replace("?", "&"); } list.Add(b); } else { var bs = bundle.GetBundleContents(path);//取原路径 if (bs == null) { list.Add(path); } else { list.AddRange(bs); } } foreach (var bc in list) { var u = UrlHelper.GenerateContentUrl(bc, context); sb.AppendFormat(html, u + parm); sb.Append(Environment.NewLine); } } return(new HtmlString(sb.ToString())); }
public void DynamicBundleGetBundleContentsTest() { BundleCollection bundles = new BundleCollection(); bundles.Add(new DynamicFolderBundle("js", "*.js", new JsMinify())); BundleTable.VirtualPathProvider = new FileVirtualPathProvider(TestContext.DeploymentDirectory); BundleResolver resolver = new BundleResolver(bundles, new Moq.Mock <HttpContextBase>().Object); string output = ""; foreach (var s in resolver.GetBundleContents("~/scripts/js")) { output += s + "|"; } Assert.AreEqual("~/scripts/first.js|~/scripts/second.js|", output); }
public static BundleCollection JoinScriptBundle(this BundleCollection bundleCollection, string virtualPath, params string[] bundlesToJoin) { var bundleResolver = new BundleResolver(bundleCollection); Bundle allScriptBundle = new ScriptBundle(virtualPath); foreach (var bundle in bundlesToJoin) { foreach (var scriptPath in bundleResolver.GetBundleContents(bundle)) { allScriptBundle = allScriptBundle.Include(scriptPath); } } bundleCollection.Add(allScriptBundle); return(bundleCollection); }
public void WithoutConfigShouldReturnsAllActControlsTest() { var resolver = new BundleResolver(_moqCache.Object); var results = resolver.GetControlTypesInBundles(null, "nonexistantfile"); var bundleTypes = new List <Type>(); foreach (var bundleControl in ControlDependencyMap.Maps.Values) { bundleTypes.AddRange(bundleControl.Dependecies); } Assert.AreEqual(results.Count, bundleTypes.Distinct().Count()); foreach (var type in bundleTypes) { Assert.IsTrue(results.Contains(type), "Can't resolve {0}", type); } }
public void ScriptBundleGetBundleContextTest() { try { BundleCollection bundles = new BundleCollection(); bundles.Add(new ScriptBundle("~/js").Include("~/scripts/first.js", "~/scripts/second.js")); BundleTable.VirtualPathProvider = new FileVirtualPathProvider(TestContext.DeploymentDirectory); BundleResolver resolver = new BundleResolver(bundles, new Moq.Mock <HttpContextBase>().Object); string output = ""; foreach (var s in resolver.GetBundleContents("~/js")) { output += s + "|"; } Assert.AreEqual("~/scripts/first.js|~/scripts/second.js|", output); } finally { BundleTable.VirtualPathProvider = null; } }
public void UseBundle(ComposableBundle <T> bundle) { // ReSharper disable once UseObjectOrCollectionInitializer var collection = new BundleCollection(); collection.Add(_bundle); var resolver = new BundleResolver(collection); var content = resolver.GetBundleContents(_bundle.Path)?.ToList(); RemoveScripts(content); foreach (var virtualPath in bundle.VirtualPaths) { // Stop the script from being added twice if (content != null && content.Contains(virtualPath)) { continue; } _bundle.Include(virtualPath); } }
public void RenderWithOptimizationsWillIgnoreCdnPathFromBundleIfNotSpecifiedTest() { AssetManager manager = SetupAssetManager(); BundleCollection col = new BundleCollection(); manager.Bundles = col; BundleResolver resolver = new BundleResolver(col, manager.Context); manager.Resolver = resolver; manager.OptimizationEnabled = true; col.Add(new ScriptBundle("~/bundle1", "http://cdnPath1")); col.Add(new ScriptBundle("~/bundle2")); col.Add(new ScriptBundle("~/bundle3", "http://cdnPath3")); col.UseCdn = false; string expectedTags = @"<link href=""bundle1?v="" rel=""stylesheet""/> <link href=""bundle2?v="" rel=""stylesheet""/> <link href=""bundle3?v="" rel=""stylesheet""/> "; Assert.AreEqual(expectedTags, Styles.Render("~/bundle1", "~/bundle2", "~/bundle3").ToHtmlString()); }
public void CustomBundleWithoutConfigFileShouldErrorTest() { var resolver = new BundleResolver(_moqCache.Object); resolver.GetControlTypesInBundles(new[] { "Accordion" }, "nonexistantfile"); }
private static bool IsFilePathExist(Bundle bundle, string virtualFilePath) { var resolver = new BundleResolver(BUNDLE_COLLECTION); var bundleFilesPath = (List<string>)resolver.GetBundleContents(bundle.Path); return bundleFilesPath.Contains(virtualFilePath); }
public void CustomBundleWithoutConfigFileShouldErrorTest() { var resolver = new BundleResolver(_moqCache.Object); Assert.Throws <Exception>(() => resolver.GetControlTypesInBundles(new[] { "Accordion" }, "nonexistantfile"), "AjaxControlToolkit.config file is not defined"); }