Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(Git): Support $env:ComSpec calls on Unix-like systems #113

Merged
merged 11 commits into from
Jan 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 31 additions & 19 deletions lib/Diagnostic.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Use 'Write-UserMessage -Warning' to highlight the issue, and follow up with the
. (Join-Path $PSScriptRoot "$_.ps1")
}

function Test-Drive {
function Test-DiagDrive {
<#
.SYNOPSIS
Test disk drive requirements/configuration.
Expand All @@ -34,7 +34,7 @@ function Test-Drive {
return $result
}

function Test-WindowsDefender {
function Test-DiagWindowsDefender {
<#
.SYNOPSIS
Test windows defender exclusions.
Expand All @@ -43,6 +43,8 @@ function Test-WindowsDefender {
[OutputType([bool])]
param([Switch] $Global)

if (Test-IsUnix) { return $true }

$defender = Get-Service -Name 'WinDefend' -ErrorAction 'SilentlyContinue'
if (($defender -and $defender.Status) -and ($defender.Status -eq [System.ServiceProcess.ServiceControllerStatus]::Running)) {
if (Test-CommandAvailable -Command 'Get-MpPreference') {
Expand All @@ -65,7 +67,7 @@ function Test-WindowsDefender {
return $true
}

function Test-MainBucketAdded {
function Test-DiagMainBucketAdded {
<#
.SYNOPSIS
Test if main bucket was added after migration from core repository.
Expand All @@ -87,7 +89,7 @@ function Test-MainBucketAdded {
return $true
}

function Test-LongPathEnabled {
function Test-DiagLongPathEnabled {
<#
.SYNOPSIS
Test if long paths option is enabled.
Expand All @@ -96,6 +98,8 @@ function Test-LongPathEnabled {
[OutputType([bool])]
param()

if (Test-IsUnix) { return $true }

# Verify supported windows version
if ([System.Environment]::OSVersion.Version.Major -lt 10 -or [System.Environment]::OSVersion.Version.Build -lt 1607) {
Write-UserMessage -Message 'LongPath configuration is not supported in older Windows versions' -Warning
Expand All @@ -116,7 +120,7 @@ function Test-LongPathEnabled {
return $true
}

function Test-EnvironmentVariable {
function Test-DiagEnvironmentVariable {
<#
.SYNOPSIS
Test if scoop's related environment variables are defined.
Expand All @@ -127,20 +131,28 @@ function Test-EnvironmentVariable {

$result = $true

# Comspec
if (($null -eq $env:COMSPEC) -or (!(Test-Path $env:COMSPEC -PathType 'Leaf'))) {
Write-UserMessage -Message '''COMSPEC'' is not configured' -Warning
Write-UserMessage -Message @(
' By default the variable should points to the cmd.exe in Windows: ''%SystemRoot%\system32\cmd.exe''.'
' Fixable with running following command in elevated prompt:'
' [Environment]::SetEnvironmentVariable(''COMSPEC'', "$env:SystemRoot\system32\cmd.exe", ''Machine'')'
)
$result = $false
if (Test-IsUnix) {
# Unix "comspec"
if (!(Test-Path $env:SHELL -PathType 'Leaf')) {
Write-UserMessage -Message '''SHELL'' environment variable is not configured' -Warning
$result = $false
}
} else {
# Windows Comspec
if (($null -eq $env:COMSPEC) -or (!(Test-Path $env:COMSPEC -PathType 'Leaf'))) {
Write-UserMessage -Message '''COMSPEC'' environment variable is not configured' -Warning
Write-UserMessage -Message @(
' By default the variable should points to the cmd.exe in Windows: ''%SystemRoot%\system32\cmd.exe''.'
' Fixable with running following command in elevated prompt:'
' [Environment]::SetEnvironmentVariable(''COMSPEC'', "$env:SystemRoot\system32\cmd.exe", ''Machine'')'
)
$result = $false
}
}

# Scoop ENV
if (!$env:SCOOP) {
Write-UserMessage -Message '''SCOOP'' is not configured' -Warning
Write-UserMessage -Message '''SCOOP'' environment variable is not configured' -Warning
Write-UserMessage -Message @(
' SCOOP environment should be set as it is widely used by users and documentation to reference scoop installation directory'
' Fixable with running following command:'
Expand All @@ -161,7 +173,7 @@ function Test-EnvironmentVariable {
return $result
}

function Test-HelpersInstalled {
function Test-DiagHelpersInstalled {
<#
.SYNOPSIS
Test if all widely used helpers are installed.
Expand Down Expand Up @@ -219,7 +231,7 @@ function Test-HelpersInstalled {
return $result
}

function Test-Config {
function Test-DiagConfig {
<#
.SYNOPSIS
Test if various recommended scoop configurations are set correctly.
Expand All @@ -241,7 +253,7 @@ function Test-Config {
return $result
}

function Test-CompletionRegistered {
function Test-DiagCompletionRegistered {
<#
.SYNOPSIS
Test if native completion is imported.
Expand All @@ -266,7 +278,7 @@ function Test-CompletionRegistered {
return $true
}

function Test-ShovelAdoption {
function Test-DiagShovelAdoption {
<#
.SYNOPSIS
Test if shovel implementation was fully adopted by user.
Expand Down
13 changes: 6 additions & 7 deletions lib/Git.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,18 @@ function Invoke-GitCmd {
default { $action = $Command }
}

$commandToRun = 'git', ($preAction -join ' '), $action, ($Argument -join ' ') -join ' '
$commandToRun = $commandToRunNix = $commandToRunWindows = ('git', ($preAction -join ' '), $action, ($Argument -join ' ')) -join ' '

if ($Proxy) {
$prox = get_config 'proxy' 'none'

# TODO: Drop comspec
if ($prox -and ($prox -ne 'none')) { $commandToRun = "SET HTTPS_PROXY=$prox && SET HTTP_PROXY=$prox && $commandToRun" }
if ($prox -and ($prox -ne 'none')) {
$keyword = if (Test-IsUnix) { 'export' } else { 'SET' }
$commandToRunWindows = $commandToRunNix = "$keyword HTTPS_PROXY=$prox && $keyword HTTP_PROXY=$prox && $commandToRun"
}
}

debug $commandToRun

# TODO: Drop comspec
& "$env:ComSpec" /d /c $commandToRun
Invoke-SystemComSpecCommand -Windows $commandToRunWindows -Unix $commandToRunNix
}
}

Expand Down
47 changes: 47 additions & 0 deletions lib/core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,53 @@ function Show-DeprecatedWarning {
Write-UserMessage -Message " -> $($Invocation.PSCommandPath):$($Invocation.ScriptLineNumber):$($Invocation.OffsetInLine)" -Color 'DarkGray'
}

function Test-IsUnix {
<#
.SYNOPSIS
Custom check to identify non-windows hosts.
.DESCRIPTION
$isWindows is not defind in PW5, thus null and boolean checks are needed.
#>
[CmdletBinding()]
[OutputType([bool])]
param()

process { return !(($null -eq $isWindows) -or ($isWindows -eq $true)) }
}

function Invoke-SystemComSpecCommand {
<#
.SYNOPSIS
Wrapper around $env:ComSpec/$env:SHELL calls.
.PARAMETER Windows
Specifies the command to be executed on Windows using $env:ComSpec.
.PARAMETER Unix
Specifies the command to be executed on *Nix like systems using $env:SHELL.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[String] $Windows,
[Parameter(Mandatory)]
[String] $Unix
)

process {
if (Test-IsUnix) {
$shell = $env:SHELL
$parameters = @('-c', $Unix)
} else {
$shell = $env:ComSpec
$parameters = @('/d', '/c', $Windows)
}

$debugShell = "& ""$shell"" $($parameters -join ' ')"
debug $debugShell

& "$shell" @parameters
}
}

function load_cfg($file) {
if (!(Test-Path $file)) { return $null }

Expand Down
20 changes: 10 additions & 10 deletions libexec/scoop-checkup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
}

$issues = 0
$issues += !(Test-WindowsDefender)
$issues += !(Test-WindowsDefender -Global)
$issues += !(Test-MainBucketAdded)
$issues += !(Test-LongPathEnabled)
$issues += !(Test-EnvironmentVariable)
$issues += !(Test-HelpersInstalled)
$issues += !(Test-Drive)
$issues += !(Test-Config)
$issues += !(Test-CompletionRegistered)
$issues += !(Test-ShovelAdoption)
$issues += !(Test-DiagWindowsDefender)
$issues += !(Test-DiagWindowsDefender -Global)
$issues += !(Test-DiagMainBucketAdded)
$issues += !(Test-DiagLongPathEnabled)
$issues += !(Test-DiagEnvironmentVariable)
$issues += !(Test-DiagHelpersInstalled)
$issues += !(Test-DiagDrive)
$issues += !(Test-DiagConfig)
$issues += !(Test-DiagCompletionRegistered)
$issues += !(Test-DiagShovelAdoption)

if ($issues -gt 0) {
Write-UserMessage -Message '', "Found $issues potential $(pluralize $issues 'problem' 'problems')." -Warning
Expand Down