예제 #1
0
        string ProcessCssFile(string file, string outputFile, bool asImport = false)
        {
            string css = null;

            if (file.ToLower().EndsWith(".less") || file.ToLower().EndsWith(".less.css"))
            {
                css = ProcessLess(file);
            }
            else
            {
                css = ReadFile(file);
            }

            if (ShouldImport)
            {
                css = ProcessImport(file, outputFile, css);
            }

            ICssAssetsFileHasher fileHasher = null;

            if (ShouldAppendHashForAssets)
            {
                var fileResolver = new FileSystemResolver();
                fileHasher = new CssAssetsFileHasher(HashKeyName, fileResolver, hasher);
            }

            return(CSSPathRewriter.RewriteCssPaths(outputFile, file, css, fileHasher, asImport));
        }
예제 #2
0
        protected override string BeforeMinify(string outputFile, List <string> filePaths)
        {
            var outputCss = new StringBuilder();

            foreach (string file in filePaths)
            {
                string css;
                if (file.ToLower().EndsWith(".less") || file.ToLower().EndsWith(".less.css"))
                {
                    css = ProcessLess(file);
                }
                else
                {
                    css = ReadFile(file);
                }

                if (ShouldImport)
                {
                    css = ProcessImport(css);
                }

                ICssAssetsFileHasher fileHasher = null;

                if (ShouldAppendHashForAssets)
                {
                    var fileResolver = new FileResolver();
                    fileHasher = new CssAssetsFileHasher(HashKeyName, fileResolver, hasher);
                }

                css = CSSPathRewriter.RewriteCssPaths(outputFile, file, css, fileHasher);
                outputCss.Append(css + "\n");
            }

            return(outputCss.ToString());
        }
예제 #3
0
        private string CompressCss(string outputFilePath, List <string> files, ICssCompressor compressor, bool appendHashToAssets)
        {
            var outputCss = new StringBuilder();

            foreach (string file in files)
            {
                string css;
                if (file.ToLower().EndsWith(".less") || file.ToLower().EndsWith(".less.css"))
                {
                    css = ProcessLess(file);
                }
                else
                {
                    css = ReadFile(file);
                }

                if (processImports)
                {
                    css = ProcessImport(css);
                }

                ICssAssetsFileHasher fileHasher = null;

                if (appendHashToAssets)
                {
                    var fileResolver = new FileResolver();
                    fileHasher = new CssAssetsFileHasher(hashQueryStringKeyName, fileResolver, hasher);
                }

                css = CssPathRewriter.RewriteCssPaths(outputFilePath, file, css, fileHasher);
                outputCss.Append(css + "\n");
            }
            return(compressor.CompressContent(outputCss.ToString()));
        }
예제 #4
0
        string ProcessCssFile(string file, string outputFile, bool asImport = false)
        {
            string css = null;

            var preprocessors = FindPreprocessors(file);

            if (preprocessors != null)
            {
                css = PreprocessFile(file, preprocessors);
            }
            else
            {
                css = ReadFile(file);
            }

            if (ShouldImport)
            {
                css = ProcessImport(file, outputFile, css);
            }

            ICssAssetsFileHasher fileHasher = null;

            if (ShouldAppendHashForAssets)
            {
                var fileResolver = new FileSystemResolver();
                fileHasher = new CssAssetsFileHasher(bundleState.HashKeyName, fileResolver, hasher);
            }

            return(CSSPathRewriter.RewriteCssPaths(outputFile, file, css, fileHasher, asImport));
        }
예제 #5
0
        public void DoesNotAppendHashIfFileDoesNotExists()
        {
            var hashQueryStringKeyName = "v";
            var fileResolver           = new FileResolver();
            var hasher      = new StubHasher("hash");
            var cssFilePath = @"C:\somepath\output.css";
            var url         = "/doesnotexist.jpg";

            var assetsFileHasher = new CssAssetsFileHasher(hashQueryStringKeyName, fileResolver, hasher);

            var rewrittenUrl = assetsFileHasher.AppendFileHash(cssFilePath, url);

            Assert.That(rewrittenUrl, Is.EqualTo(url));
        }
