22using System . Linq ;
33using System . Threading ;
44using System . Threading . Tasks ;
5- using Octokit ;
65using System . Collections . Generic ;
6+ using GitHub . Logging ;
77
88namespace GitHub . Unity
99{
1010 abstract class ApplicationManagerBase : IApplicationManager
1111 {
12- protected static ILogging Logger { get ; } = Logging . GetLogger < IApplicationManager > ( ) ;
12+ protected static ILogging Logger { get ; } = LogHelper . GetLogger < IApplicationManager > ( ) ;
1313
1414 private RepositoryManager repositoryManager ;
1515
@@ -36,49 +36,49 @@ protected void Initialize()
3636 LocalSettings . Initialize ( ) ;
3737 SystemSettings . Initialize ( ) ;
3838
39- Logging . TracingEnabled = UserSettings . Get ( Constants . TraceLoggingKey , false ) ;
39+ LogHelper . TracingEnabled = UserSettings . Get ( Constants . TraceLoggingKey , false ) ;
4040 ProcessManager = new ProcessManager ( Environment , Platform . GitEnvironment , CancellationToken ) ;
4141 Platform . Initialize ( ProcessManager , TaskManager ) ;
42- GitClient = new GitClient ( Environment , ProcessManager , TaskManager ) ;
42+ GitClient = new GitClient ( Environment , ProcessManager , TaskManager . Token ) ;
4343 SetupMetrics ( ) ;
4444 }
4545
4646 public void Run ( bool firstRun )
47- {
48- new ActionTask ( SetupGit ( ) )
49- . Then ( RestartRepository )
50- . ThenInUI ( InitializeUI )
51- . Start ( ) ;
52- }
53-
54- private async Task SetupGit ( )
5547 {
5648 Logger . Trace ( "Run - CurrentDirectory {0}" , NPath . CurrentDirectory ) ;
5749
58- if ( Environment . GitExecutablePath == null )
50+ var gitExecutablePath = SystemSettings . Get ( Constants . GitInstallPathKey ) ? . ToNPath ( ) ;
51+ if ( gitExecutablePath != null && gitExecutablePath . FileExists ( ) ) // we have a git path
5952 {
60- Environment . GitExecutablePath = await DetermineGitExecutablePath ( ) ;
61-
62- Logger . Trace ( "Environment.GitExecutablePath \" {0}\" Exists:{1}" , Environment . GitExecutablePath , Environment . GitExecutablePath . FileExists ( ) ) ;
63-
64- if ( Environment . IsWindows )
65- {
66- var credentialHelper = await GitClient . GetConfig ( "credential.helper" , GitConfigSource . Global ) . StartAwait ( ) ;
67-
68- if ( ! string . IsNullOrEmpty ( credentialHelper ) )
69- {
70- Logger . Trace ( "Windows CredentialHelper: {0}" , credentialHelper ) ;
71- }
72- else
73- {
74- Logger . Warning ( "No Windows CredentialHeloper found: Setting to wincred" ) ;
75-
76- await GitClient . SetConfig ( "credential.helper" , "wincred" , GitConfigSource . Global ) . StartAwait ( ) ;
77- }
78- }
53+ Logger . Trace ( "Using git install path from settings: {0}" , gitExecutablePath ) ;
54+ InitializeEnvironment ( gitExecutablePath ) ;
55+ }
56+ else // we need to go find git
57+ {
58+ Logger . Trace ( "No git path found in settings" ) ;
59+
60+ var initEnvironmentTask = new ActionTask < NPath > ( CancellationToken , ( b , path ) => InitializeEnvironment ( path ) ) { Affinity = TaskAffinity . UI } ;
61+ var findExecTask = new FindExecTask ( "git" , CancellationToken )
62+ . FinallyInUI ( ( b , ex , path ) => {
63+ if ( b && path != null )
64+ {
65+ Logger . Trace ( "FindExecTask Success: {0}" , path ) ;
66+ InitializeEnvironment ( gitExecutablePath ) ;
67+ }
68+ else
69+ {
70+ Logger . Warning ( "FindExecTask Failure" ) ;
71+ Logger . Error ( "Git not found" ) ;
72+ }
73+ } ) ;
74+
75+ var applicationDataPath = Environment . GetSpecialFolder ( System . Environment . SpecialFolder . LocalApplicationData ) . ToNPath ( ) ;
76+ var installDetails = new GitInstallDetails ( applicationDataPath , true ) ;
77+ var gitInstaller = new GitInstaller ( Environment , CancellationToken , installDetails ) ;
78+
79+ // if successful, continue with environment initialization, otherwise try to find an existing git installation
80+ gitInstaller . SetupGitIfNeeded ( initEnvironmentTask , findExecTask ) ;
7981 }
80-
81- Environment . User . Initialize ( GitClient ) ;
8282 }
8383
8484 public ITask InitializeRepository ( )
@@ -136,27 +136,6 @@ public void RestartRepository()
136136 }
137137 }
138138
139- private async Task < NPath > DetermineGitExecutablePath ( ProgressReport progress = null )
140- {
141- var gitExecutablePath = SystemSettings . Get ( Constants . GitInstallPathKey ) ? . ToNPath ( ) ;
142- if ( gitExecutablePath != null && gitExecutablePath . FileExists ( ) )
143- {
144- Logger . Trace ( "Using git install path from settings" ) ;
145- return gitExecutablePath ;
146- }
147-
148- var gitInstaller = new GitInstaller ( Environment , CancellationToken ) ;
149- var setupDone = await gitInstaller . SetupIfNeeded ( progress ? . Percentage , progress ? . Remaining ) ;
150- if ( setupDone )
151- {
152- Logger . Trace ( "Setup performed using new path" ) ;
153- return gitInstaller . GitExecutablePath ;
154- }
155-
156- Logger . Trace ( "Finding git install path" ) ;
157- return await GitClient . FindGitInstallation ( ) . SafeAwait ( ) ;
158- }
159-
160139 protected void SetupMetrics ( string unityVersion , bool firstRun )
161140 {
162141 Logger . Trace ( "Setup metrics" ) ;
@@ -187,6 +166,45 @@ protected void SetupMetrics(string unityVersion, bool firstRun)
187166 protected abstract void InitializeUI ( ) ;
188167 protected abstract void SetProjectToTextSerialization ( ) ;
189168
169+ /// <summary>
170+ /// Initialize environment after finding where git is. This needs to run on the main thread
171+ /// </summary>
172+ /// <param name="gitExecutablePath"></param>
173+ private void InitializeEnvironment ( NPath gitExecutablePath )
174+ {
175+ var afterGitSetup = new ActionTask ( CancellationToken , RestartRepository )
176+ . ThenInUI ( InitializeUI ) ;
177+
178+ Environment . GitExecutablePath = gitExecutablePath ;
179+ Environment . User . Initialize ( GitClient ) ;
180+
181+ if ( Environment . IsWindows )
182+ {
183+ GitClient
184+ . GetConfig ( "credential.helper" , GitConfigSource . Global )
185+ . Then ( ( b , credentialHelper ) => {
186+ if ( ! string . IsNullOrEmpty ( credentialHelper ) )
187+ {
188+ Logger . Trace ( "Windows CredentialHelper: {0}" , credentialHelper ) ;
189+ afterGitSetup . Start ( ) ;
190+ }
191+ else
192+ {
193+ Logger . Warning ( "No Windows CredentialHeloper found: Setting to wincred" ) ;
194+
195+ GitClient . SetConfig ( "credential.helper" , "wincred" , GitConfigSource . Global )
196+ . Then ( afterGitSetup )
197+ . Start ( ) ;
198+ }
199+ } )
200+ . Start ( ) ;
201+ }
202+ else
203+ {
204+ afterGitSetup . Start ( ) ;
205+ }
206+ }
207+
190208 private bool disposed = false ;
191209 protected virtual void Dispose ( bool disposing )
192210 {
0 commit comments