예제 #1
0
        private bool ProcessBundle(string baseFolder, Bundle bundle)
        {
            OnProcessing(bundle, baseFolder);
            var  inputs  = bundle.GetAbsoluteInputFiles();
            bool changed = false;

            if (bundle.GetAbsoluteInputFiles(true).Count > 1 || bundle.InputFiles.FirstOrDefault() != bundle.OutputFileName)
            {
                BundleHandler.ProcessBundle(baseFolder, bundle);

                if (!bundle.IsMinificationEnabled || !bundle.OutputIsMinFile)
                {
                    string outputFile      = bundle.GetAbsoluteOutputFile();
                    bool   containsChanges = FileHelpers.HasFileContentChanged(outputFile, bundle.Output);

                    if (containsChanges)
                    {
                        OnBeforeBundling(bundle, baseFolder, containsChanges);
                        DirectoryInfo outputFileDirectory = Directory.GetParent(outputFile);
                        outputFileDirectory.Create();

                        File.WriteAllText(outputFile, bundle.Output, new UTF8Encoding(false));
                        OnAfterBundling(bundle, baseFolder, containsChanges);
                        changed = true;
                    }
                }
            }

            string minFile = BundleMinifier.GetMinFileName(bundle.GetAbsoluteOutputFile());

            if (bundle.IsMinificationEnabled || bundle.IsGzipEnabled)
            {
                var result = BundleMinifier.MinifyBundle(bundle);

                changed |= result.Changed;

                if (bundle.IsMinificationEnabled && bundle.SourceMap && !string.IsNullOrEmpty(result.SourceMap))
                {
                    string mapFile   = minFile + ".map";
                    bool   smChanges = FileHelpers.HasFileContentChanged(mapFile, result.SourceMap);

                    if (smChanges)
                    {
                        OnBeforeWritingSourceMap(minFile, mapFile, smChanges);
                        File.WriteAllText(mapFile, result.SourceMap, new UTF8Encoding(false));
                        OnAfterWritingSourceMap(minFile, mapFile, smChanges);
                        changed = true;
                    }
                }
            }

            return(changed);
        }
예제 #2
0
        public override bool Execute()
        {
            try
            {
                FileInfo configFile = new FileInfo(FileName);

                Log.LogMessage(MessageImportance.High, Environment.NewLine + "Bundler: Cleaning output from " + configFile.Name);

                if (!configFile.Exists)
                {
                    Log.LogWarning(configFile.FullName + " does not exist");
                    return(true);
                }

                var bundles = BundleHandler.GetBundles(configFile.FullName);

                if (bundles != null)
                {
                    foreach (Bundle bundle in bundles)
                    {
                        var outputFile = bundle.GetAbsoluteOutputFile();
                        var inputFiles = bundle.GetAbsoluteInputFiles();

                        var minFile  = BundleMinifier.GetMinFileName(outputFile, bundle.MinFileName);
                        var mapFile  = minFile + ".map";
                        var gzipFile = minFile + ".gz";

                        if (!inputFiles.Contains(outputFile))
                        {
                            Deletefile(outputFile);
                        }

                        Deletefile(minFile);
                        Deletefile(mapFile);
                        Deletefile(gzipFile);
                    }

                    Log.LogMessage(MessageImportance.High, "Bundler: Done cleaning output file from " + configFile.Name);

                    return(true);
                }

                Log.LogWarning($"There was an error reading {configFile.Name}");
                return(false);
            }
            catch (Exception e)
            {
                Log.LogErrorFromException(e);
                Log.LogErrorFromException(e.InnerException);
                return(false);
            }
        }