예제 #6
0
        public void DoesNotAppendHashIfFileIsRemote()
        {
            var hashQueryStringKeyName = "v";
            var fileResolver           = new FileSystemResolver();
            var hasher      = new StubHasher("hash");
            var cssFilePath = @"C:\somepath\output.css";
            var url         = "http://www.test.com/image.jpg";

            var assetsFileHasher = new CssAssetsFileHasher(hashQueryStringKeyName, fileResolver, hasher);

            var rewrittenUrl = assetsFileHasher.AppendFileHash(cssFilePath, url);

            Assert.That(rewrittenUrl, Is.EqualTo(url));
        }
예제 #7
0
        public void CanAppendFileHashToRelativeUriWithAnExistingQueryString()
        {
            var hashQueryStringKeyName = "v";
            var hashValue        = "hashValue";
            var hasher           = new StubHasher(hashValue);
            var fileResolver     = new FileResolver();
            var uri              = Assembly.GetExecutingAssembly().CodeBase;
            var cssFilePath      = Path.GetDirectoryName(uri) + @"\subdirectory\output.css";
            var url              = "../" + Path.GetFileName(uri) + "?test=value";
            var assetsFileHasher = new CssAssetsFileHasher(hashQueryStringKeyName, fileResolver, hasher);

            var expectedUrl = url + "&" + hashQueryStringKeyName + "=" + hashValue;

            var rewrittenUrl = assetsFileHasher.AppendFileHash(cssFilePath, url);

            Assert.That(rewrittenUrl, Is.EqualTo(expectedUrl));
        }
예제 #8
0
        public void CanAppendFileHashToAbsoluteUri()
        {
            var hashQueryStringKeyName = "v";
            var hashValue        = "hashValue";
            var hasher           = new StubHasher(hashValue);
            var uri              = Assembly.GetExecutingAssembly().CodeBase;
            var cssFilePath      = Path.GetDirectoryName(uri) + @"\output.css";
            var url              = "/" + Path.GetFileName(uri);
            var pathToResolveTo  = Assembly.GetExecutingAssembly().Location;
            var fileResolver     = new StubFileResolver(pathToResolveTo);
            var assetsFileHasher = new CssAssetsFileHasher(hashQueryStringKeyName, fileResolver, hasher);

            var expectedUrl = url + "?" + hashQueryStringKeyName + "=" + hashValue;

            var rewrittenUrl = assetsFileHasher.AppendFileHash(cssFilePath, url);

            Assert.That(rewrittenUrl, Is.EqualTo(expectedUrl));
        }
