Skip to content

Commit

Permalink
Remove dotnet bootstrap/installation code
Browse files Browse the repository at this point in the history
We should not be doing this for the developer/user.
  • Loading branch information
andyleejordan committed Sep 11, 2024
1 parent 57ae6e0 commit 08c24ea
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 272 deletions.
54 changes: 0 additions & 54 deletions Tests/Build/BuildModule.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -48,60 +48,6 @@ Describe "Build Module Tests" {
}
}

Context "Test-DotnetInstallation" {
BeforeAll {
$availableVersions = ConvertTo-PortableVersion -strVersion "2.2.400","2.2.401","2.2.405"
$foundVersion = ConvertTo-PortableVersion -strVersion 2.2.402
$missingVersion = ConvertTo-PortableVersion -strVersion 2.2.410
}

It "Test-DotnetInstallation finds a good version" {
Mock Get-InstalledCLIVersion { return $availableVersions }
Mock Get-GlobalJSonSdkVersion { return $foundVersion }
$result = Test-DotnetInstallation -requestedVersion (Get-GlobalJsonSdkVersion) -installedVersions (Get-InstalledCLIVersion)
Assert-MockCalled "Get-InstalledCLIVersion" -Times 1
Assert-MockCalled "Get-GlobalJsonSdkVersion" -Times 1
$result | Should -Be $true
}

It "Test-DotnetInstallation cannot find a good version should return false" {
Mock Get-InstalledCLIVersion { return $availableVersions }
Mock Get-GlobalJSonSdkVersion { return $missingVersion }
$result = Test-DotnetInstallation -requestedVersion (Get-GlobalJsonSdkVersion) -installedVersions (Get-InstalledCLIVersion)
Assert-MockCalled "Get-InstalledCLIVersion" -Times 1
Assert-MockCalled "Get-GlobalJsonSdkVersion" -Times 1
$result | Should -Be $false
}
}

Context "Receive-DotnetInstallScript" {

Mock -ModuleName Build Receive-File { new-item -type file TestDrive:/dotnet-install.sh }
It "Downloads the proper non-Windows file" {
try {
push-location TestDrive:
Receive-DotnetInstallScript -platform NonWindows
"TestDrive:/dotnet-install.sh" | Should -Exist
}
finally {
Pop-Location
}
}

Mock -ModuleName Build Receive-File { new-item -type file TestDrive:/dotnet-install.ps1 }
It "Downloads the proper file Windows file" {
try {
push-location TestDrive:
Receive-DotnetInstallScript -platform "Windows"
"TestDrive:/dotnet-install.ps1" | Should -Exist
}
finally {
Pop-Location
}
}

}

Context "Test result functions" {
BeforeAll {
$xmlFile = @'
Expand Down
20 changes: 2 additions & 18 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,11 @@ param(
[Parameter(ParameterSetName='Test')]
[switch] $InProcess,

[Parameter(ParameterSetName='Bootstrap')]
[switch] $Bootstrap,

[Parameter(ParameterSetName='BuildAll')]
[switch] $Catalog,

[Parameter(ParameterSetName='Package')]
[switch] $BuildNupkg,

[Parameter(ParameterSetName='Package')]
[switch] $CopyManifest,

[Parameter(ParameterSetName='Package')]
[switch] $Signed
[switch] $BuildNupkg

)

Expand Down Expand Up @@ -90,15 +81,8 @@ END {
}
Start-ScriptAnalyzerBuild @buildArgs
}
"Bootstrap" {
Install-DotNet
return
}
"Package" {
if($CopyManifest) {
Copy-Manifest -signed:$Signed
}
Start-CreatePackage -signed:$Signed
Start-CreatePackage
}
"Test" {
Test-ScriptAnalyzer -InProcess:$InProcess
Expand Down
203 changes: 3 additions & 200 deletions build.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ $analyzerName = "PSScriptAnalyzer"

function Get-AnalyzerVersion
{
$csprojPath = [io.path]::Combine($projectRoot,"Engine","Engine.csproj")
$xml = [xml](Get-Content "${csprojPath}")
[xml]$xml = Get-Content $([io.path]::Combine($projectRoot,"Engine","Engine.csproj"))
$xml.SelectSingleNode(".//VersionPrefix")."#text"
}

Expand All @@ -29,61 +28,6 @@ function Publish-File
}
}

