Esempio n. 1
0
        protected virtual void ExpandDependencies(ResourceDefinition resource, RequireSettings settings, OrderedDictionary allResources)
        {
            if (resource == null)
            {
                return;
            }
            // Settings is given so they can cascade down into dependencies. For example, if Foo depends on Bar, and Foo's required
            // location is Head, so too should Bar's location.
            // forge the effective require settings for this resource
            // (1) If a require exists for the resource, combine with it. Last settings in gets preference for its specified values.
            // (2) If no require already exists, form a new settings object based on the given one but with its own type/name.
            settings = allResources.Contains(resource)
                ? ((RequireSettings)allResources[resource]).Combine(settings)
                : new RequireSettings {
                Type = resource.Type, Name = resource.Name
            }.Combine(settings);
            if (resource.Dependencies != null)
            {
                var dependencies =
                    from d in resource.Dependencies
                    select FindResource(new RequireSettings { Type = resource.Type, Name = d });

                foreach (var dependency in dependencies)
                {
                    if (dependency == null)
                    {
                        continue;
                    }
                    ExpandDependencies(dependency, settings, allResources);
                }
            }
            allResources[resource] = settings;
        }
Esempio n. 2
0
        public string ResolveUrl(RequireSettings settings, string applicationPath)
        {
            string url;

            if (_urlResolveCache.TryGetValue(settings, out url))
            {
                return(url);
            }

            if (String.IsNullOrEmpty(url))
            {
                return(null);
            }

            if (!Uri.IsWellFormedUriString(url, UriKind.Absolute) && !VirtualPathUtility.IsAbsolute(url) && !VirtualPathUtility.IsAppRelative(url) && !String.IsNullOrEmpty(BasePath))
            {
                // relative urls are relative to the base path of the module that defined the manifest
                url = VirtualPathUtility.Combine(BasePath, url);
            }
            if (VirtualPathUtility.IsAppRelative(url))
            {
                url = VirtualPathUtility.ToAbsolute(url, applicationPath);
            }
            _urlResolveCache[settings] = url;
            return(url);
        }
Esempio n. 3
0
        private ResourceDefinition FindResource(RequireSettings settings, bool resolveInlineDefinitions)
        {
            // find the resource with the given type and name
            // that has at least the given version number. If multiple,
            // return the resource with the greatest version number.
            // If not found and an inlineDefinition is given, define the resource on the fly
            // using the action.
            var name     = settings.Name ?? "";
            var type     = settings.Type;
            var resource = (from p in ResourceProviders
                            from r in p.GetResources(type)
                            where name.Equals(r.Key, StringComparison.OrdinalIgnoreCase)
                            orderby r.Value.Version descending
                            select r.Value).FirstOrDefault();

            if (resource == null && _dynamicManifest != null)
            {
                resource = (from r in _dynamicManifest.GetResources(type)
                            where name.Equals(r.Key, StringComparison.OrdinalIgnoreCase)
                            orderby r.Value.Version descending
                            select r.Value).FirstOrDefault();
            }
            if (resolveInlineDefinitions && resource == null)
            {
                // Does not seem to exist, but it's possible it is being
                // defined by a Define() from a RequireSettings somewhere.
                if (ResolveInlineDefinitions(settings.Type))
                {
                    // if any were defined, now try to find it
                    resource = FindResource(settings, false);
                }
            }
            return(resource);
        }
