예제 #1
0
        static int Main(params string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("\x1B[33mUsage: BundlerMinifier <configPath> [configFile]");
                return(0);
            }

            string configPath = args[0];
            string file       = args.Length > 1 ? args[1] : null;
            var    configs    = GetConfigs(configPath, file);

            if (configs == null)
            {
                Console.WriteLine("\x1B[33mNo configurations matched");
                return(0);
            }

            BundleFileProcessor processor = new BundleFileProcessor();

            EventHookups(processor, configPath);

            try
            {
                processor.Process(configPath, configs);
                return(0);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"\x1B[33m{ex.Message}");
                return(1);
            }
        }
예제 #2
0
        private static int Run(BundleFileProcessor processor, string configPath, string file, bool isClean)
        {
            var configs = GetConfigs(configPath, file);

            if (configs == null || !configs.Any())
            {
                Console.WriteLine("No configurations matched".Orange().Bright());
                return(-1);
            }

            try
            {
                if (isClean)
                {
                    processor.Clean(configPath, configs);
                }
                else
                {
                    processor.Process(configPath, configs);
                }

                return(0);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"{ex.Message}".Red().Bright());
                return(-1);
            }
        }
예제 #3
0
        static int Main(params string[] args)
        {
            string configPath = args[0];
            string file = args.Length > 1 ? args[1] : null;
            var configs = GetConfigs(configPath, file);

            if (configs == null)
            {
                Console.WriteLine("\x1B[33mNo configurations matched");
                return 0;
            }

            BundleFileProcessor processor = new BundleFileProcessor();
            EventHookups(processor, configPath);

            try
            {
                processor.Process(configPath, configs);
                return 0;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"\x1B[33m{ex.Message}");
                return 1;
            }
        }
        public void DeleteOutputFiles(string bundleFileName)
        {
            var bundles = BundleHandler.GetBundles(bundleFileName);

            foreach (Bundle bundle in bundles)
            {
                var outputFile = bundle.GetAbsoluteOutputFile();
                var minFile    = BundleFileProcessor.GetMinFileName(outputFile);
                var mapFile    = minFile + ".map";

                if (File.Exists(outputFile))
                {
                    File.Delete(outputFile);
                }
                if (File.Exists(minFile))
                {
                    File.Delete(minFile);
                }
                if (File.Exists(mapFile))
                {
                    File.Delete(mapFile);
                }
            }

            Telemetry.TrackEvent("Delete output files");
        }
        /// <summary>
        /// Execute the Task
        /// </summary>
        public override bool Execute()
        {
            FileInfo configFile = new FileInfo(FileName);

            Log.LogMessage(MessageImportance.High, Environment.NewLine + "Bundler: Begin processing " + configFile.Name);

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

            BundleFileProcessor processor = new BundleFileProcessor();

            processor.BeforeProcess            += (s, e) => { RemoveReadonlyFlagFromFile(e.Bundle.GetAbsoluteOutputFile()); };
            processor.AfterProcess             += Processor_AfterProcess;
            processor.BeforeWritingSourceMap   += (s, e) => { RemoveReadonlyFlagFromFile(e.ResultFile); };
            processor.AfterWritingSourceMap    += Processor_AfterWritingSourceMap;
            BundleMinifier.ErrorMinifyingFile  += BundleMinifier_ErrorMinifyingFile;
            BundleMinifier.AfterWritingMinFile += FileMinifier_AfterWritingMinFile;

            processor.Process(configFile.FullName);

            Log.LogMessage(MessageImportance.High, "Bundler: Done processing " + configFile.Name);

            return(_isSuccessful);
        }
예제 #6
0
        /// <summary>
        /// Execute the Task
        /// </summary>
        public override bool Execute()
        {
            FileInfo configFile = new FileInfo(FileName);

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

            Log.LogMessage(MessageImportance.High, Environment.NewLine + "Bundler: Begin processing " + configFile.Name);
            Telemetry.SetDeviceName("MSBuild");

            BundleFileProcessor processor = new BundleFileProcessor();
            processor.Processing += (s, e) => { RemoveReadonlyFlagFromFile(e.Bundle.GetAbsoluteOutputFile()); };
            processor.AfterBundling += Processor_AfterProcess;
            BundleMinifier.BeforeWritingMinFile += (s, e) => { RemoveReadonlyFlagFromFile(e.ResultFile); };
            processor.BeforeWritingSourceMap += (s, e) => { RemoveReadonlyFlagFromFile(e.ResultFile); };
            processor.AfterWritingSourceMap += Processor_AfterWritingSourceMap;
            BundleMinifier.ErrorMinifyingFile += BundleMinifier_ErrorMinifyingFile;
            BundleMinifier.AfterWritingMinFile += FileMinifier_AfterWritingMinFile;

            processor.Process(configFile.FullName);

            Log.LogMessage(MessageImportance.High, "Bundler: Done processing " + configFile.Name);

            return _isSuccessful;
        }
