public static async void SaveClipboardImageToFile(IDataObject data, string fileName)
        {
            if (data.GetDataPresent(DataFormats.FileDrop))
            {
                string original = ((string[])data.GetData(DataFormats.FileDrop))[0];

                if (File.Exists(original))
                {
                    File.Copy(original, fileName, true);
                }
            }
            else
            {
                using (Bitmap image = (Bitmap)data.GetData("System.Drawing.Bitmap"))
                    using (MemoryStream ms = new MemoryStream())
                    {
                        image.Save(ms, GetImageFormat(Path.GetExtension(fileName)));
                        byte[] buffer = ms.ToArray();
                        File.WriteAllBytes(fileName, buffer);
                    }
            }

            ImageCompressor compressor = new ImageCompressor();
            await compressor.CompressFilesAsync(fileName).HandleErrors("compressing " + fileName);

            ProjectHelpers.AddFileToActiveProject(fileName);
        }
Exemplo n.º 2
0
        private async Task GenerateAsync(BundleDocument bundle, string extension, bool hasUpdated = false)
        {
            _dte.StatusBar.Text = "Generating bundle...";

            if (ProjectHelpers.GetProjectItem(bundle.FileName) == null)
            {
                ProjectHelpers.AddFileToActiveProject(bundle.FileName);
            }

            string bundleFile = Path.Combine(Path.GetDirectoryName(bundle.FileName), Path.GetFileNameWithoutExtension(bundle.FileName));

            if (!string.IsNullOrEmpty(bundle.OutputDirectory))
            {
                bundleFile = ProjectHelpers.GetAbsolutePathFromSettings(bundle.OutputDirectory, Path.Combine(Path.GetDirectoryName(bundle.FileName), Path.GetFileNameWithoutExtension(bundle.FileName)));
            }

            ProjectHelpers.CreateDirectoryInProject(bundleFile);

            bool hasChanged = await BundleGenerator.MakeBundle(bundle, bundleFile, UpdateBundleAsync);

            ProjectHelpers.AddFileToProject(bundle.FileName, bundleFile);

            if (!hasUpdated)
            {
                WebEssentialsPackage.DTE.ItemOperations.OpenFile(bundle.FileName);
            }

            if (bundle.Minified)
            {
                await BundleGenerator.MakeMinFile(bundleFile, extension, hasChanged);
            }

            _dte.StatusBar.Text = "Bundle generated";
        }
Exemplo n.º 3
0
        private async Task GenerateAsync(BundleDocument bundle, string extension, bool hasUpdated = false)
        {
            _dte.StatusBar.Text = "Generating bundle...";

            if (!hasUpdated)
            {
                ProjectHelpers.AddFileToActiveProject(bundle.FileName);
            }

            string bundleFile = Path.Combine(Path.GetDirectoryName(bundle.FileName), Path.GetFileNameWithoutExtension(bundle.FileName));
            bool   hasChanged = await BundleGenerator.MakeBundle(bundle, bundleFile, UpdateBundleAsync);

            if (!hasUpdated)
            {
                ProjectHelpers.AddFileToProject(bundle.FileName, bundleFile);
                EditorExtensionsPackage.DTE.ItemOperations.OpenFile(bundle.FileName);
            }

            if (bundle.Minified)
            {
                await BundleGenerator.MakeMinFile(bundleFile, extension, hasChanged);
            }

            _dte.StatusBar.Text = "Bundle generated";
        }
Exemplo n.º 4
0
        public static void GzipFile(string file, string minFile, string content)
        {
            string gzipFile = minFile + ".gzip";

            ProjectHelpers.CheckOutFileFromSourceControl(gzipFile);
            byte[] gzipContent = Compress(content);
            File.WriteAllBytes(gzipFile, gzipContent);
            ProjectHelpers.AddFileToActiveProject(gzipFile);
            MarginBase.AddFileToProject(file, gzipFile);
        }
Exemplo n.º 5
0
 private static void GzipFile(string file, string minFile, string content)
 {
     if (WESettings.GetBoolean(WESettings.Keys.CssEnableGzipping))
     {
         string gzipFile = minFile + ".gzip";
         ProjectHelpers.CheckOutFileFromSourceControl(gzipFile);
         byte[] gzipContent = CssSaveListener.Compress(content);
         File.WriteAllBytes(gzipFile, gzipContent);
         ProjectHelpers.AddFileToActiveProject(gzipFile);
         MarginBase.AddFileToProject(file, gzipFile);
     }
 }
