@@ -6,7 +6,7 @@ namespace GitHub.Unity
66{
77 abstract class ApplicationManagerBase : IApplicationManager
88 {
9- protected static readonly ILogging logger = Logging . GetLogger < IApplicationManager > ( ) ;
9+ protected static ILogging Logger { get ; } = Logging . GetLogger < IApplicationManager > ( ) ;
1010
1111 private IEnvironment environment ;
1212 private AppConfiguration appConfiguration ;
@@ -19,129 +19,107 @@ public ApplicationManagerBase(SynchronizationContext synchronizationContext)
1919 ThreadingHelper . SetMainThread ( ) ;
2020 UIScheduler = TaskScheduler . FromCurrentSynchronizationContext ( ) ;
2121 ThreadingHelper . MainThreadScheduler = UIScheduler ;
22- CancellationTokenSource = new CancellationTokenSource ( ) ;
22+ TaskManager = new TaskManager ( UIScheduler ) ;
2323 // accessing Environment triggers environment initialization if it hasn't happened yet
24- Platform = new Platform ( Environment , Environment . FileSystem ) ;
24+ Platform = new Platform ( Environment ) ;
2525 }
2626
27- protected void Initialize ( IUIDispatcher uiDispatcher )
27+ protected void Initialize ( )
2828 {
2929 UserSettings = new UserSettings ( Environment ) ;
30- UserSettings . Initialize ( ) ;
31- Logging . TracingEnabled = UserSettings . Get ( "EnableTraceLogging" , false ) ;
32-
3330 LocalSettings = new LocalSettings ( Environment ) ;
34- LocalSettings . Initialize ( ) ;
35-
3631 SystemSettings = new SystemSettings ( Environment ) ;
32+
33+ UserSettings . Initialize ( ) ;
34+ LocalSettings . Initialize ( ) ;
3735 SystemSettings . Initialize ( ) ;
3836
37+ Logging . TracingEnabled = UserSettings . Get ( "EnableTraceLogging" , false ) ;
3938 ProcessManager = new ProcessManager ( Environment , Platform . GitEnvironment , CancellationToken ) ;
40- Platform . Initialize ( ProcessManager , uiDispatcher ) ;
39+ Platform . Initialize ( ProcessManager , TaskManager ) ;
40+ GitClient = new GitClient ( Environment , ProcessManager , Platform . CredentialManager , TaskManager ) ;
4141 }
4242
43- public virtual Task Run ( )
43+ public virtual ITask Run ( )
4444 {
45- Task task = null ;
46- try
47- {
48- task = RunInternal ( ) ;
49- }
50- catch ( Exception ex )
51- {
52- logger . Error ( ex ) ;
53- throw ;
54- }
55- return task ;
45+ var progress = new ProgressReport ( ) ;
46+ return new ActionTask ( SetupAndRestart ( progress ) ) ;
5647 }
5748
5849 protected abstract string DetermineInstallationPath ( ) ;
5950 protected abstract string GetAssetsPath ( ) ;
6051 protected abstract string GetUnityPath ( ) ;
6152
62- public virtual async Task RestartRepository ( )
53+ public virtual ITask RestartRepository ( )
6354 {
64- await ThreadingHelper . SwitchToThreadAsync ( ) ;
55+ return new FuncTask < bool > ( TaskManager . Token , _ =>
56+ {
57+ Environment . Initialize ( ) ;
6558
66- Environment . Initialize ( ) ;
59+ if ( Environment . RepositoryPath == null )
60+ return false ;
61+ return true ;
6762
68- if ( Environment . RepositoryPath != null )
63+ } )
64+ . Defer ( async s =>
6965 {
70- try
71- {
72- var repositoryManagerFactory = new RepositoryManagerFactory ( ) ;
73- repositoryManager = repositoryManagerFactory . CreateRepositoryManager ( Platform , TaskRunner , Environment . RepositoryPath , CancellationToken ) ;
74- }
75- catch ( Exception ex )
76- {
77- logger . Error ( ex ) ;
78- }
66+ if ( ! s )
67+ return false ;
68+
69+ var repositoryPathConfiguration = new RepositoryPathConfiguration ( Environment . RepositoryPath ) ;
70+ var gitConfig = new GitConfig ( repositoryPathConfiguration . DotGitConfig ) ;
71+
72+ var repositoryWatcher = new RepositoryWatcher ( Platform , repositoryPathConfiguration , TaskManager . Token ) ;
73+ repositoryManager = new RepositoryManager ( Platform , TaskManager , gitConfig , repositoryWatcher ,
74+ GitClient , repositoryPathConfiguration , TaskManager . Token ) ;
75+
76+ await repositoryManager . Initialize ( ) . SafeAwait ( ) ;
7977 Environment . Repository = repositoryManager . Repository ;
80- repositoryManager . Initialize ( ) ;
78+ Logger . Trace ( $ "Got a repository? { Environment . Repository } " ) ;
8179 repositoryManager . Start ( ) ;
82- }
80+ return true ;
81+ } )
82+ . Finally ( ( _ , __ ) => { } ) ;
8383 }
8484
85- private async Task RunInternal ( )
85+ private async Task SetupAndRestart ( ProgressReport progress )
8686 {
87- await ThreadingHelper . SwitchToThreadAsync ( ) ;
88-
89- var gitSetup = new GitSetup ( Environment , Environment . FileSystem , CancellationToken ) ;
87+ var gitSetup = new GitSetup ( Environment , CancellationToken ) ;
9088 var expectedPath = gitSetup . GitInstallationPath ;
91-
92- var setupDone = await gitSetup . SetupIfNeeded (
93- //new Progress<float>(x => logger.Trace("Percentage: {0}", x)),
94- //new Progress<long>(x => logger.Trace("Remaining: {0}", x))
95- ) ;
96-
89+ var setupDone = await gitSetup . SetupIfNeeded ( progress . Percentage , progress . Remaining ) ;
9790 if ( setupDone )
9891 Environment . GitExecutablePath = gitSetup . GitExecutablePath ;
9992 else
10093 Environment . GitExecutablePath = await LookForGitInstallationPath ( ) ;
10194
102- logger . Trace ( "Environment.GitExecutablePath \" {0}\" Exists:{1}" , gitSetup . GitExecutablePath , gitSetup . GitExecutablePath . FileExists ( ) ) ;
95+ Logger . Trace ( "Environment.GitExecutablePath \" {0}\" Exists:{1}" , gitSetup . GitExecutablePath , gitSetup . GitExecutablePath . FileExists ( ) ) ;
10396
104- await RestartRepository ( ) ;
97+ await RestartRepository ( ) . StartAwait ( ) ;
10598
10699 if ( Environment . IsWindows )
107100 {
108- string credentialHelper = null ;
109- var gitConfigGetTask = new GitConfigGetTask ( Environment , ProcessManager ,
110- new TaskResultDispatcher < string > ( s => {
111- credentialHelper = s ;
112- } ) , "credential.helper" , GitConfigSource . Global ) ;
113-
114-
115- await gitConfigGetTask . RunAsync ( CancellationToken . None ) ;
101+ var credentialHelper = await GitClient . GetConfig ( "credential.helper" , GitConfigSource . Global ) . StartAwait ( ) ;
116102
117103 if ( string . IsNullOrEmpty ( credentialHelper ) )
118104 {
119- var gitConfigSetTask = new GitConfigSetTask ( Environment , ProcessManager ,
120- new TaskResultDispatcher < string > ( s => { } ) , "credential.helper" , "wincred" ,
121- GitConfigSource . Global ) ;
122-
123- await gitConfigSetTask . RunAsync ( CancellationToken . None ) ;
105+ await GitClient . SetConfig ( "credential.helper" , "wincred" , GitConfigSource . Global ) . StartAwait ( ) ;
124106 }
125107 }
126108 }
127109
128- private async Task < string > LookForGitInstallationPath ( )
110+ private async Task < NPath > LookForGitInstallationPath ( )
129111 {
130112 NPath cachedGitInstallPath = null ;
131113 var path = SystemSettings . Get ( "GitInstallPath" ) ;
132114 if ( ! String . IsNullOrEmpty ( path ) )
133115 cachedGitInstallPath = path . ToNPath ( ) ;
134116
135117 // Root paths
136- if ( cachedGitInstallPath == null ||
137- ! cachedGitInstallPath . DirectoryExists ( ) )
118+ if ( cachedGitInstallPath != null && cachedGitInstallPath . DirectoryExists ( ) )
138119 {
139- return await GitEnvironment . FindGitInstallationPath ( ProcessManager ) ;
140- }
141- else
142- {
143- return cachedGitInstallPath . ToString ( ) ;
120+ return cachedGitInstallPath ;
144121 }
122+ return await GitClient . FindGitInstallation ( ) . SafeAwait ( ) ;
145123 }
146124
147125 private bool disposed = false ;
@@ -151,7 +129,7 @@ protected virtual void Dispose(bool disposing)
151129 {
152130 if ( disposed ) return ;
153131 disposed = true ;
154- if ( CancellationTokenSource != null ) CancellationTokenSource . Cancel ( ) ;
132+ if ( TaskManager != null ) TaskManager . Stop ( ) ;
155133 if ( repositoryManager != null ) repositoryManager . Dispose ( ) ;
156134 }
157135 }
@@ -194,11 +172,11 @@ public IEnvironment Environment
194172 public IPlatform Platform { get ; protected set ; }
195173 public virtual IProcessEnvironment GitEnvironment { get ; set ; }
196174 public IProcessManager ProcessManager { get ; protected set ; }
197- public ITaskResultDispatcher MainThreadResultDispatcher { get ; protected set ; }
198- public CancellationToken CancellationToken { get ; protected set ; }
199- public ITaskRunner TaskRunner { get ; protected set ; }
175+ public CancellationToken CancellationToken { get { return TaskManager . Token ; } }
176+ public ITaskManager TaskManager { get ; protected set ; }
177+ public IGitClient GitClient { get ; protected set ; }
178+
200179
201- protected CancellationTokenSource CancellationTokenSource { get ; private set ; }
202180 protected TaskScheduler UIScheduler { get ; private set ; }
203181 protected SynchronizationContext SynchronizationContext { get ; private set ; }
204182 protected IRepositoryManager RepositoryManager { get { return repositoryManager ; } }
0 commit comments