Esempio n. 4
0
        public string ResolveUrl(RequireSettings settings, string applicationPath, bool ssl)
        {
            string url;

            if (_urlResolveCache.TryGetValue(settings, out url))
            {
                return(url);
            }
            // Url priority:
            if (!ssl || (ssl && CdnSupportsSsl))   //Not ssl or ssl and cdn supports it
            {
                if (settings.DebugMode)
                {
                    url = settings.CdnMode
                        ? Coalesce(UrlCdnDebug, UrlDebug, UrlCdn, Url)
                        : Coalesce(UrlDebug, Url, UrlCdnDebug, UrlCdn);
                }
                else
                {
                    url = settings.CdnMode
                        ? Coalesce(UrlCdn, Url, UrlCdnDebug, UrlDebug)
                        : Coalesce(Url, UrlDebug, UrlCdn, UrlCdnDebug);
                }
            }
            else   //ssl and cdn does not support it, only evaluate non-cdn url's
            {
                url = settings.DebugMode
                    ? Coalesce(UrlDebug, Url)
                    : Coalesce(Url, UrlDebug);
            }
            if (String.IsNullOrEmpty(url))
            {
                return(null);
            }
            if (!String.IsNullOrEmpty(settings.Culture))
            {
                string nearestCulture = FindNearestCulture(settings.Culture);
                if (!String.IsNullOrEmpty(nearestCulture))
                {
                    url = Path.ChangeExtension(url, nearestCulture + Path.GetExtension(url));
                }
            }
            if (!Uri.IsWellFormedUriString(url, UriKind.Absolute) && !VirtualPathUtility.IsAbsolute(url) && !VirtualPathUtility.IsAppRelative(url) && !String.IsNullOrEmpty(BasePath))
            {
                // relative urls are relative to the base path of the module that defined the manifest
                url = VirtualPathUtility.Combine(BasePath, url);
            }
            if (VirtualPathUtility.IsAppRelative(url))
            {
                url = applicationPath != null
                    ? VirtualPathUtility.ToAbsolute(url, applicationPath)
                    : VirtualPathUtility.ToAbsolute(url);
            }
            _urlResolveCache[settings] = url;
            return(url);
        }
 public TagBuilder GetTagBuilder(RequireSettings baseSettings, string appPath) {
     var tagBuilder = new TagBuilder(Resource.TagName);
     tagBuilder.MergeAttributes(Resource.TagBuilder.Attributes);
     if (!String.IsNullOrEmpty(Resource.FilePathAttributeName)) {
         var resolvedUrl = GetResourceUrl(baseSettings, appPath);
         if (!String.IsNullOrEmpty(resolvedUrl)) {
             tagBuilder.MergeAttribute(Resource.FilePathAttributeName, resolvedUrl, true);
         }
     }
     return tagBuilder;
 }
Esempio n. 6
0
        public RequireSettings Combine(RequireSettings other)
        {
            var settings = (new RequireSettings
            {
                Name = Name,
                Type = Type
            }).AtLocation(Location).AtLocation(other.Location)
                           .WithBasePath(BasePath).WithBasePath(other.BasePath)
                           .UseCondition(Condition).UseCondition(other.Condition)
                           .Define(InlineDefinition).Define(other.InlineDefinition);

            settings._attributes = MergeAttributes(other);
            return(settings);
        }
Esempio n. 7
0
        public TagBuilder GetTagBuilder(RequireSettings baseSettings, string appPath)
        {
            var tagBuilder = new TagBuilder(Resource.TagName);

            tagBuilder.MergeAttributes(Resource.TagBuilder.Attributes);
            if (!String.IsNullOrEmpty(Resource.FilePathAttributeName))
            {
                var resolvedUrl = GetResourceUrl(baseSettings, appPath);
                if (!String.IsNullOrEmpty(resolvedUrl))
                {
                    tagBuilder.MergeAttribute(Resource.FilePathAttributeName, resolvedUrl, true);
                }
            }
            return(tagBuilder);
        }
        public RequireSettings Combine(RequireSettings other)
        {
            var settings = (new RequireSettings {
                Name = Name,
                Type = Type
            }).AtLocation(Location).AtLocation(other.Location)
                           .WithBasePath(BasePath).WithBasePath(other.BasePath)
                           .UseCdn(CdnMode).UseCdn(other.CdnMode)
                           .UseDebugMode(DebugMode).UseDebugMode(other.DebugMode)
                           .UseFileHash(FileHashMode).UseFileHash(other.FileHashMode)
                           .UseCulture(Culture).UseCulture(other.Culture)
                           .UseCondition(Condition).UseCondition(other.Condition)
                           .Define(InlineDefinition).Define(other.InlineDefinition);

            settings._attributes = MergeAttributes(other);
            return(settings);
        }
        private Dictionary <string, string> MergeAttributes(RequireSettings other)
        {
            // efficiently merge the two dictionaries, taking into account that one or both may not exist
            // and that attributes in 'other' should overridde attributes in this, even if the value is null.
            if (_attributes == null)
            {
                return(other._attributes == null ? null : new Dictionary <string, string>(other._attributes));
            }
            if (other._attributes == null)
            {
                return(new Dictionary <string, string>(_attributes));
            }
            var mergedAttributes = new Dictionary <string, string>(_attributes);

            foreach (var pair in other._attributes)
            {
                mergedAttributes[pair.Key] = pair.Value;
            }
            return(mergedAttributes);
        }
        public virtual RequireSettings Require(string resourceType, string resourceName)
        {
            if (resourceType == null)
            {
                throw new ArgumentNullException("resourceType");
            }
            if (resourceName == null)
            {
                throw new ArgumentNullException("resourceName");
            }
            RequireSettings settings;
            var             key = new Tuple <string, string>(resourceType, resourceName);

            if (!_required.TryGetValue(key, out settings))
            {
                settings = new RequireSettings {
                    Type = resourceType, Name = resourceName
                };
                _required[key] = settings;
            }
            _builtResources[resourceType] = null;
            return(settings);
        }
 public static bool AttributesEqual(this RequireSettings settings, RequireSettings other)
 {
     return settings.StringifyAttributes() == other.StringifyAttributes();
 }
