-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProfileCommon.ps1
142 lines (123 loc) · 5.84 KB
/
ProfileCommon.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
Import-Module VSSetup
Import-Module PSScriptAnalyzer
Import-Module Pester
Import-Module Terminal-Icons
Import-Module Illig
# Paths: Put user-specific paths in the OS location for that.
# - On Windows, System/Advanced System Settings/Environment Variables
# - On Mac/Linux, /etc/profile like
# PATH="$PATH:$HOME/go/bin:$HOME/.dotnet/tools:$HOME/.krew/bin"
# Windows defaults to ASCII; set UTF-8 and verify ANSI color support.
Set-ConsoleEncoding -UTF8
$Env:PYTHONIOENCODING = "UTF-8"
Test-AnsiSupport | Out-Null
# Azure Artifacts Credential Provider doesn't actually cache the token very long
# unless you keep MSAL enabled.
# https://developercommunity.visualstudio.com/t/azure-artifacts-credential-provider-unable-to-auth/1519587
# https://github.com/microsoft/artifacts-credprovider/issues/234
$Env:NUGET_CREDENTIALPROVIDER_MSAL_ENABLED = "true"
# Update path settings for Windows-specific settings.
If ($isDesktop -or $IsWindows) {
# Import VS environment
# As of VS 2019 16.2 there's a PowerShell module for developer VS prompt.
# However, it is NOT compatible with PowerShell Core.
# Instance ID can be found when locating the VS install information.
# C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -NoExit -Command "& { Import-Module .\Common7\Tools\vsdevshell\Microsoft.VisualStudio.DevShell.dll; Enter-VsDevShell -InstanceId 5a7ac072}"
Invoke-VisualStudioDevPrompt
# Put the user paths before the machine paths so dotnet install overrides are possible.
$combined = [System.Collections.ArrayList][System.Environment]::GetEnvironmentVariable("PATH").Split(";", [System.StringSplitOptions]::RemoveEmptyEntries)
$userSegments = [System.Environment]::GetEnvironmentVariable("PATH", "User").Split(";", [System.StringSplitOptions]::RemoveEmptyEntries)
$userSegments | ForEach-Object { $combined.Remove($_) }
$combined.InsertRange(0, $userSegments)
[System.Environment]::SetEnvironmentVariable("PATH", ($combined -join ";"))
}
# Fix double-wide XML icon in Terminal-Icons
# https://github.com/devblackops/Terminal-Icons/issues/34
If ($Null -ne (Get-Module Terminal-Icons)) {
Set-TerminalIconsIcon -Glyph "nf-mdi-xml" -NewGlyph "nf-mdi-file_xml"
}
# Aliases
Set-Alias -Name which -Value Get-Command
# MacOS/dotnet fix - some dotnet global commands require DOTNET_HOST_PATH but
# that doesn't always get set by the dotnet CLI.
$dotnetLocation = Get-Command "dotnet" -ErrorAction Ignore
if ($null -ne $dotnetLocation) {
[System.Environment]::SetEnvironmentVariable("DOTNET_HOST_PATH", $dotnetLocation.Source)
}
# Chocolatey profile
if ($isDesktop -or $IsWindows) {
$ChocolateyProfile = "$env:ChocolateyInstall/helpers/chocolateyProfile.psm1"
if (Test-Path($ChocolateyProfile)) {
Import-Module "$ChocolateyProfile"
}
}
# Homebrew settings
if ($IsMacOS -and ($null -ne (Get-Command "brew" -ErrorAction Ignore))) {
$(brew shellenv) | Invoke-Expression
}
# nvs auto version switching - https://github.com/jasongin/nvs
if ($null -ne (Get-Command "nvs" -ErrorAction Ignore)) {
if (Test-Path "~/.nvmrc") {
nvs use auto | Out-Null
}
nvs auto on
}
# PowerShell parameter completion shim for the dotnet CLI
Get-Command dotnet -ErrorAction Ignore | Out-Null
if ($?) {
Register-ArgumentCompleter -Native -CommandName dotnet -ScriptBlock {
param($commandName, $wordToComplete, $cursorPosition)
dotnet complete --position $cursorPosition "$wordToComplete" | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
}
# PowerShell native completions
@("helm", "istioctl", "k9s", "kubectl") | ForEach-Object {
$command = $_
if (Get-Command $command -ErrorAction SilentlyContinue) {
& $command completion powershell | Out-String | Invoke-Expression
}
}
# az CLI
# https://learn.microsoft.com/en-us/cli/azure/install-azure-cli-windows?tabs=azure-cli&pivots=winget#enable-tab-completion-in-powershell
Register-ArgumentCompleter -Native -CommandName az -ScriptBlock {
param($commandName, $wordToComplete, $cursorPosition)
$completion_file = New-TemporaryFile
$env:ARGCOMPLETE_USE_TEMPFILES = 1
$env:_ARGCOMPLETE_STDOUT_FILENAME = $completion_file
$env:COMP_LINE = $wordToComplete
$env:COMP_POINT = $cursorPosition
$env:_ARGCOMPLETE = 1
$env:_ARGCOMPLETE_SUPPRESS_SPACE = 0
$env:_ARGCOMPLETE_IFS = "`n"
$env:_ARGCOMPLETE_SHELL = 'powershell'
az 2>&1 | Out-Null
Get-Content $completion_file | Sort-Object | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
Remove-Item $completion_file, Env:\_ARGCOMPLETE_STDOUT_FILENAME, Env:\ARGCOMPLETE_USE_TEMPFILES, Env:\COMP_LINE, Env:\COMP_POINT, Env:\_ARGCOMPLETE, Env:\_ARGCOMPLETE_SUPPRESS_SPACE, Env:\_ARGCOMPLETE_IFS, Env:\_ARGCOMPLETE_SHELL
}
# For commands installed by Homebrew that also generate Powershell completions, register those.
# https://docs.brew.sh/Shell-Completion
if ((Get-Command brew) -and (Test-Path ($completions = "$(brew --prefix)/share/pwsh/completions"))) {
foreach ($f in Get-ChildItem -Path $completions -File) {
. $f
}
}
# Bash completions in PowerShell
$enableBashCompletions = ($Null -ne (Get-Command bash -ErrorAction Ignore)) -or ($Null -ne (Get-Command git -ErrorAction Ignore))
if ($enableBashCompletions) {
Import-Module PSBashCompletions
$completionPath = [System.IO.Path]::Combine([System.IO.Path]::GetDirectoryName($profile), "bash-completion")
Get-ChildItem $completionPath -Exclude ".editorconfig" | ForEach-Object {
$completerFullPath = $_.FullName
$completerCommandName = $_.Name
Register-BashArgumentCompleter $completerCommandName "$completerFullPath"
}
}
# Set kubectl editor to VS Code if it's present.
Get-Command code -ErrorAction Ignore | Out-Null
if ($?) {
$Env:KUBE_EDITOR = "code --wait"
}