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

Commit a15a78c

Browse files
committed
Change env and fs initialization
We need the file system initialized as early as possible for niceio to work, and environment needs npath, so might as well have them together.
1 parent f55729a commit a15a78c

24 files changed

+273
-235
lines changed

src/GitHub.Api/ApplicationManagerBase.cs

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
namespace GitHub.Unity
66
{
7-
class ApplicationManagerBase : IApplicationManager
7+
abstract class ApplicationManagerBase : IApplicationManager
88
{
99
protected static readonly ILogging logger = Logging.GetLogger<IApplicationManager>();
1010

11+
private IEnvironment environment;
1112
private AppConfiguration appConfiguration;
12-
private RepositoryLocator repositoryLocator;
1313
private RepositoryManager repositoryManager;
1414

1515
public ApplicationManagerBase(SynchronizationContext synchronizationContext)
@@ -20,13 +20,12 @@ public ApplicationManagerBase(SynchronizationContext synchronizationContext)
2020
UIScheduler = TaskScheduler.FromCurrentSynchronizationContext();
2121
ThreadingHelper.MainThreadScheduler = UIScheduler;
2222
CancellationTokenSource = new CancellationTokenSource();
23+
// accessing Environment triggers environment initialization if it hasn't happened yet
24+
Platform = new Platform(Environment, Environment.FileSystem);
2325
}
2426

2527
protected void Initialize(IUIDispatcher uiDispatcher)
2628
{
27-
// accessing Environment triggers environment initialization if it hasn't happened yet
28-
Platform = new Platform(Environment, FileSystem, uiDispatcher);
29-
3029
UserSettings = new UserSettings(Environment);
3130
UserSettings.Initialize();
3231
Logging.TracingEnabled = UserSettings.Get("EnableTraceLogging", false);
@@ -37,9 +36,8 @@ protected void Initialize(IUIDispatcher uiDispatcher)
3736
SystemSettings = new SystemSettings(Environment);
3837
SystemSettings.Initialize();
3938

40-
4139
ProcessManager = new ProcessManager(Environment, Platform.GitEnvironment, CancellationToken);
42-
Platform.Initialize(Environment, ProcessManager);
40+
Platform.Initialize(ProcessManager, uiDispatcher);
4341
}
4442

4543
public virtual Task Run()
@@ -57,45 +55,22 @@ public virtual Task Run()
5755
return task;
5856
}
5957

60-
protected virtual void InitializeEnvironment()
61-
{
62-
SetupRepository();
63-
}
64-
65-
private void SetupRepository()
66-
{
67-
if (FileSystem == null)
68-
{
69-
FileSystem = new FileSystem();
70-
NPathFileSystemProvider.Current = FileSystem;
71-
}
72-
73-
FileSystem.SetCurrentDirectory(Environment.UnityProjectPath);
74-
75-
// figure out where the repository root is
76-
repositoryLocator = new RepositoryLocator(Environment.UnityProjectPath);
77-
var repositoryPath = repositoryLocator.FindRepositoryRoot();
78-
if (repositoryPath != null)
79-
{
80-
// Make sure CurrentDirectory always returns the repository root, so all
81-
// file system path calculations use it as a base
82-
FileSystem.SetCurrentDirectory(repositoryPath);
83-
}
84-
}
58+
protected abstract string DetermineInstallationPath();
59+
protected abstract string GetAssetsPath();
60+
protected abstract string GetUnityPath();
8561

