Generate settings classes from a json file

This commit is contained in:
Andreia Gaita 2016-02-25 12:58:48 +01:00
Родитель 9752a1909c
Коммит 0c62477940
11 изменённых файлов: 185 добавлений и 120 удалений

Просмотреть файл

@ -95,6 +95,9 @@
<None Include="..\..\script\Key.snk" Condition="$(Buildtype) == 'Internal'">
<Link>Key.snk</Link>
</None>
<None Include="..\common\settings.json">
<Link>Properties\settings.json</Link>
</None>
<Compile Include="Collections\ICopyable.cs" />
<Compile Include="ExceptionExtensions.cs" />
<Compile Include="Extensions\VSExtensions.cs" />
@ -117,13 +120,11 @@
<Compile Include="Services\IMenuHandler.cs" />
<Compile Include="Services\IMenuProvider.cs" />
<Compile Include="Services\INotificationDispatcher.cs" />
<Compile Include="Services\IPackageSettings.cs" />
<Compile Include="Services\IStatusBarNotificationService.cs" />
<Compile Include="Services\ITeamExplorerServices.cs" />
<Compile Include="Services\ITeamExplorerServiceHolder.cs" />
<Compile Include="Services\INotificationService.cs" />
<Compile Include="Services\Logger.cs" />
<Compile Include="Services\PackageSettings.cs" />
<Compile Include="Services\Services.cs" />
<Compile Include="Services\StatusBarNotificationService.cs" />
<Compile Include="Services\TeamExplorerServices.cs" />
@ -137,6 +138,11 @@
<ItemGroup>
<Compile Include="Authentication\AuthenticationResultExtensions.cs" />
<Compile Include="Extensions\ServiceProviderExtensions.cs" />
<Compile Include="Settings\IPackageSettings.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>IPackageSettings.tt</DependentUpon>
</Compile>
<Compile Include="ViewModels\IServiceProviderAware.cs" />
<Compile Include="UI\IView.cs" />
<Compile Include="UI\Octicon.cs" />
@ -181,6 +187,15 @@
<Private>True</Private>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Content Include="Settings\IPackageSettings.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>IPackageSettings.cs</LastGenOutput>
</Content>
</ItemGroup>
<ItemGroup>
<Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.

Просмотреть файл

