From 3feabfdb52c643b6a26935eca9062563b67c7292 Mon Sep 17 00:00:00 2001 From: Maxim Lobanov Date: Mon, 2 Nov 2020 11:19:30 +0300 Subject: [PATCH] fix todos --- images/macos/helpers/Common.Helpers.psm1 | 13 ++++++ images/macos/helpers/Xcode.Installer.psm1 | 56 +++++++++++++++++------ 2 files changed, 54 insertions(+), 15 deletions(-) diff --git a/images/macos/helpers/Common.Helpers.psm1 b/images/macos/helpers/Common.Helpers.psm1 index dd8a2184..174ea050 100644 --- a/images/macos/helpers/Common.Helpers.psm1 +++ b/images/macos/helpers/Common.Helpers.psm1 @@ -83,3 +83,16 @@ function Invoke-RestMethodWithRetry { ) Invoke-RestMethod $Url -MaximumRetryCount 10 -RetryIntervalSec 30 } + +function Invoke-ExpressionWithValidation { + param( + [Parameter(Mandatory)] + [string]$Command + ) + + $output = Invoke-Expression -Command $Command + if ($LASTEXITCODE -ne 0) { + throw "Command '$Command' has finished with exit code $LASTEXITCODE" + } + return $output +} \ No newline at end of file diff --git a/images/macos/helpers/Xcode.Installer.psm1 b/images/macos/helpers/Xcode.Installer.psm1 index c7213861..0f0af1ae 100644 --- a/images/macos/helpers/Xcode.Installer.psm1 +++ b/images/macos/helpers/Xcode.Installer.psm1 @@ -30,8 +30,7 @@ function Invoke-DownloadXcodeArchive { # TO-DO: Consider replacing of xcversion with own implementation Write-Host "Downloading Xcode $resolvedVersion" - # TO-DO: handle exit code - xcversion install "$resolvedVersion" --no-install + Invoke-ExpressionWithValidation "xcversion install '$resolvedVersion' --no-install" } function Resolve-ExactXcodeVersion { @@ -40,8 +39,38 @@ function Resolve-ExactXcodeVersion { [string]$Version ) - # TO-DO - return $Version + # if toolset string contains spaces, consider it as a full name of Xcode + if ($Version -match "\s") { + return $Version + } + + $semverVersion = [SemVer]::Parse($Version) + $availableVersions = Get-AvailableXcodeVersions + $satisfiedVersions = $availableVersions | Where-Object { $semverVersion -eq $_.stableSemver } + return $satisfiedVersions | Select-Object -Last 1 -ExpandProperty rawVersion +} + +function Get-AvailableXcodeVersions { + $rawVersionsList = & xcversion list | ForEach-Object { $_.Trim() } | Where-Object { $_ -match "^\d" } + $availableVersions = $rawVersionsList | ForEach-Object { + $parts = $_.Split(" ", 2) + $stableSemver = [SemVer]::Parse($parts[0]) + if ($parts.Count -eq 1) { + $semver = $stableSemver + } else { + # Convert 'beta 3' -> 'beta.3', 'Release Candidate' -> 'releasecandidate', 'GM Seed 2' -> 'gmseed.2' + $normalizedLabel = $parts[1].toLower() -replace " (\d)", '.$1' -replace " ([a-z])", '$1' + $semver = [SemVer]::new($stableSemver.Major, $stableSemver.Minor, $stableSemver.Patch, $normalizedLabel) + } + + return [PSCustomObject]@{ + semver = $semver + rawVersion = $_ + stableSemver = $stableSemver + } + } + + return $availableVersions | Sort-Object -Property semver } function Expand-XcodeXipArchive { @@ -56,8 +85,7 @@ function Expand-XcodeXipArchive { Write-Host "Extracting Xcode from '$xcodeXipPath'" Push-Location $DownloadDirectory - # TO-DO: handle exit code - xip -x $xcodeXipPath + Invoke-ExpressionWithValidation "xip -x $xcodeXipPath" Pop-Location if (Test-Path "$DownloadDirectory/Xcode-beta.app") { @@ -81,8 +109,7 @@ function Confirm-XcodeIntegrity { $XcodeRootPath = Get-XcodeRootPath -Version $Version if (Test-XcodeStableRelease -XcodeRootPath $XcodeRootPath) { - # TO-DO: handle exit code - spctl --assess --raw $XcodeRootPath + Invoke-ExpressionWithValidation "spctl --assess --raw $XcodeRootPath" } } @@ -93,8 +120,7 @@ function Approve-XcodeLicense { ) $xcodeBuildPath = Get-XcodeToolPath -Version $Version -ToolName "xcodebuild" - # TO-DO: handle exit code - sudo $xcodeBuildPath -license accept + Invoke-ExpressionWithValidation "sudo $xcodeBuildPath -license accept" } function Install-XcodeAdditionalPackages { @@ -106,8 +132,9 @@ function Install-XcodeAdditionalPackages { Write-Host "Installing additional packages for Xcode $Version..." $xcodeRootPath = Get-XcodeRootPath -Version $Version $packages = Get-ChildItem -Path "$xcodeRootPath/Contents/Resources/Packages" -Filter "*.pkg" -File - # TO-DO: handle exit code - $packages | ForEach-Object { & sudo installer -pkg $_.FullName -target / -allowUntrusted } + $packages | ForEach-Object { + Invoke-ExpressionWithValidation "sudo installer -pkg $($_.FullName) -target / -allowUntrusted" + } } function Invoke-XcodeRunFirstLaunch { @@ -122,8 +149,7 @@ function Invoke-XcodeRunFirstLaunch { Write-Host "Running 'runFirstLaunch' for Xcode $Version..." $xcodeRootPath = Get-XcodeToolPath -Version $Version -ToolName "xcodebuild" - # TO-DO: handle exit code - & sudo $xcodeRootPath -runFirstLaunch + Invoke-ExpressionWithValidation "sudo $xcodeRootPath -runFirstLaunch" } function Build-XcodeSymlinks { @@ -134,7 +160,7 @@ function Build-XcodeSymlinks { ) $sourcePath = Get-XcodeRootPath -Version $Version - $Symlinks | ForEach-Object { + $Symlinks | Where-Object { $_ } | ForEach-Object { $targetPath = Get-XcodeRootPath -Version $_ Write-Host "Creating symlink: '$targetPath' -> '$sourcePath'" New-Item -Path $targetPath -ItemType SymbolicLink -Value $sourcePath