コード例 #1
0
        private void ProcessBundle(string baseFolder, Bundle bundle)
        {
            BundleHandler.ProcessBundle(baseFolder, bundle);

            string outputFile = Path.Combine(baseFolder, bundle.OutputFileName);

            OnBeforeProcess(bundle, baseFolder);

            DirectoryInfo outputFileDirectory = Directory.GetParent(outputFile);

            outputFileDirectory.Create();

            File.WriteAllText(outputFile, bundle.Output, new UTF8Encoding(true));

            OnAfterProcess(bundle, baseFolder);

            if (bundle.Minify.ContainsKey("enabled") && bundle.Minify["enabled"].ToString().Equals("true", StringComparison.OrdinalIgnoreCase))
            {
                var result = BundleMinifier.MinifyBundle(bundle);

                if (result != null && bundle.SourceMaps && !string.IsNullOrEmpty(result.SourceMap))
                {
                    string minFile = FileMinifier.GetMinFileName(bundle.GetAbsoluteOutputFile());
                    string mapFile = minFile + ".map";

                    OnBeforeWritingSourceMap(minFile, mapFile);
                    File.WriteAllText(mapFile, result.SourceMap, new UTF8Encoding(true));
                    OnAfterWritingSourceMap(minFile, mapFile);
                }
            }
        }
コード例 #2
0
        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");
        }
コード例 #3
0
        public static IEnumerable <Bundle> IsFileConfigured(string configFile, string sourceFile)
        {
            List <Bundle> list = new List <Bundle>();

            try
            {
                var    configs = BundleHandler.GetBundles(configFile);
                string folder  = Path.GetDirectoryName(configFile);

                foreach (Bundle bundle in configs)
                {
                    foreach (string input in bundle.GetAbsoluteInputFiles())
                    {
                        if (input.Equals(sourceFile, StringComparison.OrdinalIgnoreCase) && !list.Contains(bundle))
                        {
                            list.Add(bundle);
                        }
                    }
                }

                return(list);
            }
            catch (Exception)
            {
                return(list);
            }
        }
コード例 #4
0
        public void Process(string fileName)
        {
            FileInfo info    = new FileInfo(fileName);
            var      bundles = BundleHandler.GetBundles(fileName);

            foreach (Bundle bundle in bundles)
            {
                ProcessBundle(info.Directory.FullName, bundle);
            }
        }
コード例 #5
0
        public void Clean(string fileName, IEnumerable <Bundle> bundles = null)
        {
            FileInfo info = new FileInfo(fileName);

            bundles = bundles ?? BundleHandler.GetBundles(fileName);

            foreach (Bundle bundle in bundles)
            {
                CleanBundle(info.Directory.FullName, bundle);
            }
        }
コード例 #6
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);
        }
コード例 #7
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);
            }
        }
コード例 #8
0
        public bool Process(string fileName, IEnumerable <Bundle> bundles = null)
        {
            FileInfo info = new FileInfo(fileName);

            bundles = bundles ?? BundleHandler.GetBundles(fileName);
            bool result = false;

            foreach (Bundle bundle in bundles)
            {
                result |= ProcessBundle(info.Directory.FullName, bundle);
            }

            return(result);
        }
コード例 #9
0
ファイル: Program.cs プロジェクト: zyj0021/BundlerMinifier
        private static bool GetConfigFileFromArgs(string[] args, out string configPath)
        {
            int index = args.Length - 1;
            IEnumerable <Bundle> bundles;
            bool fileExists     = false;
            bool fallbackExists = fileExists = File.Exists(DefaultConfigFileName);

            if (index > -1)
            {
                fileExists = File.Exists(args[index]);

                if (BundleHandler.TryGetBundles(args[index], out bundles))
                {
                    configPath = args[index];
                    return(true);
                }
            }

            if (BundleHandler.TryGetBundles(DefaultConfigFileName, out bundles))
            {
                configPath = new FileInfo(DefaultConfigFileName).FullName;
                return(false);
            }

            if (args.Length > 0)
            {
                if (!fileExists)
                {
                    Console.WriteLine($"A configuration file called {args[index]} could not be found".Red().Bright());
                }
                else
                {
                    Console.WriteLine($"Configuration file {args[index]} has errors".Red().Bright());
                }
            }

            if (!fallbackExists)
            {
                Console.WriteLine($"A configuration file called {DefaultConfigFileName} could not be found".Red().Bright());
            }
            else
            {
                Console.WriteLine($"Configuration file {DefaultConfigFileName} has errors".Red().Bright());
            }

            configPath = null;
            return(false);
        }
