Skip to content

Commit 062fd10

Browse files
committed
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' ```
1 parent 7a9d45d commit 062fd10

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

build.psm1

+44
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,45 @@ function Start-DocumentationBuild
110110
$null = New-ExternalHelp -Path $markdownDocsPath -OutputPath $outputDocsPath -Force
111111
}
112112

113+
# this function checks to be sure that the version of dotnet is useable to build analyzer
114+
function Assert-UsableDotNet
115+
{
116+
if ( ! (Get-Command dotnet)) {
117+
throw "dotnet not found"
118+
}
119+
$globalJson = Get-Content (Join-Path $PSScriptRoot global.json)
120+
[System.Version]$neededVersion = ($globalJson | ConvertFrom-Json).sdk.version
121+
[System.Version]$dotnetVersion = try {
122+
push-location $PSHOME
123+
dotnet --version
124+
}
125+
catch {
126+
throw "dotnet execution error"
127+
}
128+
finally {
129+
pop-location
130+
}
131+
# we can't just do a simple check for the version
132+
# see https://docs.microsoft.com/en-us/dotnet/core/tools/global-json
133+
if ( $neededVersion.Major -ne $dotnetVersion.Major ) {
134+
throw "Incorrect dotnet version: have '$dotnetVersion' need '$neededVersion'"
135+
}
136+
if ( $neededVersion.Minor -ne $dotnetVersion.Minor ) {
137+
throw "Incorrect dotnet version: have '$dotnetVersion' need '$neededVersion'"
138+
}
139+
# Special logic for the build number
140+
$neededBuildVersion = $neededVersion.Build / 100 -as [int]
141+
$dotnetBuildVersion = $dotnetVersion.Build / 100 -as [int]
142+
if ( $neededBuildVersion -ne $dotnetBuildVersion ) {
143+
throw "Incorrect dotnet version: have '$dotnetVersion' need '$neededVersion'"
144+
}
145+
if ( $neededVersion.Build -gt $dotnetVersion.Build ) {
146+
throw "Incorrect dotnet version: have '$dotnetVersion' need '$neededVersion'"
147+
}
148+
# if we haven't thrown yet, we have a dotnet we can use
149+
return
150+
}
151+
113152
# build script analyzer (and optionally build everything with -All)
114153
function Start-ScriptAnalyzerBuild
115154
{
@@ -126,6 +165,11 @@ function Start-ScriptAnalyzerBuild
126165
[switch]$Documentation
127166
)
128167

168+
BEGIN {
169+
if ( ! $IsWindows ) {
170+
Assert-UsableDotNet
171+
}
172+
}
129173
END {
130174
if ( $All )
131175
{

0 commit comments

Comments
 (0)