Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinmoris committed Feb 10, 2019
2 parents 3de1c09 + c652b55 commit 1a824da
Show file tree
Hide file tree
Showing 16 changed files with 288 additions and 87 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -330,4 +330,7 @@ ASALocalRun/
.mfractor/

# macOS
.DS_Store
.DS_Store

# Ionide
.ionide/
174 changes: 127 additions & 47 deletions .psscripts/build-functions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,37 @@ function Test-IsWindows
[environment]::OSVersion.Platform -ne "Unix"
}

function Invoke-UnsafeCmd ($cmd)
function Test-IsMonoInstalled
{
<#
.DESCRIPTION
Checks to see whether the current environment has the Mono framework installed.
.EXAMPLE
if (Test-IsMonoInstalled) { Write-Host "Mono is available." }
#>

$result = Invoke-Cmd "mono --version" -Silent
return $result.StartsWith("Mono JIT compiler version")
}

function Get-UbuntuVersion
{
<#
.DESCRIPTION
Gets the Ubuntu version.
.EXAMPLE
$ubuntuVersion = Get-UbuntuVersion
#>

$version = Invoke-Cmd "lsb_release -r -s" -Silent
return $version
}

function Invoke-UnsafeCmd (
[string] $Cmd,
[switch] $Silent)
{
<#
.DESCRIPTION
Expand All @@ -31,12 +61,14 @@ function Invoke-UnsafeCmd ($cmd)
Use this PowerShell command to execute any CLI commands which might not exit with 0 on a success.
#>

Write-Host $cmd -ForegroundColor DarkCyan
if (!($Silent.IsPresent)) { Write-Host $cmd -ForegroundColor DarkCyan }
if (Test-IsWindows) { $cmd = "cmd.exe /C $cmd" }
Invoke-Expression -Command $cmd
}

function Invoke-Cmd ($Cmd)
function Invoke-Cmd (
[string] $Cmd,
[switch] $Silent)
{
<#
.DESCRIPTION
Expand All @@ -52,7 +84,7 @@ function Invoke-Cmd ($Cmd)
Use this PowerShell command to execute any dotnet CLI commands in order to ensure that they behave the same way in the case of an error across different environments (Windows, OSX and Linux).
#>

Invoke-UnsafeCmd $cmd
if ($Silent.IsPresent) { Invoke-UnsafeCmd $cmd -Silent } else { Invoke-UnsafeCmd $cmd }
if ($LastExitCode -ne 0) { Write-Error "An error occured when executing '$Cmd'."; return }
}

Expand Down Expand Up @@ -113,34 +145,28 @@ function Test-CompareVersions ($version, [string]$gitTag)
}
}

function Add-ToPathVariable ($path)
{
if (Test-IsWindows)
{
$updatedPath = "$path;$env:Path"
[Environment]::SetEnvironmentVariable("Path", $updatedPath, "Process")
[Environment]::SetEnvironmentVariable("Path", $updatedPath, "User")
[Environment]::SetEnvironmentVariable("Path", $updatedPath, "Machine")
}
else
{
$updatedPath = "$path`:$env:PATH"
[Environment]::SetEnvironmentVariable("PATH", $updatedPath, "Process")
[Environment]::SetEnvironmentVariable("PATH", $updatedPath, "User")
[Environment]::SetEnvironmentVariable("PATH", $updatedPath, "Machine")
}
}

# ----------------------------------------------
# .NET Core functions
# ----------------------------------------------

function dotnet-info { Invoke-Cmd "dotnet --info" }
function dotnet-version { Invoke-Cmd "dotnet --version" }
function dotnet-restore ($project, $argv) { Invoke-Cmd "dotnet restore $project $argv" }
function dotnet-build ($project, $argv) { Invoke-Cmd "dotnet build $project $argv" }
function dotnet-run ($project, $argv) { Invoke-Cmd "dotnet run --project $project $argv" }
function dotnet-pack ($project, $argv) { Invoke-Cmd "dotnet pack $project $argv" }
function dotnet-publish ($project, $argv) { Invoke-Cmd "dotnet publish $project $argv" }

