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

Commit d287f1f

Browse files
Merge branch 'master' into fixes/git-lfs
2 parents 6f4b037 + f956879 commit d287f1f

38 files changed

+610
-486
lines changed

build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ else
3232
nuget restore
3333
fi
3434

35-
xbuild GitHub.Unity.sln /property:Configuration=$Configuration /target:$Target
35+
xbuild GitHub.Unity.sln /verbosity:minimal /property:Configuration=$Configuration /target:$Target
3636

3737
cp -r unity/PackageProject/Assets/Editor/GitHub ../github-unity-test/GitHubExtensionProject/Assets/Editor || true
3838

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: 72 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,55 @@ namespace GitHub.Unity
55
{
66
class DefaultEnvironment : IEnvironment
77
{
8-
private static readonly ILogging logger = Logging.GetLogger<DefaultEnvironment>();
8+
private const string logFile = "github-unity.log";
9+
private IFileSystem filesystem;
10+
11+
public NPath LogPath { get; }
12+
public DefaultEnvironment()
13+
{
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
28+
{
29+
localAppData = GetSpecialFolder(Environment.SpecialFolder.LocalApplicationData).ToNPath();
30+
commonAppData = "/usr/local/share/";
31+
}
32+
33+
UserCachePath = localAppData.Combine(ApplicationInfo.ApplicationName);
34+
SystemCachePath = commonAppData.Combine(ApplicationInfo.ApplicationName);
35+
LogPath = UserCachePath.Combine(logFile);
36+
}
37+
38+
public void Initialize(NPath extensionInstallPath, NPath unityPath, NPath assetsPath)
39+
{
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);
56+
}
957

1058
public string GetSpecialFolder(Environment.SpecialFolder folder)
1159
{
@@ -22,10 +70,28 @@ public string GetEnvironmentVariable(string variable)
2270
return Environment.GetEnvironmentVariable(variable);
2371
}
2472

25-
public string UnityApplication { get; set; }
26-
public string UnityAssetsPath { get; set; }
27-
public string UnityProjectPath { get; set; }
28-
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; }
2995
public NPath UserCachePath { get; set; }
3096
public NPath SystemCachePath { get; set; }
3197
public string Path { get { return Environment.GetEnvironmentVariable("PATH"); } }
@@ -37,7 +103,6 @@ public string GitExecutablePath
37103
get { return gitExecutablePath; }
38104
set
39105
{
40-
logger.Trace("Setting GitExecutablePath to " + value);
41106
gitExecutablePath = value;
42107
gitInstallPath = null;
43108
}
@@ -61,7 +126,6 @@ public string GitInstallPath
61126
{
62127
gitInstallPath = GitExecutablePath.ToNPath().Parent;
63128
}
64-
logger.Trace("Setting GitInstallPath to " + gitInstallPath);
65129
}
66130
else
67131
gitInstallPath = GitExecutablePath;
@@ -70,7 +134,7 @@ public string GitInstallPath
70134
}
71135
}
72136

73-
public string RepositoryPath { get { return Repository.LocalPath; } }
137+
public string RepositoryPath { get; private set; }
74138
public IRepository Repository { get; set; }
75139

76140
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: 8 additions & 5 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());
@@ -203,13 +203,16 @@ public Task<bool> SetupGitIfNeeded(NPath tempPath, IProgress<float> zipFileProgr
203203

204204
try
205205
{
206-
logger.Trace("Copying \"{0}\" to \"{1}\"", unzipPath, PackageDestinationDirectory);
206+
PackageDestinationDirectory.DeleteIfExists();
207+
PackageDestinationDirectory.EnsureParentDirectoryExists();
207208

208-
unzipPath.Copy(PackageDestinationDirectory);
209+
logger.Trace("Moving \"{0}\" to \"{1}\"", unzipPath, PackageDestinationDirectory);
210+
211+
unzipPath.Move(PackageDestinationDirectory);
209212
}
210213
catch (Exception ex)
211214
{
212-
logger.Error(ex, "Error CopyingArchive Source:\"{0}\" OutDir:\"{1}\"", tempPath, PackageDestinationDirectory);
215+
logger.Error(ex, "Error Moving \"{0}\" to \"{1}\"", tempPath, PackageDestinationDirectory);
213216
return TaskEx.FromResult(false);
214217
}
215218
unzipPath.DeleteIfExists();
@@ -236,7 +239,7 @@ public Task<bool> SetupGitLfsIfNeeded(NPath tempPath, IProgress<float> zipFilePr
236239
{
237240
logger.Warning("Archive \"{0}\" missing", archiveFilePath.ToString());
238241

239-
archiveFilePath = environment.ExtensionInstallPath.ToNPath().Combine(archiveFilePath);
242+
archiveFilePath = environment.ExtensionInstallPath.Combine(archiveFilePath);
240243
if (!archiveFilePath.FileExists())
241244
{
242245
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)