Exemplo n.º 1
0
        /// <summary>
        /// Get the processed css of a file.
        ///
        /// Files with extension .less or .less.css will be generated with dotless.
        /// If appentHashToAssets = true, hashes of local existing files will be appending to processed css.
        /// </summary>
        /// <returns>The processed contents of a file.</returns>
        public string GetContents()
        {
            _contentsLock.EnterUpgradeableReadLock();

            try
            {
                if (_contents == null)
                {
                    _contentsLock.EnterWriteLock();
                    try
                    {
                        if (_contents == null)
                        {
                            _contents = _retryableFileOpener.ReadAllText(_fileInfo);

                            foreach (var module in _modules)
                            {
                                _contents = module.Process(_cssRootUri, _fileInfo, _contents);
                            }
                        }
                    }
                    finally
                    {
                        _contentsLock.ExitWriteLock();
                    }
                }

                return(_contents);
            }
            finally
            {
                _contentsLock.ExitUpgradeableReadLock();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Get the processed css of a file.
        ///
        /// Files with extension .less or .less.css will be generated with dotless.
        /// If appentHashToAssets = true, hashes of local existing files will be appending to processed css.
        /// </summary>
        /// <returns>The processed contents of a file.</returns>
        public string GetContents()
        {
            _lock.EnterUpgradeableReadLock();

            try
            {
                if (_contents == null)
                {
                    _lock.EnterWriteLock();
                    try
                    {
                        _contents = RetryableFileOpener.ReadAllText(FileInfo);

                        var fileName = FileInfo.Name.ToLower();

                        if (fileName.EndsWith(".less") || fileName.EndsWith(".less.css"))
                        {
                            _contents = ProcessDotLess(_contents);
                        }

                        var cssRootPathUri        = GetRootPathUri(CssRootUri);
                        var distinctRelativePaths = CssPathRewriter.FindDistinctRelativePaths(_contents);
                        _contents = CssPathRewriter.RewriteCssPathsToBeRelativeToPath(distinctRelativePaths,
                                                                                      cssRootPathUri,
                                                                                      RelativeRootUri, _contents);

                        if (AppendHashToAssets)
                        {
                            _contents = ProcessAppendHash(cssRootPathUri, _contents);
                        }
                    }
                    finally
                    {
                        _lock.ExitWriteLock();
                    }
                }

                return(_contents);
            }
            finally
            {
                _lock.ExitUpgradeableReadLock();
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Checkes if meta data is still valid against files to be crushed.
        /// </summary>
        /// <param name="outputPath"></param>
        /// <param name="filesToWatch"></param>
        /// <returns></returns>
        public virtual bool IsMetaDataFresh(FileInfo outputPath, IEnumerable <FileInfo> filesToWatch)
        {
            if (!outputPath.Exists)
            {
                return(false);
            }

            var metaDataFile = new FileInfo(outputPath.FullName + ".metadata");

            if (!metaDataFile.Exists)
            {
                return(false);
            }

            var cachedMetaData = _retryableFileOpener.ReadAllText(metaDataFile);
            var metaData       = GetaMetaData((new FileInfo[] { outputPath }).Concat(filesToWatch));

            return(cachedMetaData == metaData);
        }
Exemplo n.º 4
0
        public SingleFileMetaData(FileInfo metaDataFile, IRetryableFileOpener retryableFileOpener, IRetryableFileWriter retryableFileWriter)
        {
            _timer           = new System.Timers.Timer();
            _timer.Elapsed  += OnTimerElapsed;
            _timer.Interval  = PollTime;
            _timer.Enabled   = false;
            _timer.AutoReset = false;

            _metaDataFile        = metaDataFile;
            _retryableFileWriter = retryableFileWriter;

            if (metaDataFile.Exists)
            {
                _metaDataForFiles = retryableFileOpener.ReadAllText(_metaDataFile)
                                    .Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
                                    .ToDictionary(k => k.Split(new[] { "|" }, StringSplitOptions.RemoveEmptyEntries)[0], v => v.Split(new[] { "|" }, StringSplitOptions.RemoveEmptyEntries)[1]);
            }
            else
            {
                _metaDataForFiles = new Dictionary <string, string>();
            }
        }
Exemplo n.º 5
0
        public SingleFileMetaData(FileInfo metaDataFile, IRetryableFileOpener retryableFileOpener, IRetryableFileWriter retryableFileWriter)
        {
            _timer = new System.Timers.Timer();
            _timer.Elapsed += OnTimerElapsed;
            _timer.Interval = PollTime;
            _timer.Enabled = false;
            _timer.AutoReset = false;

            _metaDataFile = metaDataFile;
            _retryableFileWriter = retryableFileWriter;

            if (metaDataFile.Exists)
            {

                _metaDataForFiles = retryableFileOpener.ReadAllText(_metaDataFile)
                    .Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
                    .ToDictionary(k => k.Split(new[] { "|" }, StringSplitOptions.RemoveEmptyEntries)[0], v => v.Split(new[] { "|" }, StringSplitOptions.RemoveEmptyEntries)[1]);
            }
            else
            {
                _metaDataForFiles = new Dictionary<string, string>();
            }
        }
Exemplo n.º 6
0
 public string GetContents()
 {
     return(_contents ?? (_contents = RetryableFileOpener.ReadAllText(FileInfo)));
 }
Exemplo n.º 7
0
        /// <summary>
        /// Compress the js files and store them in the specified js file.
        /// </summary>
        /// <param name="outputPath">The path for the crushed js file.</param>
        /// <param name="files">The js files to be crushed.</param>
        private static void ProcessFiles(string outputPath, IEnumerable <JsFile> files)
        {
            var uncompressedContents   = new StringBuilder();
            var toBeCompressedContents = new StringBuilder();

            foreach (var file in files)
            {
                var fileInfo     = new FileInfo(HostingEnvironment.MapPath(file.FilePath));
                var fileContents = _retryableFileOpener.ReadAllText(fileInfo);

                switch (file.CompressionType)
                {
                case JsCompressionType.None:
                    uncompressedContents.AppendLine(fileContents);
                    break;

                case JsCompressionType.Min:
                    toBeCompressedContents.AppendLine(fileContents);
                    break;
                }
            }

            var uncompressedContent = uncompressedContents.ToString();
            var compressedContent   = toBeCompressedContents.ToString();

            if (!string.IsNullOrEmpty(compressedContent))
            {
                compressedContent = JavaScriptCompressor.Compress(compressedContent);
            }

            using (var writer = new MemoryStream())
            {
                var uniEncoding = Encoding.Default;
                if (!string.IsNullOrEmpty(uncompressedContent))
                {
                    writer.Write(uniEncoding.GetBytes(uncompressedContent), 0, uniEncoding.GetByteCount(uncompressedContent));
                }

                if (!string.IsNullOrEmpty(compressedContent))
                {
                    writer.Write(uniEncoding.GetBytes(compressedContent), 0, uniEncoding.GetByteCount(compressedContent));
                }

                //We might be competing with the web server for the output file, so try to overwrite it at regular intervals
                using (var outputFile = _retryableFileOpener.OpenFileStream(new FileInfo(HostingEnvironment.MapPath(outputPath)), 5, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None))
                {
                    var overwrite = true;
                    if (outputFile.Length > 0)
                    {
                        var newOutputFileHash = _hasher.CalculateMd5Etag(writer);
                        var outputFileHash    = _hasher.CalculateMd5Etag(outputFile);

                        overwrite = (newOutputFileHash != outputFileHash);
                    }

                    if (overwrite)
                    {
                        writer.Seek(0, SeekOrigin.Begin);
                        outputFile.SetLength(writer.Length); //Truncate current file
                        outputFile.Seek(0, SeekOrigin.Begin);

                        var bufferSize = Convert.ToInt32(Math.Min(writer.Length, BufferSize));
                        var buffer     = new byte[bufferSize];

                        int bytesRead;
                        while ((bytesRead = writer.Read(buffer, 0, bufferSize)) > 0)
                        {
                            outputFile.Write(buffer, 0, bytesRead);
                        }
                        outputFile.Flush();
                    }
                }
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Compress the css files and store them in the specified css file.
        /// </summary>
        /// <param name="outputPath">The path for the crushed css file.</param>
        /// <param name="files">The css files to be crushed.</param>
        private static void ProcessFiles(string outputPath, IEnumerable <CssFile> files)
        {
            outputPath = HostingEnvironment.MapPath(outputPath);
            var uncompressedContents                  = new StringBuilder();
            var toBeStockYuiCompressedContents        = new StringBuilder();
            var toBeMichaelAshRegexCompressedContents = new StringBuilder();
            var toBeHybridCompressedContents          = new StringBuilder();

            foreach (var file in files)
            {
                var fileInfo     = new FileInfo(HostingEnvironment.MapPath(file.FilePath));
                var fileContents = _retryableFileOpener.ReadAllText(fileInfo);
                var fileName     = fileInfo.Name.ToLower();

                if (fileName.EndsWith(".less") || fileName.EndsWith(".less.css"))
                {
                    fileContents = ProcessDotLess(fileContents);
                }

                fileContents = _cssPathRewriter.RewriteCssPaths(outputPath, fileInfo.FullName, fileContents);

                switch (file.CompressionType)
                {
                case CssCompressionType.None:
                    uncompressedContents.AppendLine(fileContents);
                    break;

                case CssCompressionType.StockYuiCompressor:
                    toBeStockYuiCompressedContents.AppendLine(fileContents);
                    break;

                case CssCompressionType.MichaelAshRegexEnhancements:
                    toBeMichaelAshRegexCompressedContents.AppendLine(fileContents);
                    break;

                case CssCompressionType.Hybrid:
                    toBeHybridCompressedContents.AppendLine(fileContents);
                    break;
                }
            }

            var uncompressedContent = uncompressedContents.ToString();
            var stockYuiCompressedCompressedContent = toBeStockYuiCompressedContents.ToString();
            var michaelAshRegexCompressedContent    = toBeMichaelAshRegexCompressedContents.ToString();
            var hybridCompressedContent             = toBeHybridCompressedContents.ToString();

            if (!string.IsNullOrEmpty(stockYuiCompressedCompressedContent))
            {
                stockYuiCompressedCompressedContent = CssCompressor.Compress(stockYuiCompressedCompressedContent, 0, Yahoo.Yui.Compressor.CssCompressionType.StockYuiCompressor);
            }

            if (!string.IsNullOrEmpty(michaelAshRegexCompressedContent))
            {
                michaelAshRegexCompressedContent = CssCompressor.Compress(michaelAshRegexCompressedContent, 0, Yahoo.Yui.Compressor.CssCompressionType.MichaelAshRegexEnhancements);
            }

            if (!string.IsNullOrEmpty(hybridCompressedContent))
            {
                hybridCompressedContent = CssCompressor.Compress(hybridCompressedContent, 0, Yahoo.Yui.Compressor.CssCompressionType.Hybrid);
            }

            using (var writer = new MemoryStream())
            {
                var uniEncoding = Encoding.Default;
                if (!string.IsNullOrEmpty(uncompressedContent))
                {
                    writer.Write(uniEncoding.GetBytes(uncompressedContent), 0, uniEncoding.GetByteCount(uncompressedContent));
                }

                if (!string.IsNullOrEmpty(stockYuiCompressedCompressedContent))
                {
                    writer.Write(uniEncoding.GetBytes(stockYuiCompressedCompressedContent), 0, uniEncoding.GetByteCount(stockYuiCompressedCompressedContent));
                }

                if (!string.IsNullOrEmpty(michaelAshRegexCompressedContent))
                {
                    writer.Write(uniEncoding.GetBytes(michaelAshRegexCompressedContent), 0, uniEncoding.GetByteCount(michaelAshRegexCompressedContent));
                }

                if (!string.IsNullOrEmpty(hybridCompressedContent))
                {
                    writer.Write(uniEncoding.GetBytes(hybridCompressedContent), 0, uniEncoding.GetByteCount(hybridCompressedContent));
                }

                //We might be competing with the web server for the output file, so try to overwrite it at regular intervals
                using (var outputFile = _retryableFileOpener.OpenFileStream(new FileInfo(outputPath), 5, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None))
                {
                    var overwrite = true;
                    if (outputFile.Length > 0)
                    {
                        var newOutputFileHash = _hasher.CalculateMd5Etag(writer);
                        var outputFileHash    = _hasher.CalculateMd5Etag(outputFile);

                        overwrite = (newOutputFileHash != outputFileHash);
                    }

                    if (overwrite)
                    {
                        writer.Seek(0, SeekOrigin.Begin);
                        outputFile.SetLength(writer.Length); //Truncate current file
                        outputFile.Seek(0, SeekOrigin.Begin);

                        var bufferSize = Convert.ToInt32(Math.Min(writer.Length, BufferSize));
                        var buffer     = new byte[bufferSize];

                        int bytesRead;
                        while ((bytesRead = writer.Read(buffer, 0, bufferSize)) > 0)
                        {
                            outputFile.Write(buffer, 0, bytesRead);
                        }
                        outputFile.Flush();
                    }
                }
            }
        }
Exemplo n.º 9
0
        public bool IsAnonymousAmdModule(FileInfo fileInfo)
        {
            var fileContent = _retryableFileOpener.ReadAllText(fileInfo);

            return(AnonymousAmdModuleRegex.IsMatch(fileContent));
        }