コード例 #10
0
        public void SourceFileChanged(string bundleFile, string sourceFile)
        {
            var    bundles          = BundleHandler.GetBundles(bundleFile);
            string bundleFileFolder = Path.GetDirectoryName(bundleFile),
                   sourceFileFolder = Path.GetDirectoryName(sourceFile);

            foreach (Bundle bundle in bundles)
            {
                foreach (string input in bundle.GetAbsoluteInputFiles())
                {
                    if (input.Equals(sourceFile, StringComparison.OrdinalIgnoreCase) || input.Equals(sourceFileFolder, StringComparison.OrdinalIgnoreCase))
                    {
                        ProcessBundle(bundleFileFolder, bundle);
                    }
                }
            }
        }
コード例 #11
0
        private void ProcessBundle(string baseFolder, Bundle bundle)
        {
            var inputLastModified = bundle.GetAbsoluteInputFiles().Max(inputFile => File.GetLastWriteTimeUtc(inputFile));

            if ((bundle.GetAbsoluteInputFiles().Count > 1 || bundle.InputFiles.FirstOrDefault() != bundle.OutputFileName) &&
                inputLastModified > File.GetLastWriteTimeUtc(bundle.GetAbsoluteOutputFile()))
            {
                BundleHandler.ProcessBundle(baseFolder, bundle);

                string outputFile      = Path.Combine(baseFolder, bundle.OutputFileName);
                bool   containsChanges = FileHelpers.HasFileContentChanged(outputFile, bundle.Output);

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

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

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

            if (bundle.Minify.ContainsKey("enabled") && bundle.Minify["enabled"].ToString().Equals("true", StringComparison.OrdinalIgnoreCase) &&
                inputLastModified > File.GetLastWriteTimeUtc(minFile))
            {
                var result = BundleMinifier.MinifyBundle(bundle);

                if (result != null && 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);
                    }
                }
            }

            Telemetry.TrackCompile(bundle);
        }
コード例 #12
0
        public void SourceFileChanged(string bundleFile, string sourceFile)
        {
            var    bundles          = BundleHandler.GetBundles(bundleFile);
            string bundleFileFolder = Path.GetDirectoryName(bundleFile),
                   sourceFileFolder = Path.GetDirectoryName(sourceFile);

            foreach (Bundle bundle in bundles)
            {
                foreach (string inputFile in bundle.InputFiles)
                {
                    string input = Path.Combine(bundleFileFolder, inputFile.Replace("/", "\\"));

                    if (input.Equals(sourceFile, System.StringComparison.OrdinalIgnoreCase) ||
                        input.Equals(sourceFileFolder, System.StringComparison.OrdinalIgnoreCase))
                    {
                        ProcessBundle(bundleFileFolder, bundle);
                    }
                }
            }
        }
コード例 #13
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);
        }
コード例 #14
0
        private static IEnumerable <Bundle> GetConfigs(string configPath, string file)
        {
            var configs = BundleHandler.GetBundles(configPath);

            if (configs == null || !configs.Any())
            {
                return(null);
            }

            if (file != null)
            {
                if (file.StartsWith("*"))
                {
                    configs = configs.Where(c => Path.GetExtension(c.OutputFileName).Equals(file.Substring(1), StringComparison.OrdinalIgnoreCase));
                }
                else
                {
                    configs = configs.Where(c => c.OutputFileName.Equals(file, StringComparison.OrdinalIgnoreCase));
                }
            }

            return(configs);
        }
コード例 #15
0
        public void DeleteOutputFiles(string bundleFileName)
        {
            var bundles = BundleHandler.GetBundles(bundleFileName);

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

                var minFile  = GetMinFileName(outputFile);
                var mapFile  = minFile + ".map";
                var gzipFile = minFile + ".gz";

                if (!inputFiles.Contains(outputFile))
                {
                    if (File.Exists(outputFile))
                    {
                        File.Delete(outputFile);
                    }
                }

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

            Telemetry.TrackEvent("Delete output files");
        }
コード例 #16
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);
        }
