1
+ param (
2
+ [Parameter (Mandatory = $true )]
3
+ [string ] $ExtractRootPath ,
4
+ [Parameter (Mandatory = $true )]
5
+ [string ] $Version ,
6
+ [ValidateSet (' GitHub' , ' AppVeyor' , ignorecase= $False )]
7
+ [string ] $Source = " GitHub"
8
+ )
9
+
10
+ $LatestVersionName = " latest" ;
11
+
12
+ function Test-LocalCopy
13
+ {
14
+ Param (
15
+ [Parameter (Mandatory = $true , Position = 0 )]
16
+ [string ] $ExtractPath ,
17
+ [Parameter (Mandatory = $true , Position = 1 )]
18
+ [string ] $FileName
19
+ )
20
+
21
+ $FilePath = [System.IO.Path ]::Combine($ExtractPath , $FileName );
22
+ if (Test-Path $FilePath )
23
+ {
24
+ Write-Host " Download '$FileName ' already exists." ;
25
+ return $true ;
26
+ }
27
+
28
+ return $false ;
29
+ }
30
+
31
+ function Find-ArchiveUrl
32
+ {
33
+ param (
34
+ [Parameter (Mandatory = $true , Position = 0 )]
35
+ [string ] $Version ,
36
+ [Parameter (Mandatory = $true , Position = 1 )]
37
+ [ValidateSet (' GitHub' , ' AppVeyor' , ignorecase= $False )]
38
+ [string ] $Source
39
+ )
40
+
41
+ Write-Host " Searching for Git Extensions release '$Version ' on '$Source '." ;
42
+ if ($Source -eq " GitHub" )
43
+ {
44
+ return Find-ArchiveUrlFromGitHub - Version $Version ;
45
+ }
46
+
47
+ if ($Source -eq " AppVeyor" )
48
+ {
49
+ return Find-ArchiveUrlFromAppVeyor - Version $Version ;
50
+ }
51
+
52
+ throw " Unable to find download URL for 'Git Extensions $Version '" ;
53
+ }
54
+
55
+ function Find-ArchiveUrlFromGitHub
56
+ {
57
+ param (
58
+ [Parameter (Mandatory = $true , Position = 0 )]
59
+ [string ] $Version
60
+ )
61
+
62
+ $BaseUrl = ' https://api.github.com/repos/gitextensions/gitextensions/releases' ;
63
+ $SelectedRelease = $null ;
64
+ if ($Version -eq $LatestVersionName )
65
+ {
66
+ $SelectedRelease = Invoke-RestMethod - Uri " $BaseUrl /latest" ;
67
+ $Version = $SelectedRelease.tag_name ;
68
+ Write-Host " Selected release '$ ( $SelectedRelease.name ) '." ;
69
+ }
70
+ else
71
+ {
72
+ $Releases = Invoke-RestMethod - Uri $BaseUrl ;
73
+ foreach ($Release in $Releases )
74
+ {
75
+ if ($Release.tag_name -eq $Version )
76
+ {
77
+ Write-Host " Selected release '$ ( $Release.name ) '." ;
78
+ $SelectedRelease = $Release ;
79
+ break ;
80
+ }
81
+ }
82
+ }
83
+
84
+ if (! ($null -eq $SelectedRelease ))
85
+ {
86
+ foreach ($Asset in $SelectedRelease.assets )
87
+ {
88
+ if ($Asset.content_type -eq " application/zip" -and $Asset.name.Contains (' Portable' ))
89
+ {
90
+ Write-Host " Selected asset '$ ( $Asset.name ) '." ;
91
+ return $Version , $Asset.browser_download_url ;
92
+ }
93
+ }
94
+ }
95
+
96
+ throw " Unable to find download URL for 'Git Extensions $Version ' on GitHub" ;
97
+ }
98
+
99
+ function Find-ArchiveUrlFromAppVeyor
100
+ {
101
+ param (
102
+ [Parameter (Mandatory = $true , Position = 0 )]
103
+ [string ] $Version
104
+ )
105
+
106
+ $UrlVersion = $Version ;
107
+ if ($UrlVersion.StartsWith (" v" ))
108
+ {
109
+ $UrlVersion = $UrlVersion.Substring (1 );
110
+ }
111
+
112
+ $UrlBase = " https://ci.appveyor.com/api" ;
113
+
114
+ try
115
+ {
116
+ if ($Version -eq $LatestVersionName )
117
+ {
118
+ $Url = " $UrlBase /projects/gitextensions/gitextensions/branch/master" ;
119
+ }
120
+ else
121
+ {
122
+ $Url = " $UrlBase /projects/gitextensions/gitextensions/build/$UrlVersion " ;
123
+ }
124
+
125
+ $BuildInfo = Invoke-RestMethod - Uri $Url ;
126
+ $Version = " v$ ( $BuildInfo.build.version ) " ;
127
+ $Job = $BuildInfo.build.jobs [0 ];
128
+ if ($Job.Status -eq " success" )
129
+ {
130
+ $JobId = $Job.jobId ;
131
+ Write-Host " Selected build job '$JobId '." ;
132
+
133
+ $AssetsUrl = " $UrlBase /buildjobs/$JobId /artifacts" ;
134
+ $Assets = Invoke-RestMethod - Method Get - Uri $AssetsUrl ;
135
+ foreach ($Asset in $Assets )
136
+ {
137
+ if ($Asset.type -eq " zip" -and $Asset.FileName.Contains (' Portable' ))
138
+ {
139
+ Write-Host " Selected asset '$ ( $Asset.FileName ) '." ;
140
+ return $Version , ($AssetsUrl + " /" + $Asset.FileName );
141
+ }
142
+ }
143
+ }
144
+ }
145
+ catch
146
+ {
147
+ if (! ($_.Exception.Response.StatusCode -eq 404 ))
148
+ {
149
+ throw ;
150
+ }
151
+ }
152
+
153
+ throw " Unable to find download URL for 'Git Extensions $Version ' on AppVeyor" ;
154
+ }
155
+
156
+ function Get-Application
157
+ {
158
+ param (
159
+ [Parameter (Mandatory = $true , Position = 0 )]
160
+ [string ] $ArchiveUrl ,
161
+ [Parameter (Mandatory = $true , Position = 1 )]
162
+ [string ] $ExtractPath ,
163
+ [Parameter (Mandatory = $true , Position = 2 )]
164
+ [string ] $FileName
165
+ )
166
+
167
+ if (! (Test-Path $ExtractPath ))
168
+ {
169
+ New-Item - ItemType directory - Path $ExtractPath | Out-Null ;
170
+ }
171
+
172
+ $FilePath = [System.IO.Path ]::Combine($ExtractPath , $FileName );
173
+
174
+ Write-Host " Downloading '$ArchiveUrl '..." ;
175
+
176
+ Invoke-WebRequest - Uri $ArchiveUrl - OutFile $FilePath ;
177
+ Expand-Archive $FilePath - DestinationPath $ExtractPath - Force;
178
+
179
+ Write-Host " Application extracted to '$ExtractPath '." ;
180
+ }
181
+
182
+ function Get-ZipFileName {
183
+ param (
184
+ [Parameter (Mandatory = $true , Position = 0 )]
185
+ [string ] $Version
186
+ )
187
+
188
+ return " GitExtensions-$Version .zip" ;
189
+ }
190
+
191
+
192
+ Push-Location $PSScriptRoot ;
193
+ try
194
+ {
195
+ $ExtractRootPath = Resolve-Path $ExtractRootPath ;
196
+ Write-Host " Extraction root path is '$ExtractRootPath '." ;
197
+
198
+ [Net.ServicePointManager ]::SecurityProtocol = [Net.SecurityProtocolType ]::Tls12;
199
+
200
+ if (! ($Version -eq $LatestVersionName ))
201
+ {
202
+ $FileName = Get-ZipFileName - Version $Version ;
203
+ if (Test-LocalCopy - ExtractPath $ExtractRootPath - FileName $FileName )
204
+ {
205
+ exit 0 ;
206
+ }
207
+ }
208
+
209
+ $SelectedVersion , $DownloadUrl = Find-ArchiveUrl - Version $Version - Source $Source ;
210
+ if ($Version -eq $LatestVersionName )
211
+ {
212
+ $FileName = Get-ZipFileName - Version $SelectedVersion ;
213
+ if (Test-LocalCopy - ExtractPath $ExtractRootPath - FileName $FileName )
214
+ {
215
+ exit 0 ;
216
+ }
217
+ }
218
+
219
+ Get-Application - ArchiveUrl $DownloadUrl - ExtractPath $ExtractRootPath - FileName $FileName ;
220
+ }
221
+ catch
222
+ {
223
+ Write-Host $_.Exception - ForegroundColor Red;
224
+ exit -1 ;
225
+ }
226
+ finally
227
+ {
228
+ Pop-Location ;
229
+ }
0 commit comments