function Get-DotNetRuntimeVersion
{
<#
.DESCRIPTION
Runs the dotnet --info command and extracts the .NET Core Runtime version number.
.NOTES
The .NET Core Runtime version can sometimes be useful for other dotnet CLI commands (e.g. dotnet xunit -fxversion ".NET Core Runtime version").
#>

$info = dotnet-info
[System.Array]::Reverse($info)
$version = $info | Where-Object { $_.Contains("Version") } | Select-Object -First 1
$version.Split(":")[1].Trim()
}

function Get-TargetFrameworks ($projFile)
{
<#
Expand Down Expand Up @@ -184,20 +210,43 @@ function Get-NetCoreTargetFramework ($projFile)
Get-TargetFrameworks $projFile | Where-Object { $_ -like "netstandard*" -or $_ -like "netcoreapp*" }
}

function dotnet-test ($project, $argv)
function Invoke-DotNetCli ($cmd, $proj, $argv)
{
# Currently dotnet test does not work for net461 on Linux/Mac
# See: https://github.com/Microsoft/vstest/issues/1318
#
# Previously dotnet-xunit was a working alternative, however
# after issues with the maintenance of dotnet xunit it has been
# discontinued since xunit 2.4: https://xunit.github.io/releases/2.4
if(!(Test-IsWindows))

if((!(Test-IsWindows) -and !(Test-IsMonoInstalled)) `
-or (!(Test-IsWindows) -and ($cmd -eq "test")))
{
$fw = Get-NetCoreTargetFramework $project;
$fw = Get-NetCoreTargetFramework($proj)
$argv = "-f $fw " + $argv
}
Invoke-Cmd "dotnet test $project $argv"
Invoke-Cmd "dotnet $cmd $proj $argv"
}

function dotnet-info { Invoke-Cmd "dotnet --info" -Silent }
function dotnet-version { Invoke-Cmd "dotnet --version" -Silent }
function dotnet-restore ($project, $argv) { Invoke-Cmd "dotnet restore $project $argv" }
function dotnet-build ($project, $argv) { Invoke-DotNetCli -Cmd "build" -Proj $project -Argv $argv }
function dotnet-test ($project, $argv) { Invoke-DotNetCli -Cmd "test" -Proj $project -Argv $argv }
function dotnet-run ($project, $argv) { Invoke-Cmd "dotnet run --project $project $argv" }
function dotnet-pack ($project, $argv) { Invoke-Cmd "dotnet pack $project $argv" }
function dotnet-publish ($project, $argv) { Invoke-Cmd "dotnet publish $project $argv" }

function Get-DotNetRuntimeVersion
{
<#
.DESCRIPTION
Runs the dotnet --info command and extracts the .NET Core Runtime version number.
.NOTES
The .NET Core Runtime version can sometimes be useful for other dotnet CLI commands (e.g. dotnet xunit -fxversion ".NET Core Runtime version").
#>

$info = dotnet-info
[System.Array]::Reverse($info)
$version = $info | Where-Object { $_.Contains("Version") } | Select-Object -First 1
$version.Split(":")[1].Trim()
}

function Write-DotnetCoreVersions
Expand Down Expand Up @@ -235,25 +284,33 @@ function Get-NetCoreSdkFromWeb ($version)
The SDK version which should be downloaded.
#>

$os = if (Test-IsWindows) { "windows" } else { "linux" }
Write-Host "Downloading .NET Core SDK $version..."

$response = Invoke-WebRequest `
-Uri "https://www.microsoft.com/net/download/thank-you/dotnet-sdk-$version-$os-x64-binaries" `
-Method Get `
-MaximumRedirection 0 `
$os = if (Test-IsWindows) { "windows" } else { "linux" }
$ext = if (Test-IsWindows) { ".zip" } else { ".tar.gz" }
$uri = "https://www.microsoft.com/net/download/thank-you/dotnet-sdk-$version-$os-x64-binaries"
Write-Host "Finding download link..."

$response = Invoke-WebRequest -Uri $uri

$downloadLink =
$response.Links `
| Where-Object { $_.onclick -eq "recordManualDownload()" } `
| Select-Object -Expand href

$tempFile = [System.IO.Path]::GetTempFileName() + ".zip"
Write-Host "Creating temporary file..."

$tempFile = [System.IO.Path]::GetTempFileName() + $ext

$webClient = New-Object System.Net.WebClient
$webClient.DownloadFile($downloadLink, $tempFile)

Write-Host "Download finished. SDK has been saved to '$tempFile'."

return $tempFile
}

function Install-NetCoreSdk ($sdkZipPath)
function Install-NetCoreSdkFromArchive ($sdkArchivePath)
{
<#
.DESCRIPTION
Expand All @@ -263,12 +320,35 @@ function Install-NetCoreSdk ($sdkZipPath)
The zip archive which contains the .NET Core SDK.
#>

if (Test-IsWindows)
{
$dotnetInstallDir = [System.IO.Path]::Combine($pwd, ".dotnetsdk")
New-Item $dotnetInstallDir -ItemType Directory -Force | Out-Null
Write-Host "Created folder '$dotnetInstallDir'."
Expand-Archive -LiteralPath $sdkArchivePath -DestinationPath $dotnetInstallDir -Force
Write-Host "Extracted '$sdkArchivePath' to folder '$dotnetInstallDir'."
}
else
{
$dotnetInstallDir = "$env:HOME/.dotnetsdk"
Invoke-Cmd "mkdir -p $dotnetInstallDir"
Write-Host "Created folder '$dotnetInstallDir'."
Invoke-Cmd "tar -xf $sdkArchivePath -C $dotnetInstallDir"
Write-Host "Extracted '$sdkArchivePath' to folder '$dotnetInstallDir'."
}

$env:DOTNET_INSTALL_DIR = "$pwd\.dotnetsdk"
New-Item $env:DOTNET_INSTALL_DIR -ItemType Directory -Force
Add-ToPathVariable $dotnetInstallDir
Write-Host "Added '$dotnetInstallDir' to the PATH environment variable:"
Write-Host $env:PATH
}

Expand-Archive -Path $sdkZipPath -DestinationPath $env:DOTNET_INSTALL_DIR
$env:Path = "$env:DOTNET_INSTALL_DIR;$env:Path"
function Install-NetCoreSdkForUbuntu ($ubuntuVersion, $sdkVersion)
{
Invoke-Cmd "wget -q https://packages.microsoft.com/config/ubuntu/$ubuntuVersion/packages-microsoft-prod.deb"
Invoke-Cmd "sudo dpkg -i packages-microsoft-prod.deb"
Invoke-Cmd "sudo apt-get install apt-transport-https"
Invoke-Cmd "sudo apt-get update"
Invoke-Cmd "sudo apt-get -y install dotnet-sdk-$sdkVersion"
}

# ----------------------------------------------
Expand Down
20 changes: 19 additions & 1 deletion .psscripts/install-dotnet.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,27 @@
# Install .NET Core SDK
# ----------------------------------------------

param
(
[switch] $ForceUbuntuInstall
)

$ErrorActionPreference = "Stop"

Import-module "$PSScriptRoot\build-functions.ps1" -Force

if ($ForceUbuntuInstall.IsPresent)
{
$desiredSdk = Get-DesiredSdk
$versionParts = $desiredSdk.Split(".")
$majorMinorVer = $versionParts[0] + "." + $versionParts[1]
$ubuntuVersion = Get-UbuntuVersion

Write-Host "Ubuntu version: $ubuntuVersion"
Install-NetCoreSdkForUbuntu $ubuntuVersion $majorMinorVer
return
}

# Rename the global.json before making the dotnet --version call
# This will prevent AppVeyor to fail because it might not find
# the desired SDK specified in the global.json
Expand All @@ -27,10 +44,11 @@ if ($desiredSdk -eq $currentSdk)
}

Write-Host "The current .NET SDK ($currentSdk) doesn't match the project's desired .NET SDK ($desiredSdk)." -ForegroundColor Yellow

Write-Host "Attempting to download and install the correct .NET SDK..."

$sdkZipPath = Get-NetCoreSdkFromWeb $desiredSdk
Install-NetCoreSdk $sdkZipPath
Install-NetCoreSdkFromArchive $sdkZipPath

Write-Host ".NET SDK installation complete." -ForegroundColor Green
dotnet-version
8 changes: 4 additions & 4 deletions .psscripts/nuget-updates.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function Get-NuGetPackageInfo ($nugetPackageId, $currentVersion)
$url = "https://api-v2v3search-0.nuget.org/query?q=$nugetPackageId&prerelease=false"
$result = Invoke-RestMethod -Uri $url -Method Get
$latestVersion = $result.data[0].version
$upgradeAvailable = !$latestVersion.StartsWith($currentVersion.Replace("*", ""))
$upgradeAvailable = $currentVersion -and !$latestVersion.StartsWith($currentVersion.Replace("*", ""))

[PSCustomObject]@{
PackageName = $nugetPackageId;
Expand All @@ -31,9 +31,9 @@ filter Highlight-Upgrades

Write-Host ""
Write-Host ""
Write-Host "--------------------------------------------------"
Write-Host " Scanning all projects for NuGet package upgrades "
Write-Host "--------------------------------------------------"
Write-Host "--------------------------------------------------" -ForegroundColor DarkYellow
Write-Host " Scanning all projects for NuGet package upgrades " -ForegroundColor DarkYellow
Write-Host "--------------------------------------------------" -ForegroundColor DarkYellow
Write-Host ""

$projects = Get-ChildItem "$PSScriptRoot\..\**\*.*proj" -Recurse | % { $_.FullName }
Expand Down
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ language: csharp
sudo: required
dist: trusty

dotnet: 2.1.403
dotnet: 2.2.101
mono:
- 4.6.1
- 4.8.1
- 5.14.0
- 5.16.0

os:
- linux
Expand All @@ -16,7 +16,7 @@ before_install:
- curl https://packages.microsoft.com/config/ubuntu/14.04/prod.list | sudo tee /etc/apt/sources.list.d/microsoft.list
- sudo apt-get update
- sudo apt-get install -y powershell
- sudo pwsh ./install-dotnet.ps1
- sudo pwsh ./.psscripts/install-dotnet.ps1

script:
- export FrameworkPathOverride=$(dirname $(which mono))/../lib/mono/4.5/
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type Startup() =

### razorView

The `razorView` http handler utilises the official ASP.NET Core MVC Razor view engine to compile a view into a HTML page and sets the body of the `HttpResponse` object. It requires the content type, the view name, an optional view model and an optional view data dictionary as input parameters.
The `razorView` http handler utilises the official ASP.NET Core MVC Razor view engine to compile a view into a HTML page and sets the body of the `HttpResponse` object. It requires the content type, the view name, an optional view model, an optional view data dictionary, and an optional model state dictionary as input parameters.

#### Example:

Expand All @@ -63,7 +63,7 @@ let model = { WelcomeText = "Hello, World" }
let app =
choose [
// Assuming there is a view called "Index.cshtml"
       route "/" >=> razorView "text/html" "Index" (Some model) None
       route "/" >=> razorView "text/html" "Index" (Some model) None None
]
```

Expand All @@ -78,6 +78,7 @@ Use the razorView function:
```fsharp
open Giraffe
open Giraffe.Razor
open Microsoft.AspNetCore.Mvc.ModelBinding
[<CLIMutable>]
type TestModel =
Expand All @@ -94,10 +95,12 @@ let viewData =
"Bar", true :> obj
]
let modelState = ModelStateDictionary()
let app =
choose [
// Assuming there is a view called "Index.cshtml"
       route "/" >=> razorHtmlView "Index" (Some model) (Some viewData)
       route "/" >=> razorHtmlView "Index" (Some model) (Some viewData) (Some modelState)
]
```

Expand Down
Loading

0 comments on commit 1a824da

Please sign in to comment.