-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProfileCommon.ps1
More file actions
184 lines (163 loc) · 8 KB
/
Copy pathProfileCommon.ps1
File metadata and controls
184 lines (163 loc) · 8 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
. (Join-Path -Path $PSScriptRoot -ChildPath ProfileDiagnostics.ps1)
Initialize-ProfileDiagnostics
Write-ProfileLog 'Importing modules'
# Don't import PSScriptAnalyzer or Pester - these will get auto-imported on usage.
@('Terminal-Icons', 'Illig') | ForEach-Object {
$moduleName = $_
Write-ProfileLog " Importing $moduleName"
Import-Module $moduleName -ErrorAction Stop
Write-ProfileLog " Imported $moduleName"
}
Write-ProfileLog 'Modules imported'
# 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) {
# 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))) {
Write-ProfileLog 'Homebrew shell environment'
# Uncomment this if you re-enable the brew shell completions below.
# $script:brewPrefix = & brew --prefix
$(brew shellenv) | Invoke-Expression
Write-ProfileLog 'Homebrew shell environment complete'
}
# nvs auto version switching - https://github.com/jasongin/nvs
if ($null -ne (Get-Command 'nvs' -ErrorAction Ignore)) {
Write-ProfileLog 'nvs auto version switching'
if (Test-Path '~/.nvmrc') {
nvs use auto | Out-Null
}
nvs auto on
Write-ProfileLog 'nvs auto version switching complete'
}
# PowerShell parameter completion shim for the dotnet CLI
Get-Command dotnet -ErrorAction Ignore | Out-Null
if ($?) {
Write-ProfileLog 'dotnet completion registration'
Register-ArgumentCompleter -Native -CommandName dotnet -ScriptBlock {
param($commandName, $wordToComplete, $cursorPosition)
dotnet complete --position $cursorPosition "$wordToComplete" | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
Write-ProfileLog 'dotnet completion registration complete'
}
# Lazy-load git-completion: register a placeholder completer that imports the
# module on first Tab, then re-invokes completion so results appear immediately.
# After this runs once, the real git-completion completer will be registered and
# this placeholder will be ignored.
Write-ProfileLog 'Registering deferred git-completion'
Register-ArgumentCompleter -Native -CommandName git -ScriptBlock {
param($wordToComplete, $commandAst, $cursorPosition)
Import-Module git-completion -Global
$line = $commandAst.ToString()
$result = [System.Management.Automation.CommandCompletion]::CompleteInput($line, $cursorPosition, $null)
$result.CompletionMatches
}
Write-ProfileLog 'Deferred git-completion registered'
# PowerShell native completions
Write-ProfileLog 'Native completions (helm, istioctl, k9s, kubectl, etc.)'
@('crane', 'helm', 'istioctl', 'k9s', 'kubectl', 'minikube', 'oras', 'skopeo') | ForEach-Object {
$command = $_
if (Get-Command $command -ErrorAction SilentlyContinue) {
Write-ProfileLog " Generating completions for $command"
& $command completion powershell | Out-String | Invoke-Expression
Write-ProfileLog " Completions for $command complete"
}
}
Write-ProfileLog 'Native completions complete'
# az CLI
# https://learn.microsoft.com/en-us/cli/azure/install-azure-cli-windows?tabs=azure-cli&pivots=winget#enable-tab-completion-in-powershell
if ($null -ne (Get-Command 'az' -ErrorAction Ignore)) {
Write-ProfileLog 'az CLI completion registration'
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
}
Write-ProfileLog 'az CLI completion registration complete'
}
# For commands installed by Homebrew that also generate Powershell completions,
# register those. https://docs.brew.sh/Shell-Completion
#
# Currently ignored to allow completions to be generated by the native command
# output above and also work on Windows. If you re-enable this, also uncomment
# the setting of brewPrefix, above.
#
# if ($null -ne $script:brewPrefix -and (Test-Path ($completions = "$script:brewPrefix/share/pwsh/completions"))) {
# Write-ProfileLog 'Homebrew PowerShell completions'
# foreach ($f in Get-ChildItem -Path $completions -File) {
# . $f
# }
# Write-ProfileLog 'Homebrew PowerShell completions complete'
# }
# Bash completions in PowerShell - only register if the command is found.
$enableBashCompletions = ([String]::IsNullOrEmpty($env:DISABLE_BASH_COMPLETIONS)) -and (($Null -ne (Get-Command bash -ErrorAction Ignore)) -or ($Null -ne (Get-Command git -ErrorAction Ignore)))
if ($enableBashCompletions) {
Write-ProfileLog 'Bash completions'
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
if (Get-Command $completerCommandName -ErrorAction SilentlyContinue) {
Register-BashArgumentCompleter $completerCommandName "$completerFullPath"
}
}
Write-ProfileLog 'Bash completions complete'
}
# Set kubectl editor to VS Code if it's present.
Get-Command code -ErrorAction Ignore | Out-Null
if ($?) {
$Env:KUBE_EDITOR = 'code --wait'
}