public void AddDefinitionIfFound_AddsDefinitionWithNoVerSupportIfFileFound()
        {
            // Arrange
            var map = new ScriptResourceMapping();
            var sd  = new ScriptResourceDefinition
            {
                Path = "~/Scripts/"
            };
            var vpp  = Substitute.For <VirtualPathProvider>();
            var vd   = Substitute.For <VirtualDirectory>("~/Scripts/");
            var file = Substitute.For <VirtualFile>("foo.js");

            file.Name.Returns("foo.js");
            vd.Files.Returns(new List <VirtualFile> {
                file
            });
            vpp.GetDirectory("~/Scripts/").Returns(vd);
            ScriptResourceMappingExtensions.VirtualPathProvider = vpp;

            // Act
            map.AddDefinitionIfFound("foo", typeof(Page).Assembly, sd, @"^foo.js$");

            // Assert
            Assert.IsNotNull(map.GetDefinition("foo", typeof(Page).Assembly));
        }
        public void AddDefinitionIfFound_AddsDefinitionWithVerSupportIfFileFound()
        {
            // Arrange
            var map = new ScriptResourceMapping();
            var sd  = new ScriptResourceDefinition
            {
                Path         = "~/Scripts/",
                DebugPath    = "~/Scripts/",
                CdnPath      = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-{0}.min.js",
                CdnDebugPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-{0}.js",
                CdnSupportsSecureConnection = true
            };
            var vpp  = Substitute.For <VirtualPathProvider>();
            var vd   = Substitute.For <VirtualDirectory>("~/Scripts/");
            var file = Substitute.For <VirtualFile>("jquery-1.6.2.js");

            file.Name.Returns("jquery-1.6.2.js");
            vd.Files.Returns(new List <VirtualFile> {
                file
            });
            vpp.GetDirectory("~/Scripts/").Returns(vd);
            ScriptResourceMappingExtensions.VirtualPathProvider = vpp;

            // Act
            map.AddDefinitionIfFound("jquery", typeof(Page).Assembly, sd,
                                     @"^jquery-" + ScriptResourceMappingExtensions.VerRegexPattern + @"(?:\.min){0,1}\.js$");

            // Assert
            Assert.IsNotNull(map.GetDefinition("jquery", typeof(Page).Assembly));
        }
Example #3
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
                });
            }
        }
 /// <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="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.
 /// </param>
 public static void AddDefinitionIfFound(this ScriptResourceMapping map, string name, ScriptResourceDefinition definition, string fileNamePattern)
 {
     AddDefinitionIfFound(map, name, typeof(ScriptManager).Assembly, definition, fileNamePattern);
 }