예제 #7
0
        /// <summary>
        /// Execute the Task
        /// </summary>
        public override bool Execute()
        {
            try
            {
                Log.LogMessage(MessageImportance.High, $"{Environment.NewLine}BundleConfig: {FileName}");
                FileInfo configFile = new FileInfo(FileName);

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

                Log.LogMessage(MessageImportance.High, Environment.NewLine + $"Bundler: Begin processing {configFile.Name}");
                BundleFileProcessor processor = new BundleFileProcessor();
                processor.Processing                += (s, e) => { RemoveReadonlyFlagFromFile(e.Bundle.GetAbsoluteOutputFile()); };
                processor.AfterBundling             += Processor_AfterProcess;
                BundleMinifier.BeforeWritingMinFile += (s, e) => { RemoveReadonlyFlagFromFile(e.ResultFile); };
                processor.BeforeWritingSourceMap    += (s, e) => { RemoveReadonlyFlagFromFile(e.ResultFile); };
                processor.AfterWritingSourceMap     += Processor_AfterWritingSourceMap;
                BundleMinifier.ErrorMinifyingFile   += BundleMinifier_ErrorMinifyingFile;
                BundleMinifier.AfterWritingMinFile  += FileMinifier_AfterWritingMinFile;
                processor.Process(configFile.FullName);
                Log.LogMessage(MessageImportance.High, $"Bundler: Done processing {configFile.Name}");
            }
            catch (Exception e)
            {
                _isSuccessful = false;
                Log.LogErrorFromException(e);
                Log.LogErrorFromException(e.InnerException);
            }

            return(_isSuccessful);
        }
예제 #8
0
        private static void EventHookups(BundleFileProcessor processor, string configPath)
        {
            // For console colors, see http://stackoverflow.com/questions/23975735/what-is-this-u001b9-syntax-of-choosing-what-color-text-appears-on-console

            processor.Processing += (s, e) => { Console.WriteLine($"Processing \x1B[36m{e.Bundle.OutputFileName}"); FileHelpers.RemoveReadonlyFlagFromFile(e.Bundle.GetAbsoluteOutputFile()); };
            processor.AfterBundling += (s, e) => { Console.WriteLine($"  \x1B[32mBundled"); };
            processor.BeforeWritingSourceMap += (s, e) => { FileHelpers.RemoveReadonlyFlagFromFile(e.ResultFile); };
            processor.AfterWritingSourceMap += (s, e) => { Console.WriteLine($"  \x1B[32mSourcemapped"); };

            BundleMinifier.BeforeWritingMinFile += (s, e) => { FileHelpers.RemoveReadonlyFlagFromFile(e.ResultFile); };
            BundleMinifier.AfterWritingMinFile += (s, e) => { Console.WriteLine($"  \x1B[32mMinified"); };
            BundleMinifier.BeforeWritingGzipFile += (s, e) => { FileHelpers.RemoveReadonlyFlagFromFile(e.ResultFile); };
            BundleMinifier.AfterWritingGzipFile += (s, e) => { Console.WriteLine($"  \x1B[32mGZipped"); };
            BundleMinifier.ErrorMinifyingFile += (s, e) => { Console.WriteLine($"{string.Join(Environment.NewLine, e.Result.Errors)}"); };
        }