예제 #9
0
        static int Main(string[] args)
        {
            var showHelp             = false;
            var configPath           = string.Empty;
            var crusherSectionName   = "Crusher";
            var cssSpriteSectionName = "CssSprite";
            var applicationPath      = "/";

            var options = new OptionSet()
            {
                {
                    "c=|configPath=",
                    "the configuration path to the web.config or app.config file. E.g. ../../../Talifun.Web.Examples/Crusher.Demo/web.config",
                    c => configPath = c
                },
                {
                    "cs=|crusherSectionName=",
                    "the section name of the configuration element for the Talifun.Crusher configuration. Defaults to 'Crusher' if not specified.",
                    cs => crusherSectionName = cs
                },
                {
                    "css=|cssSpriteSectionName=",
                    "the section name of the configuration element for the Talifun.CssSprite configuration. Defaults to 'CssSprite' if not specified.",
                    css => cssSpriteSectionName = css
                },
                {
                    "a=|applicationPath=",
                    "the application path to be relative from. Defaults to  '/' if not specified.",
                    a => applicationPath = a
                },
                {
                    "?|h|help",
                    "display help screen",
                    h => showHelp = h != null
                }
            };

            try
            {
                options.Parse(args);
            }
            catch (OptionException e)
            {
                Console.WriteLine(HeaderMessage);
                Console.WriteLine(e.Message);
                Console.WriteLine(UsageMessage);
                Console.WriteLine(HelpMessage);
                return(DisplayHelpScreenExitCode);
            }

            if (showHelp)
            {
                DisplayHelp(options);
                return(DisplayHelpScreenExitCode);
            }

            if (string.IsNullOrEmpty(configPath))
            {
                Console.WriteLine(HeaderMessage);
                Console.WriteLine(UsageMessage);
                Console.WriteLine(HelpMessage);
                return(DisplayHelpScreenExitCode);
            }

            var crusherConfiguration   = GetCrusherSection(configPath, crusherSectionName);
            var cssSpriteConfiguration = GetCssSpriteSection(configPath, cssSpriteSectionName);

            if (crusherConfiguration == null && cssSpriteConfiguration == null)
            {
                Console.WriteLine(HeaderMessage);
                Console.WriteLine("\"{0}\" section name not found in {1} ", crusherSectionName, configPath);
                Console.WriteLine("\"{0}\" section name not found in {1} ", cssSpriteSectionName, configPath);
                Console.WriteLine(HelpMessage);
                return(DisplayHelpScreenExitCode);
            }

            try
            {
                Console.WriteLine();
                Console.WriteLine("Settings used:");
                Console.WriteLine("configPath = " + configPath);
                Console.WriteLine("crusherSectionName = " + crusherSectionName);
                Console.WriteLine("cssSpriteSectionName = " + cssSpriteSectionName);
                Console.WriteLine("applicationPath = " + applicationPath);

                var physicalApplicationPath = new FileInfo(configPath).DirectoryName;
                var retryableFileOpener     = new RetryableFileOpener();
                var hasher = new Hasher(retryableFileOpener);
                var retryableFileWriter = new RetryableFileWriter(BufferSize, retryableFileOpener, hasher);
                var pathProvider        = new PathProvider(applicationPath, physicalApplicationPath);
                var cacheManager        = new HttpCacheManager();

                if (crusherConfiguration == null)
                {
                    Console.WriteLine();
                    Console.WriteLine("Skipping css/js crushed content creation. \"{0}\" section name not found in \"{1}\"", crusherSectionName, configPath);
                }
                else
                {
                    var hashQueryStringKeyName = crusherConfiguration.QuerystringKeyName;
                    var cssAssetsFileHasher    = new CssAssetsFileHasher(hashQueryStringKeyName, hasher, pathProvider);
                    var cssPathRewriter        = new CssPathRewriter(cssAssetsFileHasher, pathProvider);
                    var cssCrusher             = new CssCrusher(cacheManager, pathProvider, retryableFileOpener, retryableFileWriter, cssPathRewriter);
                    var jsCrusher = new JsCrusher(cacheManager, pathProvider, retryableFileOpener, retryableFileWriter);

                    var cssGroups = crusherConfiguration.CssGroups;
                    var jsGroups  = crusherConfiguration.JsGroups;
                    CreateCrushedFiles(pathProvider, cssGroups, jsGroups, cssCrusher, jsCrusher);

                    Console.WriteLine();
                    Console.WriteLine(_cssOutput);
                    Console.WriteLine();
                    Console.WriteLine(_jsOutput);
                }

                if (cssSpriteConfiguration == null)
                {
                    Console.WriteLine();
                    Console.WriteLine("Skipping css sprite creation. \"{0}\" section name not found in \"{1}\"", cssSpriteSectionName, configPath);
                }
                else
                {
                    var cssSpriteGroups  = cssSpriteConfiguration.CssSpriteGroups;
                    var cssSpriteCreator = new CssSpriteCreator(cacheManager, retryableFileOpener, pathProvider, retryableFileWriter);
                    CreateCssSpriteFiles(pathProvider, cssSpriteGroups, cssSpriteCreator);

                    Console.WriteLine();
                    Console.WriteLine(_cssSpriteOutput);
                }
            }
            catch (Exception exception)
            {
                Console.Write(exception);
                return(ErrorExitCode);
            }

            return(SuccessExitCode);
        }
        public object Clone()
        {
            var filePaths         = new List <string>();
            var fileRelativePaths = new List <string>();

            AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
            try
            {
                var cssSpriteOutput = string.Empty;
                var jsOutput        = string.Empty;
                var cssOutput       = string.Empty;

                var countdownEvents = new CountdownEvent(3);

                ThreadPool.QueueUserWorkItem(data =>
                {
                    var countdownEvent = (CountdownEvent)data;
                    try
                    {
                        if (_crusherConfiguration != null)
                        {
                            var cssSpriteGroups          = _crusherConfiguration.CssSpriteGroups;
                            var cssSpriteCreator         = new CssSpriteCreator(_cacheManager, _retryableFileOpener, _pathProvider, _retryableFileWriter, _fileMetaData);
                            var cssSpriteGroupsProcessor = new CssSpriteGroupsProcessor();

                            cssSpriteOutput = cssSpriteGroupsProcessor.ProcessGroups(_pathProvider, cssSpriteCreator, cssSpriteGroups).ToString();

                            var cssFilePaths         = new List <string>();
                            var cssFileRelativePaths = new List <string>();
                            foreach (CssSpriteGroupElement cssSpriteGroup in cssSpriteGroups)
                            {
                                cssFilePaths.Add(new Uri(_pathProvider.MapPath(cssSpriteGroup.CssOutputFilePath)).LocalPath);
                                cssFilePaths.Add(new Uri(_pathProvider.MapPath(cssSpriteGroup.ImageOutputFilePath)).LocalPath);

                                cssFileRelativePaths.Add(MakeRelative(cssSpriteGroup.CssOutputFilePath));
                                cssFileRelativePaths.Add(MakeRelative(cssSpriteGroup.ImageOutputFilePath));
                            }

                            filePaths.AddRange(cssFilePaths);
                            fileRelativePaths.AddRange(cssFileRelativePaths);

                            _logMessage(cssSpriteOutput);
                        }
                    }
                    catch (Exception exception)
                    {
                        _logError(exception.ToString());
                    }
                    countdownEvent.Signal();
                }, countdownEvents);

                ThreadPool.QueueUserWorkItem(data =>
                {
                    var countdownEvent = (CountdownEvent)data;

                    try
                    {
                        if (_crusherConfiguration != null)
                        {
                            var jsCrusher         = new JsCrusher(_cacheManager, _pathProvider, _retryableFileOpener, _retryableFileWriter, _fileMetaData);
                            var jsGroups          = _crusherConfiguration.JsGroups;
                            var jsGroupsProcessor = new JsGroupsProcessor();
                            jsOutput = jsGroupsProcessor.ProcessGroups(_pathProvider, jsCrusher, jsGroups).ToString();

                            var jsFilePaths         = new List <string>();
                            var jsFileRelativePaths = new List <string>();
                            foreach (JsGroupElement jsGroup in jsGroups)
                            {
                                jsFilePaths.Add(new Uri(_pathProvider.MapPath(jsGroup.OutputFilePath)).LocalPath);
                                jsFileRelativePaths.Add(MakeRelative(jsGroup.OutputFilePath));
                            }

                            filePaths.AddRange(jsFilePaths);
                            fileRelativePaths.AddRange(jsFileRelativePaths);

                            _logMessage(jsOutput);
                        }
                    }
                    catch (Exception exception)
                    {
                        _logError(exception.ToString());
                    }
                    countdownEvent.Signal();
                }, countdownEvents);

                ThreadPool.QueueUserWorkItem(data =>
                {
                    var countdownEvent = (CountdownEvent)data;

                    try
                    {
                        if (_crusherConfiguration != null)
                        {
                            var hashQueryStringKeyName = _crusherConfiguration.QuerystringKeyName;
                            var cssAssetsFileHasher    = new CssAssetsFileHasher(hashQueryStringKeyName, _hasher, _pathProvider);
                            var cssPathRewriter        = new CssPathRewriter(cssAssetsFileHasher, _pathProvider);
                            var cssCrusher             = new CssCrusher(_cacheManager, _pathProvider, _retryableFileOpener, _retryableFileWriter, cssPathRewriter, _fileMetaData, _crusherConfiguration.WatchAssets);
                            var cssGroups        = _crusherConfiguration.CssGroups;
                            var cssGroupsCrusher = new CssGroupsProcessor();
                            cssOutput            = cssGroupsCrusher.ProcessGroups(_pathProvider, cssCrusher, cssGroups).ToString();

                            var cssFilePaths         = new List <string>();
                            var cssFileRelativePaths = new List <string>();

                            foreach (CssGroupElement cssGroup in cssGroups)
                            {
                                cssFilePaths.Add(new Uri(_pathProvider.MapPath(cssGroup.OutputFilePath)).LocalPath);
                                cssFileRelativePaths.Add(MakeRelative(cssGroup.OutputFilePath));
                            }

                            filePaths.AddRange(cssFilePaths);
                            fileRelativePaths.AddRange(cssFileRelativePaths);

                            _logMessage(cssOutput);
                        }
                    }
                    catch (Exception exception)
                    {
                        _logError(exception.ToString());
                    }
                    countdownEvent.Signal();
                }, countdownEvents);

                countdownEvents.Wait();
            }
            catch (Exception exception)
            {
                _logError(exception.ToString());
            }
            finally
            {
                AppDomain.CurrentDomain.UnhandledException -= OnUnhandledException;
            }

            _setOutputFilePaths(filePaths.ToArray());
            _setOutputFileRelativePaths(fileRelativePaths.ToArray());

            return(null);
        }
