11using System ;
22using System . Threading ;
3- using System . Threading . Tasks ;
43using GitHub . Logging ;
54
65namespace GitHub . Unity
@@ -11,59 +10,75 @@ class GitInstaller
1110 private readonly CancellationToken cancellationToken ;
1211
1312 private readonly IEnvironment environment ;
13+ private readonly IProcessManager processManager ;
1414 private readonly GitInstallDetails installDetails ;
1515 private readonly IZipHelper sharpZipLibHelper ;
1616
17+ GitInstallationState installationState ;
1718 ITask < NPath > installationTask ;
1819
19- public GitInstaller ( IEnvironment environment , CancellationToken cancellationToken ,
20+ public GitInstaller ( IEnvironment environment , IProcessManager processManager ,
21+ ITaskManager taskManager ,
2022 GitInstallDetails installDetails = null )
2123 {
2224 this . environment = environment ;
25+ this . processManager = processManager ;
2326 this . sharpZipLibHelper = ZipHelper . Instance ;
24- this . cancellationToken = cancellationToken ;
27+ this . cancellationToken = taskManager . Token ;
2528 this . installDetails = installDetails ?? new GitInstallDetails ( environment . UserCachePath , environment . IsWindows ) ;
2629 }
2730
2831 public ITask < NPath > SetupGitIfNeeded ( )
2932 {
3033 //Logger.Trace("SetupGitIfNeeded");
3134
32- installationTask = new FuncTask < GitInstallationState , NPath > ( cancellationToken , ( _ , r ) => installDetails . GitExecutablePath )
35+ installationTask = new FuncTask < NPath , NPath > ( cancellationToken , ( success , path ) =>
36+ {
37+ return path ;
38+ } )
3339 { Name = "Git Installation - Complete" } ;
3440 installationTask . OnStart += thisTask => thisTask . UpdateProgress ( 0 , 100 ) ;
3541 installationTask . OnEnd += ( thisTask , result , success , exception ) => thisTask . UpdateProgress ( 100 , 100 ) ;
3642
43+ ITask < NPath > startTask = null ;
3744 if ( ! environment . IsWindows )
38- return installationTask ;
39-
40- var startTask = new FuncTask < GitInstallationState > ( cancellationToken , ( ) =>
45+ {
46+ startTask = new FindExecTask ( "git" , cancellationToken )
47+ . Configure ( processManager ) ;
48+ // we should doublecheck that system git is usable here
49+ installationState = new GitInstallationState
4150 {
42- var state = VerifyGitInstallation ( ) ;
43- if ( ! state . GitIsValid && ! state . GitLfsIsValid )
44- state = GrabZipFromResources ( state ) ;
45- else
46- Logger . Trace ( "SetupGitIfNeeded: Skipped" ) ;
47- return state ;
48- } )
51+ GitIsValid = true ,
52+ GitLfsIsValid = true
53+ } ;
54+ }
55+ else
56+ {
57+ startTask = new FuncTask < NPath > ( cancellationToken , ( ) =>
58+ {
59+ installationState = VerifyGitInstallation ( ) ;
60+ if ( ! installationState . GitIsValid && ! installationState . GitLfsIsValid )
61+ installationState = GrabZipFromResources ( installationState ) ;
62+ else
63+ Logger . Trace ( "SetupGitIfNeeded: Skipped" ) ;
64+ return installDetails . GitExecutablePath ;
65+ } )
4966 { Name = "Git Installation - Extract" } ;
5067
68+ }
5169
52- startTask . OnEnd += ( thisTask , state , success , exception ) =>
70+ startTask . OnEnd += ( thisTask , path , success , exception ) =>
5371 {
54- if ( ! state . GitIsValid && ! state . GitLfsIsValid )
72+ if ( ! installationState . GitIsValid && ! installationState . GitLfsIsValid )
5573 {
56- if ( ! state . GitZipExists || ! state . GitLfsZipExists )
57- thisTask = thisTask . Then ( CreateDownloadTask ( state ) ) ;
58- thisTask = thisTask . Then ( ExtractPortableGit ( state ) ) ;
74+ if ( ! installationState . GitZipExists || ! installationState . GitLfsZipExists )
75+ thisTask = thisTask . Then ( CreateDownloadTask ( installationState ) ) ;
76+ thisTask = thisTask . Then ( ExtractPortableGit ( installationState ) ) ;
5977 }
6078 thisTask . Then ( installationTask ) ;
6179 } ;
6280
63- // we want to start the startTask and not the installationTask because the latter only gets
64- // appended to the task chain when startTask ends, so calling Start() on it wouldn't work
65- startTask . Start ( ) ;
66- return installationTask ;
81+ return startTask ;
6782 }
6883
6984 private GitInstallationState VerifyGitInstallation ( )
@@ -123,7 +138,7 @@ private GitInstallationState GrabZipFromResources(GitInstallationState state)
123138 return state ;
124139 }
125140
126- private ITask < GitInstallationState > CreateDownloadTask ( GitInstallationState state )
141+ private ITask < NPath > CreateDownloadTask ( GitInstallationState state )
127142 {
128143 var downloader = new Downloader ( ) ;
129144 downloader . QueueDownload ( installDetails . GitZipUrl , installDetails . GitZipMd5Url , installDetails . ZipPath ) ;
@@ -133,11 +148,11 @@ private ITask<GitInstallationState> CreateDownloadTask(GitInstallationState stat
133148 state . GitZipExists = installDetails . GitZipPath . FileExists ( ) ;
134149 state . GitLfsZipExists = installDetails . GitLfsZipPath . FileExists ( ) ;
135150 installationTask . UpdateProgress ( 40 , 100 ) ;
136- return state ;
151+ return installDetails . ZipPath ;
137152 } ) ;
138153 }
139154
140- private FuncTask < GitInstallationState > ExtractPortableGit ( GitInstallationState state )
155+ private FuncTask < NPath > ExtractPortableGit ( GitInstallationState state )
141156 {
142157 ITask < NPath > task = null ;
143158 var tempZipExtractPath = NPath . CreateTempDirectory ( "git_zip_extract_zip_paths" ) ;
@@ -146,7 +161,7 @@ private FuncTask<GitInstallationState> ExtractPortableGit(GitInstallationState s
146161 if ( ! state . GitIsValid )
147162 {
148163 ITask < NPath > unzipTask = new UnzipTask ( cancellationToken , installDetails . GitZipPath , gitExtractPath , sharpZipLibHelper ,
149- environment . FileSystem , GitInstallDetails . GitExtractedMD5 ) ;
164+ environment . FileSystem ) ;
150165 unzipTask . Progress ( p => installationTask . UpdateProgress ( 40 + ( long ) ( 20 * p . Percentage ) , 100 , unzipTask . Name ) ) ;
151166
152167 unzipTask = unzipTask . Then ( ( s , path ) =>
@@ -169,7 +184,7 @@ private FuncTask<GitInstallationState> ExtractPortableGit(GitInstallationState s
169184 if ( ! state . GitLfsIsValid )
170185 {
171186 ITask < NPath > unzipTask = new UnzipTask ( cancellationToken , installDetails . GitLfsZipPath , gitLfsExtractPath , sharpZipLibHelper ,
172- environment . FileSystem , GitInstallDetails . GitLfsExtractedMD5 ) ;
187+ environment . FileSystem ) ;
173188 unzipTask . Progress ( p => installationTask . UpdateProgress ( 60 + ( long ) ( 20 * p . Percentage ) , 100 , unzipTask . Name ) ) ;
174189
175190 unzipTask = unzipTask . Then ( ( s , path ) =>
@@ -187,10 +202,10 @@ private FuncTask<GitInstallationState> ExtractPortableGit(GitInstallationState s
187202 task = task ? . Then ( unzipTask ) ?? unzipTask ;
188203 }
189204
190- return task . Finally ( new FuncTask < GitInstallationState > ( cancellationToken , ( success ) =>
205+ return task . Finally ( new FuncTask < NPath > ( cancellationToken , ( success ) =>
191206 {
192207 tempZipExtractPath . DeleteIfExists ( ) ;
193- return state ;
208+ return installDetails . GitInstallationPath ;
194209 } ) ) ;
195210 }
196211
0 commit comments