예제 #9
0
        private static void EventHookups(BundleFileProcessor processor, string configPath)
        {
            // For console colors, see http://stackoverflow.com/questions/23975735/what-is-this-u001b9-syntax-of-choosing-what-color-text-appears-on-console

            processor.Processing             += (s, e) => { Console.WriteLine($"Processing \x1B[36m{e.Bundle.OutputFileName}"); FileHelpers.RemoveReadonlyFlagFromFile(e.Bundle.GetAbsoluteOutputFile()); };
            processor.AfterBundling          += (s, e) => { Console.WriteLine($"  \x1B[32mBundled"); };
            processor.BeforeWritingSourceMap += (s, e) => { FileHelpers.RemoveReadonlyFlagFromFile(e.ResultFile); };
            processor.AfterWritingSourceMap  += (s, e) => { Console.WriteLine($"  \x1B[32mSourcemapped"); };

            BundleMinifier.BeforeWritingMinFile  += (s, e) => { FileHelpers.RemoveReadonlyFlagFromFile(e.ResultFile); };
            BundleMinifier.AfterWritingMinFile   += (s, e) => { Console.WriteLine($"  \x1B[32mMinified"); };
            BundleMinifier.BeforeWritingGzipFile += (s, e) => { FileHelpers.RemoveReadonlyFlagFromFile(e.ResultFile); };
            BundleMinifier.AfterWritingGzipFile  += (s, e) => { Console.WriteLine($"  \x1B[32mGZipped"); };
            BundleMinifier.ErrorMinifyingFile    += (s, e) => { Console.WriteLine($"{string.Join(Environment.NewLine, e.Result.Errors)}"); };
        }
예제 #10
0
        /// <summary>
        /// Execute the Task
        /// </summary>
        public override bool Execute()
        {
            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;
            }

            BundleFileProcessor processor = new BundleFileProcessor();
            processor.DeleteOutputFiles(configFile.FullName);

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

            return true;
        }
예제 #11
0
        internal static bool Configure(BundleFileProcessor processor, List <string> configurations, string configPath)
        {
            _processor = processor;

            IEnumerable <Bundle> bundles;

            if (!BundleHandler.TryGetBundles(configPath, out bundles))
            {
                return(false);
            }

            if (configurations.Count > 0)
            {
                foreach (string config in configurations)
                {
                    Bundle bundle = bundles.FirstOrDefault(x => string.Equals(x.OutputFileName, config, StringComparison.OrdinalIgnoreCase));

                    if (bundle != null)
                    {
                        ChangeHandlers.Add(new ChangeHandler(processor, configPath, bundle));
                    }
                }
            }
            else
            {
                foreach (Bundle bundle in bundles)
                {
                    ChangeHandlers.Add(new ChangeHandler(processor, configPath, bundle));
                }

                _watchingAll = true;
            }

            if (ChangeHandlers.Count > 0)
            {
                ConfigureWatcher(configPath);
            }

            return(ChangeHandlers.Count > 0);
        }
예제 #12
0
        /// <summary>
        /// Execute the Task
        /// </summary>
        public override bool Execute()
        {
            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);
            }

            Telemetry.SetDeviceName("MSBuild");

            BundleFileProcessor processor = new BundleFileProcessor();

            processor.DeleteOutputFiles(configFile.FullName);

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

            return(true);
        }
예제 #13
0
        public bool FilesChanged(FileSystemEventArgs e)
        {
            if (!IsFileValid(e.FullPath))
            {
                return(false);
            }

            if (!BundleFileProcessor.IsFileConfigured(_configFile, e.FullPath).Any())
            {
                return(false);
            }

            var inputs            = _bundle.GetAbsoluteInputFiles();
            var inputLastModified = inputs.Count > 0 ? inputs.Max(inputFile => File.GetLastWriteTimeUtc(inputFile)) : DateTime.MaxValue;

            if ((_bundle.GetAbsoluteInputFiles().Count > 1 || _bundle.InputFiles.FirstOrDefault() != _bundle.OutputFileName) &&
                inputLastModified > File.GetLastWriteTimeUtc(_bundle.GetAbsoluteOutputFile()))
            {
                return(_processor.Process(_configFile, new Bundle[] { _bundle }));
            }

            return(false);
        }
예제 #14
0
        private bool IsFileValid(string file)
        {
            string fileName = Path.GetFileName(file);

            // VS adds ~ to temp file names so let's ignore those
            if (fileName.Contains('~') || fileName.Contains(".min."))
            {
                return(false);
            }

            if (_ignorePatterns.Any(p => file.IndexOf(p) > -1))
            {
                //var fsw = (FileSystemWatcher)sender;
                //fsw.EnableRaisingEvents = false;
                return(false);
            }

            if (!BundleFileProcessor.IsSupported(file))
            {
                return(false);
            }

            return(true);
        }
