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

Allow ability to change powershell version #16

Merged
merged 3 commits into from
May 20, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions autoload/coc/powershell.vim
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
let s:root = expand('<sfile>:h:h:h')
let s:is_win = has('win32') || has('win64')
let s:is_mac = !s:is_win && !has('win32unix')
\ && (has('mac') || has('macunix') || has('gui_macvim') ||
\ (!isdirectory('/proc') && executable('sw_vers')))
\ && (has('mac') || has('macunix') || has('gui_macvim') ||
\ (!isdirectory('/proc') && executable('sw_vers')))
let s:is_vim = !has('nvim')
let s:install_script = s:root.'/install.ps1'

Expand Down
80 changes: 80 additions & 0 deletions autoload/coc/powershell/utils.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
let s:is_win = has('win32') || has('win64')
let s:is_mac = !s:is_win && !has('win32unix')
\ && (has('mac') || has('macunix') || has('gui_macvim') ||
\ (!isdirectory('/proc') && executable('sw_vers')))

let s:linuxExe = { "versionName": "PowerShell Core", "executablePath": "/usr/bin/pwsh" }
let s:linuxPreviewExe = { "versionName": "PowerShell Core Preview", "executablePath": "/usr/bin/pwsh-preview" }
let s:snapExe = { "versionName": "PowerShell Core Snap", "executablePath": "/snap/bin/pwsh" }
let s:snapPreviewExe = { "versionName": "PowerShell Core Preview Snap", "executablePath": "/snap/bin/pwsh-preview" }
let s:macOSExe = { "versionName": "PowerShell Core", "executablePath": "/usr/local/bin/pwsh" }
let s:macOSPreviewExe = { "versionName": "PowerShell Core Preview", "executablePath": "/usr/local/bin/pwsh-preview" }

function! s:getAvailablePowerShellExecutables ()
let paths = []
if(s:is_win)
call add(paths, {
\ versionName: "Windows PowerShell",
\ executablePath: "C:\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
\ })

let psCoreInstallPath = "C:\\System32\\PowerShell\\"
if(isdirectory(psCoreInstallPath))
let psCoreExePaths = split(glob(psCoreInstallPath . "**\\pwsh.exe"), "\n")
if(!empty(psCoreExePaths))
call add(paths, {
\ versionName: "PowerShell Core",
\ executablePath: psCoreExePaths[0]
\ })
endif
endif

return paths
endif

" macOS and Linux
let powershellExecutables = [ s:linuxExe, s:linuxPreviewExe, s:snapExe, s:snapPreviewExe ]
if(s:is_mac)
let powershellExecutables = [ s:macOSExe, s:macOSPreviewExe ]
endif

for powershellExecutable in powershellExecutables
if(filereadable(powershellExecutable.executablePath))
call add(paths, powershellExecutable)
endif
endfor

return paths
endfunction

function! coc#powershell#utils#switch_powershell_executable (...)
let s:choice = ""
if(!exists("a:1"))
let powershellExecutables = s:getAvailablePowerShellExecutables()
let index = 1
let choiceStr = ""
while(index <= len(powershellExecutables))
let choiceStr = choiceStr . index . " " . powershellExecutables[index - 1].versionName
if(index != len(powershellExecutables))
let choiceStr = choiceStr . "\n"
endif
let index = index + 1
endwhile
let s:choice = powershellExecutables[confirm("Which PowerShell executable would you like to use?", choiceStr) - 1].executablePath
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

help confirm(): If the user aborts the dialog by pressing , CTRL-C, or another valid interrupt key, confirm() returns 0.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed - now it silently returns if you cancel the prompt.

else
let s:choice = a:1
endif

if(!executable(s:choice))
throw "Executable not found: " . s:choice
endif

if(g:pses_powershell_executable == s:choice)
echo "PowerShell executable already set to: " . s:choice
else
let g:pses_powershell_executable = s:choice

echo "Restarting coc client to apply change..."
call coc#client#restart_all()
endif
endfunction