/// <summary> /// Remove and get framework group. /// </summary> /// <param name="itemGroup">The web asset to remove.</param> /// <returns>The web asset item group</returns> private static WebAssetItemGroup RemoveAndGetFrameworkGroup(WebAssetItemGroup itemGroup) { WebAssetItemGroup frameworkGroup = new WebAssetItemGroup("framework", false) { Combined = itemGroup.Combined, Compress = itemGroup.Compress, CacheDurationInDays = itemGroup.CacheDurationInDays, DefaultPath = itemGroup.DefaultPath, Version = itemGroup.Version }; for (int i = itemGroup.Items.Count - 1; i >= 0; i--) { WebAssetItem item = itemGroup.Items[i]; string fileName = Path.GetFileName(item.Source); if ((!fileName.Equals(ScriptRegistrar.jQuery, StringComparison.OrdinalIgnoreCase)) && (ScriptRegistrar.FrameworkScriptFileNames.Contains(fileName, StringComparer.OrdinalIgnoreCase))) { frameworkGroup.Items.Add(new WebAssetItem(item.Source)); itemGroup.Items.RemoveAt(i); } } frameworkGroup.Items.Reverse(); return(frameworkGroup); }
/// <summary> /// /// </summary> /// <param name="contentType"></param> /// <param name="assetGroup"></param> /// <returns></returns> private static MergedAsset CreateMergedAssetWith(string contentType, WebAssetItemGroup assetGroup) { Func <string, string> getDirectory = path => path.Substring(2, path.LastIndexOf("/", StringComparison.Ordinal) - 2); Func <string, string> getFile = path => path.Substring(path.LastIndexOf("/", StringComparison.Ordinal) + 1); MergedAsset asset = new MergedAsset { ContentType = contentType, Version = assetGroup.Version, Compress = assetGroup.Compress, CacheDurationInDays = assetGroup.CacheDurationInDays }; IEnumerable <string> directories = assetGroup.Items.Select(item => getDirectory(item.Source)).Distinct(StringComparer.OrdinalIgnoreCase); directories.Each(directory => asset.Directories.Add(new MergedAssetDirectory { Path = directory })); for (int i = 0; i < assetGroup.Items.Count; i++) { string item = assetGroup.Items[i].Source; string directory = getDirectory(item); string file = getFile(item); MergedAssetDirectory assetDirectory = asset.Directories.Single(d => d.Path.IsCaseInsensitiveEqual(directory)); assetDirectory.Files.Add(new MergedAssetFile { Order = i, Name = file }); } return(asset); }
/// <summary> /// Stores the specified asset group. /// </summary> /// <param name="contentType">Type of the content.</param> /// <param name="assetGroup">The asset group.</param> /// <returns></returns> public string Store(string contentType, WebAssetItemGroup assetGroup) { MergedAsset mergedAsset = CreateMergedAssetWith(contentType, assetGroup); string id = assetGroup.IsShared ? assetGroup.Name : CreateIdFrom(mergedAsset); EnsureAsset(mergedAsset, id); return(id); }
/// <summary> /// Inserts the specified source as <see cref="WebAssetItem"/> at the specified index in the specified <see cref="WebAssetItemGroup"/>. /// </summary> /// <param name="index">The index.</param> /// <param name="groupName">Name of the group.</param> /// <param name="itemSource">The item source.</param> public virtual void Insert(int index, string groupName, string itemSource) { WebAssetItemGroup itemGroup = FindGroupByName(groupName); if (itemGroup == null) { itemGroup = CreateGroup(groupName); Insert(index, itemGroup); } itemGroup.Items.Add(CreateItem(itemSource)); }
/// <summary> /// /// </summary> /// <param name="id"></param> /// <returns></returns> private static MergedAsset CreateMergedAssetFromConfiguration(string id) { WebAssetItemGroup assetGroup = SharedGroup.FindScriptGroup(id); string contentType = "application/x-javascript"; if (assetGroup == null) { assetGroup = SharedGroup.FindStyleSheetGroup(id); contentType = "text/css"; } return((assetGroup != null) ? CreateMergedAssetWith(contentType, assetGroup) : null); }
/// <summary> /// Does the web asset already exist. /// </summary> /// <param name="item">The item to test.</param> /// <returns>True if it exists else false.</returns> private bool AlreadyExists(IWebAssetItem item) { WebAssetItem assetItem = item as WebAssetItem; WebAssetItemGroup assetItemGroup = item as WebAssetItemGroup; if (assetItem != null) { return(AssetItems.Any(i => i != item && i.Source.IsCaseInsensitiveEqual(assetItem.Source))); } if (assetItemGroup != null) { return(AssetGroups.Any(i => i != item && i.Name.IsCaseInsensitiveEqual(assetItemGroup.Name))); } return(false); }
/// <summary> /// Remove and get native files. /// </summary> /// <param name="itemGroup">The native web asset to remove</param> /// <returns>The new list of paths.</returns> private static IList <string> RemoveAndGetNativeFiles(WebAssetItemGroup itemGroup) { List <string> nativeFiles = new List <string>(); for (int i = itemGroup.Items.Count - 1; i >= 0; i--) { WebAssetItem item = itemGroup.Items[i]; if (IsNativeFile(item)) { nativeFiles.Add(Path.GetFileName(item.Source)); itemGroup.Items.RemoveAt(i); } } nativeFiles.Reverse(); return(nativeFiles); }
/// <summary> /// Executes the provided delegate that is used to configure the group fluently. /// </summary> /// <param name="name">The name.</param> /// <param name="configureAction">The configure action.</param> public virtual WebAssetItemCollectionBuilder GetGroup(string name, Action <WebAssetItemGroupBuilder> configureAction) { WebAssetItemGroup itemGroup = _assets.FindGroupByName(name); if (itemGroup == null) { throw new ArgumentException("Group with specified name does not exist please make sure you have specified a correct name", "name"); } if (itemGroup.IsShared) { throw new InvalidOperationException("You cannot configure a shared web asset group"); } WebAssetItemGroupBuilder builder = new WebAssetItemGroupBuilder(itemGroup); configureAction(builder); return(this); }
/// <summary> /// Adds the group. /// </summary> /// <param name="name">The name.</param> /// <param name="configureAction">The configure action.</param> /// <returns>The shared asset builder.</returns> public virtual SharedGroupBuilder AddGroup(string name, Action <WebAssetItemGroupBuilder> configureAction) { WebAssetItemGroup group; if (_assets.TryGetValue(name, out group)) { throw new ArgumentException("Group with specified name already exists please specify a different name", "name"); } group = new WebAssetItemGroup(name, true) { DefaultPath = _defaultPath }; _assets.Add(name, group); WebAssetItemGroupBuilder builder = new WebAssetItemGroupBuilder(group); configureAction(builder); return(this); }
/// <summary> /// Adds a new web asset group. /// </summary> /// <param name="name">The name.</param> /// <param name="configureAction">The configure action.</param> /// <example> /// <code lang="CS"> /// <%= Html.NequeoUI().ScriptRegistrar() /// .Scripts(scripts => scripts.AddGroup("Group1", group => /// { /// group.Add("script1.js"); /// } /// )) /// %> /// </code> /// </example> public virtual WebAssetItemCollectionBuilder AddGroup(string name, Action <WebAssetItemGroupBuilder> configureAction) { WebAssetItemGroup itemGroup = _assets.FindGroupByName(name); if (itemGroup != null) { throw new ArgumentException("Group with specified name already exists please specify a different name", "name"); } itemGroup = new WebAssetItemGroup(name) { DefaultPath = _assets.DefaultPath }; _assets.Add(itemGroup); WebAssetItemGroupBuilder builder = new WebAssetItemGroupBuilder(itemGroup); configureAction(builder); return(this); }
/// <summary> /// Adds the specified shared group. /// </summary> /// <param name="name">The name.</param> /// <example> /// <code lang="CS"> /// <%= Html.NequeoUI().ScriptRegistrar() /// .Scripts(scripts => scripts.AddShareGroup("SharedGroup1")) /// %> /// </code> /// </example> public virtual WebAssetItemCollectionBuilder AddSharedGroup(string name) { WebAssetItemGroup group = (_assetType == WebAssetType.StyleSheet) ? SharedGroup.FindStyleSheetGroup(name) : SharedGroup.FindScriptGroup(name); if (group == null) { throw new ArgumentException("GroupWithSpecifiedNameDoesNotExistInAssetTypeOfSharedWebAssets", "name"); } if (_assets.FindGroupByName(name) == null) { // People might have the same group reference in multiple place. // So we will skip it once it is added. // throw new ArgumentException(TextResource.LocalGroupWithSpecifiedNameAlreadyExists.FormatWith(name)); // Add a copy of the shared asset WebAssetItemGroup localGroup = new WebAssetItemGroup(group.Name, true) { DefaultPath = group.DefaultPath, Version = group.Version, Compress = group.Compress, CacheDurationInDays = group.CacheDurationInDays, Combined = group.Combined }; foreach (WebAssetItem item in group.Items) { localGroup.Items.Add(new WebAssetItem(item.Source)); } _assets.Add(localGroup); } return(this); }
/// <summary> /// Default constructor. /// </summary> /// <param name="assetItemGroup">The current web asset item group to add the item to.</param> public WebAssetItemGroupBuilder(WebAssetItemGroup assetItemGroup) { _assetItemGroup = assetItemGroup; }
/// <summary> /// Merges the specified assets. /// </summary> /// <param name="contentType">Type of the content.</param> /// <param name="assetHandlerPath">The asset handler path.</param> /// <param name="isSecured">if set to <c>true</c> [is secure].</param> /// <param name="canCompress">if set to <c>true</c> [can compress].</param> /// <param name="assets">The assets.</param> /// <returns>The collection of web asset paths.</returns> public IList <string> Merge(string contentType, string assetHandlerPath, bool isSecured, bool canCompress, WebAssetItemCollection assets) { // If the instance object is null. if (contentType == null) { throw new System.ArgumentNullException("contentType"); } if (assetHandlerPath == null) { throw new System.ArgumentNullException("assetHandlerPath"); } if (assets == null) { throw new System.ArgumentNullException("assets"); } IList <string> mergedList = new List <string>(); Func <string, string, string> getRelativePath = (source, version) => _urlResolver.Resolve(_assetRegistry.Locate(source, version)); Action <WebAssetItemGroup> processGroup = group => { if (group.Combined) { string id = _assetRegistry.Store(contentType, group); string virtualPath = "{0}?{1}={2}".FormatWith(assetHandlerPath, _urlEncoder.Encode(WebAssetHttpHandler.IdParameterName), _urlEncoder.Encode(id)); string relativePath = _urlResolver.Resolve(virtualPath); if (!mergedList.Contains(relativePath, StringComparer.OrdinalIgnoreCase)) { mergedList.Add(relativePath); } } else { group.Items.Each(i => { if (!mergedList.Contains(i.Source, StringComparer.OrdinalIgnoreCase)) { mergedList.Add(getRelativePath(i.Source, group.Version)); } }); } }; if (!assets.IsEmpty()) { foreach (IWebAssetItem asset in assets) { WebAssetItem item = asset as WebAssetItem; WebAssetItemGroup itemGroup = asset as WebAssetItemGroup; if (item != null) { mergedList.Add(getRelativePath(item.Source, null)); } else if (itemGroup != null) { WebAssetItemGroup frameworkGroup = null; if ((frameworkGroup != null) && !frameworkGroup.Items.IsEmpty()) { processGroup(frameworkGroup); } if (!itemGroup.Items.IsEmpty()) { processGroup(itemGroup); } } } } // Return the list. return(mergedList.ToList()); }