Skip to content

Commit

Permalink
Settings: Import legacy settings file if necessary
Browse files Browse the repository at this point in the history
Also fix some property names in the new settings classes
  • Loading branch information
SLaks committed Jan 13, 2014
1 parent c6954bc commit b089f3e
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 137 deletions.
2 changes: 1 addition & 1 deletion EditorExtensions/EditorExtensionsPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ protected override void Initialize()
// Hook up event handlers
Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() => {
DTE.Events.BuildEvents.OnBuildDone += BuildEvents_OnBuildDone;
DTE.Events.SolutionEvents.Opened += delegate { SettingsStore.Load(); SettingsStore.UpdateStatusBar("applied"); ShowTopMenu(); };
DTE.Events.SolutionEvents.Opened += delegate { SettingsStore.Load(); ShowTopMenu(); };
DTE.Events.SolutionEvents.AfterClosing += delegate { DTE.StatusBar.Clear(); ShowTopMenu(); };

}), DispatcherPriority.ApplicationIdle, null);
Expand Down
197 changes: 87 additions & 110 deletions EditorExtensions/Settings/LegacySettings.cs
Original file line number Diff line number Diff line change
@@ -1,155 +1,132 @@

using System.Xml.Linq;

namespace MadsKristensen.EditorExtensions
{
partial class WESettings
///<summary>Migrates settings from legacy XML settings files to the new ConfOxide-based settings objects.</summary>
public class SettingsMigrator
{
public static class Keys
readonly XDocument sourceFile;
readonly XElement settingsElement;
public SettingsMigrator(string sourcePath) : this(XDocument.Load(sourcePath)) { }
public SettingsMigrator(XDocument source)
{
sourceFile = source;
settingsElement = source.Root.Element("settings");
}

public void ApplyTo(WESettings target)
{
public const string JavaScriptCamelCasePropertyNames = "JavaScriptCamelCasePropertyNames";
public const string JavaScriptCamelCaseClassNames = "JavaScriptCamelCaseClassNames";
target.CodeGen.CamelCasePropertyNames = GetBoolean("JavaScriptCamelCasePropertyNames");
target.CodeGen.CamelCaseTypeNames = GetBoolean("JavaScriptCamelCaseClassNames");
// General
public const string KeepImportantComments = "KeepImportantComments";
public const string AllMessagesToOutputWindow = "AllMessagesToOutputWindow";
target.General.KeepImportantComments = GetBoolean("KeepImportantComments");
target.General.AllMessagesToOutputWindow = GetBoolean("AllMessagesToOutputWindow");

// HTML
public const string EnableEnterFormat = "EnableEnterFormat";
public const string EnableAngularValidation = "EnableAngularValidation";
public const string EnableHtmlMinification = "HtmlEnableMinification";
target.Html.EnableEnterFormat = GetBoolean("EnableEnterFormat");
target.Html.EnableAngularValidation = GetBoolean("EnableAngularValidation");
target.Html.MinifyOnSave = GetBoolean("HtmlEnableMinification");

// LESS
public const string GenerateCssFileFromLess = "LessGenerateCssFile";
public const string ShowLessPreviewWindow = "LessShowPreviewWindow";
public const string LessMinify = "LessMinify";
public const string LessCompileOnBuild = "LessCompileOnBuild";
public const string LessSourceMaps = "LessSourceMaps";
public const string LessCompileToLocation = "LessCompileToLocation";
target.Less.CompileOnSave = GetBoolean("LessGenerateCssFile");
target.Less.ShowPreviewPane = GetBoolean("LessShowPreviewWindow");
target.Less.Minify = GetBoolean("LessMinify");
target.Less.CompileOnBuild = GetBoolean("LessCompileOnBuild");
target.Less.GenerateSourceMaps = GetBoolean("LessSourceMaps");
target.Less.OutputDirectory = GetString("LessCompileToLocation");

// SASS
public const string GenerateCssFileFromSass = "SassGenerateCssFile";
public const string ShowSassPreviewWindow = "SassShowPreviewWindow";
public const string SassMinify = "SassMinify";
public const string SassCompileOnBuild = "SassCompileOnBuild";
public const string SassSourceMaps = "SassSourceMaps";
public const string SassCompileToLocation = "SassCompileToLocation";
target.Sass.CompileOnSave = GetBoolean("SassGenerateCssFile");
target.Sass.ShowPreviewPane = GetBoolean("SassShowPreviewWindow");
target.Sass.Minify = GetBoolean("SassMinify");
target.Sass.CompileOnBuild = GetBoolean("SassCompileOnBuild");
target.Sass.GenerateSourceMaps = GetBoolean("SassSourceMaps");
target.Sass.OutputDirectory = GetString("SassCompileToLocation");

// TypeScript
public const string ShowTypeScriptPreviewWindow = "TypeScriptShowPreviewWindow";
public const string TypeScriptBraceCompletion = "TypeScriptBraceCompletion";
target.TypeScript.ShowPreviewPane = GetBoolean("TypeScriptShowPreviewWindow");
target.TypeScript.ShowPreviewPane = GetBoolean("TypeScriptBraceCompletion");

// CoffeeScript
public const string GenerateJsFileFromCoffeeScript = "CoffeeScriptGenerateJsFile";
public const string ShowCoffeeScriptPreviewWindow = "CoffeeScriptShowPreviewWindow";
public const string CoffeeScriptMinify = "CoffeeScriptMinify";
public const string WrapCoffeeScriptClosure = "CoffeeScriptWrapClosure";
public const string CoffeeScriptCompileOnBuild = "CoffeeScriptCompileOnBuild";
public const string CoffeeScriptSourceMaps = "CoffeeScriptSourceMaps";
public const string CoffeeScriptCompileToLocation = "CoffeeScriptCompileToLocation";
target.CoffeeScript.CompileOnSave = GetBoolean("CoffeeScriptGenerateJsFile");
target.CoffeeScript.ShowPreviewPane = GetBoolean("CoffeeScriptShowPreviewWindow");
target.CoffeeScript.Minify = GetBoolean("CoffeeScriptMinify");
target.CoffeeScript.WrapClosure = GetBoolean("CoffeeScriptWrapClosure");
target.CoffeeScript.CompileOnBuild = GetBoolean("CoffeeScriptCompileOnBuild");
target.CoffeeScript.GenerateSourceMaps = GetBoolean("CoffeeScriptSourceMaps");
target.CoffeeScript.OutputDirectory = GetString("CoffeeScriptCompileToLocation");

// Markdown
public const string MarkdownShowPreviewWindow = "MarkdownShowPreviewWindow";
public const string MarkdownEnableCompiler = "MarkdownEnableCompiler";
public const string MarkdownCompileToLocation = "MarkdownCompileToLocation";
target.Markdown.ShowPreviewPane = GetBoolean("MarkdownShowPreviewWindow");
target.Markdown.CompileOnSave = GetBoolean("MarkdownEnableCompiler");
target.Markdown.OutputDirectory = GetString("MarkdownCompileToLocation");

public const string MarkdownAutoHyperlinks = "MarkdownAutoHyperlinks";
public const string MarkdownLinkEmails = "MarkdownLinkEmails";
public const string MarkdownAutoNewLine = "MarkdownAutoNewLine";
public const string MarkdownGenerateXHTML = "MarkdownGenerateXHTML";
public const string MarkdownEncodeProblemUrlCharacters = "MarkdownEncodeProblemUrlCharacters";
public const string MarkdownStrictBoldItalic = "MarkdownStrictBoldItalic";
target.Markdown.AutoHyperlinks = GetBoolean("MarkdownAutoHyperlinks");
target.Markdown.LinkEmails = GetBoolean("MarkdownLinkEmails");
target.Markdown.AutoNewLines = GetBoolean("MarkdownAutoNewLine");
target.Markdown.GenerateXHTML = GetBoolean("MarkdownGenerateXHTML");
target.Markdown.EncodeProblemUrlCharacters = GetBoolean("MarkdownEncodeProblemUrlCharacters");
target.Markdown.StrictBoldItalic = GetBoolean("MarkdownStrictBoldItalic");

// SVG
public const string SvgShowPreviewWindow = "SvgShowPreviewWindow";
target.General.SvgPreviewPane = GetBoolean("SvgShowPreviewWindow");

// CSS
public const string ValidateStarSelector = "CssValidateStarSelector";
public const string ValidateOverQualifiedSelector = "CSSValidateOverQualifiedSelector";
public const string CssErrorLocation = "CssErrorLocation";
public const string ValidateEmbedImages = "CssValidateEmbedImages";
public const string ShowBrowserTooltip = "CssShowBrowserTooltip";
public const string SyncVendorValues = "CssSyncVendorValues";
public const string ShowInitialInherit = "CssShowInitialInherit";
public const string ShowUnsupported = "CssShowUnsupported";
public const string EnableCssMinification = "CssEnableMinification";
public const string ValidateZeroUnit = "CssValidateZeroUnit";
public const string ValidateVendorSpecifics = "ValidateVendorSpecifics";
public const string CssEnableGzipping = "CssEnableGzipping";
public const string CssPreserveRelativePathsOnMinify = "CssPreserveRelativePathsOnMinify";
target.Css.ValidateStarSelector = GetBoolean("CssValidateStarSelector");
target.Css.ValidateOverQualifiedSelector = GetBoolean("CSSValidateOverQualifiedSelector");
target.Css.ValidationLocation = (WarningLocation)GetInt("CssErrorLocation");
target.Css.ValidateEmbedImages = GetBoolean("CssValidateEmbedImages");
target.Css.ShowBrowserTooltip = GetBoolean("CssShowBrowserTooltip");
target.Css.SyncVendorValues = GetBoolean("CssSyncVendorValues");
target.Css.ShowInitialInherit = GetBoolean("CssShowInitialInherit");
target.Css.ShowUnsupported = GetBoolean("CssShowUnsupported");
target.Css.MinifyOnSave = GetBoolean("CssEnableMinification");
target.Css.ValidateZeroUnit = GetBoolean("CssValidateZeroUnit");
target.Css.ValidateVendorSpecifics = GetBoolean("ValidateVendorSpecifics");
target.Css.GzipMinifiedFiles = GetBoolean("CssEnableGzipping");
target.Css.AdjustRelativePaths = !GetBoolean("CssPreserveRelativePathsOnMinify");

// JavaScript
public const string EnableJsMinification = "JavaScriptEnableMinification";
public const string GenerateJavaScriptSourceMaps = "JavaScriptGenerateSourceMaps";
public const string JavaScriptEnableGzipping = "JavaScriptEnableGzipping";
public const string JavaScriptCommentCompletion = "JavaScriptCommentCompletion";
target.JavaScript.MinifyOnSave = GetBoolean("JavaScriptEnableMinification");
target.JavaScript.GenerateSourceMaps = GetBoolean("JavaScriptGenerateSourceMaps");
target.JavaScript.GzipMinifiedFiles = GetBoolean("JavaScriptEnableGzipping");
target.JavaScript.BlockCommentCompletion = GetBoolean("JavaScriptCommentCompletion");

// JSHint
public const string EnableJsHint = "JsHintEnable";
public const string RunJsHintOnBuild = "JsHintRunOnBuild";
public const string JsHintErrorLocation = "JsHintErrorLocation";
target.JavaScript.LintOnSave = GetBoolean("JsHintEnable");
target.JavaScript.LintOnBuild = GetBoolean("JsHintRunOnBuild");
target.JavaScript.LintResultLocation = (ErrorLocation)GetInt("JsHintErrorLocation");

// TSLint
public const string EnableTsLint = "TsLintEnable";
public const string RunTsLintOnBuild = "TsLintRunOnBuild";
public const string TsLintErrorLocation = "TsLintErrorLocation";
target.TypeScript.LintOnSave = GetBoolean("TsLintEnable");
target.TypeScript.LintOnBuild = GetBoolean("TsLintRunOnBuild");
target.TypeScript.LintResultLocation = (ErrorLocation)GetInt("TsLintErrorLocation");

// Browser Link
public const string UnusedCss_IgnorePatterns = "UnusedCss_IgnorePatterns";
public const string EnableBrowserLinkMenu = "EnableBrowserLinkMenu";
target.BrowserLink.IgnorePatterns = GetString("UnusedCss_IgnorePatterns");
target.BrowserLink.EnableBrowserLinkMenu = GetBoolean("EnableBrowserLinkMenu");

//Pixel Pushing mode
public const string PixelPushing_OnByDefault = "PixelPushing_OnByDefault";

public const string BrowserLink_ShowMenu = "BrowserLink_ShowMenu";

public enum ErrorLocation
{
Warnings = 0,
Messages = 1,
}

public enum FullErrorLocation
{
Errors = 0,
Warnings = 1,
Messages = 2,
}
}

public static bool GetBoolean(string propertyName)
{
bool result;
object value = Settings.GetValue(propertyName);
target.BrowserLink.EnablePixelPushing = GetBoolean("PixelPushing_OnByDefault");

if (value != null && bool.TryParse(value.ToString(), out result))
{
return result;
}
//target. = GetBoolean("BrowserLink_ShowMenu");

return false;
}

public static int GetInt(string propertyName)
bool GetBoolean(string propertyName)
{
int result;
object value = Settings.GetValue(propertyName);

if (value != null && int.TryParse(value.ToString(), out result))
{
return result;
}

return -1;
return (bool?)settingsElement.Element(propertyName) ?? false;
}

public static string GetString(string propertyName)
int GetInt(string propertyName)
{
object value = Settings.GetValue(propertyName);

if (value != null)
{
return value.ToString();
}
return (int?)settingsElement.Element(propertyName) ?? -1;
}

return null;
string GetString(string propertyName)
{
return (string)settingsElement.Element(propertyName);
}
}
}
27 changes: 16 additions & 11 deletions EditorExtensions/Settings/SettingsStore.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
using ConfOxide;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.Settings;
using Microsoft.VisualStudio.Shell.Settings;
using Keys = MadsKristensen.EditorExtensions.WESettings.Keys;

namespace MadsKristensen.EditorExtensions
{
Expand All @@ -30,7 +23,21 @@ public static bool SolutionSettingsExist
///<summary>Loads the active settings file.</summary>
public static void Load()
{
WESettings.Instance.ReadJsonFile(GetFilePath());
string jsonPath = GetFilePath();
if (!File.Exists(jsonPath))
{
var legacyPath = jsonPath.Replace(_fileName, _legacyFileName);
if (File.Exists(legacyPath))
{
new SettingsMigrator(legacyPath).ApplyTo(WESettings.Instance);
Save(jsonPath);
UpdateStatusBar("imported from legacy XML settings file");
}
return;
}

WESettings.Instance.ReadJsonFile(jsonPath);
UpdateStatusBar("applied");
}

///<summary>Saves the current settings to the active settings file.</summary>
Expand Down Expand Up @@ -59,11 +66,9 @@ public static void CreateSolutionSettings()
?? solution.AddSolutionFolder(_solutionFolder);

project.ProjectItems.AddFromFile(path);
UpdateStatusBar("applied");
UpdateStatusBar("created");
}



private static string GetFilePath()
{
string path = GetSolutionFilePath();
Expand Down
Loading

0 comments on commit b089f3e

Please sign in to comment.