Created
March 26, 2019 15:04
-
-
Save FaronBracy/2071494bd5d8a92425dbd00f1d3f67c1 to your computer and use it in GitHub Desktop.
Convert .dotsettings to .editorconfig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Xml.Linq; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
namespace DotSettingsConverter | |
{ | |
[TestClass] | |
public class DotSettingsConverterTest | |
{ | |
[TestMethod] | |
public void GenerateEditorConfig() | |
{ | |
List<DotSetting> dotSettings = new List<DotSetting>(); | |
XElement element = XElement.Load( @"present/Present.dotsettings" ); | |
foreach ( XElement child in element.Elements() ) | |
{ | |
string key = (string) child.FirstAttribute; | |
string value = child.Value; | |
dotSettings.Add( new DotSetting { Key = key, Line = child.ToString(), Value = value } ); | |
} | |
dotSettings.Sort( ( x, y ) => x.Key.CompareTo( y.Key ) ); | |
foreach ( DotSetting setting in dotSettings ) | |
{ | |
WriteEditorConfig( setting ); | |
} | |
} | |
private void WriteEditorConfig( DotSetting setting ) | |
{ | |
if ( setting.Key.Contains( "CSharpFormat" ) ) | |
{ | |
Console.WriteLine( ExtractPropertyName( setting ) ); | |
} | |
} | |
private string ExtractPropertyName( DotSetting setting ) | |
{ | |
string key = setting.Key; | |
string search = "CSharpFormat/"; | |
int indexOfFirstSlash = key.IndexOf( search ) + search.Length; | |
int indexOfLastSlash = key.IndexOf( '/', indexOfFirstSlash ); | |
string propertyName = FormatPropertyNameForEditorConfig( key.Substring( indexOfFirstSlash, indexOfLastSlash - indexOfFirstSlash ) ); | |
return $"{propertyName} = {setting.Value.ToLower()}"; | |
} | |
private string FormatPropertyNameForEditorConfig( string key ) | |
{ | |
return $"resharper_csharp_{key.ToLower()}"; | |
} | |
} | |
public class DotSetting | |
{ | |
public string Line | |
{ | |
get; | |
set; | |
} | |
public string Key | |
{ | |
get; | |
set; | |
} | |
public string Value | |
{ | |
get; | |
set; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment