Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 2cd9aef

Browse files
authored
Merge pull request #312 from github/fixes/settings
Fix to initialize settings and store EnableTraceLogging
2 parents 9cec196 + b61570f commit 2cd9aef

File tree

11 files changed

+51
-48
lines changed

11 files changed

+51
-48
lines changed

src/GitHub.Api/AppConfiguration.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,11 @@ class AppConfiguration : IAppConfiguration
88
{
99
public AppConfiguration()
1010
{
11-
applicationName = ApplicationInfo.ApplicationName;
12-
applicationDescription = ApplicationInfo.ApplicationDescription;
13-
1411
var executingAssembly = typeof(AppConfiguration).Assembly;
1512
AssemblyName = executingAssembly.GetName();
1613
ProductHeader = new ProductHeaderValue(ApplicationInfo.ApplicationSafeName, AssemblyName.Version.ToString());
1714
}
1815

19-
readonly string applicationName;
20-
readonly string applicationDescription;
21-
22-
/// <summary>
23-
/// Name of this application
24-
/// </summary>
25-
public string ApplicationName { get { return applicationName; } }
26-
27-
/// <summary>
28-
/// Name of this application
29-
/// </summary>
30-
public string ApplicationDescription { get { return applicationDescription; } }
31-
3216
/// <summary>
3317
/// The currently executing assembly.
3418
/// </summary>

src/GitHub.Api/ApplicationManagerBase.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,16 @@ protected void Initialize(IUIDispatcher uiDispatcher)
2626
{
2727
// accessing Environment triggers environment initialization if it hasn't happened yet
2828
LocalSettings = new LocalSettings(Environment);
29-
UserSettings = new UserSettings(Environment, ApplicationInfo.ApplicationName);
30-
SystemSettings = new SystemSettings(Environment, ApplicationInfo.ApplicationName);
29+
UserSettings = new UserSettings(Environment);
30+
SystemSettings = new SystemSettings(Environment);
31+
32+
LocalSettings.Initialize();
33+
34+
UserSettings.Initialize();
35+
36+
Logging.TracingEnabled = UserSettings.Get("EnableTraceLogging", false);
37+
38+
SystemSettings.Initialize();
3139

3240
Platform = new Platform(Environment, FileSystem, uiDispatcher);
3341
ProcessManager = new ProcessManager(Environment, Platform.GitEnvironment, CancellationToken);

src/GitHub.Api/Authentication/Keychain.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class ConnectionCacheItem
2828
/// </summary>
2929
class Keychain : IKeychain
3030
{
31+
const string ConnectionFile = "connections.json";
32+
3133
private readonly ILogging logger = Logging.GetLogger<Keychain>();
3234

3335
private readonly ICredentialManager credentialManager;
@@ -42,7 +44,7 @@ class Keychain : IKeychain
4244

4345
public Keychain(IEnvironment environment, ICredentialManager credentialManager)
4446
{
45-
cachePath = environment.ConnectionCachePath;
47+
cachePath = environment.UserCachePath.Combine(ConnectionFile);
4648
this.credentialManager = credentialManager;
4749
}
4850

src/GitHub.Api/DefaultEnvironment.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ public string GetEnvironmentVariable(string variable)
2626
public string UnityAssetsPath { get; set; }
2727
public string UnityProjectPath { get; set; }
2828
public string ExtensionInstallPath { get; set; }
29-
public NPath ConnectionCachePath { get; set; }
29+
public NPath UserCachePath { get; set; }
30+
public NPath SystemCachePath { get; set; }
3031
public string UserProfilePath { get { return Environment.GetEnvironmentVariable("USERPROFILE"); } }
3132
public string Path { get { return Environment.GetEnvironmentVariable("PATH"); } }
3233
public string NewLine { get { return Environment.NewLine; } }

src/GitHub.Api/IAppConfiguration.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ namespace GitHub.Unity
66
// Represents the currently executing program.
77
interface IAppConfiguration
88
{
9-
string ApplicationName { get; }
10-
string ApplicationDescription { get; }
119
AssemblyName AssemblyName { get; }
1210
ProductHeaderValue ProductHeader { get; }
1311
}

src/GitHub.Api/IEnvironment.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ interface IEnvironment
1919
string UnityAssetsPath { get; set; }
2020
string UnityProjectPath { get; set; }
2121
string ExtensionInstallPath { get; set; }
22-
NPath ConnectionCachePath { get; set; }
22+
NPath UserCachePath { get; set; }
2323
string RepositoryPath { get; }
2424
string GitInstallPath { get; }
2525
IRepository Repository { get; set; }
26+
NPath SystemCachePath { get; set; }
2627
}
2728
}

src/GitHub.Api/LocalSettings.cs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,11 @@ abstract class BaseSettings : ISettings
1616
public abstract void Unset(string key);
1717

1818
protected virtual string SettingsFileName { get; set; }
19-
protected virtual string SettingsPath { get; set; }
19+
protected virtual NPath SettingsPath { get; set; }
2020
}
2121

