Skip to content

Commit

Permalink
Merge pull request #610 from am11/master
Browse files Browse the repository at this point in the history
Misc. bug fixes
  • Loading branch information
madskristensen committed Feb 7, 2014
2 parents 137009c + c673ea9 commit 7eb19bb
Show file tree
Hide file tree
Showing 19 changed files with 93 additions and 62 deletions.
Binary file modified CssSorter.dll
Binary file not shown.
16 changes: 1 addition & 15 deletions EditorExtensions/Commands/Code/IntellisenseWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static void Write(IEnumerable<IntellisenseObject> objects, string file)
else
WriteJavaScript(objects, sb);

WriteFileToDisk(file, sb);
File.WriteAllText(file, sb.ToString());
}

private static string CamelCasePropertyName(string name)
Expand Down Expand Up @@ -158,19 +158,5 @@ private static void WriteTSInterfaceDefinition(StringBuilder sb, string prefix,

sb.Append(prefix).Append("}");
}

private static void WriteFileToDisk(string fileName, StringBuilder sb)
{
//string current = string.Empty;
//if (File.Exists(fileName))
//{
// current = File.ReadAllText(fileName);
//}

//if (current != sb.ToString())
//{
File.WriteAllText(fileName, sb.ToString(), Encoding.UTF8);
//}
}
}
}
2 changes: 1 addition & 1 deletion EditorExtensions/Commands/IFileSaveListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace MadsKristensen.EditorExtensions.Commands
///<summary>A listener called when files are saved in the editor, as well as for compiled files on save or build.</summary>
public interface IFileSaveListener
{
void FileSaved(IContentType contentType, string path, bool forceSave);
void FileSaved(IContentType contentType, string path, bool forceSave, bool minifyInPlace);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace MadsKristensen.EditorExtensions
{
internal class MinifySelection : CommandTargetBase<MinifyCommandId>
{

public MinifySelection(IVsTextView adapter, IWpfTextView textView)
: base(adapter, textView, MinifyCommandId.MinifySelection)
{
Expand Down
4 changes: 1 addition & 3 deletions EditorExtensions/Commands/Shared/PasteImageCommandTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ protected override bool Execute(VSConstants.VSStd97CmdID commandId, uint nCmdexe
{
IDataObject data = Clipboard.GetDataObject();

// This is to check if the image is text copied from PowerPoint etc.
bool trueBitmap = data.GetFormats().Contains("DeviceIndependentBitmap");
bool hasBitmap = data.GetDataPresent("System.Drawing.Bitmap") || data.GetDataPresent(DataFormats.FileDrop);

if (!hasBitmap || !trueBitmap || !IsValidTextBuffer())
if (!hasBitmap || !IsValidTextBuffer())
return false;

string fileName = null;
Expand Down
2 changes: 1 addition & 1 deletion EditorExtensions/Commands/Shared/TextCreationListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void VsTextViewCreated(IVsTextView textViewAdapter)
return;

foreach (var listener in saveListeners)
listener.FileSaved(document.TextBuffer.ContentType, e.FilePath, false);
listener.FileSaved(document.TextBuffer.ContentType, e.FilePath, false, false);
};

document.FileActionOccurred += saveHandler;
Expand Down
11 changes: 8 additions & 3 deletions EditorExtensions/Compilers/CompilerRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,15 @@ public static bool ShouldCompile(string sourcePath)
///<summary>Gets the default save location for the compiled results of the specified file, based on user settings.</summary>
public string GetTargetPath(string sourcePath)
{
var ext = TargetExtension;

if (Settings.MinifyInPlace && WESettings.Instance.ForContentType<IMinifierSettings>(TargetContentType).AutoMinify)
ext = ".min" + ext;

if (string.IsNullOrEmpty(Settings.OutputDirectory))
return Path.ChangeExtension(sourcePath, TargetExtension);
return Path.ChangeExtension(sourcePath, ext);

string compiledFileName = Path.GetFileName(Path.ChangeExtension(sourcePath, TargetExtension));
string compiledFileName = Path.GetFileName(Path.ChangeExtension(sourcePath, ext));
string sourceDir = Path.GetDirectoryName(sourcePath);

// If the output path is not project-relative, combine it directly.
Expand Down Expand Up @@ -112,7 +117,7 @@ public async Task<CompilerResult> CompileAsync(string sourcePath, string targetP
ProjectHelpers.AddFileToProject(targetPath, targetPath + ".map");

foreach (var listener in _listeners)
listener.FileSaved(TargetContentType, result.TargetFileName, true);
listener.FileSaved(TargetContentType, result.TargetFileName, true, Settings.MinifyInPlace);
}

return result;
Expand Down
1 change: 0 additions & 1 deletion EditorExtensions/Compilers/EditorCompilerInvoker.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.IO;
using System.Linq;
using EnvDTE;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Text;
Expand Down
3 changes: 1 addition & 2 deletions EditorExtensions/Compilers/LESS/LessCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ public class LessCompiler : CssCompilerBase

public override string TargetExtension { get { return ".css"; } }
public override string ServiceName { get { return "LESS"; } }

protected override string CompilerPath { get { return _compilerPath; } }
protected override Regex ErrorParsingPattern { get { return _errorParsingPattern; } }

public override bool GenerateSourceMap { get { return WESettings.Instance.Less.GenerateSourceMaps; } }

protected override string GetArguments(string sourceFileName, string targetFileName)
{
var args = new StringBuilder("--no-color --relative-urls ");
Expand Down
11 changes: 8 additions & 3 deletions EditorExtensions/Compilers/NodeExecutorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public async Task<CompilerResult> CompileAsync(string sourceFileName, string tar
if (RequireMatchingFileName && Path.GetFileName(targetFileName) != Path.GetFileNameWithoutExtension(sourceFileName) + TargetExtension)
throw new ArgumentException(ServiceName + " cannot compile to a targetFileName with a different name. Only the containing directory can be different.", "targetFileName");


var scriptArgs = GetArguments(sourceFileName, targetFileName);

var errorOutputFile = Path.GetTempFileName();
Expand All @@ -51,8 +50,8 @@ public async Task<CompilerResult> CompileAsync(string sourceFileName, string tar

try
{

ProjectHelpers.CheckOutFileFromSourceControl(targetFileName);

if (GenerateSourceMap)
ProjectHelpers.CheckOutFileFromSourceControl(targetFileName + ".map");

Expand Down Expand Up @@ -80,7 +79,13 @@ private CompilerResult ProcessResult(Process process, string errorText, string s

if (result.IsSuccess)
{
result.Result = PostProcessResult(result.Result, sourceFileName, targetFileName);
var renewedResult = PostProcessResult(result.Result, sourceFileName, targetFileName);

if (!ReferenceEquals(result.Result, renewedResult))
{
File.WriteAllText(targetFileName, renewedResult);
result.Result = renewedResult;
}
}
else
{
Expand Down
3 changes: 1 addition & 2 deletions EditorExtensions/Compilers/SASS/SassCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ public class SassCompiler : CssCompilerBase

public override string ServiceName { get { return "SASS"; } }
public override string TargetExtension { get { return ".css"; } }

protected override string CompilerPath { get { return _compilerPath; } }
protected override Regex ErrorParsingPattern { get { return _errorParsingPattern; } }

public override bool GenerateSourceMap { get { return WESettings.Instance.Sass.GenerateSourceMaps; } }

protected override string GetArguments(string sourceFileName, string targetFileName)
{
var args = new StringBuilder();
Expand Down
3 changes: 2 additions & 1 deletion EditorExtensions/Compilers/TypeScriptCompilationNotifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,9 @@ private void FileTouched(object sender, FileSystemEventArgs e)
if (File.Exists(TargetFilePath))
{
RaiseReady();

foreach (var listener in _listeners)
listener.FileSaved(ContentTypeManager.GetContentType("JavaScript"), TargetFilePath, false);
listener.FileSaved(ContentTypeManager.GetContentType("JavaScript"), TargetFilePath, false, false);
}
_isReady = false;
}
Expand Down
2 changes: 1 addition & 1 deletion EditorExtensions/DropTargets/HtmlImageDrop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public IDropHandler GetAssociatedDropHandler(IWpfTextView wpfTextView)

public class HtmlImageDropHandler : IDropHandler
{
const string HtmlTemplate = "<IMG src=\"{2}\" alt=\"\" />";
const string HtmlTemplate = "<img src=\"{2}\" alt=\"\" />";
const string MarkdownTemplate = "![{0}]({1})";
static readonly HashSet<string> _imageExtensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { ".jpg", ".jpeg", ".bmp", ".png", ".gif", ".svg", ".tif", ".tiff" };

Expand Down
28 changes: 21 additions & 7 deletions EditorExtensions/Helpers/ProjectHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using EnvDTE;
using EnvDTE80;
using Microsoft.CSS.Core;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.ComponentModelHost;
using Microsoft.VisualStudio.Editor;
Expand Down Expand Up @@ -439,8 +438,9 @@ internal static ProjectItem GetProjectItem(string fileName)

public static ProjectItem AddFileToProject(string parentFileName, string fileName)
{
if (!File.Exists(fileName))
if (Path.GetFullPath(parentFileName) == Path.GetFullPath(fileName) || !File.Exists(fileName))
return null;

fileName = Path.GetFullPath(fileName); // WAP projects don't like paths with forward slashes

var item = ProjectHelpers.GetProjectItem(parentFileName);
Expand All @@ -459,9 +459,15 @@ public static ProjectItem AddFileToProject(string parentFileName, string fileNam
if (item == null)
return null;

var addedItem = item.ProjectItems.AddFromFile(fileName);
ProjectItem addedItem = null;

try
{
addedItem = item.ProjectItems.AddFromFile(fileName);
}
catch (COMException) { }

if (Path.GetDirectoryName(parentFileName) == Path.GetDirectoryName(fileName))
if (addedItem != null && Path.GetDirectoryName(parentFileName) == Path.GetDirectoryName(fileName))
{
// create nesting
var dependentUponProperty = addedItem.Properties.Item("DependentUpon");
Expand All @@ -472,11 +478,19 @@ public static ProjectItem AddFileToProject(string parentFileName, string fileNam
}
else if (item.ContainingProject.GetType().Name != "OAProject" && item.ProjectItems != null && Path.GetDirectoryName(parentFileName) == Path.GetDirectoryName(fileName))
{ // WAP
return item.ProjectItems.AddFromFile(fileName);
try
{
return item.ProjectItems.AddFromFile(fileName);
}
catch (COMException) { }
}
else if (Path.GetFullPath(fileName).StartsWith(GetRootFolder(item.ContainingProject), StringComparison.OrdinalIgnoreCase))
{ // Website
return item.ContainingProject.ProjectItems.AddFromFile(fileName);
try
{
return item.ContainingProject.ProjectItems.AddFromFile(fileName);
}
catch (COMException) { }
}

return null;
Expand Down
1 change: 1 addition & 0 deletions EditorExtensions/MenuItems/MinifyFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ private void Execute()
{
MinificationService.CreateMinFile(ContentType, file);
}

CheckEnableSync();
}

Expand Down
6 changes: 6 additions & 0 deletions EditorExtensions/Optimization/Minification/IFileMinifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public override string MinifyString(string source)

var minifier = new HtmlMinifier(settings);
MarkupMinificationResult result = minifier.Minify(source, generateStatistics: true);

if (result.Errors.Count == 0)
{
EditorExtensionsPackage.DTE.StatusBar.Text = "Web Essentials: HTML minified by " + result.Statistics.SavedInPercent + "%";
Expand All @@ -83,6 +84,7 @@ public class CssFileMinifer : InMemoryMinifier
public override string MinifyString(string source)
{
Minifier minifier = new Minifier();

var settings = new Microsoft.Ajax.Utilities.CssSettings
{
CommentMode = WESettings.Instance.General.KeepImportantComments ? CssComment.Hacks : CssComment.Important
Expand All @@ -97,6 +99,7 @@ public override string MinifyString(string source)
public class JavaScriptFileMinifer : InMemoryMinifier
{
public override bool GenerateSourceMap { get { return WESettings.Instance.JavaScript.GenerateSourceMaps; } }

static CodeSettings CreateSettings()
{
return new CodeSettings()
Expand All @@ -106,6 +109,7 @@ static CodeSettings CreateSettings()
PreserveImportantComments = WESettings.Instance.General.KeepImportantComments
};
}

public override string MinifyString(string source)
{
return new Minifier().MinifyJavaScript(source, CreateSettings());
Expand All @@ -118,6 +122,7 @@ public override bool MinifyFile(string sourcePath, string targetPath)
else
return base.MinifyFile(sourcePath, targetPath);
}

private static bool MinifyFileWithSourceMap(string file, string minFile)
{
string mapPath = minFile + ".map";
Expand All @@ -137,6 +142,7 @@ private static bool MinifyFileWithSourceMap(string file, string minFile)
return result;
}
}

private static bool MinifyFile(string file, string minFile, CodeSettings settings)
{
Minifier minifier = new Minifier();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,40 @@ namespace MadsKristensen.EditorExtensions.Optimization.Minification
[ContentType("JavaScript")]
class MinificationSaveListener : IFileSaveListener
{
public void FileSaved(IContentType contentType, string path, bool forceSave)
public void FileSaved(IContentType contentType, string path, bool forceSave, bool minifyInPlace)
{
// This will also be called for derived ContentTypes like LESS & Markdown. Ignore those.
var settings = WESettings.Instance.ForContentType<IMinifierSettings>(contentType);

if (settings == null || !settings.AutoMinify)
return;
ReMinify(contentType, path, forceSave, settings);

if (minifyInPlace)
MinifyFile(contentType, path, path, settings);
else
ReMinify(contentType, path, forceSave, settings);
}

///<summary>Minifies an existing file if it should be minified.</summary>
public void ReMinify(IContentType contentType, string path, bool forceSave, IMinifierSettings settings = null)
{
// Don't minify ".min" files
if (!ShouldMinify(path))
return;

if (!forceSave && !File.Exists(GetMinFileName(path)))
string minPath = GetMinFileName(path);

if (!forceSave && !File.Exists(minPath))
return;

MinifyFile(contentType, path, settings ?? WESettings.Instance.ForContentType<IMinifierSettings>(contentType));
MinifyFile(contentType, path, minPath, settings ?? WESettings.Instance.ForContentType<IMinifierSettings>(contentType));
}

public static string GetMinFileName(string path)
{
return path.Insert(path.Length - Path.GetExtension(path).Length, ".min");
}

public static bool ShouldMinify(string path)
{
var baseName = Path.GetFileNameWithoutExtension(path);
Expand All @@ -47,36 +57,22 @@ public static bool ShouldMinify(string path)
public void CreateMinFile(IContentType contentType, string sourcePath)
{
var settings = WESettings.Instance.ForContentType<IMinifierSettings>(contentType);
MinifyFile(contentType, sourcePath, settings);

var minPath = GetMinFileName(sourcePath);

ProjectHelpers.AddFileToProject(sourcePath, minPath);

if (File.Exists(minPath + ".map"))
{
string mapPath = minPath + ".map";
ProjectHelpers.AddFileToProject(minPath, mapPath);
}
MinifyFile(contentType, sourcePath, minPath, settings);

if (settings.GzipMinifiedFiles)
ProjectHelpers.AddFileToProject(minPath, minPath + ".gzip");
}

private static void MinifyFile(IContentType contentType, string sourcePath, IMinifierSettings settings)
private static void MinifyFile(IContentType contentType, string sourcePath, string minPath, IMinifierSettings settings)
{
IFileMinifier minifier = Mef.GetImport<IFileMinifier>(contentType);
string minPath = GetMinFileName(sourcePath);
bool minExist = File.Exists(minPath);
bool changed = minifier.MinifyFile(sourcePath, minPath);

if (!minExist)
ProjectHelpers.AddFileToProject(sourcePath, minPath);

if (settings.GzipMinifiedFiles && (changed || !File.Exists(minPath + ".gzip")))
{
FileHelpers.GzipFile(minPath);
}
}
}
}
Loading

0 comments on commit 7eb19bb

Please sign in to comment.