/// <summary>Hash the file.</summary>
        /// <param name="contentItem">The content item.</param>
        /// <returns>The result file after the hash.</returns>
        internal ContentItem Hash(ContentItem contentItem)
        {
            var originRelativePath  = contentItem.RelativeContentPath;
            var hashedFileName      = contentItem.GetContentHash(this.context) + Path.GetExtension(originRelativePath);
            var destinationFilePath = this.GetDestinationFilePath(this.DestinationDirectory, hashedFileName, contentItem.RelativeContentPath);

            var hashedDestinationFolder = this.context.Configuration.DestinationDirectory ?? this.DestinationDirectory;

            var relativeHashedPath = destinationFilePath;

            if (!string.IsNullOrWhiteSpace(hashedDestinationFolder) && Path.IsPathRooted(relativeHashedPath))
            {
                relativeHashedPath = relativeHashedPath.MakeRelativeToDirectory(hashedDestinationFolder);
            }

            contentItem = ContentItem.FromContentItem(contentItem, null, relativeHashedPath);

            // Do not overwrite if exists, since filename is md5 hash, filename changes if content changes.
            contentItem.WriteToRelativeHashedPath(hashedDestinationFolder);

            // Append to the log
            this.AppendToWorkLog(contentItem);

            // Return it as a result file.
            return(contentItem);
        }
        /// <summary>The append to work log.</summary>
        /// <param name="hashedContentItem">The hashed content item.</param>
        /// <param name="originalFiles">The original files.</param>
        /// <returns>The hashed items.</returns>
        internal IEnumerable <ContentItem> AppendToWorkLog(ContentItem hashedContentItem, IEnumerable <string> originalFiles)
        {
            var hashedFiles = new List <ContentItem>();

            foreach (var destinationFile in originalFiles)
            {
                var alternateHashedContentItem = ContentItem.FromContentItem(hashedContentItem, destinationFile);
                this.AppendToWorkLog(alternateHashedContentItem);
                hashedFiles.Add(alternateHashedContentItem);
            }

            return(hashedFiles);
        }
        internal IEnumerable <ContentItem> Hash(ContentItem contentItem, IEnumerable <string> originalFiles)
        {
            var hashedFiles = new List <ContentItem>();

            if (originalFiles.Any())
            {
                var firstOriginalFile = originalFiles.FirstOrDefault();
                var hashedContentItem = this.Hash(ContentItem.FromContentItem(contentItem, firstOriginalFile));
                hashedFiles.Add(hashedContentItem);

                hashedFiles.AddRange(this.AppendToWorkLog(hashedContentItem, originalFiles.Skip(1)));
            }

            return(hashedFiles);
        }
Ejemplo n.º 4
0
        /// <summary>The execute method for the activity under question.</summary>
        /// <param name="resultContentItemType">The result Content Type.</param>
        /// <returns>The <see cref="ContentItem"/> or null if it failed.</returns>
        internal ContentItem Execute(ContentItemType resultContentItemType = ContentItemType.Path)
        {
            if (string.IsNullOrWhiteSpace(this.OutputFile))
            {
                throw new ArgumentException("AssemblerActivity - The output file path cannot be null or whitespace.");
            }

            var assembleType = Path.GetExtension(this.OutputFile);

            if (!string.IsNullOrWhiteSpace(assembleType))
            {
                assembleType = assembleType.Trim('.');
            }

            ContentItem contentItem = null;

            this.context.SectionedAction(SectionIdParts.AssemblerActivity, assembleType)
            .MakeCachable(new { this.Inputs, this.PreprocessingConfig, this.AddSemicolons, output = resultContentItemType == ContentItemType.Path ? this.OutputFile : null })
            .RestoreFromCacheAction(cacheSection =>
            {
                var cachedContentItem = cacheSection.GetCachedContentItem(CacheFileCategories.AssemblerResult);
                if (cachedContentItem == null)
                {
                    return(false);
                }

                contentItem = ContentItem.FromContentItem(
                    cachedContentItem,
                    Path.GetFileName(this.OutputFile));
                return(true);
            })
            .Execute(cacheSection =>
            {
                try
                {
                    // Add source inputs
                    this.Inputs.ForEach(this.context.Cache.CurrentCacheSection.AddSourceDependency);

                    // Create if the directory does not exist.
                    var outputDirectory = Path.GetDirectoryName(this.OutputFile);
                    if (resultContentItemType == ContentItemType.Path && !string.IsNullOrWhiteSpace(outputDirectory))
                    {
                        Directory.CreateDirectory(outputDirectory);
                    }

                    // set the semicolon flag to true so the first file doesn't get a semicolon added before it.
                    // IF we are interesting in adding semicolons between bundled files (eg: JavaScript), then this flag
                    // will get set after outputting each file, depending on whether or not that file ends in a semicolon.
                    // the NEXT file will look at the flag and add one if the previous one didn't end in a semicolon.
                    this.endedInSemicolon = true;

                    contentItem = this.Bundle(resultContentItemType, outputDirectory, this.OutputFile, this.context.Configuration.SourceDirectory);

                    cacheSection.AddResult(contentItem, CacheFileCategories.AssemblerResult);
                }
                catch (Exception exception)
                {
                    throw new WorkflowException("AssemblerActivity - Error happened while executing the assembler activity", exception);
                }

                return(true);
            });

            return(contentItem);
        }