# attempt to get the users module directory
function Get-UserModulePath
{
if ( $IsCoreCLR -and -not $IsWindows )
{
$platformType = "System.Management.Automation.Platform" -as [Type]
if ( $platformType ) {
${platformType}::SelectProductNameForDirectory("USER_MODULES")
}
else {
throw "Could not determine users module path"
}
}
else {
"${HOME}/Documents/WindowsPowerShell/Modules"
}
}


function Uninstall-ScriptAnalyzer
{
[CmdletBinding(SupportsShouldProcess)]
param ( $ModulePath = $(Join-Path -Path (Get-UserModulePath) -ChildPath ${analyzerName}) )
END {
if ( $PSCmdlet.ShouldProcess("$modulePath") ) {
Remove-Item -Recurse -Path "$ModulePath" -Force
}
}
}

# install script analyzer, by default into the users module path
function Install-ScriptAnalyzer
{
[CmdletBinding(SupportsShouldProcess)]
param ( $ModulePath = $(Join-Path -Path (Get-UserModulePath) -ChildPath ${analyzerName}) )
END {
if ( $PSCmdlet.ShouldProcess("$modulePath") ) {
Copy-Item -Recurse -Path "$script:destinationDir" -Destination "$ModulePath\." -Force
}
}
}

# if script analyzer is installed, remove it
function Uninstall-ScriptAnalyzer
{
[CmdletBinding(SupportsShouldProcess)]
param ( $ModulePath = $(Join-Path -Path (Get-UserModulePath) -ChildPath ${analyzerName}) )
END {
if ((Test-Path $ModulePath) -and (Get-Item $ModulePath).PSIsContainer )
{
Remove-Item -Force -Recurse $ModulePath
}
}
}