2222
class JsonBackedSettings : BaseSettings
2323
{
24-
private static readonly ILogging logger = Logging.GetLogger<JsonBackedSettings>();
25-
26-
private const string SettingsParseError = "Failed to parse settings file at '{0}'";
27-
2824
private string cachePath;
2925
private CacheData cacheData = new CacheData();
3026
private Action<string> dirCreate;
@@ -33,9 +29,11 @@ class JsonBackedSettings : BaseSettings
3329
private Func<string, bool> fileExists;
3430
private Func<string, Encoding, string> readAllText;
3531
private Action<string, string> writeAllText;
32+
private readonly ILogging logger;
3633

3734
public JsonBackedSettings()
3835
{
36+
logger = Logging.GetLogger(GetType());
3937
fileExists = (path) => File.Exists(path);
4038
readAllText = (path, encoding) => File.ReadAllText(path, encoding);
4139
writeAllText = (path, content) => File.WriteAllText(path, content);
@@ -46,7 +44,7 @@ public JsonBackedSettings()
4644

4745
public override void Initialize()
4846
{
49-
cachePath = Path.Combine(SettingsPath, SettingsFileName);
47+
cachePath = SettingsPath.Combine(SettingsFileName);
5048
LoadFromCache(cachePath);
5149
}
5250

@@ -100,6 +98,8 @@ public override void Rename(string oldKey, string newKey)
10098

10199
private void LoadFromCache(string cachePath)
102100
{
101+
logger.Trace("LoadFromCache: {0}", cachePath);
102+
103103
EnsureCachePath(cachePath);
104104

105105
if (!fileExists(cachePath))
@@ -111,8 +111,9 @@ private void LoadFromCache(string cachePath)
111111
{
112112
cacheData = SimpleJson.DeserializeObject<CacheData>(data);
113113
}
114-
catch
114+
catch(Exception ex)
115115
{
116+
logger.Error(ex, "LoadFromCache Error");
116117
cacheData = null;
117118
}
118119

@@ -126,6 +127,8 @@ private void LoadFromCache(string cachePath)
126127

127128
private bool SaveToCache(string cachePath)
128129
{
130+
logger.Trace("SaveToCache: {0}", cachePath);
131+
129132
EnsureCachePath(cachePath);
130133

131134
try
@@ -135,8 +138,7 @@ private bool SaveToCache(string cachePath)
135138
}
136139
catch (Exception ex)
137140
{
138-
logger.Error(SettingsParseError, cachePath);
139-
logger.Error(ex);
141+
logger.Error(ex, "SaveToCache Error");
140142
return false;
141143
}
142144

@@ -155,7 +157,7 @@ private void EnsureCachePath(string cachePath)
155157

156158
private class CacheData
157159
{
158-
public Dictionary<string, object> GitHubUnity = new Dictionary<string, object>();
160+
public Dictionary<string, object> GitHubUnity { get; set; } = new Dictionary<string, object>();
159161
}
160162

161163
}
@@ -177,9 +179,9 @@ class UserSettings : JsonBackedSettings
177179
{
178180
private const string settingsFileName = "settings.json";
179181

180-
public UserSettings(IEnvironment environment, string path)
182+
public UserSettings(IEnvironment environment)
181183
{
182-
SettingsPath = environment.GetSpecialFolder(Environment.SpecialFolder.LocalApplicationData).ToNPath().Combine(path);
184+
SettingsPath = environment.UserCachePath;
183185
}
184186

185187
protected override string SettingsFileName { get { return settingsFileName; } }
@@ -189,9 +191,9 @@ class SystemSettings : JsonBackedSettings
189191
{
190192
private const string settingsFileName = "settings.json";
191193

192-
public SystemSettings(IEnvironment environment, string path)
194+
public SystemSettings(IEnvironment environment)
193195
{
194-
SettingsPath = environment.GetSpecialFolder(Environment.SpecialFolder.ApplicationData).ToNPath().Combine(path);
196+
SettingsPath = environment.SystemCachePath;
195197
}
196198

197199
protected override string SettingsFileName { get { return settingsFileName; } }

src/GitHub.Logging/Logging.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,6 @@ namespace GitHub.Unity
44
{
55
public static class Logging
66
{
7-
static Logging()
8-
{
9-
#if DEVELOPER_BUILD
10-
tracingEnabled = true;
11-
#endif
12-
}
13-
147
private static bool tracingEnabled;
158

169
public static bool TracingEnabled

src/IntegrationTests/Git/IntegrationTestEnvironment.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,20 @@ public string ExtensionInstallPath
108108
set { throw new NotImplementedException(); }
109109
}
110110

111-
public NPath ConnectionCachePath
111+
public NPath UserCachePath
112112
{
113113
get
114114
{
115-
return GetSpecialFolder(Environment.SpecialFolder.LocalApplicationData).ToNPath().Combine("connections.json");
115+
return GetSpecialFolder(Environment.SpecialFolder.LocalApplicationData).ToNPath().Combine(ApplicationInfo.ApplicationName);
116+
}
117+
set { throw new NotImplementedException(); }
118+
}
119+
120+
public NPath SystemCachePath
121+
{
122+
get
123+
{
124+
return GetSpecialFolder(Environment.SpecialFolder.CommonApplicationData).ToNPath().Combine(ApplicationInfo.ApplicationName);
116125
}
117126
set { throw new NotImplementedException(); }
118127
}

src/UnityExtension/Assets/Editor/GitHub.Unity/ApplicationManager.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,12 @@ protected override void InitializeEnvironment()
8484
Environment.UnityAssetsPath = assetsPath.ToString(SlashMode.Forward);
8585
Environment.UnityProjectPath = projectPath.ToString(SlashMode.Forward);
8686

87-
Environment.ConnectionCachePath = Environment.GetSpecialFolder(System.Environment.SpecialFolder.LocalApplicationData)
87+
Environment.UserCachePath = Environment.GetSpecialFolder(System.Environment.SpecialFolder.LocalApplicationData)
8888
.ToNPath()
89-
.Combine(AppConfiguration.ApplicationName, "connections.json");
89+
.Combine(ApplicationInfo.ApplicationName);
90+
Environment.SystemCachePath = Environment.GetSpecialFolder(System.Environment.SpecialFolder.CommonApplicationData)
91+
.ToNPath()
92+
.Combine(ApplicationInfo.ApplicationName);
9093

9194
base.InitializeEnvironment();
9295
}

0 commit comments

Comments
 (0)