Esempio n. 12
0
 public string GetResourceUrl(RequireSettings baseSettings, string appPath)
 {
     return(Resource.ResolveUrl(baseSettings == null ? Settings : baseSettings.Combine(Settings), appPath));
 }
Esempio n. 13
0
 protected virtual void ExpandDependencies(ResourceDefinition resource, RequireSettings settings, OrderedDictionary allResources) {
     if (resource == null) {
         return;
     }
     // Settings is given so they can cascade down into dependencies. For example, if Foo depends on Bar, and Foo's required
     // location is Head, so too should Bar's location.
     // forge the effective require settings for this resource
     // (1) If a require exists for the resource, combine with it. Last settings in gets preference for its specified values.
     // (2) If no require already exists, form a new settings object based on the given one but with its own type/name.
     settings = allResources.Contains(resource)
         ? ((RequireSettings)allResources[resource]).Combine(settings)
         : new RequireSettings { Type = resource.Type, Name = resource.Name }.Combine(settings);
     if (resource.Dependencies != null) {
         var dependencies = from d in resource.Dependencies
                            select FindResource(new RequireSettings { Type = resource.Type, Name = d });
         foreach (var dependency in dependencies) {
             if (dependency == null) {
                 continue;
             }
             ExpandDependencies(dependency, settings, allResources);
         }
     }
     allResources[resource] = settings;
 }
Esempio n. 14
0
 private ResourceDefinition FindResource(RequireSettings settings, bool resolveInlineDefinitions) {
     // find the resource with the given type and name
     // that has at least the given version number. If multiple,
     // return the resource with the greatest version number.
     // If not found and an inlineDefinition is given, define the resource on the fly
     // using the action.
     var name = settings.Name ?? "";
     var type = settings.Type;
     var resource = (from p in ResourceProviders
                     from r in p.GetResources(type)
                     where name.Equals(r.Key, StringComparison.OrdinalIgnoreCase)
                     orderby r.Value.Version descending
                     select r.Value).FirstOrDefault();
     if (resource == null && _dynamicManifest != null) {
         resource = (from r in _dynamicManifest.GetResources(type)
                     where name.Equals(r.Key, StringComparison.OrdinalIgnoreCase)
                     orderby r.Value.Version descending
                     select r.Value).FirstOrDefault();
     }
     if (resolveInlineDefinitions && resource == null) {
         // Does not seem to exist, but it's possible it is being
         // defined by a Define() from a RequireSettings somewhere.
         if (ResolveInlineDefinitions(settings.Type)) {
             // if any were defined, now try to find it
             resource = FindResource(settings, false);
         }
     }
     return resource;
 }