# Clean up the build location
function Remove-Build
{
Expand Down Expand Up @@ -157,9 +101,6 @@ function Start-ScriptAnalyzerBuild

BEGIN {
# don't allow the build to be started unless we have the proper Cli version
# this will not actually install dotnet if it's already present, but it will
# install the proper version
Install-Dotnet
if ( -not (Test-SuitableDotnet) ) {
$requiredVersion = $script:wantedVersion
$foundVersion = Get-InstalledCLIVersion
Expand Down Expand Up @@ -431,49 +372,6 @@ function Get-TestFailures
$results.SelectNodes(".//test-case[@result='Failure']")
}

# BOOTSTRAPPING CODE FOR INSTALLING DOTNET
# install dotnet cli tools based on the version mentioned in global.json
function Install-Dotnet
{
[CmdletBinding(SupportsShouldProcess=$true)]
param (
[Parameter()][Switch]$Force,
[Parameter()]$version = $( Get-GlobalJsonSdkVersion -Raw )
)

if ( Test-DotnetInstallation -requestedversion $version ) {
if ( $Force ) {
Write-Verbose -Verbose "Installing again"
}
else {
return
}
}

try {
Push-Location $PSScriptRoot
$installScriptPath = Receive-DotnetInstallScript
$installScriptName = [System.IO.Path]::GetFileName($installScriptPath)
If ( $PSCmdlet.ShouldProcess("$installScriptName for $version")) {
& "${installScriptPath}" -c release -version $version -SkipNonVersionedFiles
}
# this may be the first time that dotnet has been installed,
# set up the executable variable
if ( -not $script:DotnetExe ) {
$script:DotnetExe = Get-DotnetExe
}
}
catch {
throw $_
}
finally {
if ( Test-Path $installScriptPath ) {
Remove-Item $installScriptPath
}
Pop-Location
}
}

function Get-GlobalJsonSdkVersion {
param ( [switch]$Raw )
$json = Get-Content -raw (Join-Path $PSScriptRoot global.json) | ConvertFrom-Json
Expand Down Expand Up @@ -612,68 +510,6 @@ function Get-InstalledCLIVersion {
return (ConvertTo-PortableVersion $installedVersions)
}

function Test-DotnetInstallation
{
param (
$requestedVersion = $script:wantedVersion,
$installedVersions = $( Get-InstalledCLIVersion )
)
return (Test-SuitableDotnet -availableVersions $installedVersions -requiredVersion $requestedVersion )
}

function Receive-File {
param ( [Parameter(Mandatory,Position=0)]$uri )

# enable Tls12 for the request
# -SslProtocol parameter for Invoke-WebRequest wasn't in PSv3
$securityProtocol = [System.Net.ServicePointManager]::SecurityProtocol
$tls12 = [System.Net.SecurityProtocolType]::Tls12
try {
if ( ([System.Net.ServicePointManager]::SecurityProtocol -band $tls12) -eq 0 ) {
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor $tls12
}
$null = Invoke-WebRequest -Uri ${uri} -OutFile "${installScriptName}"
}
finally {
[System.Net.ServicePointManager]::SecurityProtocol = $securityProtocol
}
if ( (Test-Path Variable:IsWindows) -and -not $IsWindows ) {
chmod +x $installScriptName
}
$installScript = Get-Item $installScriptName -ErrorAction Stop
if ( -not $installScript ) {
throw "Download failure of ${uri}"
}
return $installScript
}

function Receive-DotnetInstallScript
{
# param '$platform' is a hook to enable forcing download of a specific
# install script, generally it should not be used except in testing.
param ( $platform = "" )

# if $platform has been set, it has priority
# if it's not set to Windows or NonWindows, it will be ignored
if ( $platform -eq "Windows" ) {
$installScriptName = "dotnet-install.ps1"
}
elseif ( $platform -eq "NonWindows" ) {
$installScriptName = "dotnet-install.sh"
}
elseif ( ((Test-Path Variable:IsWindows) -and -not $IsWindows) ) {
# if the variable IsWindows exists and it is set to false
$installScriptName = "dotnet-install.sh"
}
else { # the default case - we're running on a Windows system
$installScriptName = "dotnet-install.ps1"
}
$uri = "https://dot.net/v1/${installScriptName}"

$installScript = Receive-File -Uri $uri
return $installScript.FullName
}

function Get-DotnetExe
{
param ( $version = $script:wantedVersion )
Expand Down Expand Up @@ -730,7 +566,7 @@ try {
$script:DotnetExe = Get-DotnetExe
}
catch {
Write-Warning "Could not find dotnet executable"
Write-Warning "The dotnet CLI was not found, please install it: https://aka.ms/dotnet-cli"
}

# Copies the built PSCompatibilityCollector module to the output destination for PSSA
Expand Down Expand Up @@ -780,44 +616,11 @@ function Copy-CrossCompatibilityModule
}
}

# copy the manifest into the module if is present
function Copy-Manifest
{
param ( [switch]$signed )
if ( $signed ) {
$buildRoot = "signed"
}
else {
$buildRoot = "out"
}
$analyzerVersion = Get-AnalyzerVersion
# location where analyzer goes
# debugging
(Get-ChildItem -File -Recurse)|ForEach-Object {Write-Verbose -Verbose -Message $_}
$modBaseDir = [io.path]::Combine($projectRoot,${buildRoot},"${analyzerName}", $analyzerVersion)
# copy the manifest files
Push-Location $buildRoot
if ( Test-Path _manifest ) {
Copy-Item -Recurse -Path _manifest -Destination $modBaseDir -Verbose
}
else {
Write-Warning -Message "_manifest not found in $PWD"
}
Pop-Location
}

# creates the nuget package which can be used for publishing to the gallery
function Start-CreatePackage
{
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseCompatibleCommands', '')]
param ( [switch]$signed )
try {
if ( $signed ) {
$buildRoot = "signed"
}
else {
$buildRoot = "out"
}
$buildRoot = "out"
$repoName = [guid]::NewGuid().ToString()
$nupkgDir = Join-Path $PSScriptRoot $buildRoot
$null = Register-PSRepository -Name $repoName -InstallationPolicy Trusted -SourceLocation $nupkgDir
Expand Down

0 comments on commit 08c24ea

Please sign in to comment.