From 062fd10834b3383c8c7c19edeba4f151bd5efacb Mon Sep 17 00:00:00 2001 From: James Truher Date: Sun, 20 Jan 2019 13:14:52 -0800 Subject: [PATCH] Add new function which checks for the proper version of dotnet This will improve the experience when the wrong version of dotnet is present by creating an error very early on in the process with a better error message Now the developer will see something similar to the following: ``` PS /users/james/src/github/forks/JamesWTruher/PSScriptAnalyzer> ./build Incorrect dotnet version: have '2.1.403' need '2.2.102' At /users/james/src/github/forks/JamesWTruher/PSScriptAnalyzer/build.psm1:137 char:9 throw 'Incorrect dotnet version: have '' need ' ... ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CategoryInfo : OperationStopped: (Incorrect dotne... need '2.2.102':String) [], RuntimeException FullyQualifiedErrorId : Incorrect dotnet version: have '2.1.403' need '2.2.102' ``` --- build.psm1 | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/build.psm1 b/build.psm1 index f9f216554..a9c4da380 100644 --- a/build.psm1 +++ b/build.psm1 @@ -110,6 +110,45 @@ function Start-DocumentationBuild $null = New-ExternalHelp -Path $markdownDocsPath -OutputPath $outputDocsPath -Force } +# this function checks to be sure that the version of dotnet is useable to build analyzer +function Assert-UsableDotNet +{ + if ( ! (Get-Command dotnet)) { + throw "dotnet not found" + } + $globalJson = Get-Content (Join-Path $PSScriptRoot global.json) + [System.Version]$neededVersion = ($globalJson | ConvertFrom-Json).sdk.version + [System.Version]$dotnetVersion = try { + push-location $PSHOME + dotnet --version + } + catch { + throw "dotnet execution error" + } + finally { + pop-location + } + # we can't just do a simple check for the version + # see https://docs.microsoft.com/en-us/dotnet/core/tools/global-json + if ( $neededVersion.Major -ne $dotnetVersion.Major ) { + throw "Incorrect dotnet version: have '$dotnetVersion' need '$neededVersion'" + } + if ( $neededVersion.Minor -ne $dotnetVersion.Minor ) { + throw "Incorrect dotnet version: have '$dotnetVersion' need '$neededVersion'" + } + # Special logic for the build number + $neededBuildVersion = $neededVersion.Build / 100 -as [int] + $dotnetBuildVersion = $dotnetVersion.Build / 100 -as [int] + if ( $neededBuildVersion -ne $dotnetBuildVersion ) { + throw "Incorrect dotnet version: have '$dotnetVersion' need '$neededVersion'" + } + if ( $neededVersion.Build -gt $dotnetVersion.Build ) { + throw "Incorrect dotnet version: have '$dotnetVersion' need '$neededVersion'" + } + # if we haven't thrown yet, we have a dotnet we can use + return +} + # build script analyzer (and optionally build everything with -All) function Start-ScriptAnalyzerBuild { @@ -126,6 +165,11 @@ function Start-ScriptAnalyzerBuild [switch]$Documentation ) + BEGIN { + if ( ! $IsWindows ) { + Assert-UsableDotNet + } + } END { if ( $All ) {