-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-VSCodeAG.psm1
260 lines (251 loc) · 10 KB
/
Get-VSCodeAG.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
using namespace System.Net.Http
using namespace System.Net.Security
using namespace System.Net.ServicePointManager
function Get-RedirectedUrl {
Param (
[Parameter(Mandatory=$true)]
[String]$URL
)
# Add-Type -AssemblyName System.Net.Http, System.Net.ServicePointManager, System.Net.SecurityProtocolType
# Write-Host "Get-RedirectedUrl"
[System.Net.ServicePointManager]::DefaultConnectionLimit = 1024
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
$handler = New-Object HttpClientHandler
$handler.AllowAutoRedirect=$false
$client = New-Object HttpClient($handler)
# $response = ($client.GetStringAsync($URL).GetAwaiter().GetResult())
# $URL = "https://code.visualstudio.com/sha/download?build=stable&os=win32-x64-archive"
try {
$response = $client.GetAsync($URL)
} catch {
Write-Host $_.Exception
$StatusCode = $_.Exception.Response.StatusCode.value_
Write-Host "Error:"+ $StatusCode
}
if($response.Result.ReasonPhrase -eq "Found") {
return $response.Result.Headers.Location.OriginalString
}
}
function New-DataFolder() {
param(
$Destination,
$ReleaseDir,
$DataFolderPath
)
$extensionsDir = $ReleaseDir+"extensions"
# Write-Host $DataFolderPath
if(Test-Path -Path $DataFolderPath) {
if(Test-Path -Path $extensionsDir) {
# $dataPath = $Destination+"data"
# $extensionsDir = $ReleaseDir+"extensions"
[void](Move-Item -Path $extensionsDir -Destination $DataFolderPath.ToString())
} else {
Write-Host "No extensions dir to copy"
}
$ProfileDataToCopy = $env:APPDATA+"\Code"
Copy-Item -Recurse -Path $ProfileDataToCopy -Destination $DataFolderPath.ToString()
$ProfileDataDirToRename = $DataFolderPath.ToString()+"\Code"
$ProfileDataDirNewName = $DataFolderPath.ToString()+"\user-data"
Move-Item -Path $ProfileDataDirToRename -Destination $ProfileDataDirNewName
} else {
[void](New-Item -ItemType Directory -Path $Destination -Name "data")
}
$ProfileDataToCopy = $env:APPDATA+"\Code"
$ProfileDataDirToRename = $DataFolderPath.ToString()+"\Code"
$ProfileDataDirNewName = $DataFolderPath.ToString()+"\user-data"
if(Test-Path -Path $ProfileDataToCopy) {
if(Test-Path -Path $ProfileDataDirToRename) {
Move-Item -Path $ProfileDataDirToRename -Destination $ProfileDataDirNewName
} else {
if(Test-Path -Path $ProfileDataDirNewName) {
Write-Information -MessageData "Nothing to do, all VS Code Portable directories are correctly named"
# Write-EventLog -LogName 'Windows PowerShell' -EntryType Information -Message "Nothing to do, all VS Code Portable directories are correctly named"
}
}
}
}
function New-VSCodePortable() {
param(
$ReleaseDir
)
foreach($item in (Get-ChildItem -Path $ReleaseDir)) {
if($item.Mode -ne "d-----"){
if($item.Name -like "*.zip" -and $item.Name -notlike "*server*") {
# write-Host $item
[string] $archive = $item
}
}
}
$dirNameFromArchiveName = $archive.Remove($archive.LastIndexOf("."),4)
$DestinationPath = ""
$DestinationPath = $ReleaseDir+$dirNameFromArchiveName
if(Test-Path -Path $DestinationPath) {
# $NewDir = $ReleaseDir+$dirNameFromArchiveName
Write-Host $DestinationPath
$PathToArchive = $ReleaseDir+$archive
[void](Remove-Item -Recurse $DestinationPath)
[void](New-Item -ItemType Directory -Path $ReleaseDir -Name $dirNameFromArchiveName)
Expand-Archive -Path $PathToArchive -DestinationPath $DestinationPath
} else {
# $NewDir = $ReleaseDir+$dirNameFromArchiveName
$PathToArchive = $ReleaseDir+$archive
[void](New-Item -ItemType Directory -Path $ReleaseDir -Name $dirNameFromArchiveName)
Expand-Archive -Path $PathToArchive -DestinationPath $DestinationPath
}
$dataFolder = $DestinationPath+"\data"
New-DataFolder -DataFolderPath $dataFolder -Destination $DestinationPath -ReleaseDir $ReleaseDir
}
function Get-VSCodeArchive() {
param(
$ReleaseDir
)
# Write-Host "Get-VSCodeArchive"
$files = @(
@{
Uri = "https://code.visualstudio.com/sha/download?build=stable&os=win32-x64-archive"
OutFile = "$ReleaseDir"
},
@{
Uri = "https://code.visualstudio.com/sha/download?build=stable&os=win32-x64"
OutFile = "$ReleaseDir"
}
)
$jobs = @()
foreach ($file in $files) {
[string] $urlForFileName = $file.Uri
$FileName = [System.IO.Path]::GetFileName((Get-RedirectedUrl -URL $urlForFileName))
# Write-Host "...."
# Write-Host $FileName
# Write-Host "...."
$FileName = $FileName.TrimStart(" ")
$file.Outfile = $file.Outfile+$FileName
$jobs += Start-ThreadJob -Name $file.OutFile -ScriptBlock {
$params = $using:file
try {
$Response = Invoke-WebRequest @params -UserAgent ([Microsoft.PowerShell.Commands.PSUserAgent]::Chrome)
$StatusCode = $Response.StatusCode
} catch {
Write-Host $_.Exception
$StatusCode = $_.Exception.Response.StatusCode.value_
Write-Host "Error:"+ $StatusCode
}
}
}
Write-Host "Downloads started for VS Code Archive..."
Wait-Job -Job $jobs
foreach ($job in $jobs) {
Receive-Job -Job $job
}
}
function Get-VSCodeServer() {
param(
$ReleaseDir,
# $Guid,
$Win,
$Linux
)
$files = @(
@{
Uri = "https://update.code.visualstudio.com/commit:$linux/server-linux-x64/stable"
OutFile = $ReleaseDir+"vscode-linux-x64.tar.gz"
}
@{
Uri = "https://update.code.visualstudio.com/commit:$win/server-win32-x64/stable"
OutFile = $ReleaseDir+"vscode-server-win32-x64.zip"
}
)
$jobs = @()
foreach ($file in $files) {
$jobs += Start-ThreadJob -Name $file.OutFile -ScriptBlock {
$params = $using:file
try {
$Response = Invoke-WebRequest @params -UserAgent ([Microsoft.PowerShell.Commands.PSUserAgent]::Chrome)
$StatusCode = $Response.StatusCode
} catch {
Write-Host $_.Exception
$StatusCode = $_.Exception.Response.StatusCode.value_
Write-Host "Error: $StatusCode"
}
}
}
Write-Host "Downloads started for VS Code Server specific files..."
Wait-Job -Job $jobs
foreach ($job in $jobs) {
Receive-Job -Job $job
}
}
function New-VSCodeServer() {
param(
$ReleaseDir
)
$arch_win = ConvertFrom-Json (Invoke-WebRequest -UseBasicParsing "https://update.code.visualstudio.com/api/commits/stable/win32-x64").content
$arch_linux = ConvertFrom-Json (Invoke-WebRequest -UseBasicParsing "https://update.code.visualstudio.com/api/commits/stable/linux-x64").content
Get-VSCodeServer -ReleaseDir $ReleaseDir -Win $arch_win[0] -Linux $arch_linux[0]
}
function Get-VSCodeExtensions() {
param(
$URLs
)
$jobs = @()
foreach ($file in $URLs) {
$jobs += Start-ThreadJob -Name $file.OutFile -ScriptBlock {
$params = $using:file
try {
$Response = Invoke-WebRequest @params -UserAgent ([Microsoft.PowerShell.Commands.PSUserAgent]::Chrome)
$StatusCode = $Response.StatusCode
} catch {
Write-Host $_.Exception
$StatusCode = $_.Exception.Response.StatusCode.value_
Write-Host "Error: $StatusCode"
# Write-Host $params.OutFile $params.Uri
}
}
}
Write-Host "Downloads started for VS Code Extensions specific files..."
Wait-Job -Job $jobs
foreach ($job in $jobs) {
Receive-Job -Job $job
}
}
function New-VSCodeExtensions() {
param(
$VSCodeExtension,
$ReleaseDir
)
if(Test-Path -Path $VSCodeExtension) {
$vscodeExtensionList = ConvertFrom-Json (Get-Content -Path ($VSCodeExtension + "\extensions.json"))
$files = @()
For($y = 0; $y -lt $vscodeExtensionList.Length; $y++) {
$version = $vscodeExtensionList[$y].version
[String] $relativeLocation = ($vsCodeExtensionList[$y].relativeLocation)
$publisher = $relativeLocation.Split(".")[0]
$extensionName = ($vsCodeExtensionList[$y].identifier.id).Split(".",2)[1]
if(Test-Path ($ReleaseDir+"extensions")){
$ExtensionsDir = $ReleaseDir+"extensions"
} else {
[void](New-Item -ItemType Directory -Path ($ReleaseDir+"extensions"))
$ExtensionsDir = $ReleaseDir+"extensions"
}
$fileName = ("$ExtensionsDir\$relativeLocation" +".vsix")
$files += @{
Uri = "https://$publisher.gallery.vsassets.io/_apis/public/gallery/publisher/$publisher/extension/$extensionName/$version/assetbyname/Microsoft.VisualStudio.Services.VSIXPackage"
OutFile = $fileName
}
}
Get-VSCodeExtensions -URLs $files
foreach($item in (Get-ChildItem -Path $ReleaseDir)) {
if($item.Mode -ne "d-----"){
if($item.Name -like "*.zip" -and $item.Name -notlike "*server*") {
# write-Host $item
[string] $archive = $item
}
}
}
# TODO Need to sort out what to do when option 3 is chosen
$dirNameFromArchiveName = $archive.Remove($archive.LastIndexOf("."),4)
$DestinationPath = ""
$DestinationPath = $ReleaseDir+$dirNameFromArchiveName
$dataFolder = $DestinationPath+"\data"
New-DataFolder -DataFolderPath $dataFolder -Destination $DestinationPath -ReleaseDir $ReleaseDir
}
}