コード例 #17
0
        private static bool ReloadConfig()
        {
            bool anyChanges = false;
            IEnumerable <Bundle> bundles;

            if (!BundleHandler.TryGetBundles(_configPath, out bundles))
            {
                throw new Exception("Unable to load bundles.");
            }

            var oldHandlers = ChangeHandlers.ToList();

            if (!_watchingAll)
            {
                foreach (ChangeHandler handler in oldHandlers)
                {
                    Bundle bundle = bundles.FirstOrDefault(x => string.Equals(x.OutputFileName, handler.Bundle.OutputFileName, StringComparison.OrdinalIgnoreCase));

                    if (bundle != null)
                    {
                        ChangeHandler newHandler = new ChangeHandler(_processor, bundle.FileName, bundle);

                        if (!newHandler.Equals(handler))
                        {
                            ChangeHandlers.Remove(handler);
                            ChangeHandlers.Add(newHandler);
                            _processor.Process(_configPath, new[] { bundle });
                            anyChanges = true;
                        }
                    }
                    else
                    {
                        ChangeHandlers.Remove(handler);
                        Console.WriteLine($"Cannot find configuration {handler.Bundle.OutputFileName}".Orange().Bright());
                    }
                }
            }
            else
            {
                HashSet <Bundle> bundlesToProcess = new HashSet <Bundle>(bundles);

                foreach (ChangeHandler handler in oldHandlers)
                {
                    Bundle bundle = bundles.FirstOrDefault(x => string.Equals(x.OutputFileName, handler.Bundle.OutputFileName, StringComparison.OrdinalIgnoreCase));

                    if (bundle != null)
                    {
                        bundlesToProcess.Remove(bundle);
                        ChangeHandler newHandler = new ChangeHandler(_processor, bundle.FileName, bundle);

                        if (!newHandler.Equals(handler))
                        {
                            ChangeHandlers.Remove(handler);
                            ChangeHandlers.Add(newHandler);
                            _processor.Process(_configPath, new[] { bundle });
                            anyChanges = true;
                        }
                    }
                }

                foreach (Bundle bundle in bundlesToProcess)
                {
                    ChangeHandlers.Add(new ChangeHandler(_processor, _configPath, bundle));
                    _processor.Process(_configPath, new[] { bundle });
                    anyChanges = true;
                }
            }

            return(anyChanges);
        }
コード例 #18
0
        private void AddBundle(object sender, EventArgs e)
        {
            var item = ProjectHelpers.GetSelectedItems().FirstOrDefault();

            if (item == null || item.ContainingProject == null)
                return;

            string folder = item.ContainingProject.GetRootFolder();
            string configFile = Path.Combine(folder, Constants.FILENAME);
            IEnumerable<string> files = ProjectHelpers.GetSelectedItemPaths().Select(f => MakeRelative(configFile, f));
            string inputFile = item.Properties.Item("FullPath").Value.ToString();
            string outputFile = GetOutputFileName(inputFile, Path.GetExtension(files.First()));

            if (string.IsNullOrEmpty(outputFile))
                return;

            BundlerMinifierPackage._dte.StatusBar.Progress(true, "Creating bundle", 0, 3);

            string relativeOutputFile = MakeRelative(configFile, outputFile);
            Bundle bundle = CreateBundleFile(files, relativeOutputFile);

            BundleHandler bundler = new BundleHandler();
            bundler.AddBundle(configFile, bundle);

            BundlerMinifierPackage._dte.StatusBar.Progress(true, "Creating bundle", 1, 3);

            item.ContainingProject.AddFileToProject(configFile, "None");
            BundlerMinifierPackage._dte.StatusBar.Progress(true, "Creating bundle", 2, 3);

            BundlerMinifierPackage._dte.ItemOperations.OpenFile(configFile);
            BundlerMinifierPackage._dte.StatusBar.Progress(true, "Creating bundle", 3, 3);

            BundleService.Process(configFile);
            BundlerMinifierPackage._dte.StatusBar.Progress(false, "Creating bundle");
            BundlerMinifierPackage._dte.StatusBar.Text = "Bundle created";
        }
コード例 #19
0
 public void Setup()
 {
     _bundler = new BundleHandler();
     _processor = new BundleFileProcessor();
     _guid = Guid.NewGuid();
 }