예제 #1
0
 private static void RegisterScripts(ScriptResourceMapping scriptResourceMapping)
 {
     scriptResourceMapping.AddDefinition("jquery", new ScriptResourceDefinition
     {
         Path         = "~/Scripts/jquery-2.1.1.min.js",
         DebugPath    = "~/Scripts/jquery-2.1.1.js",
         CdnPath      = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.1.1.min.js",
         CdnDebugPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.1.1.js",
         CdnSupportsSecureConnection = true,
         LoadSuccessExpression       = "window.jQuery"
     });
 }
        /// <summary>
        ///    Adds a System.Web.UI.ScriptResourceDefinition object to the System.Web.UI.ScriptResourceMapping
        ///    object, but only if a file matching the supplied regex pattern is found. If the pattern includes
        ///    a capture group called "ver", it will be used to determine the highest version found.
        /// </summary>
        /// <param name="map">The ScriptResourceMapping object.</param>
        /// <param name="name">The name of the script resource.</param>
        /// <param name="assembly">A System.Reflection.Assembly object that is used along with the name as the script resource key.</param>
        /// <param name="definition">
        ///     A System.Web.UI.ScriptResourceDefinition object that specifies location support
        ///     for script resources. The Path and optionally DebugPath properties should contain
        ///     the path to the virtual folder containing the script to find, e.g. ~/Scripts/
        /// </param>
        /// <param name="fileNamePattern">
        ///     The regex pattern to match files against. If the pattern includes
        ///     a capture group called "ver", it will be used to determine the highest version found.
        ///     Use ScriptResourceMappingExtensions.VerRegexPattern for a pattern matching standard
        ///     versioning strings, e.g. 1, 1.0, 1.1.2, etc.
        /// </param>
        public static void AddDefinitionIfFound(this ScriptResourceMapping map, string name, Assembly assembly, ScriptResourceDefinition definition, string fileNamePattern)
        {
            var scriptsFolder = VirtualPathProvider.GetDirectory(definition.Path);

            if (scriptsFolder == null)
            {
                return;
            }

            var scripts = scriptsFolder.Files.Cast <VirtualFile>();

            var fileNamePatternRegex = new Regex(fileNamePattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);

            var supportVer = fileNamePatternRegex.GroupNumberFromName("ver") >= 0;

            var foundFile = (from file in scripts
                             let match = fileNamePatternRegex.Match(file.Name)
                                         where match.Success
                                         let fileVer = supportVer ? GetVersion(match) : null
                                                       orderby fileVer descending
                                                       select new
            {
                file.Name,
                Version = fileVer
            }).FirstOrDefault();

            if (foundFile != null)
            {
                var ver = foundFile.Version != null
                    ? Regex.Match(foundFile.Name, VerRegexPattern, RegexOptions.IgnoreCase).Groups["ver"].Value
                    : "";

                var isMin = foundFile.Name.EndsWith(".min.js", StringComparison.OrdinalIgnoreCase);

                var fileNamePrefix = foundFile.Name.Substring(0,
                                                              foundFile.Name.IndexOf(ver + (isMin ? ".min.js" : ".js")));

                var altFile = scripts.SingleOrDefault(f => f.Name.Equals(
                                                          String.Format(isMin ? "{0}{1}.js" : "{0}{1}.min.js", fileNamePrefix, ver),
                                                          StringComparison.OrdinalIgnoreCase));

                var hasBoth = altFile != null;

                var minFileName = isMin
                    ? foundFile.Name
                    : hasBoth
                        ? altFile.Name
                        : null;

                var nonMinFileName = !isMin
                    ? foundFile.Name
                    : hasBoth
                        ? altFile.Name
                        : null;

                var minOnly = isMin && !hasBoth;

                var path = minOnly || hasBoth
                    ? minFileName
                    : nonMinFileName;

                var debugPath = hasBoth
                    ? nonMinFileName
                    : null;

                path = path == null ? null : definition.Path + path;

                debugPath = debugPath == null ? null : (definition.DebugPath ?? definition.Path) + debugPath;

                var cdnPath = definition.CdnPath != null
                    ? ver != null
                        ? String.Format(definition.CdnPath, ver)
                        : definition.CdnPath
                    : null;

                var cdnDebugPath = definition.CdnDebugPath != null
                    ? ver != null
                        ? String.Format(definition.CdnDebugPath, ver)
                        : definition.CdnDebugPath
                    : null;

                map.AddDefinition(name, assembly, new ScriptResourceDefinition
                {
                    Path         = path,
                    DebugPath    = debugPath,
                    CdnPath      = cdnPath,
                    CdnDebugPath = cdnDebugPath,
                    CdnSupportsSecureConnection = definition.CdnSupportsSecureConnection
                });
            }
        }