Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 0c62477

Browse files
committed
Generate settings classes from a json file
1 parent 9752a19 commit 0c62477

File tree

11 files changed

+185
-120
lines changed

11 files changed

+185
-120
lines changed

src/GitHub.Exports/GitHub.Exports.csproj

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@
9595
<None Include="..\..\script\Key.snk" Condition="$(Buildtype) == 'Internal'">
9696
<Link>Key.snk</Link>
9797
</None>
98+
<None Include="..\common\settings.json">
99+
<Link>Properties\settings.json</Link>
100+
</None>
98101
<Compile Include="Collections\ICopyable.cs" />
99102
<Compile Include="ExceptionExtensions.cs" />
100103
<Compile Include="Extensions\VSExtensions.cs" />
@@ -117,13 +120,11 @@
117120
<Compile Include="Services\IMenuHandler.cs" />
118121
<Compile Include="Services\IMenuProvider.cs" />
119122
<Compile Include="Services\INotificationDispatcher.cs" />
120-
<Compile Include="Services\IPackageSettings.cs" />
121123
<Compile Include="Services\IStatusBarNotificationService.cs" />
122124
<Compile Include="Services\ITeamExplorerServices.cs" />
123125
<Compile Include="Services\ITeamExplorerServiceHolder.cs" />
124126
<Compile Include="Services\INotificationService.cs" />
125127
<Compile Include="Services\Logger.cs" />
126-
<Compile Include="Services\PackageSettings.cs" />
127128
<Compile Include="Services\Services.cs" />
128129
<Compile Include="Services\StatusBarNotificationService.cs" />
129130
<Compile Include="Services\TeamExplorerServices.cs" />
@@ -137,6 +138,11 @@
137138
<ItemGroup>
138139
<Compile Include="Authentication\AuthenticationResultExtensions.cs" />
139140
<Compile Include="Extensions\ServiceProviderExtensions.cs" />
141+
<Compile Include="Settings\IPackageSettings.cs">
142+
<AutoGen>True</AutoGen>
143+
<DesignTime>True</DesignTime>
144+
<DependentUpon>IPackageSettings.tt</DependentUpon>
145+
</Compile>
140146
<Compile Include="ViewModels\IServiceProviderAware.cs" />
141147
<Compile Include="UI\IView.cs" />
142148
<Compile Include="UI\Octicon.cs" />
@@ -181,6 +187,15 @@
181187
<Private>True</Private>
182188
</ProjectReference>
183189
</ItemGroup>
190+
<ItemGroup>
191+
<Content Include="Settings\IPackageSettings.tt">
192+
<Generator>TextTemplatingFileGenerator</Generator>
193+
<LastGenOutput>IPackageSettings.cs</LastGenOutput>
194+
</Content>
195+
</ItemGroup>
196+
<ItemGroup>
197+
<Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
198+
</ItemGroup>
184199
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
185200
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
186201
Other similar extension points exist, see Microsoft.Common.targets.

