Esempio n. 1
0
 /// <summary>Gets the md5 hash for the current content item.</summary>
 /// <param name="context">The context.</param>
 /// <returns>The md5 hash as a string.</returns>
 internal string GetContentHash(IWebGreaseContext context)
 {
     return(this.contentHash ??
            (this.contentHash = ContentItemType == ContentItemType.Value
             ? context.GetValueHash(this.Content)
             : context.GetFileHash(this.AbsoluteContentPath)));
 }
        /// <summary>Removes duplicate images</summary>
        /// <param name="inputImages">list of input images to dedup</param>
        /// <param name="context">The webgrease context</param>
        /// <returns>deduped list of images</returns>
        private static ReadOnlyCollection <InputImage> DedupImages(ReadOnlyCollection <InputImage> inputImages, IWebGreaseContext context)
        {
            var inputImagesDeduped  = new List <InputImage>();
            var imageHashDictionary = new Dictionary <string, InputImage>();

            foreach (var inputImage in inputImages)
            {
                if (!File.Exists(inputImage.AbsoluteImagePath))
                {
                    throw new FileNotFoundException("Could not find image to sprite: {0}".InvariantFormat(inputImage.AbsoluteImagePath), inputImage.AbsoluteImagePath);
                }

                var imageHashString = context.GetFileHash(inputImage.AbsoluteImagePath) + "." + inputImage.Position;
                if (imageHashDictionary.ContainsKey(imageHashString))
                {
                    var matchingImage = imageHashDictionary[imageHashString];
                    matchingImage.DuplicateImagePaths.Add(inputImage.AbsoluteImagePath);
#if DEBUG
                    Console.WriteLine(ResourceStrings.DuplicateFoundFormat, matchingImage.OriginalImagePath, inputImage.OriginalImagePath, inputImage.Position);
#endif
                }
                else
                {
                    imageHashDictionary.Add(imageHashString, inputImage);
                    inputImagesDeduped.Add(inputImage);
                }
            }

            return(inputImagesDeduped.AsReadOnly());
        }
        private static bool HasCachedEndResultThatChanged(IWebGreaseContext context, CacheResult r)
        {
            if (r == null)
            {
                return(true);
            }

            var absoluteEndResultPath = Path.Combine(context.Configuration.DestinationDirectory, r.RelativeHashedContentPath ?? r.RelativeContentPath);

            return(!File.Exists(absoluteEndResultPath) || !r.ContentHash.Equals(context.GetFileHash(absoluteEndResultPath)));
        }
Esempio n. 4
0
        public ContentItem Process(IWebGreaseContext context, ContentItem contentItem, PreprocessingConfig preprocessingConfig, bool minimalOutput)
        {
            var settingsMinimalOutput = preprocessingConfig != null && preprocessingConfig.Element != null && (bool?)preprocessingConfig.Element.Attribute("minimalOutput") == true;
            var relativeContentPath   = contentItem.RelativeContentPath;

            context.Log.Information("Sass: Processing contents for file {0}".InvariantFormat(relativeContentPath));

            context.SectionedAction(SectionIdParts.Preprocessing, SectionIdParts.Process, "Sass")
            .Execute(
                () =>
            {
                var sassCacheImportsSection = context.Cache.CurrentCacheSection;

                string fileToProcess = null;
                var isTemp           = false;
                try
                {
                    var workingDirectory = Path.IsPathRooted(relativeContentPath)
                                                       ? Path.GetDirectoryName(relativeContentPath)
                                                       : context.GetWorkingSourceDirectory(relativeContentPath);

                    var content = ParseImports(contentItem.Content, workingDirectory, sassCacheImportsSection, minimalOutput || settingsMinimalOutput);

                    var currentContentHash = context.GetValueHash(content);

                    var contentIsUnchangedFromDisk = !string.IsNullOrWhiteSpace(contentItem.AbsoluteDiskPath) &&
                                                     File.Exists(contentItem.AbsoluteDiskPath) &&
                                                     context.GetFileHash(contentItem.AbsoluteDiskPath).Equals(currentContentHash);

                    if (contentIsUnchangedFromDisk)
                    {
                        fileToProcess = contentItem.AbsoluteDiskPath;
                    }
                    else if (!string.IsNullOrWhiteSpace(relativeContentPath))
                    {
                        fileToProcess = Path.Combine(context.Configuration.SourceDirectory ?? string.Empty, relativeContentPath);

                        fileToProcess       = PrependToExtension(fileToProcess, ".imports");
                        relativeContentPath = PrependToExtension(relativeContentPath, ".imports");

                        if (!File.Exists(fileToProcess) || !context.GetFileHash(fileToProcess).Equals(currentContentHash))
                        {
                            File.WriteAllText(fileToProcess, content);
                        }
                    }
                    else
                    {
                        isTemp        = true;
                        fileToProcess = Path.GetTempFileName() + Path.GetExtension(relativeContentPath);
                        File.WriteAllText(fileToProcess, content);
                    }

                    content = ProcessFile(fileToProcess, workingDirectory, relativeContentPath, context);

                    contentItem = content != null ? ContentItem.FromContent(content, contentItem) : null;

                    return(true);
                }
                finally
                {
                    if (isTemp && !string.IsNullOrWhiteSpace(fileToProcess))
                    {
                        try
                        {
                            File.Delete(fileToProcess);
                        }
                        catch (Exception)
                        {
                        }
                    }
                }
            });

            return(contentItem);
        }