Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using existing min file on Bundle file Update #76

Open
wschacker opened this issue Oct 16, 2014 · 0 comments
Open

Using existing min file on Bundle file Update #76

wschacker opened this issue Oct 16, 2014 · 0 comments

Comments

@wschacker
Copy link

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


            }
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant