From 7a0527dad798d5da541dd3b5d1fb6f811a875d36 Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Thu, 2 Nov 2017 00:40:04 -0700 Subject: [PATCH 1/4] onboard to InvokeBuild and fix -Encoding Byte breaking change in pwsh beta 9 --- .gitignore | 5 +- Polaris.build.ps1 | 113 ++++++++++++++++++++++++++++++++++++++++++++++ Polaris.psm1 | 6 ++- 3 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 Polaris.build.ps1 diff --git a/.gitignore b/.gitignore index 7e5e73f..3e958ff 100644 --- a/.gitignore +++ b/.gitignore @@ -297,4 +297,7 @@ __pycache__/ # Exclude .NET assemblies from source *.dll -# End of https://www.gitignore.io/api/csharp,powershell \ No newline at end of file +# End of https://www.gitignore.io/api/csharp,powershell + +# Pester output +test/TestsResults.xml \ No newline at end of file diff --git a/Polaris.build.ps1 b/Polaris.build.ps1 new file mode 100644 index 0000000..ec8baa9 --- /dev/null +++ b/Polaris.build.ps1 @@ -0,0 +1,113 @@ +#Requires -Modules @{ModuleName="InvokeBuild";ModuleVersion="3.7.1"} + +$script:IsCIBuild = $env:APPVEYOR -ne $null +$script:IsUnix = $PSVersionTable.PSEdition -and $PSVersionTable.PSEdition -eq "Core" -and !$IsWindows + +if ($PSVersionTable.PSEdition -ne "Core") { + Add-Type -Assembly System.IO.Compression.FileSystem +} + +task SetupDotNet -Before Restore, Build, Clean, Test { + + $requiredSdkVersion = "2.0.0" + + $dotnetPath = "$PSScriptRoot/.dotnet" + $dotnetExePath = if ($script:IsUnix) { "$dotnetPath/dotnet" } else { "$dotnetPath/dotnet.exe" } + $originalDotNetExePath = $dotnetExePath + + if (!(Test-Path $dotnetExePath)) { + $installedDotnet = Get-Command dotnet -ErrorAction Ignore + if ($installedDotnet) { + $dotnetExePath = $installedDotnet.Source + } + else { + $dotnetExePath = $null + } + } + + # Make sure the dotnet we found is the right version + if ($dotnetExePath -and (& $dotnetExePath --version) -eq $requiredSdkVersion) { + $script:dotnetExe = $dotnetExePath + } + else { + # Clear the path so that we invoke installation + $script:dotnetExe = $null + } + + if ($script:dotnetExe -eq $null) { + + Write-Host "`n### Installing .NET CLI $requiredSdkVersion...`n" -ForegroundColor Green + + # The install script is platform-specific + $installScriptExt = if ($script:IsUnix) { "sh" } else { "ps1" } + + # Download the official installation script and run it + $installScriptPath = "$([System.IO.Path]::GetTempPath())dotnet-install.$installScriptExt" + Invoke-WebRequest "https://raw.githubusercontent.com/dotnet/cli/master/scripts/obtain/dotnet-install.$installScriptExt" -OutFile $installScriptPath + $env:DOTNET_INSTALL_DIR = "$PSScriptRoot/.dotnet" + + if (!$script:IsUnix) { + & $installScriptPath -Version $requiredSdkVersion -InstallDir "$env:DOTNET_INSTALL_DIR" + } + else { + & /bin/bash $installScriptPath -Version $requiredSdkVersion -InstallDir "$env:DOTNET_INSTALL_DIR" + $env:PATH = $dotnetExeDir + [System.IO.Path]::PathSeparator + $env:PATH + } + + Write-Host "`n### Installation complete." -ForegroundColor Green + $script:dotnetExe = $originalDotnetExePath + } + + # This variable is used internally by 'dotnet' to know where it's installed + $script:dotnetExe = Resolve-Path $script:dotnetExe + if (!$env:DOTNET_INSTALL_DIR) + { + $dotnetExeDir = [System.IO.Path]::GetDirectoryName($script:dotnetExe) + $env:PATH = $dotnetExeDir + [System.IO.Path]::PathSeparator + $env:PATH + $env:DOTNET_INSTALL_DIR = $dotnetExeDir + } + + Write-Host "`n### Using dotnet v$requiredSDKVersion at path $script:dotnetExe`n" -ForegroundColor Green +} + +function NeedsRestore($rootPath) { + # This checks to see if the number of folders under a given + # path (like "src" or "test") is greater than the number of + # obj\project.assets.json files found under that path, implying + # that those folders have not yet been restored. + $projectAssets = (Get-ChildItem "$rootPath\*\obj\project.assets.json") + return ($projectAssets -eq $null) -or ((Get-ChildItem $rootPath).Length -gt $projectAssets.Length) +} + +task Restore -If { "Restore" -in $BuildTask -or (NeedsRestore(".\PolarisCore")) } -Before Clean, Build, Test { + exec { & $script:dotnetExe restore .\PolarisCore\Polaris.csproj } +} + +task Clean { + exec { & $script:dotnetExe clean .\PolarisCore\Polaris.csproj } +} + +task Build { + if (!$script:IsUnix) { + exec { & $script:dotnetExe build .\PolarisCore\Polaris.csproj -f net451 } + Write-Host "" # Newline + } + + exec { & $script:dotnetExe build .\PolarisCore\Polaris.csproj -f netstandard2.0 } +} + +task Test { + # TODO: Add tests + if ($PSVersionTable.PSEdition -ne "Core") { + Install-Module Pester -Force -Scope CurrentUser + } + cd .\test + $res = Invoke-Pester -OutputFormat NUnitXml -OutputFile TestsResults.xml -PassThru; + if ($env:APPVEYOR) { + (New-Object System.Net.WebClient).UploadFile("https://ci.appveyor.com/api/testresults/nunit/$($env:APPVEYOR_JOB_ID)", (Resolve-Path .\TestsResults.xml)); + } + if ($res.FailedCount -gt 0) { throw "$($res.FailedCount) tests failed."} +} + +# The default task is to run the entire CI build +task . Clean, Build, Test \ No newline at end of file diff --git a/Polaris.psm1 b/Polaris.psm1 index b2841d6..59179af 100644 --- a/Polaris.psm1 +++ b/Polaris.psm1 @@ -162,7 +162,11 @@ function New-StaticRoute { $allPaths | ForEach-Object { $scriptTemplate = @" -`$bytes = Get-Content "$_" -Encoding Byte -ReadCount 0 +if(`$PSVersionTable.PSEdition -eq "Core") { + `$bytes = Get-Content "$_" -AsByteStream -ReadCount 0 +} else { + `$bytes = Get-Content "$_" -Encoding Byte -ReadCount 0 +} `$response.SetContentType(([PolarisCore.PolarisResponse]::GetContentType("$_"))) `$response.ByteResponse = `$bytes "@ From ce5c05439c62767452afedf2ed0adbdf12790641 Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Thu, 2 Nov 2017 00:42:18 -0700 Subject: [PATCH 2/4] use invokebuild for appveyor --- appveyor.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 9c2f19d..a3f0dd6 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -30,8 +30,8 @@ install: # If any of the tests fail, consider the pipeline failed test_script: - ps: | - pushd C:\projects\polaris\build - . .\appveyor.ps1 + Install-Module InvokeBuild + Invoke-Build notifications: - provider: Webhook From 5f0af4556dcbf534c13656dd09876f9ab3a14c89 Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Thu, 2 Nov 2017 20:09:10 -0700 Subject: [PATCH 3/4] update README steps --- README.md | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index bd0507e..b5f72f1 100644 --- a/README.md +++ b/README.md @@ -34,20 +34,10 @@ Polaris' differentiation is that it is cross-platform and uses the .NET HttpList * [PowerShell Core 6](https://github.com/powershell/powershell) ### Steps -#### Windows 1. Clone or download the zip of the repo 1. Open [PowerShell Core 6](https://github.com/powershell/powershell) -1. run `cd Polaris/PolarisCore` -1. run `dotnet restore` -1. run `dotnet build` -1. run `cd ..` -#### Unix/Linux -1. Clone or download the zip of the repo -1. Open [PowerShell Core 6](https://github.com/powershell/powershell) -1. run `cd Polaris/PolarisCore` -1. run `dotnet restore` -1. run `dotnet build -f netstandard2.0` -1. run `cd ..` +1. run `Install-Module InvokeBuild` +1. run `Invoke-Build Build` At this point, you can now run `Import-Module ./Polaris.psm1` to start using Polaris! Checkout [the wiki](https://github.com/PowerShell/Polaris/wiki) for more usage! From 40e186e3507a0f1ba4361fb123fb3d8f66bb2af2 Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Fri, 3 Nov 2017 09:08:27 -0700 Subject: [PATCH 4/4] add if check for Core, import the right dll --- Polaris.psm1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Polaris.psm1 b/Polaris.psm1 index 59179af..8946e06 100644 --- a/Polaris.psm1 +++ b/Polaris.psm1 @@ -1,7 +1,7 @@ -if (Test-Path "$PSScriptRoot/PolarisCore/bin/Debug/net451/") { - Add-Type -Path "$PSScriptRoot/PolarisCore/bin/Debug/net451/Polaris.dll" -} else { +if ($PSVersionTable.PSEdition -eq "Core") { Add-Type -Path "$PSScriptRoot/PolarisCore/bin/Debug/netstandard2.0/Polaris.dll" +} else { + Add-Type -Path "$PSScriptRoot/PolarisCore/bin/Debug/net451/Polaris.dll" } $global:Polaris = $null