|
| 1 | +# |
| 2 | +# Copyright (c) Samsung Electronics. All rights reserved. |
| 3 | +# Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 4 | +# |
| 5 | + |
| 6 | +<# |
| 7 | +.SYNOPSIS |
| 8 | +Installs Tizen workload manifest. |
| 9 | +.DESCRIPTION |
| 10 | +Installs the WorkloadManifest.json and WorkloadManifest.targets files for Tizen to the dotnet sdk. |
| 11 | +.PARAMETER Version |
| 12 | +Use specific VERSION |
| 13 | +.PARAMETER DotnetInstallDir |
| 14 | +Dotnet SDK Location installed |
| 15 | +#> |
| 16 | + |
| 17 | +[cmdletbinding()] |
| 18 | +param( |
| 19 | + [Alias('v')][string]$Version="<latest>", |
| 20 | + [Alias('d')][string]$DotnetInstallDir="<auto>", |
| 21 | + [Alias('t')][string]$DotnetTargetVersionBand="<auto>", |
| 22 | + [Alias('u')][switch]$UpdateAllWorkloads |
| 23 | +) |
| 24 | + |
| 25 | +Set-StrictMode -Version Latest |
| 26 | +$ErrorActionPreference = "Stop" |
| 27 | +$ProgressPreference = "SilentlyContinue" |
| 28 | + |
| 29 | +$ManifestBaseName = "Samsung.NET.Sdk.Tizen.Manifest" |
| 30 | +$global:FallbackId = "" |
| 31 | + |
| 32 | +$LatestVersionMap = [ordered]@{ |
| 33 | + "$ManifestBaseName-6.0.100" = "7.0.101"; |
| 34 | + "$ManifestBaseName-6.0.200" = "7.0.100-preview.13.6"; |
| 35 | + "$ManifestBaseName-6.0.300" = "8.0.133"; |
| 36 | + "$ManifestBaseName-6.0.400" = "8.0.140"; |
| 37 | + "$ManifestBaseName-7.0.100-preview.6" = "7.0.100-preview.6.14"; |
| 38 | + "$ManifestBaseName-7.0.100-preview.7" = "7.0.100-preview.7.20"; |
| 39 | + "$ManifestBaseName-7.0.100-rc.1" = "7.0.100-rc.1.22"; |
| 40 | + "$ManifestBaseName-7.0.100-rc.2" = "7.0.100-rc.2.24"; |
| 41 | + "$ManifestBaseName-7.0.100" = "7.0.103"; |
| 42 | + "$ManifestBaseName-7.0.200" = "7.0.105"; |
| 43 | + "$ManifestBaseName-7.0.300" = "7.0.120"; |
| 44 | + "$ManifestBaseName-7.0.400" = "8.0.141"; |
| 45 | + "$ManifestBaseName-8.0.100-alpha.1" = "7.0.104"; |
| 46 | + "$ManifestBaseName-8.0.100-preview.2" = "7.0.106"; |
| 47 | + "$ManifestBaseName-8.0.100-preview.3" = "7.0.107"; |
| 48 | + "$ManifestBaseName-8.0.100-preview.4" = "7.0.108"; |
| 49 | + "$ManifestBaseName-8.0.100-preview.5" = "7.0.110"; |
| 50 | + "$ManifestBaseName-8.0.100-preview.6" = "7.0.121"; |
| 51 | + "$ManifestBaseName-8.0.100-preview.7" = "7.0.122"; |
| 52 | + "$ManifestBaseName-8.0.100-rc.1" = "7.0.124"; |
| 53 | + "$ManifestBaseName-8.0.100-rc.2" = "7.0.125"; |
| 54 | + "$ManifestBaseName-8.0.100-rtm" = "7.0.127"; |
| 55 | + "$ManifestBaseName-8.0.100" = "8.0.144"; |
| 56 | + "$ManifestBaseName-8.0.200" = "8.0.145"; |
| 57 | + "$ManifestBaseName-8.0.300" = "8.0.149"; |
| 58 | + "$ManifestBaseName-9.0.100-alpha.1" = "8.0.134"; |
| 59 | + "$ManifestBaseName-9.0.100-preview.1" = "8.0.135"; |
| 60 | + "$ManifestBaseName-9.0.100-preview.2" = "8.0.137"; |
| 61 | +} |
| 62 | + |
| 63 | +function New-TemporaryDirectory { |
| 64 | + $parent = [System.IO.Path]::GetTempPath() |
| 65 | + $name = [System.IO.Path]::GetRandomFileName() |
| 66 | + New-Item -ItemType Directory -Path (Join-Path $parent $name) |
| 67 | +} |
| 68 | + |
| 69 | +function Ensure-Directory([string]$TestDir) { |
| 70 | + Try { |
| 71 | + New-Item -ItemType Directory -Path $TestDir -Force -ErrorAction stop | Out-Null |
| 72 | + [io.file]::OpenWrite($(Join-Path -Path $TestDir -ChildPath ".test-write-access")).Close() |
| 73 | + Remove-Item -Path $(Join-Path -Path $TestDir -ChildPath ".test-write-access") -Force |
| 74 | + } |
| 75 | + Catch [System.UnauthorizedAccessException] { |
| 76 | + Write-Error "No permission to install. Try run with administrator mode." |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +function Get-LatestVersion([string]$Id) { |
| 81 | + $attempts=3 |
| 82 | + $sleepInSeconds=3 |
| 83 | + do |
| 84 | + { |
| 85 | + try |
| 86 | + { |
| 87 | + $Response = Invoke-WebRequest -Uri https://api.nuget.org/v3-flatcontainer/$Id/index.json -UseBasicParsing | ConvertFrom-Json |
| 88 | + return $Response.versions | Select-Object -Last 1 |
| 89 | + } |
| 90 | + catch { |
| 91 | + Write-Host "Id: $Id" |
| 92 | + Write-Host "An exception was caught: $($_.Exception.Message)" |
| 93 | + } |
| 94 | + |
| 95 | + $attempts-- |
| 96 | + if ($attempts -gt 0) { Start-Sleep $sleepInSeconds } |
| 97 | + } while ($attempts -gt 0) |
| 98 | + |
| 99 | + if ($LatestVersionMap.Contains($Id)) |
| 100 | + { |
| 101 | + Write-Host "Return cached latest version." |
| 102 | + return $LatestVersionMap.$Id |
| 103 | + } |
| 104 | + else |
| 105 | + { |
| 106 | + $SubStringId = $Id.Substring(0, $ManifestBaseName.Length + 2); |
| 107 | + $MatchingFallbackId = @() |
| 108 | + $MatchingFallbackVersion = @() |
| 109 | + foreach ($key in $LatestVersionMap.Keys) { |
| 110 | + if ($key -like "$SubStringId*") { |
| 111 | + $MatchingFallbackId += $key |
| 112 | + $MatchingFallbackVersion += $LatestVersionMap[$key] |
| 113 | + } |
| 114 | + } |
| 115 | + if ($MatchingFallbackVersion) |
| 116 | + { |
| 117 | + $global:FallbackId = $MatchingFallbackId[-1] |
| 118 | + $FallbackVersion = $MatchingFallbackVersion[-1] |
| 119 | + Write-Host "Return fallback version: $FallbackVersion" |
| 120 | + return $FallbackVersion |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + Write-Error "Wrong Id: $Id" |
| 125 | +} |
| 126 | + |
| 127 | +function Get-Package([string]$Id, [string]$Version, [string]$Destination, [string]$FileExt = "nupkg") { |
| 128 | + $OutFileName = "$Id.$Version.$FileExt" |
| 129 | + $OutFilePath = Join-Path -Path $Destination -ChildPath $OutFileName |
| 130 | + |
| 131 | + if ($Id -match ".net[0-9]+$") { |
| 132 | + $Id = $Id -replace (".net[0-9]+", "") |
| 133 | + } |
| 134 | + |
| 135 | + Invoke-WebRequest -Uri "https://www.nuget.org/api/v2/package/$Id/$Version" -OutFile $OutFilePath |
| 136 | + |
| 137 | + return $OutFilePath |
| 138 | +} |
| 139 | + |
| 140 | +function Install-Pack([string]$Id, [string]$Version, [string]$Kind) { |
| 141 | + $TempZipFile = $(Get-Package -Id $Id -Version $Version -Destination $TempDir -FileExt "zip") |
| 142 | + $TempUnzipDir = Join-Path -Path $TempDir -ChildPath "unzipped\$Id" |
| 143 | + |
| 144 | + switch ($Kind) { |
| 145 | + "manifest" { |
| 146 | + Expand-Archive -Path $TempZipFile -DestinationPath $TempUnzipDir |
| 147 | + New-Item -Path $TizenManifestDir -ItemType "directory" -Force | Out-Null |
| 148 | + Copy-Item -Path "$TempUnzipDir\data\*" -Destination $TizenManifestDir -Force |
| 149 | + } |
| 150 | + {($_ -eq "sdk") -or ($_ -eq "framework")} { |
| 151 | + Expand-Archive -Path $TempZipFile -DestinationPath $TempUnzipDir |
| 152 | + if ( ($kind -eq "sdk") -and ($Id -match ".net[0-9]+$")) { |
| 153 | + $Id = $Id -replace (".net[0-9]+", "") |
| 154 | + } |
| 155 | + $TargetDirectory = $(Join-Path -Path $DotnetInstallDir -ChildPath "packs\$Id\$Version") |
| 156 | + New-Item -Path $TargetDirectory -ItemType "directory" -Force | Out-Null |
| 157 | + Copy-Item -Path "$TempUnzipDir/*" -Destination $TargetDirectory -Recurse -Force |
| 158 | + } |
| 159 | + "template" { |
| 160 | + $TargetFileName = "$Id.$Version.nupkg".ToLower() |
| 161 | + $TargetDirectory = $(Join-Path -Path $DotnetInstallDir -ChildPath "template-packs") |
| 162 | + New-Item -Path $TargetDirectory -ItemType "directory" -Force | Out-Null |
| 163 | + Copy-Item $TempZipFile -Destination $(Join-Path -Path $TargetDirectory -ChildPath "$TargetFileName") -Force |
| 164 | + } |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +function Remove-Pack([string]$Id, [string]$Version, [string]$Kind) { |
| 169 | + switch ($Kind) { |
| 170 | + "manifest" { |
| 171 | + Remove-Item -Path $TizenManifestDir -Recurse -Force |
| 172 | + } |
| 173 | + {($_ -eq "sdk") -or ($_ -eq "framework")} { |
| 174 | + $TargetDirectory = $(Join-Path -Path $DotnetInstallDir -ChildPath "packs\$Id\$Version") |
| 175 | + Remove-Item -Path $TargetDirectory -Recurse -Force |
| 176 | + } |
| 177 | + "template" { |
| 178 | + $TargetFileName = "$Id.$Version.nupkg".ToLower(); |
| 179 | + Remove-Item -Path $(Join-Path -Path $DotnetInstallDir -ChildPath "template-packs\$TargetFileName") -Force |
| 180 | + } |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +function Install-TizenWorkload([string]$DotnetVersion) |
| 185 | +{ |
| 186 | + $VersionSplitSymbol = '.' |
| 187 | + $SplitVersion = $DotnetVersion.Split($VersionSplitSymbol) |
| 188 | + |
| 189 | + $CurrentDotnetVersion = [Version]"$($SplitVersion[0]).$($SplitVersion[1])" |
| 190 | + $DotnetVersionBand = $SplitVersion[0] + $VersionSplitSymbol + $SplitVersion[1] + $VersionSplitSymbol + $SplitVersion[2][0] + "00" |
| 191 | + $ManifestName = "$ManifestBaseName-$DotnetVersionBand" |
| 192 | + |
| 193 | + if ($DotnetTargetVersionBand -eq "<auto>" -or $UpdateAllWorkloads.IsPresent) { |
| 194 | + if ($CurrentDotnetVersion -ge "7.0") |
| 195 | + { |
| 196 | + $IsPreviewVersion = $DotnetVersion.Contains("-preview") -or $DotnetVersion.Contains("-rc") -or $DotnetVersion.Contains("-alpha") |
| 197 | + if ($IsPreviewVersion -and ($SplitVersion.Count -ge 4)) { |
| 198 | + $DotnetTargetVersionBand = $DotnetVersionBand + $SplitVersion[2].SubString(3) + $VersionSplitSymbol + $($SplitVersion[3]) |
| 199 | + $ManifestName = "$ManifestBaseName-$DotnetTargetVersionBand" |
| 200 | + } |
| 201 | + elseif ($DotnetVersion.Contains("-rtm") -and ($SplitVersion.Count -ge 3)) { |
| 202 | + $DotnetTargetVersionBand = $DotnetVersionBand + $SplitVersion[2].SubString(3) |
| 203 | + $ManifestName = "$ManifestBaseName-$DotnetTargetVersionBand" |
| 204 | + } |
| 205 | + else { |
| 206 | + $DotnetTargetVersionBand = $DotnetVersionBand |
| 207 | + } |
| 208 | + } |
| 209 | + else { |
| 210 | + $DotnetTargetVersionBand = $DotnetVersionBand |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + # Check latest version of manifest. |
| 215 | + if ($Version -eq "<latest>" -or $UpdateAllWorkloads.IsPresent) { |
| 216 | + $Version = Get-LatestVersion -Id $ManifestName |
| 217 | + } |
| 218 | + |
| 219 | + # Check workload manifest directory. |
| 220 | + $ManifestDir = Join-Path -Path $DotnetInstallDir -ChildPath "sdk-manifests" | Join-Path -ChildPath $DotnetTargetVersionBand |
| 221 | + $TizenManifestDir = Join-Path -Path $ManifestDir -ChildPath "samsung.net.sdk.tizen" |
| 222 | + $TizenManifestFile = Join-Path -Path $TizenManifestDir -ChildPath "WorkloadManifest.json" |
| 223 | + |
| 224 | + # Check and remove already installed old version. |
| 225 | + if (Test-Path $TizenManifestFile) { |
| 226 | + $ManifestJson = $(Get-Content $TizenManifestFile | ConvertFrom-Json) |
| 227 | + $OldVersion = $ManifestJson.version |
| 228 | + if ($OldVersion -eq $Version) { |
| 229 | + $DotnetWorkloadList = Invoke-Expression "& '$DotnetCommand' workload list | Select-String -Pattern '^tizen'" |
| 230 | + if ($DotnetWorkloadList) |
| 231 | + { |
| 232 | + Write-Host "Tizen Workload $Version version is already installed." |
| 233 | + Continue |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + Ensure-Directory $ManifestDir |
| 238 | + Write-Host "Removing $ManifestName/$OldVersion from $ManifestDir..." |
| 239 | + Remove-Pack -Id $ManifestName -Version $OldVersion -Kind "manifest" |
| 240 | + $ManifestJson.packs.PSObject.Properties | ForEach-Object { |
| 241 | + Write-Host "Removing $($_.Name)/$($_.Value.version)..." |
| 242 | + Remove-Pack -Id $_.Name -Version $_.Value.version -Kind $_.Value.kind |
| 243 | + } |
| 244 | + } |
| 245 | + |
| 246 | + Ensure-Directory $ManifestDir |
| 247 | + $TempDir = $(New-TemporaryDirectory) |
| 248 | + |
| 249 | + # Install workload manifest. |
| 250 | + Write-Host "Installing $ManifestName/$Version to $ManifestDir..." |
| 251 | + if ($global:FallbackId) { |
| 252 | + Install-Pack -Id $global:FallbackId -Version $Version -Kind "manifest" |
| 253 | + } else { |
| 254 | + Install-Pack -Id $ManifestName -Version $Version -Kind "manifest" |
| 255 | + } |
| 256 | + |
| 257 | + # Download and install workload packs. |
| 258 | + $NewManifestJson = $(Get-Content $TizenManifestFile | ConvertFrom-Json) |
| 259 | + $NewManifestJson.packs.PSObject.Properties | ForEach-Object { |
| 260 | + Write-Host "Installing $($_.Name)/$($_.Value.version)..." |
| 261 | + Install-Pack -Id $_.Name -Version $_.Value.version -Kind $_.Value.kind |
| 262 | + } |
| 263 | + |
| 264 | + # Add tizen to the installed workload metadata. |
| 265 | + # Featured version band for metadata does NOT include any preview specifier. |
| 266 | + # https://github.com/dotnet/sdk/blob/main/documentation/general/workloads/user-local-workloads.md |
| 267 | + New-Item -Path $(Join-Path -Path $DotnetInstallDir -ChildPath "metadata\workloads\$DotnetVersionBand\InstalledWorkloads\tizen") -Force | Out-Null |
| 268 | + if (Test-Path $(Join-Path -Path $DotnetInstallDir -ChildPath "metadata\workloads\$DotnetVersionBand\InstallerType\msi")) { |
| 269 | + New-Item -Path "HKLM:\SOFTWARE\Microsoft\dotnet\InstalledWorkloads\Standalone\x64\$DotnetTargetVersionBand\tizen" -Force | Out-Null |
| 270 | + } |
| 271 | + |
| 272 | + # Clean up |
| 273 | + Remove-Item -Path $TempDir -Force -Recurse |
| 274 | + |
| 275 | + Write-Host "Done installing Tizen workload $Version" |
| 276 | +} |
| 277 | + |
| 278 | +# Check dotnet install directory. |
| 279 | +if ($DotnetInstallDir -eq "<auto>") { |
| 280 | + if ($Env:DOTNET_ROOT -And $(Test-Path "$Env:DOTNET_ROOT")) { |
| 281 | + $DotnetInstallDir = $Env:DOTNET_ROOT |
| 282 | + } else { |
| 283 | + $DotnetInstallDir = Join-Path -Path $Env:Programfiles -ChildPath "dotnet" |
| 284 | + } |
| 285 | +} |
| 286 | +if (-Not $(Test-Path "$DotnetInstallDir")) { |
| 287 | + Write-Error "No installed dotnet '$DotnetInstallDir'." |
| 288 | +} |
| 289 | + |
| 290 | +# Check installed dotnet version |
| 291 | +$DotnetCommand = "$DotnetInstallDir\dotnet" |
| 292 | +if (Get-Command $DotnetCommand -ErrorAction SilentlyContinue) |
| 293 | +{ |
| 294 | + if ($UpdateAllWorkloads.IsPresent) |
| 295 | + { |
| 296 | + $InstalledDotnetSdks = Invoke-Expression "& '$DotnetCommand' --list-sdks | Select-String -Pattern '^6|^7'" | ForEach-Object {$_ -replace (" \[.*","")} |
| 297 | + } |
| 298 | + else |
| 299 | + { |
| 300 | + $InstalledDotnetSdks = Invoke-Expression "& '$DotnetCommand' --version" |
| 301 | + } |
| 302 | +} |
| 303 | +else |
| 304 | +{ |
| 305 | + Write-Error "'$DotnetCommand' occurs an error." |
| 306 | +} |
| 307 | + |
| 308 | +if (-Not $InstalledDotnetSdks) |
| 309 | +{ |
| 310 | + Write-Host "`n.NET SDK version 6 or later is required to install Tizen Workload." |
| 311 | +} |
| 312 | +else |
| 313 | +{ |
| 314 | + foreach ($DotnetSdk in $InstalledDotnetSdks) |
| 315 | + { |
| 316 | + try { |
| 317 | + Write-Host "`nCheck Tizen Workload for sdk $DotnetSdk" |
| 318 | + Install-TizenWorkload -DotnetVersion $DotnetSdk |
| 319 | + } |
| 320 | + catch { |
| 321 | + Write-Host "Failed to install Tizen Workload for sdk $DotnetSdk" |
| 322 | + Write-Host "$_" |
| 323 | + Continue |
| 324 | + } |
| 325 | + } |
| 326 | +} |
| 327 | + |
| 328 | +Write-Host "`nDone" |
0 commit comments