Esempio n. 15
0
 public string ResolveUrl(RequireSettings settings, string applicationPath) {
     string url;
     if (_urlResolveCache.TryGetValue(settings, out url)) {
         return url;
     }
     // Url priority:
     if (settings.DebugMode) {
         url = settings.CdnMode
             ? Coalesce(UrlCdnDebug, UrlDebug, UrlCdn, Url)
             : Coalesce(UrlDebug, Url, UrlCdnDebug, UrlCdn);
     }
     else {
         url = settings.CdnMode
             ? Coalesce(UrlCdn, Url, UrlCdnDebug, UrlDebug)
             : Coalesce(Url, UrlDebug, UrlCdn, UrlCdnDebug);
     }
     if (String.IsNullOrEmpty(url)) {
         return null;
     }
     if (!String.IsNullOrEmpty(settings.Culture)) {
         string nearestCulture = FindNearestCulture(settings.Culture);
         if (!String.IsNullOrEmpty(nearestCulture)) {
             url = Path.ChangeExtension(url, nearestCulture + Path.GetExtension(url));
         }
     }
     if (!Uri.IsWellFormedUriString(url, UriKind.Absolute) && !VirtualPathUtility.IsAbsolute(url) && !VirtualPathUtility.IsAppRelative(url) && !String.IsNullOrEmpty(BasePath)) {
         // relative urls are relative to the base path of the module that defined the manifest
         url = VirtualPathUtility.Combine(BasePath, url);
     }
     if (VirtualPathUtility.IsAppRelative(url)) {
         url = applicationPath != null 
             ? VirtualPathUtility.ToAbsolute(url, applicationPath) 
             : VirtualPathUtility.ToAbsolute(url);
     }
     _urlResolveCache[settings] = url;
     return url;
 }
Esempio n. 16
0
        private void WriteResources(dynamic Display, TextWriter Output, string resourceType, ResourceLocation? includeLocation, ResourceLocation? excludeLocation) {
            bool debugMode;
            var site = _workContext.Value.CurrentSite;
            switch (site.ResourceDebugMode) {
                case ResourceDebugMode.Enabled:
                    debugMode = true;
                    break;
                case ResourceDebugMode.Disabled:
                    debugMode = false;
                    break;
                default:
                    Debug.Assert(site.ResourceDebugMode == ResourceDebugMode.FromAppSetting, "Unknown ResourceDebugMode value.");
                    debugMode = _httpContextAccessor.Value.Current().IsDebuggingEnabled;
                    break;
            }
            var defaultSettings = new RequireSettings {
                DebugMode = debugMode,
                CdnMode = site.UseCdn,
                Culture = _workContext.Value.CurrentCulture,
            };
            var requiredResources = _resourceManager.Value.BuildRequiredResources(resourceType);
            var appPath = _httpContextAccessor.Value.Current().Request.ApplicationPath;
            foreach (var context in requiredResources.Where(r =>
                (includeLocation.HasValue ? r.Settings.Location == includeLocation.Value : true) &&
                (excludeLocation.HasValue ? r.Settings.Location != excludeLocation.Value : true))) {

                var path = context.GetResourceUrl(defaultSettings, appPath);
                var condition = context.Settings.Condition;
                var attributes = context.Settings.HasAttributes ? context.Settings.Attributes : null;
                IHtmlString result;
                if (resourceType == "stylesheet") {
                    result = Display.Style(Url: path, Condition: condition, Resource: context.Resource, TagAttributes: attributes);
                }
                else if (resourceType == "script") { 
                    result = Display.Script(Url: path, Condition: condition, Resource: context.Resource, TagAttributes: attributes);
                }
                else {
                    result = Display.Resource(Url: path, Condition: condition, Resource: context.Resource, TagAttributes: attributes);
                }
                Output.Write(result);
            }
        }
Esempio n. 17
0
 public RequireSettings Combine(RequireSettings other) {
     var settings = (new RequireSettings {
         Name = Name,
         Type = Type
     }).AtLocation(Location).AtLocation(other.Location)
         .WithBasePath(BasePath).WithBasePath(other.BasePath)
         .UseCdn(CdnMode).UseCdn(other.CdnMode)
         .UseDebugMode(DebugMode).UseDebugMode(other.DebugMode)
         .UseCulture(Culture).UseCulture(other.Culture)
         .UseCondition(Condition).UseCondition(other.Condition)
         .Define(InlineDefinition).Define(other.InlineDefinition);
     settings._attributes = MergeAttributes(other);
     return settings;
 }
