build.ps1 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #!/usr/bin/env pwsh
  2. $DotNetInstallerUri = 'https://dot.net/v1/dotnet-install.ps1';
  3. $DotNetUnixInstallerUri = 'https://dot.net/v1/dotnet-install.sh'
  4. $DotNetChannel = 'LTS'
  5. $PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
  6. [string] $CakeVersion = ''
  7. [string] $DotNetVersion= ''
  8. foreach($line in Get-Content (Join-Path $PSScriptRoot 'build.config'))
  9. {
  10. if ($line -like 'CAKE_VERSION=*') {
  11. $CakeVersion = $line.SubString(13)
  12. }
  13. elseif ($line -like 'DOTNET_VERSION=*') {
  14. $DotNetVersion =$line.SubString(15)
  15. }
  16. }
  17. if ([string]::IsNullOrEmpty($CakeVersion) -or [string]::IsNullOrEmpty($DotNetVersion)) {
  18. 'Failed to parse Cake / .NET Core SDK Version'
  19. exit 1
  20. }
  21. # Make sure tools folder exists
  22. $ToolPath = Join-Path $PSScriptRoot "tools"
  23. if (!(Test-Path $ToolPath)) {
  24. Write-Verbose "Creating tools directory..."
  25. New-Item -Path $ToolPath -Type Directory -Force | out-null
  26. }
  27. if ($PSVersionTable.PSEdition -ne 'Core') {
  28. # Attempt to set highest encryption available for SecurityProtocol.
  29. # PowerShell will not set this by default (until maybe .NET 4.6.x). This
  30. # will typically produce a message for PowerShell v2 (just an info
  31. # message though)
  32. try {
  33. # Set TLS 1.2 (3072), then TLS 1.1 (768), then TLS 1.0 (192), finally SSL 3.0 (48)
  34. # Use integers because the enumeration values for TLS 1.2 and TLS 1.1 won't
  35. # exist in .NET 4.0, even though they are addressable if .NET 4.5+ is
  36. # installed (.NET 4.5 is an in-place upgrade).
  37. [System.Net.ServicePointManager]::SecurityProtocol = 3072 -bor 768 -bor 192 -bor 48
  38. } catch {
  39. Write-Output 'Unable to set PowerShell to use TLS 1.2 and TLS 1.1 due to old .NET Framework installed. If you see underlying connection closed or trust errors, you may need to upgrade to .NET Framework 4.5+ and PowerShell v3'
  40. }
  41. }
  42. ###########################################################################
  43. # INSTALL .NET CORE CLI
  44. ###########################################################################
  45. $env:DOTNET_SKIP_FIRST_TIME_EXPERIENCE=1
  46. $env:DOTNET_CLI_TELEMETRY_OPTOUT=1
  47. $env:DOTNET_ROLL_FORWARD_ON_NO_CANDIDATE_FX=1
  48. Function Remove-PathVariable([string]$VariableToRemove)
  49. {
  50. $SplitChar = ';'
  51. if ($IsMacOS -or $IsLinux) {
  52. $SplitChar = ':'
  53. }
  54. $path = [Environment]::GetEnvironmentVariable("PATH", "User")
  55. if ($path -ne $null)
  56. {
  57. $newItems = $path.Split($SplitChar, [StringSplitOptions]::RemoveEmptyEntries) | Where-Object { "$($_)" -inotlike $VariableToRemove }
  58. [Environment]::SetEnvironmentVariable("PATH", [System.String]::Join($SplitChar, $newItems), "User")
  59. }
  60. $path = [Environment]::GetEnvironmentVariable("PATH", "Process")
  61. if ($path -ne $null)
  62. {
  63. $newItems = $path.Split($SplitChar, [StringSplitOptions]::RemoveEmptyEntries) | Where-Object { "$($_)" -inotlike $VariableToRemove }
  64. [Environment]::SetEnvironmentVariable("PATH", [System.String]::Join($SplitChar, $newItems), "Process")
  65. }
  66. }
  67. # Get .NET Core CLI path if installed.
  68. $FoundDotNetCliVersion = $null;
  69. if (Get-Command dotnet -ErrorAction SilentlyContinue) {
  70. $FoundDotNetCliVersion = dotnet --version;
  71. }
  72. if($FoundDotNetCliVersion -ne $DotNetVersion) {
  73. $InstallPath = Join-Path $PSScriptRoot ".dotnet"
  74. if (!(Test-Path $InstallPath)) {
  75. New-Item -Path $InstallPath -ItemType Directory -Force | Out-Null;
  76. }
  77. if ($IsMacOS -or $IsLinux) {
  78. $ScriptPath = Join-Path $InstallPath 'dotnet-install.sh'
  79. (New-Object System.Net.WebClient).DownloadFile($DotNetUnixInstallerUri, $ScriptPath);
  80. & bash $ScriptPath --version "$DotNetVersion" --install-dir "$InstallPath" --channel "$DotNetChannel" --no-path
  81. Remove-PathVariable "$InstallPath"
  82. $env:PATH = "$($InstallPath):$env:PATH"
  83. }
  84. else {
  85. $ScriptPath = Join-Path $InstallPath 'dotnet-install.ps1'
  86. (New-Object System.Net.WebClient).DownloadFile($DotNetInstallerUri, $ScriptPath);
  87. & $ScriptPath -Channel $DotNetChannel -Version $DotNetVersion -InstallDir $InstallPath;
  88. Remove-PathVariable "$InstallPath"
  89. $env:PATH = "$InstallPath;$env:PATH"
  90. }
  91. $env:DOTNET_ROOT=$InstallPath
  92. }
  93. ###########################################################################
  94. # INSTALL CAKE
  95. ###########################################################################
  96. # Make sure Cake has been installed.
  97. [string] $CakeExePath = ''
  98. [string] $CakeInstalledVersion = Get-Command dotnet-cake -ErrorAction SilentlyContinue | % {&$_.Source --version}
  99. if ($CakeInstalledVersion -eq $CakeVersion) {
  100. # Cake found locally
  101. $CakeExePath = (Get-Command dotnet-cake).Source
  102. }
  103. else {
  104. $CakePath = [System.IO.Path]::Combine($ToolPath,'.store', 'cake.tool', $CakeVersion) # Old PowerShell versions Join-Path only supports one child path
  105. $CakeExePath = (Get-ChildItem -Path $ToolPath -Filter "dotnet-cake*" -File| ForEach-Object FullName | Select-Object -First 1)
  106. if ((!(Test-Path -Path $CakePath -PathType Container)) -or (!(Test-Path $CakeExePath -PathType Leaf))) {
  107. if ((![string]::IsNullOrEmpty($CakeExePath)) -and (Test-Path $CakeExePath -PathType Leaf))
  108. {
  109. & dotnet tool uninstall --tool-path $ToolPath Cake.Tool
  110. }
  111. & dotnet tool install --tool-path $ToolPath --version $CakeVersion Cake.Tool
  112. if ($LASTEXITCODE -ne 0)
  113. {
  114. 'Failed to install cake'
  115. exit 1
  116. }
  117. $CakeExePath = (Get-ChildItem -Path $ToolPath -Filter "dotnet-cake*" -File| ForEach-Object FullName | Select-Object -First 1)
  118. }
  119. }
  120. ###########################################################################
  121. # RUN BUILD SCRIPT
  122. ###########################################################################
  123. & "$CakeExePath" ./build.cake --bootstrap
  124. if ($LASTEXITCODE -eq 0)
  125. {
  126. & "$CakeExePath" ./build.cake $args
  127. }
  128. exit $LASTEXITCODE