예제 #11
0
        public object Clone()
        {
            AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
            try
            {
                var cssSpriteOutput = string.Empty;
                var jsOutput        = string.Empty;
                var cssOutput       = string.Empty;

                var countdownEvents = new CountdownEvent(1);

                ThreadPool.QueueUserWorkItem(data =>
                {
                    var countdownEvent = (CountdownEvent)data;

                    try
                    {
                        if (_cssSpriteConfiguration != null)
                        {
                            var cssSpriteGroups          = _cssSpriteConfiguration.CssSpriteGroups;
                            var cssSpriteCreator         = new CssSpriteCreator(_cacheManager, _retryableFileOpener, _pathProvider, _retryableFileWriter, _fileMetaData);
                            var cssSpriteGroupsProcessor = new CssSpriteGroupsProcessor();

                            cssSpriteOutput = cssSpriteGroupsProcessor.ProcessGroups(_pathProvider, cssSpriteCreator, cssSpriteGroups).ToString();

                            _logMessage(cssSpriteOutput);
                        }
                    }
                    catch (Exception exception)
                    {
                        _logError(exception.ToString());
                    }
                    countdownEvent.Signal();
                }, countdownEvents);

                countdownEvents.Wait();

                countdownEvents = new CountdownEvent(2);

                ThreadPool.QueueUserWorkItem(data =>
                {
                    var countdownEvent = (CountdownEvent)data;

                    try
                    {
                        if (_crusherConfiguration != null)
                        {
                            var jsCrusher         = new JsCrusher(_cacheManager, _pathProvider, _retryableFileOpener, _retryableFileWriter, _fileMetaData);
                            var jsGroups          = _crusherConfiguration.JsGroups;
                            var jsGroupsProcessor = new JsGroupsProcessor();

                            jsOutput = jsGroupsProcessor.ProcessGroups(_pathProvider, jsCrusher, jsGroups).ToString();

                            _logMessage(jsOutput);
                        }
                    }
                    catch (Exception exception)
                    {
                        _logError(exception.ToString());
                    }
                    countdownEvent.Signal();
                }, countdownEvents);

                ThreadPool.QueueUserWorkItem(data =>
                {
                    var countdownEvent = (CountdownEvent)data;

                    try
                    {
                        if (_crusherConfiguration != null)
                        {
                            var hashQueryStringKeyName = _crusherConfiguration.QuerystringKeyName;
                            var cssAssetsFileHasher    = new CssAssetsFileHasher(hashQueryStringKeyName, _hasher, _pathProvider);
                            var cssPathRewriter        = new CssPathRewriter(cssAssetsFileHasher, _pathProvider);
                            var cssCrusher             = new CssCrusher(_cacheManager, _pathProvider, _retryableFileOpener, _retryableFileWriter, cssPathRewriter, _fileMetaData, _crusherConfiguration.WatchAssets);
                            var cssGroups        = _crusherConfiguration.CssGroups;
                            var cssGroupsCrusher = new CssGroupsProcessor();
                            cssOutput            = cssGroupsCrusher.ProcessGroups(_pathProvider, cssCrusher, cssGroups).ToString();

                            _logMessage(cssOutput);
                        }
                    }
                    catch (Exception exception)
                    {
                        _logError(exception.ToString());
                    }
                    countdownEvent.Signal();
                }, countdownEvents);

                countdownEvents.Wait();
            }
            catch (Exception exception)
            {
                _logError(exception.ToString());
            }
            finally
            {
                AppDomain.CurrentDomain.UnhandledException -= OnUnhandledException;
            }

            return(null);
        }
