@@ -6,7 +6,7 @@ namespace GitHub.Unity
6
6
{
7
7
abstract class ApplicationManagerBase : IApplicationManager
8
8
{
9
- protected static readonly ILogging logger = Logging . GetLogger < IApplicationManager > ( ) ;
9
+ protected static ILogging Logger { get ; } = Logging . GetLogger < IApplicationManager > ( ) ;
10
10
11
11
private IEnvironment environment ;
12
12
private AppConfiguration appConfiguration ;
@@ -19,129 +19,107 @@ public ApplicationManagerBase(SynchronizationContext synchronizationContext)
19
19
ThreadingHelper . SetMainThread ( ) ;
20
20
UIScheduler = TaskScheduler . FromCurrentSynchronizationContext ( ) ;
21
21
ThreadingHelper . MainThreadScheduler = UIScheduler ;
22
- CancellationTokenSource = new CancellationTokenSource ( ) ;
22
+ TaskManager = new TaskManager ( UIScheduler ) ;
23
23
// accessing Environment triggers environment initialization if it hasn't happened yet
24
- Platform = new Platform ( Environment , Environment . FileSystem ) ;
24
+ Platform = new Platform ( Environment ) ;
25
25
}
26
26
27
- protected void Initialize ( IUIDispatcher uiDispatcher )
27
+ protected void Initialize ( )
28
28
{
29
29
UserSettings = new UserSettings ( Environment ) ;
30
- UserSettings . Initialize ( ) ;
31
- Logging . TracingEnabled = UserSettings . Get ( "EnableTraceLogging" , false ) ;
32
-
33
30
LocalSettings = new LocalSettings ( Environment ) ;
34
- LocalSettings . Initialize ( ) ;
35
-
36
31
SystemSettings = new SystemSettings ( Environment ) ;
32
+
33
+ UserSettings . Initialize ( ) ;
34
+ LocalSettings . Initialize ( ) ;
37
35
SystemSettings . Initialize ( ) ;
38
36
37
+ Logging . TracingEnabled = UserSettings . Get ( "EnableTraceLogging" , false ) ;
39
38
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 ) ;
41
41
}
42
42
43
- public virtual Task Run ( )
43
+ public virtual ITask Run ( )
44
44
{
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 ) ) ;
56
47
}
57
48
58
49
protected abstract string DetermineInstallationPath ( ) ;
59
50
protected abstract string GetAssetsPath ( ) ;
60
51
protected abstract string GetUnityPath ( ) ;
61
52
62
- public virtual async Task RestartRepository ( )
53
+ public virtual ITask RestartRepository ( )
63
54
{
64
- await ThreadingHelper . SwitchToThreadAsync ( ) ;
55
+ return new FuncTask < bool > ( TaskManager . Token , _ =>
56
+ {
57
+ Environment . Initialize ( ) ;
65
58
66
- Environment . Initialize ( ) ;
59
+ if ( Environment . RepositoryPath == null )
60
+ return false ;
61
+ return true ;
67
62
68
- if ( Environment . RepositoryPath != null )
63
+ } )
64
+ . Defer ( async s =>
69
65
{
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 ( ) ;
79
77
Environment . Repository = repositoryManager . Repository ;
80
- repositoryManager . Initialize ( ) ;
78
+ Logger . Trace ( $ "Got a repository? { Environment . Repository } " ) ;
81
79
repositoryManager . Start ( ) ;
82
- }
80
+ return true ;
81
+ } )
82
+ . Finally ( ( _ , __ ) => { } ) ;
83
83
}
84
84
85
- private async Task RunInternal ( )
85
+ private async Task SetupAndRestart ( ProgressReport progress )
86
86
{
87
- await ThreadingHelper . SwitchToThreadAsync ( ) ;
88
-
89
- var gitSetup = new GitSetup ( Environment , Environment . FileSystem , CancellationToken ) ;
87
+ var gitSetup = new GitSetup ( Environment , CancellationToken ) ;
90
88
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 ) ;
97
90
if ( setupDone )
98
91
Environment . GitExecutablePath = gitSetup . GitExecutablePath ;
99
92
else
100
93
Environment . GitExecutablePath = await LookForGitInstallationPath ( ) ;
101
94
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 ( ) ) ;
103
96
104
- await RestartRepository ( ) ;
97
+ await RestartRepository ( ) . StartAwait ( ) ;
105
98
106
99
if ( Environment . IsWindows )
107
100
{
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 ( ) ;
116
102
117
103
if ( string . IsNullOrEmpty ( credentialHelper ) )
118
104
{
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 ( ) ;
124
106
}
125
107
}
126
108
}
127
109
128
- private async Task < string > LookForGitInstallationPath ( )
110
+ private async Task < NPath > LookForGitInstallationPath ( )
129
111
{
130
112
NPath cachedGitInstallPath = null ;
131
113
var path = SystemSettings . Get ( "GitInstallPath" ) ;
132
114
if ( ! String . IsNullOrEmpty ( path ) )
133
115
cachedGitInstallPath = path . ToNPath ( ) ;
134
116
135
117
// Root paths
136
- if ( cachedGitInstallPath == null ||
137
- ! cachedGitInstallPath . DirectoryExists ( ) )
118
+ if ( cachedGitInstallPath != null && cachedGitInstallPath . DirectoryExists ( ) )
138
119
{
139
- return await GitEnvironment . FindGitInstallationPath ( ProcessManager ) ;
140
- }
141
- else
142
- {
143
- return cachedGitInstallPath . ToString ( ) ;
120
+ return cachedGitInstallPath ;
144
121
}
122
+ return await GitClient . FindGitInstallation ( ) . SafeAwait ( ) ;
145
123
}
146
124
147
125
private bool disposed = false ;
@@ -151,7 +129,7 @@ protected virtual void Dispose(bool disposing)
151
129
{
152
130
if ( disposed ) return ;
153
131
disposed = true ;
154
- if ( CancellationTokenSource != null ) CancellationTokenSource . Cancel ( ) ;
132
+ if ( TaskManager != null ) TaskManager . Stop ( ) ;
155
133
if ( repositoryManager != null ) repositoryManager . Dispose ( ) ;
156
134
}
157
135
}
@@ -194,11 +172,11 @@ public IEnvironment Environment
194
172
public IPlatform Platform { get ; protected set ; }
195
173
public virtual IProcessEnvironment GitEnvironment { get ; set ; }
196
174
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
+
200
179
201
- protected CancellationTokenSource CancellationTokenSource { get ; private set ; }
202
180
protected TaskScheduler UIScheduler { get ; private set ; }
203
181
protected SynchronizationContext SynchronizationContext { get ; private set ; }
204
182
protected IRepositoryManager RepositoryManager { get { return repositoryManager ; } }
0 commit comments