8662
public virtual async Task RestartRepository()
8763
{
8864
await ThreadingHelper.SwitchToThreadAsync();
8965

90-
SetupRepository();
66+
Environment.Initialize();
9167

92-
var repositoryRoot = repositoryLocator.FindRepositoryRoot();
93-
if (repositoryRoot != null)
68+
if (Environment.RepositoryPath != null)
9469
{
9570
try
9671
{
9772
var repositoryManagerFactory = new RepositoryManagerFactory();
98-
repositoryManager = repositoryManagerFactory.CreateRepositoryManager(Platform, TaskRunner, repositoryRoot, CancellationToken);
73+
repositoryManager = repositoryManagerFactory.CreateRepositoryManager(Platform, TaskRunner, Environment.RepositoryPath, CancellationToken);
9974
}
10075
catch (Exception ex)
10176
{
@@ -111,7 +86,7 @@ private async Task RunInternal()
11186
{
11287
await ThreadingHelper.SwitchToThreadAsync();
11388

114-
var gitSetup = new GitSetup(Environment, FileSystem, CancellationToken);
89+
var gitSetup = new GitSetup(Environment, Environment.FileSystem, CancellationToken);
11590
var expectedPath = gitSetup.GitInstallationPath;
11691

11792
var setupDone = await gitSetup.SetupIfNeeded(
@@ -194,8 +169,28 @@ public AppConfiguration AppConfiguration
194169
}
195170
}
196171

197-
public virtual IEnvironment Environment { get; set; }
198-
public IFileSystem FileSystem { get; protected set; }
172+
public IEnvironment Environment
173+
{
174+
get
175+
{
176+
// if this is called while still null, it's because Unity wants
177+
// to render something and we need to load icons, and that runs
178+
// before EntryPoint. Do an early initialization
179+
if (environment == null)
180+
{
181+
environment = new DefaultEnvironment();
182+
var assetsPath = GetAssetsPath();
183+
var unityPath = GetUnityPath();
184+
var extensionInstallPath = DetermineInstallationPath();
185+
186+
// figure out where we are
187+
environment.Initialize(extensionInstallPath.ToNPath(), unityPath.ToNPath(), assetsPath.ToNPath());
188+
}
189+
return environment;
190+
}
191+
protected set { environment = value; }
192+
}
193+
199194
public IPlatform Platform { get; protected set; }
200195
public virtual IProcessEnvironment GitEnvironment { get; set; }
201196
public IProcessManager ProcessManager { get; protected set; }

src/GitHub.Api/Authentication/IPlatform.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace GitHub.Unity
55
{
66
interface IPlatform
77
{
8-
Task<IPlatform> Initialize(IEnvironment environment, IProcessManager processManager);
8+
Task<IPlatform> Initialize(IProcessManager processManager, IUIDispatcher uiDispatcher);
99
IProcessEnvironment GitEnvironment { get; }
1010
ICredentialManager CredentialManager { get; }
1111
IEnvironment Environment { get; }

src/GitHub.Api/DefaultEnvironment.cs

Lines changed: 64 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,54 @@ namespace GitHub.Unity
55
{
66
class DefaultEnvironment : IEnvironment
77
{
8-
private static readonly ILogging logger = Logging.GetLogger<DefaultEnvironment>();
9-
108
private const string logFile = "github-unity.log";
11-
public static NPath LogPath
9+
private IFileSystem filesystem;
10+
11+
public NPath LogPath { get; }
12+
public DefaultEnvironment()
1213
{
13-
get
14+
NPath localAppData;
15+
NPath commonAppData;
16+
if (IsWindows)
17+
{
18+
localAppData = GetSpecialFolder(Environment.SpecialFolder.LocalApplicationData).ToNPath();
19+
commonAppData = GetSpecialFolder(Environment.SpecialFolder.CommonApplicationData).ToNPath();
20+
}
21+
else if (IsMac)
22+
{
23+
localAppData = NPath.HomeDirectory.Combine("Library", "Application Support");
24+
// there is no such thing on the mac that is guaranteed to be user accessible (/usr/local might not be)
25+
commonAppData = GetSpecialFolder(Environment.SpecialFolder.ApplicationData).ToNPath();
26+
}
27+
else
1428
{
15-
return Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
16-
.ToNPath()
17-
.Combine(ApplicationInfo.ApplicationName, logFile);
29+
localAppData = GetSpecialFolder(Environment.SpecialFolder.LocalApplicationData).ToNPath();
30+
commonAppData = "/usr/local/share/";
1831
}
32+
33+
UserCachePath = localAppData.Combine(ApplicationInfo.ApplicationName);
34+
SystemCachePath = commonAppData.Combine(ApplicationInfo.ApplicationName);
35+
LogPath = UserCachePath.Combine(logFile);
1936
}
2037

21-
private const string startupLogFile = "github-unity-startup.log";
22-
public static NPath StartupLogPath
38+
public void Initialize(NPath extensionInstallPath, NPath unityPath, NPath assetsPath)
2339
{
24-
get
25-
{
26-
return Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
27-
.ToNPath()
28-
.Combine(ApplicationInfo.ApplicationName, startupLogFile);
29-
}
40+
ExtensionInstallPath = extensionInstallPath;
41+
UnityApplication = unityPath;
42+
UnityAssetsPath = assetsPath;
43+
UnityProjectPath = assetsPath.Parent;
44+
Initialize();
45+
}
46+
47+
public void Initialize()
48+
{
49+
Guard.NotNull(this, UnityProjectPath, nameof(UnityProjectPath));
50+
Guard.NotNull(this, FileSystem, nameof(FileSystem));
51+
RepositoryPath = new RepositoryLocator(UnityProjectPath).FindRepositoryRoot();
52+
if (RepositoryPath == null)
53+
FileSystem.SetCurrentDirectory(UnityProjectPath);
54+
else
55+
FileSystem.SetCurrentDirectory(RepositoryPath);
3056
}
3157

3258
public string GetSpecialFolder(Environment.SpecialFolder folder)
@@ -44,10 +70,28 @@ public string GetEnvironmentVariable(string variable)
4470
return Environment.GetEnvironmentVariable(variable);
4571
}
4672

47-
public string UnityApplication { get; set; }
48-
public string UnityAssetsPath { get; set; }
49-
public string UnityProjectPath { get; set; }
50-
public string ExtensionInstallPath { get; set; }
73+
public IFileSystem FileSystem
74+
{
75+
get
76+
{
77+
if (filesystem == null)
78+
{
79+
filesystem = new FileSystem();
80+
NPathFileSystemProvider.Current = filesystem;
81+
}
82+
return filesystem;
83+
}
84+
set
85+
{
86+
filesystem = value;
87+
NPathFileSystemProvider.Current = filesystem;
88+
}
89+
}
90+
91+
public NPath UnityApplication { get; private set; }
92+
public NPath UnityAssetsPath { get; private set; }
93+
public NPath UnityProjectPath { get; private set; }
94+
public NPath ExtensionInstallPath { get; set; }
5195
public NPath UserCachePath { get; set; }
5296
public NPath SystemCachePath { get; set; }
5397
public string Path { get { return Environment.GetEnvironmentVariable("PATH"); } }
@@ -59,7 +103,6 @@ public string GitExecutablePath
59103
get { return gitExecutablePath; }
60104
set
61105
{
62-
logger.Trace("Setting GitExecutablePath to " + value);
63106
gitExecutablePath = value;
64107
gitInstallPath = null;
65108
}
@@ -83,7 +126,6 @@ public string GitInstallPath
83126
{
84127
gitInstallPath = GitExecutablePath.ToNPath().Parent;
85128
}
86-
logger.Trace("Setting GitInstallPath to " + gitInstallPath);
87129
}
88130
else
89131
gitInstallPath = GitExecutablePath;
@@ -92,7 +134,7 @@ public string GitInstallPath
92134
}
93135
}
94136

95-
public string RepositoryPath { get { return Repository.LocalPath; } }
137+
public string RepositoryPath { get; private set; }
96138
public IRepository Repository { get; set; }
97139

98140
public bool IsWindows { get { return OnWindows; } }

src/GitHub.Api/Events/RepositoryWatcher.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public RepositoryWatcher(IPlatform platform, RepositoryPathConfiguration paths,
5252
this.cancellationToken = cancellationToken;
5353

5454
ignoredPaths = new[] {
55-
platform.Environment.UnityProjectPath.ToNPath().Combine("Library"),
56-
platform.Environment.UnityProjectPath.ToNPath().Combine("Temp")
55+
platform.Environment.UnityProjectPath.Combine("Library"),
56+
platform.Environment.UnityProjectPath.Combine("Temp")
5757
};
5858

5959
autoResetEvent = new AutoResetEvent(false);

src/GitHub.Api/Guard.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,21 @@
66

77
namespace GitHub.Unity
88
{
9-
public static class Guard
9+
internal class InstanceNotInitializedException : InvalidOperationException
1010
{
11+
public InstanceNotInitializedException(object the, string property) :
12+
base(String.Format(CultureInfo.InvariantCulture, "{0} is not correctly initialized, {1} is null", the?.GetType().Name, property))
13+
{}
14+
}
15+
16+
internal static class Guard
17+
{
18+
public static void NotNull(object the, object value, string propertyName)
19+
{
20+
if (value != null) return;
21+
throw new InstanceNotInitializedException(the, propertyName);
22+
}
23+
1124
public static void ArgumentNotNull(object value, string name)
1225
{
1326
if (value != null) return;

src/GitHub.Api/IApplicationManager.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ interface IApplicationManager : IDisposable
88
{
99
CancellationToken CancellationToken { get; }
1010
IEnvironment Environment { get; }
11-
IFileSystem FileSystem { get; }
1211
IPlatform Platform { get; }
1312
IProcessEnvironment GitEnvironment { get; }
1413
IProcessManager ProcessManager { get; }

src/GitHub.Api/IEnvironment.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ namespace GitHub.Unity
44
{
55
interface IEnvironment
66
{
7+
void Initialize(NPath extensionInstallPath, NPath unityPath, NPath assetsPath);
8+
void Initialize();
79
string ExpandEnvironmentVariables(string name);
810
string GetEnvironmentVariable(string v);
911
string GetSpecialFolder(Environment.SpecialFolder folder);
@@ -14,14 +16,16 @@ interface IEnvironment
1416
bool IsWindows { get; }
1517
bool IsLinux { get; }
1618
bool IsMac { get; }
17-
string UnityApplication { get; set; }
18-
string UnityAssetsPath { get; set; }
19-
string UnityProjectPath { get; set; }
20-
string ExtensionInstallPath { get; set; }
21-
NPath UserCachePath { get; set; }
19+
NPath UnityApplication { get; }
20+
NPath UnityAssetsPath { get; }
21+
NPath UnityProjectPath { get; }
22+
NPath ExtensionInstallPath { get; }
2223
string RepositoryPath { get; }
2324
string GitInstallPath { get; }
2425
IRepository Repository { get; set; }
26+
NPath UserCachePath { get; set; }
2527
NPath SystemCachePath { get; set; }
28+
NPath LogPath { get; }
29+
IFileSystem FileSystem { get; set; }
2630
}
2731
}

src/GitHub.Api/Installer/GitInstaller.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public Task<bool> SetupGitIfNeeded(NPath tempPath, IProgress<float> zipFileProgr
174174
{
175175
logger.Warning("Archive \"{0}\" missing", archiveFilePath.ToString());
176176

177-
archiveFilePath = environment.ExtensionInstallPath.ToNPath().Combine(archiveFilePath);
177+
archiveFilePath = environment.ExtensionInstallPath.Combine(archiveFilePath);
178178
if (!archiveFilePath.FileExists())
179179
{
180180
logger.Warning("Archive \"{0}\" missing, returning", archiveFilePath.ToString());
@@ -236,7 +236,7 @@ public Task<bool> SetupGitLfsIfNeeded(NPath tempPath, IProgress<float> zipFilePr
236236
{
237237
logger.Warning("Archive \"{0}\" missing", archiveFilePath.ToString());
238238

239-
archiveFilePath = environment.ExtensionInstallPath.ToNPath().Combine(archiveFilePath);
239+
archiveFilePath = environment.ExtensionInstallPath.Combine(archiveFilePath);
240240
if (!archiveFilePath.FileExists())
241241
{
242242
logger.Warning("Archive \"{0}\" missing, returning", archiveFilePath.ToString());

src/GitHub.Api/LocalSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ class LocalSettings : JsonBackedSettings
170170

171171
public LocalSettings(IEnvironment environment)
172172
{
173-
SettingsPath = environment.UnityProjectPath.ToNPath().Combine(RelativeSettingsPath);
173+
SettingsPath = environment.UnityProjectPath.Combine(RelativeSettingsPath);
174174
}
175175

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

0 commit comments

Comments
 (0)