예제 #12
0
        static int Main(string[] args)
        {
            var showHelp             = false;
            var configPath           = string.Empty;
            var crusherSectionName   = "Crusher";
            var cssSpriteSectionName = "CssSprite";
            var applicationPath      = "/";

            var options = new OptionSet()
            {
                {
                    "c=|configPath=",
                    "the configuration path to the web.config or app.config file. E.g. ../../../Talifun.Web.Examples/Crusher.Demo/web.config",
                    c => configPath = c
                },
                {
                    "cs=|crusherSectionName=",
                    "the section name of the configuration element for the Talifun.Crusher configuration. Defaults to 'Crusher' if not specified.",
                    cs => crusherSectionName = cs
                },
                {
                    "css=|cssSpriteSectionName=",
                    "the section name of the configuration element for the Talifun.CssSprite configuration. Defaults to 'CssSprite' if not specified.",
                    css => cssSpriteSectionName = css
                },
                {
                    "a=|applicationPath=",
                    "the application path to be relative from. Defaults to  '/' if not specified.",
                    a => applicationPath = a
                },
                {
                    "?|h|help",
                    "display help screen",
                    h => showHelp = h != null
                }
            };

            try
            {
                options.Parse(args);
            }
            catch (OptionException e)
            {
                Console.WriteLine(HeaderMessage);
                Console.WriteLine(e.Message);
                Console.WriteLine(UsageMessage);
                Console.WriteLine(HelpMessage);
                return(DisplayHelpScreenExitCode);
            }

            if (showHelp)
            {
                DisplayHelp(options);
                return(DisplayHelpScreenExitCode);
            }

            if (string.IsNullOrEmpty(configPath))
            {
                Console.WriteLine(HeaderMessage);
                Console.WriteLine(UsageMessage);
                Console.WriteLine(HelpMessage);
                return(DisplayHelpScreenExitCode);
            }

            var crusherConfiguration   = GetCrusherSection(configPath, crusherSectionName);
            var cssSpriteConfiguration = GetCssSpriteSection(configPath, cssSpriteSectionName);

            if (crusherConfiguration == null && cssSpriteConfiguration == null)
            {
                Console.WriteLine(HeaderMessage);
                Console.WriteLine("\"{0}\" section name not found in {1} ", cssSpriteSectionName, configPath);
                Console.WriteLine("\"{0}\" section name not found in {1} ", crusherSectionName, configPath);
                Console.WriteLine(HelpMessage);
                return(DisplayHelpScreenExitCode);
            }

            try
            {
                Console.WriteLine();
                Console.WriteLine("Settings used:");
                Console.WriteLine("configPath = " + configPath);
                Console.WriteLine("cssSpriteSectionName = " + cssSpriteSectionName);
                Console.WriteLine("crusherSectionName = " + crusherSectionName);
                Console.WriteLine("applicationPath = " + applicationPath);

                var configUri = new Uri(configPath, UriKind.RelativeOrAbsolute);
                if (!configUri.IsAbsoluteUri)
                {
                    configUri = new Uri(Path.Combine(Environment.CurrentDirectory, configUri.ToString()));
                }

                var physicalApplicationPath = new FileInfo(configUri.LocalPath).DirectoryName;

                var retryableFileOpener = new RetryableFileOpener();
                var hasher = new Hasher(retryableFileOpener);
                var retryableFileWriter = new RetryableFileWriter(BufferSize, Encoding, retryableFileOpener, hasher);
                var pathProvider        = new PathProvider(applicationPath, physicalApplicationPath);
                var cacheManager        = new HttpCacheManager();

                var cssSpriteSpriteMetaDataFileInfo = new FileInfo("cssSprite.metadata");
                var cssSpriteMetaData = new SingleFileMetaData(cssSpriteSpriteMetaDataFileInfo, retryableFileOpener, retryableFileWriter);

                var jsSpriteMetaDataFileInfo = new FileInfo("js.metadata");
                var jsMetaData = new SingleFileMetaData(jsSpriteMetaDataFileInfo, retryableFileOpener, retryableFileWriter);

                var cssSpriteMetaDataFileInfo = new FileInfo("css.metadata");
                var cssMetaData = new SingleFileMetaData(cssSpriteMetaDataFileInfo, retryableFileOpener, retryableFileWriter);

                var jsOutput        = string.Empty;
                var cssOutput       = string.Empty;
                var cssSpriteOutput = string.Empty;


                //We want to be able to use output from css sprites in crushed content
                var countdownEvents = new CountdownEvent(1);

                var cssSpriteExceptions = new List <CssSpriteException>();
                ThreadPool.QueueUserWorkItem(data =>
                {
                    var manualResetEvent = (CountdownEvent)data;
                    try
                    {
                        if (cssSpriteConfiguration != null)
                        {
                            var cssSpriteGroups          = cssSpriteConfiguration.CssSpriteGroups;
                            var cssSpriteCreator         = new CssSpriteCreator(cacheManager, retryableFileOpener, pathProvider, retryableFileWriter, cssSpriteMetaData);
                            var cssSpriteGroupsProcessor = new CssSpriteGroupsProcessor();

                            cssSpriteOutput = cssSpriteGroupsProcessor.ProcessGroups(pathProvider, cssSpriteCreator, cssSpriteGroups).ToString();
                        }
                    }
                    catch (Exception exception)
                    {
                        cssSpriteExceptions.Add(new CssSpriteException(exception));
                    }
                    manualResetEvent.Signal();
                }, countdownEvents);

                countdownEvents.Wait();

                countdownEvents = new CountdownEvent(2);
                var jsExceptions = new List <JsException>();
                ThreadPool.QueueUserWorkItem(data =>
                {
                    var manualResetEvent = (CountdownEvent)data;
                    try
                    {
                        if (crusherConfiguration != null)
                        {
                            var jsCrusher         = new JsCrusher(cacheManager, pathProvider, retryableFileOpener, retryableFileWriter, jsMetaData);
                            var jsGroups          = crusherConfiguration.JsGroups;
                            var jsGroupsProcessor = new JsGroupsProcessor();

                            jsOutput = jsGroupsProcessor.ProcessGroups(pathProvider, jsCrusher, jsGroups).ToString();
                        }
                    }
                    catch (Exception exception)
                    {
                        jsExceptions.Add(new JsException(exception));
                    }
                    manualResetEvent.Signal();
                }, countdownEvents);

                var cssExceptions = new List <CssException>();
                ThreadPool.QueueUserWorkItem(data =>
                {
                    var manualResetEvent = (CountdownEvent)data;
                    try
                    {
                        if (crusherConfiguration != null)
                        {
                            var hashQueryStringKeyName = crusherConfiguration.QuerystringKeyName;
                            var cssAssetsFileHasher    = new CssAssetsFileHasher(hashQueryStringKeyName, hasher, pathProvider);
                            var cssPathRewriter        = new CssPathRewriter(cssAssetsFileHasher, pathProvider);
                            var cssCrusher             = new CssCrusher(cacheManager, pathProvider, retryableFileOpener, retryableFileWriter, cssPathRewriter, cssMetaData, crusherConfiguration.WatchAssets);
                            var cssGroups        = crusherConfiguration.CssGroups;
                            var cssGroupsCrusher = new CssGroupsProcessor();
                            cssOutput            = cssGroupsCrusher.ProcessGroups(pathProvider, cssCrusher, cssGroups).ToString();
                        }
                    }
                    catch (Exception exception)
                    {
                        cssExceptions.Add(new CssException(exception));
                    }
                    manualResetEvent.Signal();
                }, countdownEvents);

                countdownEvents.Wait();

                if (string.IsNullOrEmpty(cssSpriteOutput) && !cssSpriteExceptions.Any())
                {
                    Console.WriteLine();
                    Console.WriteLine("Skipping css sprite creation. \"{0}\" section name not found in \"{1}\"", cssSpriteSectionName, configPath);
                }
                else
                {
                    Console.WriteLine();
                    Console.WriteLine(cssSpriteOutput);

                    if (cssSpriteExceptions.Any())
                    {
                        Console.WriteLine("Css sprite errors:");
                        Console.WriteLine(new AggregateException(cssSpriteExceptions.Cast <Exception>()));
                    }
                }

                if (string.IsNullOrEmpty(jsOutput) && string.IsNullOrEmpty(cssOutput) && !jsExceptions.Any() && !cssExceptions.Any())
                {
                    Console.WriteLine();
                    Console.WriteLine("Skipping css/js crushed content creation. \"{0}\" section name not found in \"{1}\"", crusherSectionName, configPath);
                }
                else
                {
                    Console.WriteLine();
                    Console.WriteLine(cssOutput);
                    if (cssExceptions.Any())
                    {
                        Console.WriteLine("Css errors:");
                        Console.WriteLine(new AggregateException(cssExceptions.Cast <Exception>()));
                    }

                    Console.WriteLine();
                    Console.WriteLine(jsOutput);
                    if (jsExceptions.Any())
                    {
                        Console.WriteLine("Js errors:");
                        Console.WriteLine(new AggregateException(jsExceptions.Cast <Exception>()));
                    }
                }
            }
            catch (Exception exception)
            {
                Console.Write(exception);
                return(ErrorExitCode);
            }

            return(SuccessExitCode);
        }