Exemplo n.º 6
0
        protected override bool Execute(uint commandId, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            if (TextView == null)
            {
                return(false);
            }

            string content   = TextView.Selection.SelectedSpans[0].GetText();
            string extension = Path.GetExtension(_dte.ActiveDocument.FullName).ToLowerInvariant();

            if (!_possible.Contains(extension.ToUpperInvariant()))
            {
                extension = ".css";
            }

            string name = Interaction.InputBox("Specify the name of the file", "Web Essentials", "file1" + extension).Trim();

            if (!string.IsNullOrEmpty(name))
            {
                if (string.IsNullOrEmpty(Path.GetExtension(name)))
                {
                    name = name + extension;
                }

                string fileName = Path.Combine(Path.GetDirectoryName(_dte.ActiveDocument.FullName), name);

                if (!File.Exists(fileName))
                {
                    bool useBom = WESettings.GetBoolean(WESettings.Keys.UseBom);

                    _dte.UndoContext.Open("Extract to file...");
                    using (StreamWriter writer = new StreamWriter(fileName, false, new UTF8Encoding(useBom)))
                    {
                        writer.Write(content);
                    }

                    ProjectHelpers.AddFileToActiveProject(fileName);
                    TextView.TextBuffer.Delete(TextView.Selection.SelectedSpans[0].Span);
                    _dte.ItemOperations.OpenFile(fileName);

                    _dte.UndoContext.Close();
                }
                else
                {
                    MessageBox.Show("The file already exist", "Web Essentials", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }

            return(true);
        }
        protected override bool Execute(ExtractCommandId commandId, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            if (TextView == null)
            {
                return(false);
            }

            string content   = TextView.Selection.SelectedSpans[0].GetText();
            string extension = Path.GetExtension(_dte.ActiveDocument.FullName).ToLowerInvariant();

            if (!_possible.Contains(extension.ToUpperInvariant()))
            {
                extension = ".css";
            }

            string name = Interaction.InputBox("Specify the name of the file", "Web Essentials", "file1" + extension).Trim();

            if (!string.IsNullOrEmpty(name))
            {
                if (string.IsNullOrEmpty(Path.GetExtension(name)))
                {
                    name = name + extension;
                }

                string fileName = Path.Combine(Path.GetDirectoryName(_dte.ActiveDocument.FullName), name);

                if (!File.Exists(fileName))
                {
                    using (EditorExtensionsPackage.UndoContext("Extract to file..."))
                    {
                        using (StreamWriter writer = new StreamWriter(fileName, false, new UTF8Encoding(true)))
                        {
                            writer.Write(content);
                        }

                        ProjectHelpers.AddFileToActiveProject(fileName);
                        TextView.TextBuffer.Replace(TextView.Selection.SelectedSpans[0].Span, string.Format(CultureInfo.CurrentCulture, "@import \"{0}\";", name));
                        _dte.ItemOperations.OpenFile(fileName);
                    }
                }
                else
                {
                    Logger.ShowMessage("The file already exists.");
                }
            }

            return(true);
        }
Exemplo n.º 8
0
 private static bool TrySaveFile(string base64, string file)
 {
     try
     {
         int    index      = base64.IndexOf("base64,", StringComparison.Ordinal) + 7;
         byte[] imageBytes = Convert.FromBase64String(base64.Substring(index));
         File.WriteAllBytes(file, imageBytes);
         ProjectHelpers.AddFileToActiveProject(file);
         return(true);
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message, ex.GetType().Name, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
         return(false);
     }
 }
Exemplo n.º 9
0
 public static bool SaveDataUriToFile(string dataUri, string filePath)
 {
     try
     {
         int    index      = dataUri.IndexOf("base64,", StringComparison.Ordinal) + 7;
         byte[] imageBytes = Convert.FromBase64String(dataUri.Substring(index));
         File.WriteAllBytes(filePath, imageBytes);
         ProjectHelpers.AddFileToActiveProject(filePath);
         return(true);
     }
     catch (Exception ex)
     {
         Logger.ShowMessage(ex.Message, "Web Essentials " + ex.GetType().Name, MessageBoxButtons.OK, MessageBoxIcon.Error);
         return(false);
     }
 }
Exemplo n.º 10
0
        private async static Threading.Task WriteBundleRecipe(string filePath, IEnumerable <ProjectItem> files, string output)
        {
            string            projectRoot = ProjectHelpers.GetProjectFolder(files.ElementAt(0).FileNames[1]);
            StringBuilder     sb          = new StringBuilder();
            XmlWriterSettings settings    = new XmlWriterSettings();

            settings.Indent = true;

            using (XmlWriter writer = XmlWriter.Create(sb, settings))
            {
                writer.WriteStartElement("bundle");
                writer.WriteAttributeString("minify", "true");
                writer.WriteAttributeString("runOnBuild", "true");
                writer.WriteAttributeString("output", output);
                writer.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
                writer.WriteAttributeString("xsi", "noNamespaceSchemaLocation", null, "http://vswebessentials.com/schemas/v1/bundle.xsd");
                writer.WriteComment("The order of the <file> elements determines the order of the file contents when bundled.");

                foreach (ProjectItem item in files)
                {
                    string relative = item.IsLink() ? item.FileNames[1] : "/" + FileHelpers.RelativePath(projectRoot, item.FileNames[1]);
                    writer.WriteElementString("file", relative);
                }

                writer.WriteEndElement();
            }

            sb.Replace(Encoding.Unicode.WebName, Encoding.UTF8.WebName);

            ProjectHelpers.CheckOutFileFromSourceControl(filePath);
            await FileHelpers.WriteAllTextRetry(filePath, sb.ToString());

            ProjectHelpers.AddFileToActiveProject(filePath, "None");

            _dte.ItemOperations.OpenFile(filePath);

            //TODO: Use XLINQ
            XmlDocument doc = await GetXmlDocument(filePath);

            if (doc == null)
            {
                return;
            }

            await Dispatcher.CurrentDispatcher.BeginInvoke(
                new Action(() => WriteBundleFile(filePath, doc).DoNotWait("writing " + filePath + "file")), DispatcherPriority.ApplicationIdle, null);
        }
Exemplo n.º 11
0
        private async Task GenerateAsync(SpriteDocument sprite)
        {
            _dte.StatusBar.Text = "Generating sprite...";

            string imageFile;
            var    fragments = SpriteGenerator.CreateImage(sprite, out imageFile);

            ProjectHelpers.AddFileToActiveProject(sprite.FileName);
            ProjectHelpers.AddFileToProject(sprite.FileName, imageFile);

            Export(fragments, imageFile);

            if (sprite.Optimize)
            {
                await new ImageCompressor().CompressFilesAsync(imageFile);
            }

            _dte.StatusBar.Text = "Sprite generated";
        }
Exemplo n.º 12
0
        private static void WriteFile(string filePath, IEnumerable <ProjectItem> files, string extension, string output)
        {
            string            projectRoot = ProjectHelpers.GetProjectFolder(files.ElementAt(0).FileNames[1]);
            StringBuilder     sb          = new StringBuilder();
            XmlWriterSettings settings    = new XmlWriterSettings();

            settings.Indent = true;

            using (XmlWriter writer = XmlWriter.Create(sb, settings))
            {
                writer.WriteStartElement("bundle");
                writer.WriteAttributeString("minify", "true");
                writer.WriteAttributeString("runOnBuild", "true");
                writer.WriteAttributeString("output", output);
                writer.WriteComment("The order of the <file> elements determines the order of them when bundled.");

                foreach (ProjectItem item in files)
                {
                    string relative = item.IsLink() ? item.FileNames[1] : "/" + FileHelpers.RelativePath(projectRoot, item.FileNames[1]);
                    writer.WriteElementString("file", relative);
                }

                writer.WriteEndElement();
            }

            sb.Replace(Encoding.Unicode.WebName, Encoding.UTF8.WebName);

            ProjectHelpers.CheckOutFileFromSourceControl(filePath);
            File.WriteAllText(filePath, sb.ToString());
            ProjectHelpers.AddFileToActiveProject(filePath, "None");

            _dte.ItemOperations.OpenFile(filePath);

            XmlDocument doc = GetXmlDocument(filePath);

            if (doc != null)
            {
                Dispatcher.CurrentDispatcher.BeginInvoke(
                    new Action(() => WriteBundleFile(filePath, doc)), DispatcherPriority.ApplicationIdle, null);
            }
        }