-
Notifications
You must be signed in to change notification settings - Fork 1
/
profile.ps1
213 lines (189 loc) · 7.13 KB
/
profile.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# Detect elevation
$IsAdmin = & {
if ($IsWindows) {
$wid = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$prp = New-Object System.Security.Principal.WindowsPrincipal($wid)
$adm = [System.Security.Principal.WindowsBuiltInRole]::Administrator
$prp.IsInRole($adm)
}
elseif ($IsLinux) {
(id) -like 'uid=0*'
}
}
function Import-RequiredModule {
param(
[string[]]$Name,
[switch]$AllowClobber
)
$Name | ForEach-Object {
if (-not (Get-Module -Name $_ -ListAvailable)) {
Install-Module -Name $_ -Scope CurrentUser -AllowClobber:$AllowClobber # -Repository PSGallery
}
Import-Module $_
}
}
# Dot source functions
(Join-Path $PSScriptRoot 'Functions'), (Join-Path $PSScriptRoot "${env:COMPUTERNAME}\Functions") |
Where-Object { Test-Path $_ } |
ForEach-Object {
Get-ChildItem (Join-Path $_ '*.ps1') | ForEach-Object {
. $_.FullName
}
}
# Write banner
Import-RequiredModule Figlet -AllowClobber
if ($IsWindows) {
$script:ComputerName = $env:COMPUTERNAME
}
elseif ($IsLinux) {
if (Get-Command hostname -ErrorAction SilentlyContinue) {
$script:ComputerName = (hostname)
}
else {
$script:ComputerName = Get-Content /etc/hostname
}
}
Write-Figlet $script:ComputerName -Font big -Foreground ($IsAdmin ? 'Red' : 'Blue')
# Configure environment variables
Set-Variable -Name ProfileDir -Value (Split-Path $profile)
Set-Variable -Name ModuleDir -Value (Join-Path $ProfileDir 'Modules')
if (Get-Command -Name code -ErrorAction SilentlyContinue) {
$env:EDITOR = 'code'
}
else {
if ($IsWindows) {
$env:EDITOR = 'notepad'
}
}
# If we have a bin folder add it to the path
if (Test-Path ~/bin) {
Add-Path ~/bin
}
# If we have a scripts folder add it to the path
if (Test-Path $PSScriptRoot/scripts) {
Add-Path $PSScriptRoot/scripts
}
# If we have a ~/.git_commands folder at it to the path
if (Test-Path ~/.git_commands) {
Add-Path ~/.git_commands
}
# Create SymLinks for dotfiles
if ($IsWindows) {
foreach ($dotfile in (Get-ChildItem -File -Path (Join-Path $PSScriptRoot 'dotfiles'))) {
$destination = Join-Path ~ (Split-Path -Leaf $dotfile)
if (-not (Test-Path $destination -PathType Leaf)) {
if ($IsAdmin) {
Write-Host -ForegroundColor Blue "Linking dotfile '$destination'..."
New-Item -Path $destination -ItemType SymbolicLink -Value $dotfile | Out-Null
}
else {
Write-Warning "Unable to create symlink for '$destination'. Open an elevated PowerShell to create the symlink."
}
}
}
foreach ($dotfolder in (Get-ChildItem -Directory -Path (Join-Path $PSScriptRoot 'dotfiles'))) {
$destination = Join-Path ~ (Split-Path -Leaf $dotfolder)
if (-not (Test-Path $destination -PathType Container)) {
if ($IsAdmin) {
Write-Host -ForegroundColor Blue "Linking dotfolder '$destination'..."
New-Item -Path $destination -ItemType SymbolicLink -Value $dotfolder | Out-Null
}
else {
Write-Warning "Unable to create symlink for folder '$destination'. Open an elevated PowerShell to create the symlink."
}
}
}
}
Import-RequiredModule posh-git
if (Get-Module PowerLocation -ListAvailable) {
Import-Module PowerLocation
Set-Alias -Name z -Value Set-PowerLocation
}
. $PSScriptRoot/customprompt.ps1
# Setup our DynamicTitle
#. (Join-Path $PSScriptRoot DynamicTitle.ps1)
if (Get-Command fzf -ErrorAction SilentlyContinue) {
# Setup fzf
Import-RequiredModule PSFzf
Set-PsFzfOption -PSReadlineChordProvider 'Ctrl+t' -PSReadlineChordReverseHistory 'Ctrl+r'
}
if (Get-Command zoxide -ErrorAction SilentlyContinue) {
# Setup zoxide
Invoke-Expression (& { (zoxide init powershell --hook pwd | Out-String) })
}
. (Join-Path $PSScriptRoot psreadlinecfg.ps1)
# PowerShell parameter completion shim for the dotnet CLI
if (Get-Command -Name dotnet -ErrorAction SilentlyContinue) {
Write-Host -ForegroundColor Blue "Registering argument completer for 'dotnet'..."
Register-ArgumentCompleter -Native -CommandName dotnet -ScriptBlock {
param($commandName, $wordToComplete, $cursorPosition)
dotnet complete --position $cursorPosition "$wordToComplete" | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
}
else {
Write-Information "Command 'dotnet' not found."
}
if (Get-Command -Name winget -ErrorAction SilentlyContinue) {
Write-Host -ForegroundColor Blue "Registering argument completer for 'winget'..."
Register-ArgumentCompleter -Native -CommandName winget -ScriptBlock {
param($wordToComplete, $commandAst, $cursorPosition)
[Console]::InputEncoding = [Console]::OutputEncoding = $OutputEncoding = [System.Text.Utf8Encoding]::new()
$Local:word = $wordToComplete.Replace('"', '""')
$Local:ast = $commandAst.ToString().Replace('"', '""')
winget complete --word="$Local:word" --commandline "$Local:ast" --position $cursorPosition | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
}
else {
Write-Information "Command 'winget' not found."
}
# PowerShell parameter completion shim for the nuke CLI
if (Get-Command -Name nuke -ErrorAction SilentlyContinue) {
Write-Host -ForegroundColor Blue "Registering argument completer for 'nuke'..."
Register-ArgumentCompleter -Native -CommandName nuke -ScriptBlock {
param($commandName, $wordToComplete, $cursorPosition)
nuke :complete "$wordToComplete" | ForEach-Object {
if (-not $_.StartsWith('NUKE Global Tool')) {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
}
}
else {
Write-Information "Command 'nuke' not found."
}
# Hopefully temporary fix for "dotnet new" slowness (https://github.com/dotnet/templating/issues/2093)
#$env:DOTNET_NEW_LOCAL_SEARCH_FILE_ONLY = 1
# Invoke machine specific profile
@(Join-Path $PSScriptRoot "machine\${env:COMPUTERNAME}\$($MyInvocation.MyCommand.Name)") |
Where-Object { Test-Path $_ } |
ForEach-Object { . $_ }
# Set an alias for invoking build.ps1 scripts in the current location
Set-Alias -Name b -Value './build.ps1'
if ($IsLinux) {
Set-Alias -Name ls -Value Get-ChildItem
}
# Display notice if there's profile changes
Push-Location $ProfileDir
try {
if (Get-Command -Name git -ErrorAction SilentlyContinue) {
if (-not (git status | Select-String 'nothing to commit')) {
Write-Warning 'Profile changes need to be committed and pushed'
}
else {
git fetch
if ((git rev-list --count origin..HEAD) -gt 0) {
Write-Warning 'Local profile changes need to be pushed'
}
elseif ((git rev-list --count HEAD..origin) -gt 0) {
Write-Warning 'Remote profile changes need to be merged'
}
}
}
}
finally {
Pop-Location
}