Esempio n. 18
0
        private void WriteResources(TextWriter Output, string resourceType, ResourceLocation? includeLocation, ResourceLocation? excludeLocation)
        {
            var defaultSettings = new RequireSettings();

            var requiredResources = _resourceManager.Value.BuildRequiredResources(resourceType);
            var appPath = _httpContextAccessor.Value.Current().Request.ApplicationPath;
            foreach (var context in requiredResources.Where(r =>
                (includeLocation.HasValue ? r.Settings.Location == includeLocation.Value : true) &&
                (excludeLocation.HasValue ? r.Settings.Location != excludeLocation.Value : true)))
            {

                var path = context.GetResourceUrl(defaultSettings, appPath);
                var condition = context.Settings.Condition;
                var attributes = context.Settings.HasAttributes ? context.Settings.Attributes : null;

                //需要判断是否用了cdn地址
                ResourceManager.WriteResource(Output, context.Resource, !context.Resource.Url.StartsWith("http") ? (context.Resource.BasePath + context.Resource.Url) : context.Resource.Url,
                    condition, attributes);
                //if (resourceType == "stylesheet")
                //{
                //    context.Resource.TagBuilder.Attributes.Add(context.Resource.FilePathAttributeName, context.Resource.Url);

                //}
                //else if (resourceType == "script")
                //{
                //    result = Display.Script(Url: path, Condition: condition, Resource: context.Resource, TagAttributes: attributes);
                //}
                //else
                //{
                //    result = Display.Resource(Url: path, Condition: condition, Resource: context.Resource, TagAttributes: attributes);
                //}
                //Output.Write(result);
            }
        }
Esempio n. 19
0
 public string ResolveUrl(RequireSettings settings, string applicationPath, IResourceFileHashProvider resourceFileHashProvider)
 {
     return(ResolveUrl(settings, applicationPath, false, resourceFileHashProvider));
 }
 private void VerifyPaths(string resourceType, RequireSettings defaultSettings, string expectedPaths) {
     defaultSettings = defaultSettings ?? new RequireSettings();
     var requiredResources = _resourceManager.BuildRequiredResources(resourceType);
     var renderedResources = string.Join(",", requiredResources.Select(context => context.GetResourceUrl(defaultSettings, _appPath)).ToArray());
     Assert.That(renderedResources, Is.EqualTo(expectedPaths));
 }
 public string GetResourceUrl(RequireSettings baseSettings, string appPath) {
     return Resource.ResolveUrl(baseSettings == null ? Settings : baseSettings.Combine(Settings), appPath);
 }
Esempio n. 22
0
        public string ResolveUrl(RequireSettings settings, string applicationPath)
        {
            string url;
            if (_urlResolveCache.TryGetValue(settings, out url))
            {
                return url;
            }

            if (String.IsNullOrEmpty(url))
            {
                return null;
            }

            if (!Uri.IsWellFormedUriString(url, UriKind.Absolute) && !VirtualPathUtility.IsAbsolute(url) && !VirtualPathUtility.IsAppRelative(url) && !String.IsNullOrEmpty(BasePath))
            {
                // relative urls are relative to the base path of the module that defined the manifest
                url = VirtualPathUtility.Combine(BasePath, url);
            }
            if (VirtualPathUtility.IsAppRelative(url))
            {
                url = VirtualPathUtility.ToAbsolute(url, applicationPath);
            }
            _urlResolveCache[settings] = url;
            return url;
        }
Esempio n. 23
0
 private Dictionary<string, string> MergeAttributes(RequireSettings other) {
     // efficiently merge the two dictionaries, taking into account that one or both may not exist
     // and that attributes in 'other' should overridde attributes in this, even if the value is null.
     if (_attributes == null) {
         return other._attributes == null ? null : new Dictionary<string, string>(other._attributes);
     }
     if (other._attributes == null) {
         return new Dictionary<string, string>(_attributes);
     }
     var mergedAttributes = new Dictionary<string, string>(_attributes);
     foreach (var pair in other._attributes) {
         mergedAttributes[pair.Key] = pair.Value;
     }
     return mergedAttributes;
 }
 public virtual ResourceDefinition FindResource(RequireSettings settings)
 {
     return(FindResource(settings, true));
 }
