1
+ using System ;
2
+ using System . Threading ;
3
+ using GitHub . Logging ;
4
+
5
+ namespace GitHub . Unity
6
+ {
7
+ class OctorunInstaller
8
+ {
9
+ private const string ExpectedOctorunVersion = "8008bf3da68428f50368cf2fe3fe290df4acad54" ;
10
+ private const string OctorunExtractedMD5 = "b7341015bc701a9f5bf83f51b1b596b7" ;
11
+
12
+ private static readonly ILogging Logger = LogHelper . GetLogger < OctorunInstaller > ( ) ;
13
+ private readonly CancellationToken cancellationToken ;
14
+
15
+ private readonly IEnvironment environment ;
16
+ private readonly IZipHelper sharpZipLibHelper ;
17
+ private readonly NPath octorunArchivePath ;
18
+ private NPath octorunPath ;
19
+
20
+ public OctorunInstaller ( IEnvironment environment , CancellationToken cancellationToken , NPath octorunPath ,
21
+ NPath octorunArchivePath = default ( NPath ) ) : this ( environment , cancellationToken , octorunPath , ZipHelper . Instance ,
22
+ octorunArchivePath )
23
+ { }
24
+
25
+ public OctorunInstaller ( IEnvironment environment , CancellationToken cancellationToken , NPath octorunPath , IZipHelper sharpZipLibHelper , NPath octorunArchivePath = default ( NPath ) )
26
+ {
27
+ this . environment = environment ;
28
+ this . cancellationToken = cancellationToken ;
29
+ this . octorunPath = octorunPath ;
30
+ this . sharpZipLibHelper = sharpZipLibHelper ;
31
+ this . octorunArchivePath = octorunArchivePath ;
32
+ }
33
+
34
+ public void SetupOctorunIfNeeded ( ActionTask < NPath > onSuccess , ITask onFailure )
35
+ {
36
+ Logger . Trace ( "SetupOctorunIfNeeded" ) ;
37
+
38
+ var isOctorunExtracted = IsOctorunExtracted ( ) ;
39
+ Logger . Trace ( "isOctorunExtracted: {0}" , isOctorunExtracted ) ;
40
+
41
+ if ( ! isOctorunExtracted )
42
+ {
43
+ ExtractOctorun ( onSuccess , onFailure ) ;
44
+ }
45
+ else
46
+ {
47
+ onSuccess . PreviousResult = octorunPath ;
48
+ onSuccess . Start ( ) ;
49
+ }
50
+ }
51
+
52
+ private void ExtractOctorun ( ActionTask < NPath > onSuccess , ITask onFailure )
53
+ {
54
+ Logger . Trace ( "ExtractOctorun" ) ;
55
+
56
+ var tempZipExtractPath = NPath . CreateTempDirectory ( "octorun_extract_archive_path" ) ;
57
+ var resultTask = new UnzipTask ( cancellationToken , octorunArchivePath , tempZipExtractPath , sharpZipLibHelper ,
58
+ environment . FileSystem , OctorunExtractedMD5 )
59
+ . Then ( s => MoveOctorun ( tempZipExtractPath ) ) ;
60
+
61
+ resultTask . Then ( onFailure , TaskRunOptions . OnFailure ) ;
62
+ resultTask . Then ( onSuccess , TaskRunOptions . OnSuccess ) ;
63
+
64
+ resultTask . Start ( ) ;
65
+ }
66
+
67
+ private NPath MoveOctorun ( NPath octorunExtractPath )
68
+ {
69
+ Logger . Trace ( $ "Moving tempDirectory:'{ octorunExtractPath } ' to extractTarget:'{ octorunPath } '") ;
70
+
71
+ octorunPath . DeleteIfExists ( ) ;
72
+ octorunPath . EnsureParentDirectoryExists ( ) ;
73
+ octorunExtractPath . Move ( octorunPath ) ;
74
+
75
+ Logger . Trace ( $ "Deleting targetGitLfsExecPath:'{ octorunExtractPath } '") ;
76
+ octorunExtractPath . DeleteIfExists ( ) ;
77
+
78
+ return octorunPath ;
79
+ }
80
+
81
+ private bool IsOctorunExtracted ( )
82
+ {
83
+ if ( ! octorunPath . DirectoryExists ( ) )
84
+ {
85
+ Logger . Warning ( $ "{ octorunPath } does not exist") ;
86
+ return false ;
87
+ }
88
+
89
+ var versionFilePath = octorunPath . Combine ( "version" ) ;
90
+
91
+ if ( ! versionFilePath . FileExists ( ) )
92
+ {
93
+ Logger . Warning ( $ "{ versionFilePath } does not exist") ;
94
+ return false ;
95
+ }
96
+
97
+ var octorunVersion = versionFilePath . ReadAllText ( ) ;
98
+ if ( ! ExpectedOctorunVersion . Equals ( octorunVersion ) )
99
+ {
100
+ Logger . Warning ( "Current version {0} does not match expected {1}" , octorunVersion , ExpectedOctorunVersion ) ;
101
+ return false ;
102
+ }
103
+
104
+ return true ;
105
+ }
106
+ }
107
+ }
0 commit comments