예제 #1
0
        string RenderRelease(string key, string renderTo, IRenderer renderer)
        {
            string content;

            if (!TryGetCachedBundle(key, out content))
            {
                using (new CriticalRenderingSection(renderTo))
                {
                    if (!TryGetCachedBundle(key, out content))
                    {
                        var    uniqueFiles = new List <string>();
                        string hash        = null;

                        bundleState.DependentFiles.Clear();

                        if (renderTo == null)
                        {
                            renderTo = renderPathCache[CachePrefix + "." + key];
                        }
                        else
                        {
                            renderPathCache[CachePrefix + "." + key] = renderTo;
                        }

                        var outputFile   = pathTranslator.ResolveAppRelativePathToFileSystem(renderTo);
                        var renderToPath = ExpandAppRelativePath(renderTo);

                        if (!String.IsNullOrEmpty(BaseOutputHref))
                        {
                            renderToPath = String.Concat(BaseOutputHref.TrimEnd('/'), "/", renderToPath.TrimStart('/'));
                        }

                        var remoteAssetPaths = new List <string>();
                        remoteAssetPaths.AddRange(bundleState.Assets.Where(a => a.IsRemote).Select(a => a.RemotePath));

                        uniqueFiles.AddRange(GetFiles(bundleState.Assets.Where(asset =>
                                                                               asset.IsEmbeddedResource ||
                                                                               asset.IsLocal ||
                                                                               asset.IsRemoteDownload).ToList()).Distinct());

                        var renderedTag = string.Empty;
                        if (uniqueFiles.Count > 0 || bundleState.Assets.Count(a => a.IsArbitrary) > 0)
                        {
                            bundleState.DependentFiles.AddRange(uniqueFiles);

                            var minifiedContent = GetMinifiedContent(bundleState.Assets, outputFile);
                            if (!string.IsNullOrEmpty(bundleState.HashKeyName))
                            {
                                hash = hasher.GetHash(minifiedContent);
                            }

                            renderToPath = bundleState.CacheInvalidationStrategy.GetOutputWebPath(renderToPath, bundleState.HashKeyName, hash);
                            outputFile   = bundleState.CacheInvalidationStrategy.GetOutputFileLocation(outputFile, hash);

                            if (!(bundleState.ShouldRenderOnlyIfOutputFileIsMissing && FileExists(outputFile)))
                            {
                                renderer.Render(minifiedContent, outputFile);
                            }

                            renderedTag = FillTemplate(bundleState, renderToPath);
                        }

                        content += String.Concat(GetFilesForRemote(remoteAssetPaths, bundleState), renderedTag);
                    }
                }
                //don't cache bundles where debugging was forced via predicate
                if (!bundleState.DebugPredicate.SafeExecute())
                {
                    bundleCache.Add(key, content, bundleState.DependentFiles, IsDebuggingEnabled());
                }
            }

            return(content);
        }
예제 #2
0
        string RenderRelease(string key, string renderTo, IRenderer renderer)
        {
            string content;

            if (!TryGetCachedBundle(key, out content))
            {
                using (new CriticalRenderingSection(renderTo))
                {
                    if (!TryGetCachedBundle(key, out content))
                    {
                        var    uniqueFiles     = new List <string>();
                        string minifiedContent = null;
                        string hash            = null;
                        bool   hashInFileName  = false;

                        bundleState.DependentFiles.Clear();

                        if (renderTo == null)
                        {
                            renderTo = renderPathCache[CachePrefix + "." + key];
                        }
                        else
                        {
                            renderPathCache[CachePrefix + "." + key] = renderTo;
                        }

                        var outputFile   = FileSystem.ResolveAppRelativePathToFileSystem(renderTo);
                        var renderToPath = ExpandAppRelativePath(renderTo);

                        if (!String.IsNullOrEmpty(BaseOutputHref))
                        {
                            renderToPath = String.Concat(BaseOutputHref.TrimEnd('/'), "/", renderToPath.TrimStart('/'));
                        }

                        var remoteAssetPaths = new List <string>();
                        remoteAssetPaths.AddRange(bundleState.Assets.Where(a => a.IsRemote).Select(a => a.RemotePath));

                        uniqueFiles.AddRange(GetFiles(bundleState.Assets.Where(asset =>
                                                                               asset.IsEmbeddedResource ||
                                                                               asset.IsLocal ||
                                                                               asset.IsRemoteDownload).ToList()).Distinct());

                        var renderedTag = string.Empty;
                        if (uniqueFiles.Count > 0 || bundleState.Assets.Count(a => a.IsArbitrary) > 0)
                        {
                            bundleState.DependentFiles.AddRange(uniqueFiles);

                            if (renderTo.Contains("#"))
                            {
                                hashInFileName  = true;
                                minifiedContent = GetMinifiedContent(bundleState.Assets, outputFile);
                                hash            = hasher.GetHash(minifiedContent);
                                renderToPath    = renderToPath.Replace("#", hash);
                                outputFile      = outputFile.Replace("#", hash);
                            }

                            if (bundleState.ShouldRenderOnlyIfOutputFileIsMissing && FileExists(outputFile))
                            {
                                minifiedContent = ReadFile(outputFile);
                            }
                            else
                            {
                                minifiedContent = minifiedContent ?? GetMinifiedContent(bundleState.Assets, outputFile);
                                renderer.Render(minifiedContent, outputFile);
                            }

                            if (hash == null && !string.IsNullOrEmpty(bundleState.HashKeyName))
                            {
                                hash = hasher.GetHash(minifiedContent);
                            }

                            if (hashInFileName)
                            {
                                renderedTag = FillTemplate(bundleState, renderToPath);
                            }
                            else
                            {
                                if (string.IsNullOrEmpty(bundleState.HashKeyName))
                                {
                                    renderedTag = FillTemplate(bundleState, renderToPath);
                                }
                                else if (renderToPath.Contains("?"))
                                {
                                    renderedTag = FillTemplate(bundleState,
                                                               renderToPath + "&" + bundleState.HashKeyName + "=" + hash);
                                }
                                else
                                {
                                    renderedTag = FillTemplate(bundleState,
                                                               renderToPath + "?" + bundleState.HashKeyName + "=" + hash);
                                }
                            }
                        }

                        content += String.Concat(GetFilesForRemote(remoteAssetPaths, bundleState), renderedTag);
                    }
                }
                //don't cache bundles where debugging was forced via predicate
                if (!bundleState.DebugPredicate.SafeExecute())
                {
                    bundleCache.Add(key, content, bundleState.DependentFiles);
                }
            }

            return(content);
        }