-
Notifications
You must be signed in to change notification settings - Fork 9
/
workload-install.ps1
277 lines (246 loc) · 10.6 KB
/
workload-install.ps1
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#
# Copyright (c) Samsung Electronics. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for full license information.
#
<#
.SYNOPSIS
Installs Tizen workload manifest.
.DESCRIPTION
Installs the WorkloadManifest.json and WorkloadManifest.targets files for Tizen to the dotnet sdk.
.PARAMETER Version
Use specific VERSION
.PARAMETER DotnetInstallDir
Dotnet SDK Location installed
#>
[cmdletbinding()]
param(
[Alias('v')][string]$Version="<latest>",
[Alias('d')][string]$DotnetInstallDir="<auto>",
[Alias('t')][string]$DotnetTargetVersionBand="<auto>",
[Alias('u')][switch]$UpdateAllWorkloads
)
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
$ProgressPreference = "SilentlyContinue"
$ManifestBaseName = "Samsung.NET.Sdk.Tizen.Manifest"
$LatestVersionMap = @{
"$ManifestBaseName-6.0.100" = "7.0.101";
"$ManifestBaseName-6.0.200" = "7.0.100-preview.13.6";
"$ManifestBaseName-6.0.300" = "7.0.304";
"$ManifestBaseName-6.0.400" = "7.0.400";
"$ManifestBaseName-7.0.100-preview.6" = "7.0.100-preview.6.14";
"$ManifestBaseName-7.0.100-preview.7" = "7.0.100-preview.7.20";
"$ManifestBaseName-7.0.100-rc.1" = "7.0.100-rc.1.22";
"$ManifestBaseName-7.0.100-rc.2" = "7.0.100-rc.2.24";
"$ManifestBaseName-7.0.100" = "7.0.103";
"$ManifestBaseName-7.0.200" = "7.0.105";
"$ManifestBaseName-8.0.100-alpha.1" = "7.0.104";
"$ManifestBaseName-8.0.100-preview.2" = "7.0.106";
"$ManifestBaseName-8.0.100-preview.3" = "7.0.107";
"$ManifestBaseName-8.0.100-preview.4" = "7.0.108";
}
function New-TemporaryDirectory {
$parent = [System.IO.Path]::GetTempPath()
$name = [System.IO.Path]::GetRandomFileName()
New-Item -ItemType Directory -Path (Join-Path $parent $name)
}
function Ensure-Directory([string]$TestDir) {
Try {
New-Item -ItemType Directory -Path $TestDir -Force -ErrorAction stop | Out-Null
[io.file]::OpenWrite($(Join-Path -Path $TestDir -ChildPath ".test-write-access")).Close()
Remove-Item -Path $(Join-Path -Path $TestDir -ChildPath ".test-write-access") -Force
}
Catch [System.UnauthorizedAccessException] {
Write-Error "No permission to install. Try run with administrator mode."
}
}
function Get-LatestVersion([string]$Id) {
$attempts=3
$sleepInSeconds=3
do
{
try
{
$Response = Invoke-WebRequest -Uri https://api.nuget.org/v3-flatcontainer/$Id/index.json -UseBasicParsing | ConvertFrom-Json
return $Response.versions | Select-Object -Last 1
}
catch {
Write-Host "Id: $Id"
Write-Host "An exception was caught: $($_.Exception.Message)"
}
$attempts--
if ($attempts -gt 0) { Start-Sleep $sleepInSeconds }
} while ($attempts -gt 0)
if ($LatestVersionMap.ContainsKey($Id))
{
Write-Host "Return cached latest version."
return $LatestVersionMap.$Id
} else {
Write-Error "Wrong Id: $Id"
}
}
function Get-Package([string]$Id, [string]$Version, [string]$Destination, [string]$FileExt = "nupkg") {
$OutFileName = "$Id.$Version.$FileExt"
$OutFilePath = Join-Path -Path $Destination -ChildPath $OutFileName
Invoke-WebRequest -Uri "https://www.nuget.org/api/v2/package/$Id/$Version" -OutFile $OutFilePath
return $OutFilePath
}
function Install-Pack([string]$Id, [string]$Version, [string]$Kind) {
$TempZipFile = $(Get-Package -Id $Id -Version $Version -Destination $TempDir -FileExt "zip")
$TempUnzipDir = Join-Path -Path $TempDir -ChildPath "unzipped\$Id"
switch ($Kind) {
"manifest" {
Expand-Archive -Path $TempZipFile -DestinationPath $TempUnzipDir
New-Item -Path $TizenManifestDir -ItemType "directory" -Force | Out-Null
Copy-Item -Path "$TempUnzipDir\data\*" -Destination $TizenManifestDir -Force
}
{($_ -eq "sdk") -or ($_ -eq "framework")} {
Expand-Archive -Path $TempZipFile -DestinationPath $TempUnzipDir
$TargetDirectory = $(Join-Path -Path $DotnetInstallDir -ChildPath "packs\$Id\$Version")
New-Item -Path $TargetDirectory -ItemType "directory" -Force | Out-Null
Copy-Item -Path "$TempUnzipDir/*" -Destination $TargetDirectory -Recurse -Force
}
"template" {
$TargetFileName = "$Id.$Version.nupkg".ToLower()
$TargetDirectory = $(Join-Path -Path $DotnetInstallDir -ChildPath "template-packs")
New-Item -Path $TargetDirectory -ItemType "directory" -Force | Out-Null
Copy-Item $TempZipFile -Destination $(Join-Path -Path $TargetDirectory -ChildPath "$TargetFileName") -Force
}
}
}
function Remove-Pack([string]$Id, [string]$Version, [string]$Kind) {
switch ($Kind) {
"manifest" {
Remove-Item -Path $TizenManifestDir -Recurse -Force
}
{($_ -eq "sdk") -or ($_ -eq "framework")} {
$TargetDirectory = $(Join-Path -Path $DotnetInstallDir -ChildPath "packs\$Id\$Version")
Remove-Item -Path $TargetDirectory -Recurse -Force
}
"template" {
$TargetFileName = "$Id.$Version.nupkg".ToLower();
Remove-Item -Path $(Join-Path -Path $DotnetInstallDir -ChildPath "template-packs\$TargetFileName") -Force
}
}
}
function Install-TizenWorkload([string]$DotnetVersion)
{
$VersionSplitSymbol = '.'
$SplitVersion = $DotnetVersion.Split($VersionSplitSymbol)
$CurrentDotnetVersion = [Version]"$($SplitVersion[0]).$($SplitVersion[1])"
$DotnetVersionBand = $SplitVersion[0] + $VersionSplitSymbol + $SplitVersion[1] + $VersionSplitSymbol + $SplitVersion[2][0] + "00"
$ManifestName = "$ManifestBaseName-$DotnetVersionBand"
if ($DotnetTargetVersionBand -eq "<auto>" -or $UpdateAllWorkloads.IsPresent) {
if ($CurrentDotnetVersion -ge "7.0")
{
$IsPreviewVersion = $DotnetVersion.Contains("-preview") -or $DotnetVersion.Contains("-rc") -or $DotnetVersion.Contains("-alpha")
if ($IsPreviewVersion -and ($SplitVersion.Count -ge 4)) {
$DotnetTargetVersionBand = $DotnetVersionBand + $SplitVersion[2].SubString(3) + $VersionSplitSymbol + $($SplitVersion[3])
$ManifestName = "$ManifestBaseName-$DotnetTargetVersionBand"
}
else {
$DotnetTargetVersionBand = $DotnetVersionBand
}
}
else {
$DotnetTargetVersionBand = $DotnetVersionBand
}
}
# Check latest version of manifest.
if ($Version -eq "<latest>" -or $UpdateAllWorkloads.IsPresent) {
$Version = Get-LatestVersion -Id $ManifestName
}
# Check workload manifest directory.
$ManifestDir = Join-Path -Path $DotnetInstallDir -ChildPath "sdk-manifests" | Join-Path -ChildPath $DotnetTargetVersionBand
$TizenManifestDir = Join-Path -Path $ManifestDir -ChildPath "samsung.net.sdk.tizen"
$TizenManifestFile = Join-Path -Path $TizenManifestDir -ChildPath "WorkloadManifest.json"
# Check and remove already installed old version.
if (Test-Path $TizenManifestFile) {
$ManifestJson = $(Get-Content $TizenManifestFile | ConvertFrom-Json)
$OldVersion = $ManifestJson.version
if ($OldVersion -eq $Version) {
$DotnetWorkloadList = Invoke-Expression "& '$DotnetCommand' workload list | Select-String -Pattern '^tizen'"
if ($DotnetWorkloadList)
{
Write-Host "Tizen Workload $Version version is already installed."
Continue
}
}
Ensure-Directory $ManifestDir
Write-Host "Removing $ManifestName/$OldVersion from $ManifestDir..."
Remove-Pack -Id $ManifestName -Version $OldVersion -Kind "manifest"
$ManifestJson.packs.PSObject.Properties | ForEach-Object {
Write-Host "Removing $($_.Name)/$($_.Value.version)..."
Remove-Pack -Id $_.Name -Version $_.Value.version -Kind $_.Value.kind
}
}
Ensure-Directory $ManifestDir
$TempDir = $(New-TemporaryDirectory)
# Install workload manifest.
Write-Host "Installing $ManifestName/$Version to $ManifestDir..."
Install-Pack -Id $ManifestName -Version $Version -Kind "manifest"
# Download and install workload packs.
$NewManifestJson = $(Get-Content $TizenManifestFile | ConvertFrom-Json)
$NewManifestJson.packs.PSObject.Properties | ForEach-Object {
Write-Host "Installing $($_.Name)/$($_.Value.version)..."
Install-Pack -Id $_.Name -Version $_.Value.version -Kind $_.Value.kind
}
# Add tizen to the installed workload metadata.
# Featured version band for metadata does NOT include any preview specifier.
# https://github.com/dotnet/sdk/blob/main/documentation/general/workloads/user-local-workloads.md
New-Item -Path $(Join-Path -Path $DotnetInstallDir -ChildPath "metadata\workloads\$DotnetVersionBand\InstalledWorkloads\tizen") -Force | Out-Null
if (Test-Path $(Join-Path -Path $DotnetInstallDir -ChildPath "metadata\workloads\$DotnetVersionBand\InstallerType\msi")) {
New-Item -Path "HKLM:\SOFTWARE\Microsoft\dotnet\InstalledWorkloads\Standalone\x64\$DotnetTargetVersionBand\tizen" -Force | Out-Null
}
# Clean up
Remove-Item -Path $TempDir -Force -Recurse
Write-Host "Done installing Tizen workload $Version"
}
# Check dotnet install directory.
if ($DotnetInstallDir -eq "<auto>") {
if ($Env:DOTNET_ROOT -And $(Test-Path "$Env:DOTNET_ROOT")) {
$DotnetInstallDir = $Env:DOTNET_ROOT
} else {
$DotnetInstallDir = Join-Path -Path $Env:Programfiles -ChildPath "dotnet"
}
}
if (-Not $(Test-Path "$DotnetInstallDir")) {
Write-Error "No installed dotnet '$DotnetInstallDir'."
}
# Check installed dotnet version
$DotnetCommand = "$DotnetInstallDir\dotnet"
if (Get-Command $DotnetCommand -ErrorAction SilentlyContinue)
{
if ($UpdateAllWorkloads.IsPresent)
{
$InstalledDotnetSdks = Invoke-Expression "& '$DotnetCommand' --list-sdks | Select-String -Pattern '^6|^7'" | ForEach-Object {$_ -replace (" \[.*","")}
}
else
{
$InstalledDotnetSdks = Invoke-Expression "& '$DotnetCommand' --version"
}
}
else
{
Write-Error "'$DotnetCommand' occurs an error."
}
if (-Not $InstalledDotnetSdks)
{
Write-Host "`n.NET SDK version 6 or later is required to install Tizen Workload."
}
else
{
foreach ($DotnetSdk in $InstalledDotnetSdks)
{
try {
Write-Host "`nCheck Tizen Workload for sdk $DotnetSdk"
Install-TizenWorkload -DotnetVersion $DotnetSdk
}
catch {
Write-Host "Failed to install Tizen Workload for sdk $DotnetSdk"
Write-Host "$_"
Continue
}
}
}
Write-Host "`nDone"