src/GitHub.Exports/Helpers/SettingsStore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace GitHub.Helpers
66
{
7-
internal class SettingsStore
7+
public class SettingsStore
88
{
99
readonly WritableSettingsStore store;
1010
readonly string root;

src/GitHub.Exports/Services/IPackageSettings.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/GitHub.Exports/Services/PackageSettings.cs

Lines changed: 0 additions & 104 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// This is an automatically generated file, based on settings.json and IPackageSettings.tt
2+
namespace GitHub.Settings
3+
{
4+
public interface IPackageSettings
5+
{
6+
void Save();
7+
bool CollectMetrics { get; set; }
8+
}
9+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<#@ template debug="false" hostspecific="true" language="C#" #>
2+
<#@ assembly name="System.Core" #>
3+
<#@ import namespace="System.Linq" #>
4+
<#@ import namespace="System.Text" #>
5+
<#@ import namespace="System.Collections.Generic" #>
6+
<#@ import namespace="System.IO" #>
7+
<#@ assembly name="$(SolutionDir)\build\$(Configuration)\Newtonsoft.Json.Dll" #>
8+
<#@ import namespace="Newtonsoft.Json.Linq" #>
9+
<#@ output extension=".cs" #>
10+
<#
11+
var file = this.Host.ResolvePath(@"..\..\common\settings.json");
12+
var json = JObject.Parse(File.ReadAllText(file));
13+
#>
14+
// This is an automatically generated file, based on settings.json and IPackageSettings.tt
15+
namespace GitHub.Settings
16+
{
17+
public interface IPackageSettings
18+
{
19+
void Save();
20+
<#
21+
foreach (var j in json.Children().Children().Children()) {
22+
#>
23+
<#= j["type"] #> <#= j["name"] #> { get; set; }
24+
<#
25+
}
26+
#>
27+
}
28+
}

src/GitHub.VisualStudio/GitHub.VisualStudio.csproj

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,12 @@
249249
</Compile>
250250
<Compile Include="Services\Program.cs" />
251251
<Compile Include="Services\SharedResources.cs" />
252+
<Compile Include="Settings\PackageSettings.cs" />
253+
<Compile Include="Settings\PackageSettingsGen.cs">
254+
<AutoGen>True</AutoGen>
255+
<DesignTime>True</DesignTime>
256+
<DependentUpon>PackageSettingsGen.tt</DependentUpon>
257+
</Compile>
252258
<Compile Include="Settings\Settings.cs" />
253259
<Compile Include="Base\PackageBase.cs" />
254260
<Compile Include="Resources.Designer.cs">
@@ -339,9 +345,17 @@
339345
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
340346
<IncludeInVSIX>true</IncludeInVSIX>
341347
</Content>
348+
<Content Include="Settings\PackageSettingsGen.tt">
349+
<Generator>TextTemplatingFileGenerator</Generator>
350+
<LastGenOutput>PackageSettingsGen.cs</LastGenOutput>
351+
<CustomToolNamespace>GitHub.VisualStudio.Settings</CustomToolNamespace>
352+
</Content>
342353
<None Include="packages.config">
343354
<SubType>Designer</SubType>
344355
</None>
356+
<None Include="..\common\settings.json">
357+
<Link>Properties\settings.json</Link>
358+
</None>
345359
<None Include="source.extension.vsixmanifest">
346360
<SubType>Designer</SubType>
347361
</None>
@@ -589,6 +603,9 @@
589603
<IncludeOutputGroupsInVSIXLocalOnly>DebugSymbolsProjectOutputGroup;</IncludeOutputGroupsInVSIXLocalOnly>
590604
</ProjectReference>
591605
</ItemGroup>
606+
<ItemGroup>
607+
<Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
608+
</ItemGroup>
592609
<PropertyGroup>
593610
<UseCodebase>true</UseCodebase>
594611
</PropertyGroup>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.ComponentModel.Composition;
3+
using Microsoft.VisualStudio.Settings;
4+
using Microsoft.VisualStudio.Shell;
5+
using Microsoft.VisualStudio.Shell.Settings;
6+
using SettingsStore = GitHub.Helpers.SettingsStore;
7+
using GitHub.Settings;
8+
9+
namespace GitHub.VisualStudio.Settings
10+
{
11+
[Export(typeof(IPackageSettings))]
12+
[PartCreationPolicy(CreationPolicy.NonShared)]
13+
public partial class PackageSettings : IPackageSettings
14+
{
15+
readonly SettingsStore settingsStore;
16+
17+
[ImportingConstructor]
18+
public PackageSettings([Import(typeof(SVsServiceProvider))] IServiceProvider serviceProvider)
19+
{
20+
var sm = new ShellSettingsManager(serviceProvider);
21+
settingsStore = new SettingsStore(sm.GetWritableSettingsStore(SettingsScope.UserSettings), Info.ApplicationInfo.ApplicationSafeName);
22+
LoadSettings();
23+
}
24+
25+
public void Save()
26+
{
27+
SaveSettings();
28+
}
29+
}
30+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// This is an automatically generated file, based on settings.json and PackageSettingsGen.tt
2+
using GitHub.Settings;
3+
4+
namespace GitHub.VisualStudio.Settings {
5+
6+
public partial class PackageSettings : IPackageSettings
7+
{
8+
9+
public bool CollectMetrics { get; set; }
10+
11+
void LoadSettings()
12+
{
13+
CollectMetrics = (bool)settingsStore.Read("CollectMetrics", true);
14+
}
15+
16+
void SaveSettings()
17+
{
18+
settingsStore.Write("CollectMetrics", CollectMetrics);
19+
}
20+
21+
}
22+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<#@ template debug="false" hostspecific="true" language="C#" #>
2+
<#@ assembly name="System.Core" #>
3+
<#@ import namespace="System.Linq" #>
4+
<#@ import namespace="System.Collections.Generic" #>
5+
<#@ import namespace="System.IO" #>
6+
<#@ assembly name="$(TargetDir)\Newtonsoft.Json.Dll" #>
7+
<#@ import namespace="Newtonsoft.Json.Linq" #>
8+
<#@ output extension=".cs" #>
9+
<#
10+
var file = this.Host.ResolvePath(@"..\..\common\settings.json");
11+
var json = JObject.Parse(File.ReadAllText(file));
12+
#>
13+
// This is an automatically generated file, based on settings.json and PackageSettingsGen.tt
14+
using GitHub.Settings;
15+
16+
namespace GitHub.VisualStudio.Settings {
17+
18+
public partial class PackageSettings : IPackageSettings
19+
{
20+
21+
<#
22+
foreach (var j in json.Children().Children().Children()) {
23+
#>
24+
public <#= j["type"] #> <#= j["name"] #> { get; set; }
25+
<#
26+
}
27+
#>
28+
29+
void LoadSettings()
30+
{
31+
<#
32+
foreach (var j in json.Children().Children().Children()) {
33+
#>
34+
<#= j["name"] #> = (<#= j["type"] #>)settingsStore.Read("<#= j["name"] #>", <#= j["default"] #>);
35+
<#
36+
}
37+
#>
38+
}
39+
40+
void SaveSettings()
41+
{
42+
<#
43+
foreach (var j in json.Children().Children().Children()) {
44+
#>
45+
settingsStore.Write("<#= j["name"] #>", <#= j["name"] #>);
46+
<#
47+
}
48+
#>
49+
}
50+
51+
}
52+
}

0 commit comments

Comments
 (0)