3
3
4
4
namespace GitHub . Unity
5
5
{
6
+ class GitInstallDetails
7
+ {
8
+ public NPath GitInstallPath { get ; }
9
+ public string GitExec { get ; }
10
+ public NPath GitExecPath { get ; }
11
+ public string GitLfsExec { get ; }
12
+ public NPath GitLfsExecPath { get ; }
13
+
14
+ public const string ExtractedMD5 = "65fd0575d3b47d8207b9e19d02faca4f" ;
15
+ public const string FileListMD5 = "a152a216b2e76f6c127053251187a278" ;
16
+
17
+ private const string PackageVersion = "f02737a78695063deace08e96d5042710d3e32db" ;
18
+ private const string PackageName = "PortableGit" ;
19
+ private const string PackageNameWithVersion = PackageName + "_" + PackageVersion ;
20
+
21
+ private readonly bool onWindows ;
22
+
23
+ public GitInstallDetails ( NPath targetInstallPath , bool onWindows )
24
+ {
25
+ this . onWindows = onWindows ;
26
+ var gitInstallPath = targetInstallPath . Combine ( ApplicationInfo . ApplicationName , PackageNameWithVersion ) ;
27
+ GitInstallPath = gitInstallPath ;
28
+
29
+ if ( onWindows )
30
+ {
31
+ GitExec += "git.exe" ;
32
+ GitLfsExec += "git-lfs.exe" ;
33
+
34
+ GitExecPath = gitInstallPath . Combine ( "cmd" , GitExec ) ;
35
+ }
36
+ else
37
+ {
38
+ GitExec = "git" ;
39
+ GitLfsExec = "git-lfs" ;
40
+
41
+ GitExecPath = gitInstallPath . Combine ( "bin" , GitExec ) ;
42
+ }
43
+
44
+ GitLfsExecPath = GetGitLfsExecPath ( gitInstallPath ) ;
45
+ }
46
+
47
+ public NPath GetGitLfsExecPath ( NPath gitInstallRoot )
48
+ {
49
+ return onWindows
50
+ ? gitInstallRoot . Combine ( "mingw32" , "libexec" , "git-core" , GitLfsExec )
51
+ : gitInstallRoot . Combine ( "libexec" , "git-core" , GitLfsExec ) ;
52
+ }
53
+ }
54
+
6
55
class GitInstaller
7
56
{
8
57
private static ILogging Logger = Logging . GetLogger < GitInstaller > ( ) ;
9
58
10
59
private readonly IEnvironment environment ;
11
60
private readonly IZipHelper sharpZipLibHelper ;
12
61
private readonly CancellationToken cancellationToken ;
13
- private readonly PortableGitInstallDetails installDetails ;
62
+ private readonly GitInstallDetails installDetails ;
14
63
15
- public GitInstaller ( IEnvironment environment , CancellationToken cancellationToken , PortableGitInstallDetails installDetails )
16
- : this ( environment , null , cancellationToken , installDetails )
64
+ public GitInstaller ( IEnvironment environment , CancellationToken cancellationToken , GitInstallDetails installDetails )
65
+ : this ( environment , ZipHelper . Instance , cancellationToken , installDetails )
17
66
{
18
67
}
19
68
20
- public GitInstaller ( IEnvironment environment , IZipHelper sharpZipLibHelper , CancellationToken cancellationToken , PortableGitInstallDetails installDetails )
69
+ public GitInstaller ( IEnvironment environment , IZipHelper sharpZipLibHelper , CancellationToken cancellationToken , GitInstallDetails installDetails )
21
70
{
22
71
this . environment = environment ;
23
72
this . sharpZipLibHelper = sharpZipLibHelper ;
@@ -34,38 +83,73 @@ public void SetupGitIfNeeded(ActionTask<NPath> onSuccess, ITask onFailure)
34
83
onFailure . Start ( ) ;
35
84
}
36
85
37
- new FuncTask < bool > ( cancellationToken , IsPortableGitExtracted )
86
+ new FuncTask < bool > ( cancellationToken , IsGitExtracted )
38
87
. Then ( ( success , isPortableGitExtracted ) => {
39
88
40
89
Logger . Trace ( "IsPortableGitExtracted: {0}" , isPortableGitExtracted ) ;
41
90
42
91
if ( isPortableGitExtracted )
43
92
{
93
+ Logger . Trace ( "SetupGitIfNeeded: Skipped" ) ;
94
+
44
95
new FuncTask < NPath > ( cancellationToken , ( ) => installDetails . GitExecPath )
45
96
. Then ( onSuccess )
46
97
. Start ( ) ;
47
98
}
48
99
else
49
100
{
50
- new PortableGitInstallTask ( cancellationToken , environment , installDetails ) . Then ( ( b , path ) => {
51
- if ( b && path != null )
52
- {
53
- new FuncTask < NPath > ( cancellationToken , ( ) => path )
54
- . Then ( onSuccess )
55
- . Start ( ) ;
56
- }
57
- else
58
- {
59
- onFailure . Start ( ) ;
60
- }
61
- } ) . Start ( ) ;
62
-
101
+ var tempZipPath = NPath . CreateTempDirectory ( "git_zip_paths" ) ;
102
+ var gitArchivePath = AssemblyResources . ToFile ( ResourceType . Platform , "git.zip" , tempZipPath , environment ) ;
103
+ var gitLfsArchivePath = AssemblyResources . ToFile ( ResourceType . Platform , "git-lfs.zip" , tempZipPath , environment ) ;
104
+
105
+ var gitExtractPath = tempZipPath . Combine ( "git" ) . CreateDirectory ( ) ;
106
+ var gitLfsExtractPath = tempZipPath . Combine ( "git-lfs" ) . CreateDirectory ( ) ;
107
+
108
+ new UnzipTask ( cancellationToken , gitArchivePath , gitExtractPath , sharpZipLibHelper )
109
+ . Then ( new UnzipTask ( cancellationToken , gitLfsArchivePath , gitLfsExtractPath , sharpZipLibHelper ) )
110
+ . Then ( ( ) => {
111
+ var targetGitLfsExecPath = installDetails . GetGitLfsExecPath ( gitExtractPath ) ;
112
+ var extractGitLfsExePath = gitLfsExtractPath . Combine ( installDetails . GitLfsExec ) ;
113
+ extractGitLfsExePath . Move ( targetGitLfsExecPath ) ;
114
+
115
+ var extractedMD5 = environment . FileSystem . CalculateFolderMD5 ( gitExtractPath ) ;
116
+ if ( ! extractedMD5 . Equals ( GitInstallDetails . ExtractedMD5 , StringComparison . InvariantCultureIgnoreCase ) )
117
+ {
118
+ Logger . Warning ( "MD5 {0} does not match expected {1}" , extractedMD5 , GitInstallDetails . ExtractedMD5 ) ;
119
+ Logger . Warning ( "Failed PortableGitInstallTask" ) ;
120
+ throw new Exception ( ) ;
121
+ }
122
+
123
+ Logger . Trace ( "Moving tempDirectory:\" {0}\" to extractTarget:\" {1}\" " , gitExtractPath ,
124
+ installDetails . GitInstallPath ) ;
125
+
126
+ gitExtractPath . Move ( installDetails . GitInstallPath ) ;
127
+
128
+ Logger . Trace ( "Deleting tempZipPath:\" {0}\" " , tempZipPath ) ;
129
+ tempZipPath . DeleteIfExists ( ) ;
130
+
131
+ } ) . Finally ( ( b , exception ) => {
132
+ if ( b )
133
+ {
134
+ Logger . Trace ( "SetupGitIfNeeded: Success" ) ;
135
+
136
+ new FuncTask < NPath > ( cancellationToken , ( ) => installDetails . GitExecPath )
137
+ . Then ( onSuccess )
138
+ . Start ( ) ;
139
+ }
140
+ else
141
+ {
142
+ Logger . Trace ( "SetupGitIfNeeded: Failed" ) ;
143
+
144
+ onFailure . Start ( ) ;
145
+ }
146
+ } ) . Start ( ) ;
63
147
}
64
148
65
149
} ) . Start ( ) ;
66
150
}
67
151
68
- private bool IsPortableGitExtracted ( )
152
+ private bool IsGitExtracted ( )
69
153
{
70
154
if ( ! installDetails . GitInstallPath . DirectoryExists ( ) )
71
155
{
@@ -74,9 +158,9 @@ private bool IsPortableGitExtracted()
74
158
}
75
159
76
160
var fileListMD5 = environment . FileSystem . CalculateFolderMD5 ( installDetails . GitInstallPath , false ) ;
77
- if ( ! fileListMD5 . Equals ( PortableGitInstallDetails . FileListMD5 , StringComparison . InvariantCultureIgnoreCase ) )
161
+ if ( ! fileListMD5 . Equals ( GitInstallDetails . FileListMD5 , StringComparison . InvariantCultureIgnoreCase ) )
78
162
{
79
- Logger . Trace ( "MD5 {0} does not match expected {1}" , fileListMD5 , PortableGitInstallDetails . FileListMD5 ) ;
163
+ Logger . Trace ( "MD5 {0} does not match expected {1}" , fileListMD5 , GitInstallDetails . FileListMD5 ) ;
80
164
return false ;
81
165
}
82
166
0 commit comments