|
| 1 | +# Copyright (c) .NET Foundation and Contributors |
| 2 | +# See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +# This PS checks what binding need to be build in a PR or regular commit and takes care of performing the various checks and build the solution |
| 5 | + |
| 6 | +# setup msbuild |
| 7 | +$vswhere = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe" |
| 8 | +$msbuild = & $vswhere -latest -products * -requires Microsoft.Component.MSBuild -property installationPath |
| 9 | + |
| 10 | +if ($msbuild) { |
| 11 | + $msbuild = join-path $msbuild 'MSBuild\Current\Bin\amd64\MSBuild.exe' |
| 12 | +} |
| 13 | + |
| 14 | +# compute authorization header in format "AUTHORIZATION: basic 'encoded token'" |
| 15 | +# 'encoded token' is the Base64 of the string "nfbot:personal-token" |
| 16 | +$auth = "basic $([System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("nfbot:${env:MY_GITHUBTOKEN}"))))" |
| 17 | + |
| 18 | +if($env:System_PullRequest_PullRequestId -ne $null) |
| 19 | +{ |
| 20 | + "" | Write-Host -ForegroundColor Yellow |
| 21 | + "**********************" | Write-Host -ForegroundColor Yellow |
| 22 | + "* Building from a PR *" | Write-host -ForegroundColor Yellow |
| 23 | + "**********************" | Write-Host -ForegroundColor Yellow |
| 24 | + "" | Write-Host -ForegroundColor Yellow |
| 25 | + |
| 26 | + # get files changed in PR |
| 27 | + $pageCounter = 1 |
| 28 | + |
| 29 | + do |
| 30 | + { |
| 31 | + "##[debug] INFO: iteration $pageCounter" | Write-Host |
| 32 | + |
| 33 | + |
| 34 | + $batch = Invoke-RestMethod -Uri "https://api.github.com/repos/nanoframework/Samples/pulls/$env:System_PullRequest_PullRequestNumber/files?per_page=100&page=$pageCounter" -Header @{"Authorization"="$auth"} -ContentType "application/json" -Method GET |
| 35 | + |
| 36 | + if($null -eq $commit) |
| 37 | + { |
| 38 | + $commit = $batch |
| 39 | + } |
| 40 | + else |
| 41 | + { |
| 42 | + $commit += $batch |
| 43 | + } |
| 44 | + |
| 45 | + $pageCounter++ |
| 46 | + |
| 47 | + } until ($batch.Count -eq 0) |
| 48 | + |
| 49 | + # filter removed files |
| 50 | + $files = $commit.where{$_.status -ne 'removed'} |
| 51 | + |
| 52 | +} |
| 53 | +else |
| 54 | +{ |
| 55 | + "" | Write-Host -ForegroundColor Yellow |
| 56 | + "**************************" | Write-Host -ForegroundColor Yellow |
| 57 | + "* Building from a commit *" | Write-host -ForegroundColor Yellow |
| 58 | + "**************************" | Write-Host -ForegroundColor Yellow |
| 59 | + "" | Write-Host -ForegroundColor Yellow |
| 60 | + |
| 61 | + # get files changed in the commit, if this is NOT a PR |
| 62 | + $commit = Invoke-RestMethod -Uri "https://api.github.com/repos/nanoframework/Samples/commits/$(Build.SourceVersion)" -Header @{"Authorization"="$auth"} -ContentType "application/json" -Method GET |
| 63 | + |
| 64 | + # get files changed in the commit |
| 65 | + $pageCounter = 1 |
| 66 | + |
| 67 | + do |
| 68 | + { |
| 69 | + "##[command] Get API file change page: $pageCounter" | Write-Host |
| 70 | + |
| 71 | + $batch = Invoke-RestMethod -Uri "https://api.github.com/repos/nanoframework/Samples/commits/$env:Build_SourceVersion`?per_page=100&page=$pageCounter" -Header @{"Authorization"="$auth"} -ContentType "application/json" -Method GET |
| 72 | + |
| 73 | + if($null -eq $commit) |
| 74 | + { |
| 75 | + $commit = $batch.files |
| 76 | + } |
| 77 | + else |
| 78 | + { |
| 79 | + $commit += $batch.files |
| 80 | + } |
| 81 | + |
| 82 | + $pageCounter++ |
| 83 | + |
| 84 | + } until ($batch.files.Count -eq 0) |
| 85 | + |
| 86 | + # filter removed files |
| 87 | + $files = $commit.where{$_.status -ne 'removed' -and $_.filename -match '(devices)'} |
| 88 | +} |
| 89 | + |
| 90 | +# get file names only |
| 91 | +$files1 = $files | % {$_.filename} | Where-Object {$_ -match 'samples/*'} |
| 92 | + |
| 93 | +if($null -eq $files1) |
| 94 | +{ |
| 95 | + Write-host "No 'samples' found to build" |
| 96 | + exit |
| 97 | +} |
| 98 | + |
| 99 | +Write-host "##[group] Files changed:" |
| 100 | +$files1 | ForEach-Object { Write-host $_ } |
| 101 | +Write-host "##[endgroup]" |
| 102 | + |
| 103 | +# pattern to select samples folder name |
| 104 | +$pattern = '(samples\/)(?<folder>[a-zA-Z0-9._]+)(?>\/)' |
| 105 | + |
| 106 | +# filter out the collection |
| 107 | +$results = [Regex]::Matches($files1, $pattern) |
| 108 | + |
| 109 | +# get unique folder names |
| 110 | +$sampleFolders = $results | Sort-Object | Select-Object | Foreach-Object {$_.Groups["folder"].Value} | Get-Unique |
| 111 | + |
| 112 | +Write-host "Found $($sampleFolders.count) sample(s) to build" |
| 113 | + |
| 114 | +write-host "##[section] Processing samples..." |
| 115 | + |
| 116 | +foreach ($folder in $sampleFolders) |
| 117 | +{ |
| 118 | + "" | Write-Host -ForegroundColor Yellow |
| 119 | + "***********************" | Write-Host -ForegroundColor Yellow |
| 120 | + "##[group] Processing sample '$folder'..." | Write-Host -ForegroundColor Yellow |
| 121 | + "***********************" | Write-Host -ForegroundColor Yellow |
| 122 | + "" | Write-Host -ForegroundColor Yellow |
| 123 | + |
| 124 | + try |
| 125 | + { |
| 126 | + # try to find all solution files |
| 127 | + $solutionFiles = Get-ChildItem -Path "samples\$folder\" -Include "*.sln" -Recurse |
| 128 | + |
| 129 | + if($null -eq $solutionFiles) |
| 130 | + { |
| 131 | + "" | Write-Host -ForegroundColor Red |
| 132 | + "*****************************************" | Write-Host -ForegroundColor Red |
| 133 | + "##[error] >>>ERROR: Couldn't find any solution files!" | Write-Host -ForegroundColor Red |
| 134 | + throw "Couldn't find any solution file inside '$folder'..." |
| 135 | + } |
| 136 | + else |
| 137 | + { |
| 138 | + foreach ($slnFile in $solutionFiles) |
| 139 | + { |
| 140 | + "" | Write-Host -ForegroundColor Yellow |
| 141 | + "##[command] INFO: Building solution: '$slnFile'" | Write-Host -ForegroundColor Yellow |
| 142 | + "" | Write-Host -ForegroundColor Yellow |
| 143 | + |
| 144 | + # need to restore NuGets first |
| 145 | + write-host "##[command] Performing nuget restore for '$slnFile'." |
| 146 | + nuget restore $slnFile |
| 147 | + if (-not $?) |
| 148 | + { |
| 149 | + "" | Write-Host -ForegroundColor Red |
| 150 | + "*****************************************" | Write-Host -ForegroundColor Red |
| 151 | + "##[error] >>>ERROR: Couldn't restore solution: '$slnFile'!" | Write-Host -ForegroundColor Red |
| 152 | + # set flag |
| 153 | + $buildFailed = $true |
| 154 | + } |
| 155 | + |
| 156 | + # build solution |
| 157 | + write-host "##[command] Running msbuild." |
| 158 | + & "$msbuild" "$slnFile" /verbosity:normal /p:Configuration=Release |
| 159 | + if (-not $?) { |
| 160 | + "" | Write-Host -ForegroundColor Red |
| 161 | + "*****************************************" | Write-Host -ForegroundColor Red |
| 162 | + "##[error] >>>ERROR: Failed building solution: '$slnFile'!" | Write-Host -ForegroundColor Red |
| 163 | + # set flag |
| 164 | + $buildFailed = $true |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + catch |
| 170 | + { |
| 171 | + "" | Write-Host -ForegroundColor Red |
| 172 | + "*****************************************" | Write-Host -ForegroundColor Red |
| 173 | + "##[error] >>>ERROR: build failed, check output <<<" | Write-Host -ForegroundColor Red |
| 174 | + "*****************************************" | Write-Host -ForegroundColor Red |
| 175 | + "" | Write-Host -ForegroundColor Red |
| 176 | + |
| 177 | + "" | Write-Host -ForegroundColor Red |
| 178 | + "Exception was: $_" | Write-Host -ForegroundColor Red |
| 179 | + "" | Write-Host -ForegroundColor Red |
| 180 | + |
| 181 | + # set flag |
| 182 | + $buildFailed = $true |
| 183 | + } |
| 184 | + |
| 185 | + write-host "##[endgroup]" |
| 186 | +} |
| 187 | + |
| 188 | +if($buildFailed) |
| 189 | +{ |
| 190 | + "********************************" | Write-Host -ForegroundColor Red |
| 191 | + "##[error] Check >>>ERROR<<< messages above" | Write-Host -ForegroundColor Red |
| 192 | + "********************************" | Write-Host -ForegroundColor Red |
| 193 | + |
| 194 | + exit 1 |
| 195 | +} |
0 commit comments