예제 #15
0
        static int Main(params string[] args)
        {
            int    readConfigsUntilIndex = args.Length;
            string configPath;

            if (GetConfigFileFromArgs(args, out configPath))
            {
                --readConfigsUntilIndex;
            }

            if (configPath == null)
            {
                ShowHelp();
                return(0);
            }

            Console.WriteLine($"Bundling with configuration from {configPath}".Green().Bright());

            BundleFileProcessor processor = new BundleFileProcessor();

            EventHookups(processor, configPath);

            List <string> configurations = new List <string>();
            bool          isClean        = false;
            bool          isWatch        = false;
            bool          isNoColor      = false;
            bool          isHelp         = false;

            for (int i = 0; i < readConfigsUntilIndex; ++i)
            {
                bool currentArgIsClean   = string.Equals(args[i], "clean", StringComparison.OrdinalIgnoreCase);
                bool currentArgIsWatch   = string.Equals(args[i], "watch", StringComparison.OrdinalIgnoreCase);
                bool currentArgIsNoColor = string.Equals(args[i], "--no-color", StringComparison.OrdinalIgnoreCase);
                bool currentArgIsHelp    = string.Equals(args[i], "help", StringComparison.OrdinalIgnoreCase);
                currentArgIsHelp |= string.Equals(args[i], "-h", StringComparison.OrdinalIgnoreCase);
                currentArgIsHelp |= string.Equals(args[i], "--help", StringComparison.OrdinalIgnoreCase);
                currentArgIsHelp |= string.Equals(args[i], "help", StringComparison.OrdinalIgnoreCase);
                currentArgIsHelp |= string.Equals(args[i], "-?", StringComparison.OrdinalIgnoreCase);

                if (currentArgIsHelp)
                {
                    isHelp = true;
                    break;
                }
                else if (currentArgIsClean)
                {
                    isClean = true;
                }
                else if (currentArgIsWatch)
                {
                    isWatch = true;
                }
                else if (currentArgIsNoColor)
                {
                    isNoColor = true;
                }
                else
                {
                    configurations.Add(args[i]);
                }
            }

            if (isNoColor)
            {
                StringExtensions.NoColor = true;
            }

            if (isHelp)
            {
                ShowHelp();
                return(0);
            }

            if (isClean && isWatch)
            {
                Console.WriteLine("The clean and watch options may not be used together.".Red().Bright());
                return(-1);
            }

            if (isWatch)
            {
                bool isWatching = Watcher.Configure(processor, configurations, configPath);

                if (!isWatching)
                {
                    Console.WriteLine("No output file names were matched".Red().Bright());
                    return(-1);
                }

                Console.WriteLine("Watching... Press [Enter] to stop".LightGray().Bright());
                Console.ReadLine();
                Watcher.Stop();
                return(0);
            }

            if (configurations.Count == 0)
            {
                return(Run(processor, configPath, null, isClean));
            }

            foreach (string config in configurations)
            {
                int runResult = Run(processor, configPath, config, isClean);

                if (runResult < 0)
                {
                    return(runResult);
                }
            }

            return(0);
        }
예제 #16
0
        internal List <string> GetAbsoluteInputFiles()
        {
            List <string> files   = new List <string>();
            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 = BundleFileProcessor.GetMinFileName(output);

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

                    var matches = Minimatcher.Filter(allFiles, inputFile, options).Select(f => Path.Combine(folder, f));
                    matches = matches.Where(match => match != output && match != outputMin);
                    files.AddRange(matches.Where(f => !files.Contains(f)));
                }
                else
                {
                    string fullPath = Path.Combine(folder, inputFile).Replace("/", "\\");

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

            // 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 + "\\", ""));
                    var matches  = Minimatcher.Filter(allFiles, inputFile, options).Select(f => Path.Combine(folder, f));
                    files = matches.ToList();
                }
            }

            return(files);
        }
예제 #17
0
 public ChangeHandler(BundleFileProcessor processor, string configFile, Bundle bundle)
 {
     _processor  = processor;
     _configFile = configFile;
     _bundle     = bundle;
 }
예제 #18
0
 public void Setup()
 {
     _processor = new BundleFileProcessor();
     _guid = Guid.NewGuid();
 }