madskristensen / WebEssentials2012

http://vswebessentials.com
Other
95 stars 46 forks source link

Using existing min file on Bundle file Update #76

Open wschacker opened 9 years ago

wschacker commented 9 years ago

sometimes WebEssentials maked min file has error ,now we can use existing min file on Bundle file Update,

private static void WriteBundleFile(string filePath, XmlDocument doc) { XmlNode bundleNode = doc.SelectSingleNode("//bundle");

        if (bundleNode == null)
            return;

        XmlNode outputAttr = bundleNode.Attributes["output"];

        if (outputAttr != null && (outputAttr.InnerText.Contains("/") || outputAttr.InnerText.Contains("\\")))
        {
            MessageBox.Show("The 'output' attribute is for file names only - not paths", "Web Essentials");
            return;
        }

        Dictionary<string, string> files = new Dictionary<string, string>();
        string extension = Path.GetExtension(filePath.Replace(_ext, string.Empty));
        XmlNodeList nodes = doc.SelectNodes("//file");

        foreach (XmlNode node in nodes)
        {
            string absolute = ProjectHelpers.ToAbsoluteFilePath(node.InnerText, ProjectHelpers.GetProjectFolder(filePath)).Replace("\\\\", "\\");

            if (node.InnerText.Contains(":\\") || node.InnerText.StartsWith("\\\\"))
            {
                absolute = node.InnerText;
            }

            if (File.Exists(absolute))
            {
                if (!files.ContainsKey(absolute))
                    files.Add(absolute, node.InnerText);
            }
            else
            {
                string error = string.Format("Bundle error: The file '{0}' doesn't exist", node.InnerText);
                _dte.ItemOperations.OpenFile(filePath);
                MessageBox.Show(error, "Web Essentials");
                return;
            }
        }

        string bundlePath = outputAttr != null ? Path.Combine(Path.GetDirectoryName(filePath), outputAttr.InnerText) : filePath.Replace(_ext, string.Empty);
        StringBuilder sb = new StringBuilder();

        foreach (string file in files.Keys)
        {
            if (extension.Equals(".js", StringComparison.OrdinalIgnoreCase) && WESettings.GetBoolean(WESettings.Keys.GenerateJavaScriptSourceMaps))
            {
                sb.AppendLine("///#source 1 1 " + files[file]);
            }

            sb.AppendLine(File.ReadAllText(file));
        }

        if (!File.Exists(bundlePath) || File.ReadAllText(bundlePath) != sb.ToString())
        {
            bool useBom = WESettings.GetBoolean(WESettings.Keys.UseBom);

            ProjectHelpers.CheckOutFileFromSourceControl(bundlePath);
            using (StreamWriter writer = new StreamWriter(bundlePath, false, new UTF8Encoding(useBom)))
            {
                writer.Write(sb.ToString());
                Logger.Log("Updating bundle: " + Path.GetFileName(bundlePath));
            }
            MarginBase.AddFileToProject(filePath, bundlePath);

            if (bundleNode.Attributes["minify"] != null || bundleNode.Attributes["minify"].InnerText == "true")
            {
                //  WriteMinFile(filePath, bundlePath, sb.ToString(), extension);
                //begin add  by jackie
                StringBuilder sbMin = new StringBuilder();

                string minBundlePath = bundlePath.Replace(Path.GetExtension(bundlePath), ".min" + Path.GetExtension(bundlePath));
                if (extension.Equals(".js", StringComparison.OrdinalIgnoreCase))
                {
                    CodeSettings settings = new CodeSettings()
                    {
                        EvalTreatment = EvalTreatment.MakeImmediateSafe,
                        TermSemicolons = true,
                        PreserveImportantComments = WESettings.GetBoolean(WESettings.Keys.KeepImportantComments)
                    };
                    Minifier minifier = new Minifier();
                    foreach (string file in files.Keys)
                    {
                        string minFile = file.Insert(file.Length - 2, "min.");
                        if (File.Exists(minFile))
                        {
                            sbMin.AppendLine(File.ReadAllText(minFile));
                        }
                        else
                        {
                            minifier.FileName = Path.GetFileName(file);
                            string content = minifier.MinifyJavaScript(File.ReadAllText(file), settings);
                            sbMin.AppendLine(content);
                        }
                    }

                }
                else if (extension.Equals(".css", StringComparison.OrdinalIgnoreCase))
                {
                    foreach (string file in files.Keys)
                    {
                        string minFile = file.Insert(file.Length - 3, "min.");
                        if (File.Exists(minFile))
                        {
                            sbMin.AppendLine(File.ReadAllText(minFile));
                        }
                        else
                        {
                            string minContent = MinifyFileMenu.MinifyString(extension, File.ReadAllText(file));
                            sbMin.AppendLine(minContent);
                        }
                    }
                }

                ProjectHelpers.CheckOutFileFromSourceControl(minBundlePath);

                using (StreamWriter writer = new StreamWriter(minBundlePath, false, new UTF8Encoding(useBom)))
                {
                    writer.Write(sbMin.ToString());
                    Logger.Log("Updating min bundle : " + Path.GetFileName(minBundlePath));
                }
                MarginBase.AddFileToProject(filePath, minBundlePath);
                //end add by jackie

            }
        }
    }