예제 #13
0
        public object Clone()
        {
            var cssSpriteConfiguration = GetCssSpriteSection(_configPath, CssSpriteSectionName);
            var crusherConfiguration   = GetCrusherSection(_configPath, CrusherSectionName);

            var configUri = new Uri(_configPath, UriKind.RelativeOrAbsolute);

            if (!configUri.IsAbsoluteUri)
            {
                configUri = new Uri(Path.Combine(Environment.CurrentDirectory, configUri.ToString()));
            }

            var physicalApplicationPath = new FileInfo(configUri.LocalPath).DirectoryName;

            var retryableFileOpener = new RetryableFileOpener();
            var hasher = new Hasher(retryableFileOpener);
            var retryableFileWriter = new RetryableFileWriter(BufferSize, Encoding, retryableFileOpener, hasher);
            var pathProvider        = new PathProvider(_applicationPath, physicalApplicationPath);
            var cacheManager        = new HttpCacheManager();

            var cssSpriteOutput = string.Empty;
            var jsOutput        = string.Empty;
            var cssOutput       = string.Empty;

            var countdownEvents = new CountdownEvent(1);

            ThreadPool.QueueUserWorkItem(data =>
            {
                var countdownEvent = (CountdownEvent)data;

                try
                {
                    if (cssSpriteConfiguration != null)
                    {
                        var cssSpriteGroups          = cssSpriteConfiguration.CssSpriteGroups;
                        var cssSpriteCreator         = new CssSpriteCreator(cacheManager, retryableFileOpener, pathProvider, retryableFileWriter);
                        var cssSpriteGroupsProcessor = new CssSpriteGroupsProcessor();

                        cssSpriteOutput = cssSpriteGroupsProcessor.ProcessGroups(pathProvider, cssSpriteCreator, cssSpriteGroups).ToString();

                        _logMessage(cssSpriteOutput);
                    }
                }
                catch (Exception exception)
                {
                    _logError(exception.ToString());
                }
                countdownEvent.Signal();
            }, countdownEvents);

            countdownEvents.Wait();

            countdownEvents = new CountdownEvent(2);

            ThreadPool.QueueUserWorkItem(data =>
            {
                var countdownEvent = (CountdownEvent)data;

                try
                {
                    if (crusherConfiguration != null)
                    {
                        var jsCrusher = new JsCrusher(cacheManager, pathProvider, retryableFileOpener,
                                                      retryableFileWriter);
                        var jsGroups          = crusherConfiguration.JsGroups;
                        var jsGroupsProcessor = new JsGroupsProcessor();

                        jsOutput = jsGroupsProcessor.ProcessGroups(pathProvider, jsCrusher, jsGroups).ToString();

                        _logMessage(jsOutput);
                    }
                }
                catch (Exception exception)
                {
                    _logError(exception.ToString());
                }
                countdownEvent.Signal();
            }, countdownEvents);

            ThreadPool.QueueUserWorkItem(data =>
            {
                var countdownEvent = (CountdownEvent)data;

                try
                {
                    if (crusherConfiguration != null)
                    {
                        var hashQueryStringKeyName = crusherConfiguration.QuerystringKeyName;
                        var cssAssetsFileHasher    = new CssAssetsFileHasher(hashQueryStringKeyName, hasher,
                                                                             pathProvider);
                        var cssPathRewriter = new CssPathRewriter(cssAssetsFileHasher, pathProvider);
                        var cssCrusher      = new CssCrusher(cacheManager, pathProvider, retryableFileOpener,
                                                             retryableFileWriter, cssPathRewriter);
                        var cssGroups        = crusherConfiguration.CssGroups;
                        var cssGroupsCrusher = new CssGroupsProcessor();
                        cssOutput            = cssGroupsCrusher.ProcessGroups(pathProvider, cssCrusher, cssGroups).ToString();

                        _logMessage(cssOutput);
                    }
                }
                catch (Exception exception)
                {
                    _logError(exception.ToString());
                }
                countdownEvent.Signal();
            },
                                         countdownEvents);

            countdownEvents.Wait();

            return(null);
        }