@ -4,7 +4,7 @@ using GitHub.Extensions;
namespace GitHub.Helpers
{
internal class SettingsStore
public class SettingsStore
{
readonly WritableSettingsStore store;
readonly string root;

Просмотреть файл

@ -1,13 +0,0 @@
using GitHub.Models;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GitHub.Services
{
public interface IPackageSettings
{
bool CollectMetrics { get; set; }
void Save();
}
}

Просмотреть файл

@ -1,104 +0,0 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using GitHub.VisualStudio;
using Microsoft.VisualStudio.Settings;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Settings;
using SettingsStore = GitHub.Helpers.SettingsStore;
using System.Diagnostics;
using System.Globalization;
namespace GitHub.Services
{
[Export(typeof(IPackageSettings))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class PackageSettings : IPackageSettings
{
readonly SettingsStore settingsStore;
readonly Dictionary<string, object> SettingsProperties = new Dictionary<string, object> {
{ "CollectMetrics", true },
};
Dictionary<string, object> settings = new Dictionary<string, object>();
public bool CollectMetrics
{
get
{
object val;
TryGetSetting(nameof(CollectMetrics), out val);
return (bool)val;
}
set { settings[nameof(CollectMetrics)] = value; }
}
[ImportingConstructor]
public PackageSettings([Import(typeof(SVsServiceProvider))] IServiceProvider serviceProvider)
{
var sm = new ShellSettingsManager(serviceProvider);
settingsStore = new SettingsStore(sm.GetWritableSettingsStore(SettingsScope.UserSettings), Info.ApplicationInfo.ApplicationSafeName);
LoadSettings();
}
public void Save()
{
SaveSettings();
}
bool TryGetSetting(string name, out object value)
{
if (!settings.ContainsKey(name))
{
if (!SettingsProperties.ContainsKey(name))
Debug.Assert(false, "PackageSettings: " + name + " setting missing. Did you add this setting to the SettingsProperties dictionary?");
else
Debug.Assert(false, "PackageSettings: " + name + " setting missing. Registry might be corrupt.");
#if !DEBUG
VsOutputLogger.WriteLine("PackageSettings: " + name + " setting missing.");
#endif
value = SettingsProperties[name]; // default value
return false;
}
value = settings[name];
return true;
}
void LoadSettings()
{
try
{
foreach (var kvp in SettingsProperties)
{
settings[kvp.Key] = settingsStore.Read(kvp.Key, kvp.Value);
}
}
catch (Exception ex)
{
Debug.Fail(String.Format(CultureInfo.InvariantCulture, "PackageSettings: Unable to load settings. {0}", ex));
#if !DEBUG
VsOutputLogger.WriteLine("PackageSettings: Unable to load settings. {0}", ex);
#endif
}
}
void SaveSettings()
{
try
{
foreach (var kvp in SettingsProperties)
{
settingsStore.Write(kvp.Key, settings[kvp.Key]);
}
}
catch (Exception ex)
{
Debug.Fail(String.Format(CultureInfo.InvariantCulture, "PackageSettings: Unable to save settings. {0}", ex));
#if !DEBUG
VsOutputLogger.WriteLine("PackageSettings: Unable to save settings. {0}", ex);
#endif
}
}
}
}

Просмотреть файл

@ -0,0 +1,9 @@
// This is an automatically generated file, based on settings.json and IPackageSettings.tt
namespace GitHub.Settings
{
public interface IPackageSettings
{
void Save();
bool CollectMetrics { get; set; }
}
}

Просмотреть файл

@ -0,0 +1,28 @@
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.IO" #>
<#@ assembly name="$(SolutionDir)\build\$(Configuration)\Newtonsoft.Json.Dll" #>
<#@ import namespace="Newtonsoft.Json.Linq" #>
<#@ output extension=".cs" #>
<#
var file = this.Host.ResolvePath(@"..\..\common\settings.json");
var json = JObject.Parse(File.ReadAllText(file));
#>
// This is an automatically generated file, based on settings.json and IPackageSettings.tt
namespace GitHub.Settings
{
public interface IPackageSettings
{
void Save();
<#
foreach (var j in json.Children().Children().Children()) {
#>
<#= j["type"] #> <#= j["name"] #> { get; set; }
<#
}
#>
}
}

Просмотреть файл

@ -249,6 +249,12 @@
</Compile>
<Compile Include="Services\Program.cs" />
<Compile Include="Services\SharedResources.cs" />
<Compile Include="Settings\PackageSettings.cs" />
<Compile Include="Settings\PackageSettingsGen.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>PackageSettingsGen.tt</DependentUpon>
</Compile>
<Compile Include="Settings\Settings.cs" />
<Compile Include="Base\PackageBase.cs" />
<Compile Include="Resources.Designer.cs">
@ -339,9 +345,17 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<IncludeInVSIX>true</IncludeInVSIX>
</Content>
<Content Include="Settings\PackageSettingsGen.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>PackageSettingsGen.cs</LastGenOutput>
<CustomToolNamespace>GitHub.VisualStudio.Settings</CustomToolNamespace>
</Content>
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
<None Include="..\common\settings.json">
<Link>Properties\settings.json</Link>
</None>
<None Include="source.extension.vsixmanifest">
<SubType>Designer</SubType>
</None>
@ -589,6 +603,9 @@
<IncludeOutputGroupsInVSIXLocalOnly>DebugSymbolsProjectOutputGroup;</IncludeOutputGroupsInVSIXLocalOnly>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
</ItemGroup>
<PropertyGroup>
<UseCodebase>true</UseCodebase>
</PropertyGroup>

Просмотреть файл

@ -0,0 +1,30 @@
using System;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.Settings;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Settings;
using SettingsStore = GitHub.Helpers.SettingsStore;
using GitHub.Settings;
namespace GitHub.VisualStudio.Settings
{
[Export(typeof(IPackageSettings))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public partial class PackageSettings : IPackageSettings
{
readonly SettingsStore settingsStore;
[ImportingConstructor]
public PackageSettings([Import(typeof(SVsServiceProvider))] IServiceProvider serviceProvider)
{
var sm = new ShellSettingsManager(serviceProvider);
settingsStore = new SettingsStore(sm.GetWritableSettingsStore(SettingsScope.UserSettings), Info.ApplicationInfo.ApplicationSafeName);
LoadSettings();
}
public void Save()
{
SaveSettings();
}
}
}

Просмотреть файл

@ -0,0 +1,22 @@
// This is an automatically generated file, based on settings.json and PackageSettingsGen.tt
using GitHub.Settings;
namespace GitHub.VisualStudio.Settings {
public partial class PackageSettings : IPackageSettings
{
public bool CollectMetrics { get; set; }
void LoadSettings()
{
CollectMetrics = (bool)settingsStore.Read("CollectMetrics", true);
}
void SaveSettings()
{
settingsStore.Write("CollectMetrics", CollectMetrics);
}
}
}

Просмотреть файл

@ -0,0 +1,52 @@
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.IO" #>
<#@ assembly name="$(TargetDir)\Newtonsoft.Json.Dll" #>
<#@ import namespace="Newtonsoft.Json.Linq" #>
<#@ output extension=".cs" #>
<#
var file = this.Host.ResolvePath(@"..\..\common\settings.json");
var json = JObject.Parse(File.ReadAllText(file));
#>
// This is an automatically generated file, based on settings.json and PackageSettingsGen.tt
using GitHub.Settings;
namespace GitHub.VisualStudio.Settings {
public partial class PackageSettings : IPackageSettings
{
<#
foreach (var j in json.Children().Children().Children()) {
#>
public <#= j["type"] #> <#= j["name"] #> { get; set; }
<#
}
#>
void LoadSettings()
{
<#
foreach (var j in json.Children().Children().Children()) {
#>
<#= j["name"] #> = (<#= j["type"] #>)settingsStore.Read("<#= j["name"] #>", <#= j["default"] #>);
<#
}
#>
}
void SaveSettings()
{
<#
foreach (var j in json.Children().Children().Children()) {
#>
settingsStore.Write("<#= j["name"] #>", <#= j["name"] #>);
<#
}
#>
}
}
}

9
src/common/settings.json Normal file
Просмотреть файл

@ -0,0 +1,9 @@
{
"settings": [
{
"name": "CollectMetrics",
"type": "bool",
"default": "true"
}
]
}