예제 #3
0
        private void CleanBundle(string baseFolder, Bundle bundle)
        {
            string outputFile = bundle.GetAbsoluteOutputFile();

            baseFolder = baseFolder.DemandTrailingPathSeparatorChar();
            if (!bundle.GetAbsoluteInputFiles().Contains(outputFile, StringComparer.OrdinalIgnoreCase))
            {
                if (File.Exists(outputFile))
                {
                    FileHelpers.RemoveReadonlyFlagFromFile(outputFile);
                    File.Delete(outputFile);
                    Console.WriteLine($"Deleted {FileHelpers.MakeRelative(baseFolder, outputFile).Cyan().Bright()}");
                }
            }

            string minFile = BundleMinifier.GetMinFileName(bundle.GetAbsoluteOutputFile());
            string mapFile = minFile + ".map";
            string gzFile  = minFile + ".gz";

            if (File.Exists(minFile))
            {
                FileHelpers.RemoveReadonlyFlagFromFile(minFile);
                File.Delete(minFile);
                Console.WriteLine($"Deleted {FileHelpers.MakeRelative(baseFolder, minFile).Cyan().Bright()}");
            }

            if (File.Exists(mapFile))
            {
                FileHelpers.RemoveReadonlyFlagFromFile(mapFile);
                File.Delete(mapFile);
                Console.WriteLine($"Deleted {mapFile.Cyan().Bright()}");
            }

            if (File.Exists(gzFile))
            {
                FileHelpers.RemoveReadonlyFlagFromFile(gzFile);
                File.Delete(gzFile);
                Console.WriteLine($"Deleted {gzFile.Cyan().Bright()}");
            }
        }
예제 #4
0
        private bool ProcessBundle(string baseFolder, Bundle bundle)
        {
            OnProcessing(bundle, baseFolder);
            var  inputs  = bundle.GetAbsoluteInputFiles();
            bool changed = false;

            if (bundle.GetAbsoluteInputFiles(true).Count > 1 || bundle.InputFiles.FirstOrDefault() != bundle.OutputFileName)
            {
                BundleHandler.ProcessBundle(baseFolder, bundle);

                if (!bundle.IsMinificationEnabled || !bundle.OutputIsMinFile)
                {
                    string outputFile      = bundle.GetAbsoluteOutputFile();
                    bool   containsChanges = FileHelpers.HasFileContentChanged(outputFile, bundle.Output);

                    if (containsChanges)
                    {
                        OnBeforeBundling(bundle, baseFolder, containsChanges);
                        DirectoryInfo outputFileDirectory = Directory.GetParent(outputFile);
                        outputFileDirectory.Create();

                        File.WriteAllText(outputFile, bundle.Output, new UTF8Encoding(false));
                        OnAfterBundling(bundle, baseFolder, containsChanges);
                        changed = true;
                    }
                }
            }

            MinificationResult minResult = null;
            var minFile = BundleMinifier.GetMinFileName(bundle.GetAbsoluteOutputFile());

            if (bundle.IsMinificationEnabled)
            {
                var outputWriteTime = File.GetLastWriteTimeUtc(minFile);
                var minifyChanged   = bundle.MostRecentWrite >= outputWriteTime;

                if (minifyChanged)
                {
                    minResult = BundleMinifier.MinifyBundle(bundle);

                    // If no change is detected, then the minFile is not modified, so we need to update the write time manually
                    if (!minResult.Changed && File.Exists(minFile))
                    {
                        File.SetLastWriteTimeUtc(minFile, DateTime.UtcNow);
                    }
                    changed |= minResult.Changed;

                    if (bundle.SourceMap && !string.IsNullOrEmpty(minResult.SourceMap))
                    {
                        string mapFile   = minFile + ".map";
                        bool   smChanges = FileHelpers.HasFileContentChanged(mapFile, minResult.SourceMap);

                        if (smChanges)
                        {
                            OnBeforeWritingSourceMap(minFile, mapFile, smChanges);
                            File.WriteAllText(mapFile, minResult.SourceMap, new UTF8Encoding(false));
                            OnAfterWritingSourceMap(minFile, mapFile, smChanges);
                            changed = true;
                        }
                    }
                }
                else
                {
                    OnMinificationSkipped(bundle, baseFolder, false);
                }
            }

            if (bundle.IsGzipEnabled)
            {
                var fileToGzip = bundle.IsMinificationEnabled ?
                                 minFile : bundle.GetAbsoluteOutputFile();

                if (minResult == null)
                {
                    BundleMinifier.GzipFile(fileToGzip, bundle, false, File.ReadAllText(fileToGzip));
                }
                else
                {
                    BundleMinifier.GzipFile(fileToGzip, bundle, minResult.Changed, minResult.MinifiedContent);
                }
            }

            return(changed);
        }
예제 #5
0
        /// <summary>
        /// Returns a list of absolute file paths of all matching input files.
        /// </summary>
        /// <param name="notifyOnPatternMiss">Writes to the Console if any input file is missing on disk.</param>
        public List <string> GetAbsoluteInputFiles(bool notifyOnPatternMiss = false)
        {
            List <string> files = new List <string>();

            if (!InputFiles.Any())
            {
                return(files);
            }

            string  folder  = new DirectoryInfo(Path.GetDirectoryName(FileName)).FullName;
            string  ext     = Path.GetExtension(InputFiles.First());
            Options options = new Options {
                AllowWindowsPaths = true
            };

            foreach (string inputFile in InputFiles.Where(f => !f.StartsWith("!", StringComparison.Ordinal)))
            {
                int globIndex = inputFile.IndexOf('*');

                if (globIndex > -1)
                {
                    string relative = string.Empty;
                    int    last     = inputFile.LastIndexOf('/', globIndex);

                    if (last > -1)
                    {
                        relative = inputFile.Substring(0, last + 1);
                    }

                    var output    = GetAbsoluteOutputFile();
                    var outputMin = BundleMinifier.GetMinFileName(output);

                    string searchDir = new FileInfo(Path.Combine(folder, relative).NormalizePath()).FullName;
                    var    allFiles  = Directory.EnumerateFiles(searchDir, "*" + ext, SearchOption.AllDirectories).Select(f => f.Replace(folder + FileHelpers.PathSeparatorChar, ""));

                    var matches = Minimatcher.Filter(allFiles, inputFile, options).Select(f => Path.Combine(folder, f));
                    matches = matches.Where(match => match != output && match != outputMin).ToList();

                    if (notifyOnPatternMiss && !matches.Any())
                    {
                        Console.WriteLine($"  No files matched the pattern {inputFile}".Orange().Bright());
                    }

                    files.AddRange(matches.Where(f => !files.Contains(f)));
                }
                else
                {
                    string fullPath = Path.Combine(folder, inputFile.NormalizePath());

                    if (Directory.Exists(fullPath))
                    {
                        DirectoryInfo dir       = new DirectoryInfo(fullPath);
                        SearchOption  search    = SearchOption.TopDirectoryOnly;
                        var           dirFiles  = dir.GetFiles("*" + Path.GetExtension(OutputFileName), search);
                        var           collected = dirFiles.Select(f => f.FullName).Where(f => !files.Contains(f)).ToList();

                        if (notifyOnPatternMiss && collected.Count == 0)
                        {
                            Console.WriteLine($"  No files were found in {inputFile}".Orange().Bright());
                        }

                        files.AddRange(collected);
                    }
                    else
                    {
                        files.Add(fullPath);

                        if (notifyOnPatternMiss && !File.Exists(fullPath))
                        {
                            Console.WriteLine($"  {inputFile} was not found".Orange().Bright());
                        }
                    }
                }
            }

            // Remove files starting with a !
            foreach (string inputFile in InputFiles)
            {
                int globIndex = inputFile.IndexOf('!');

                if (globIndex == 0)
                {
                    var allFiles = files.Select(f => f.Replace(folder + FileHelpers.PathSeparatorChar, ""));
                    var matches  = Minimatcher.Filter(allFiles, inputFile, options).Select(f => Path.Combine(folder, f));
                    files = matches.ToList();
                }
            }

            return(files);
        }