Esempio n. 25
0
 public virtual RequireSettings Require(string resourceType, string resourceName) {
     if (resourceType == null) {
         throw new ArgumentNullException("resourceType");
     }
     if (resourceName == null) {
         throw new ArgumentNullException("resourceName");
     }
     RequireSettings settings;
     var key = new Tuple<string, string>(resourceType, resourceName);
     if (!_required.TryGetValue(key, out settings)) {
         settings = new RequireSettings { Type = resourceType, Name = resourceName };
         _required[key] = settings;
     }
     _builtResources[resourceType] = null;
     return settings;
 }
 public string GetResourceUrl(RequireSettings baseSettings, string appPath, bool ssl, IResourceFileHashProvider resourceFileHashProvider)
 {
     return(Resource.ResolveUrl(baseSettings == null ? Settings : baseSettings.Combine(Settings), appPath, ssl, resourceFileHashProvider));
 }
Esempio n. 27
0
 public virtual ResourceDefinition FindResource(RequireSettings settings) {
     return FindResource(settings, true);
 }
Esempio n. 28
0
        public string ResolveUrl(RequireSettings settings, string applicationPath, bool ssl, IResourceFileHashProvider resourceFileHashProvider)
        {
            string url;
            string physicalPath = null;

            // Url priority:
            if (!ssl || (ssl && CdnSupportsSsl))   //Not ssl or ssl and cdn supports it
            {
                if (settings.DebugMode)
                {
                    url = settings.CdnMode
                        ? Coalesce(UrlCdnDebug, UrlDebug, UrlCdn, Url)
                        : Coalesce(UrlDebug, Url, UrlCdnDebug, UrlCdn);
                }
                else
                {
                    url = settings.CdnMode
                        ? Coalesce(UrlCdn, Url, UrlCdnDebug, UrlDebug)
                        : Coalesce(Url, UrlDebug, UrlCdn, UrlCdnDebug);
                }
            }
            else   //ssl and cdn does not support it, only evaluate non-cdn url's
            {
                url = settings.DebugMode
                    ? Coalesce(UrlDebug, Url)
                    : Coalesce(Url, UrlDebug);
            }
            if (url == UrlDebug)
            {
                physicalPath = PhysicalPathDebug;
            }
            else if (url == Url)
            {
                physicalPath = PhysicalPath;
            }
            if (String.IsNullOrEmpty(url))
            {
                return(null);
            }
            if (!String.IsNullOrEmpty(settings.Culture))
            {
                string nearestCulture = FindNearestCulture(settings.Culture);
                if (!String.IsNullOrEmpty(nearestCulture))
                {
                    url = Path.ChangeExtension(url, nearestCulture + Path.GetExtension(url));
                }
            }
            if (!Uri.IsWellFormedUriString(url, UriKind.Absolute) && !VirtualPathUtility.IsAbsolute(url) && !VirtualPathUtility.IsAppRelative(url) && !String.IsNullOrEmpty(BasePath))
            {
                // relative urls are relative to the base path of the module that defined the manifest
                url = VirtualPathUtility.Combine(BasePath, url);
            }
            if (VirtualPathUtility.IsAppRelative(url))
            {
                url = applicationPath != null
                    ? VirtualPathUtility.ToAbsolute(url, applicationPath)
                    : VirtualPathUtility.ToAbsolute(url);
            }
            if (settings.FileHashMode && !String.IsNullOrEmpty(physicalPath) && File.Exists(physicalPath))
            {
                url = AddQueryStringValue(url, "fileHash", resourceFileHashProvider.GetResourceFileHash(physicalPath));
            }
            return(url);
        }
Esempio n. 29
0
 public string ResolveUrl(RequireSettings settings, string applicationPath)
 {
     return(ResolveUrl(settings, applicationPath, false));
 }