-
Notifications
You must be signed in to change notification settings - Fork 0
/
autorun.ps1
177 lines (138 loc) · 6.62 KB
/
autorun.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
# --------------------------------------------------
# Init
# --------------------------------------------------
$AutorunScript = $MyInvocation.MyCommand.Source
Write-Host "Running ""$AutorunScript"" (`$AutorunScript)" -ForegroundColor DarkGray
$PathSeparator = [System.IO.Path]::PathSeparator
$DirSeparator = [System.IO.Path]::DirectorySeparatorChar
$MyDocsPath = [Environment]::GetFolderPath('MyDocuments')
$DotfilesPath = Split-Path -Path $AutorunScript
# --------------------------------------------------
# Autoload file
# --------------------------------------------------
# Define variables
$ProfilePath = $profile.CurrentUserAllHosts
$LoadCommand = ". $AutorunScript"
$ProfileAppendString = @"
# Load autorun script on launch
$LoadCommand
"@
# Ensure profile folder and script exists
If (-Not(Test-Path (Split-Path $ProfilePath -Parent) -PathType Container)) {
New-Item (Split-Path $ProfilePath -Parent) -ItemType Directory -Force | Out-Null
}
If (-Not(Test-Path $ProfilePath -PathType Leaf)) {
New-Item $ProfilePath -ItemType File
}
# Add script to profile script
If ((Get-Content -Path $ProfilePath -ErrorAction SilentlyContinue) -notcontains $LoadCommand) {
Write-Host "Adding ""$LoadCommand"" to ""$ProfilePath""" -ForegroundColor DarkGray
$ProfileAppendString | Out-File -Path $ProfilePath -Append
}
# --------------------------------------------------
# PSModulePath
# --------------------------------------------------
# It seems PowerShell/AWS Tools installer does not respect removing Documents from the per-user modules path. FFS!
# PSModules path
# $ModulesPath = "C:\Users\${env:USERNAME}\.powershell\Modules"
# New-Item $ModulesPath -ItemType Directory -Force | Out-Null
# [Environment]::SetEnvironmentVariable("PSModulePath", $ModulesPath, "User")
# Remove Documents entry from module path
# $MyDocsPath = [Environment]::GetFolderPath('MyDocuments')
# $env:PSModulePath = ($env:PSModulePath -split $PathSeparator | Where-Object { $_ -ne "${MyDocsPath}\PowerShell\Modules" }) -join $PathSeparator
# --------------------------------------------------
# Load functions
# --------------------------------------------------
Get-ChildItem -Path $DotfilesPath -Filter _*.ps1 | ForEach {
Write-Host "Running ""$($_.FullName)""" -ForegroundColor DarkGray
. $_.FullName
}
# --------------------------------------------------
# Theming
# --------------------------------------------------
# If (Get-Module PowerLine -ListAvailable) {
# Import-Module PowerLine
# Set-PowerLinePrompt -SetCurrentDirectory -RestoreVirtualTerminal -Newline -Timestamp
# Add-PowerLineBlock { New-PromptText { if ($env:AWS_PROFILE) { $env:AWS_PROFILE } else { 'N/A' } } -Bg '#FFA500' } -Index 2
# #Add-PowerLineBlock { New-PromptText { "¤$(kubectl.exe config current-context)" } -Bg '#add8e6' } -Index 2
# Add-PowerLineBlock { New-PromptText { "¤$(kubectl.exe config current-context)/$(kubectl.exe config view --minify --output 'jsonpath={..namespace}')" } -Bg '#add8e6' } -Index 2
# }
# Else {
# Write-Warning "PowerLine module not found, skipping theming"
# }
# Disabled due to performance issues on work laptop
# If (Get-Module oh-my-posh -ListAvailable) {
# Import-Module oh-my-posh
# Set-PoshPrompt aliens
# }
# Else {
# Write-Warning "oh-my-posh module not found, skipping theming"
# }
# Invoke-Expression (&starship init powershell)
# --------------------------------------------------
# Other customisations
# --------------------------------------------------
$ErrorView = "ConciseView"
Import-Module PSReadLine
Set-PSReadLineOption -PredictionSource History
<#
Add when PSReadLine 2.1 is released?:
Set-PSReadLineOption -PredictionSource HistoryAndPlugin
#OPTIONAL you can also enable ListView
Set-PSReadLineOption -PredictionViewStyle ListView
#>
# --------------------------------------------------
# Aliases
# --------------------------------------------------
New-Alias grep Select-String -Force
New-Alias g git.exe -Force
New-Alias m multipass.exe -Force
New-Alias python3 python.exe -Force
New-Alias notepad++ 'C:\Program Files\Notepad++\notepad++.exe' -Force
# --------------------------------------------------
# Git
# --------------------------------------------------
# Configure Git to use Windows' OpenSSH in order to enable ssh-agent support
# https://snowdrift.tech/cli/ssh/git/tutorials/2019/01/31/using-ssh-agent-git-windows.html
# It seems however Git for Windows still uses its own ssh config (), so have to explicitly add '-T', though 'ForwardAgent' is enabled in ~/.ssh/config
# Set-Env -Name GIT_SSH_COMMAND -Value """$((Get-Command ssh).Source)"" -T " | Out-Null # configure in Git instead
# --------------------------------------------------
#
# --------------------------------------------------
# Check if using latest version
# $LatestVersion = (Invoke-RestMethod https://raw.githubusercontent.com/PowerShell/PowerShell/master/tools/metadata.json).StableReleaseTag -replace "v", ""
# $CurrentVersion = $PSVersionTable.PSVersion.ToString()
# If ($LatestVersion -ne $CurrentVersion) {
# Write-Host "PowerShell Core $LatestVersion is available" -ForegroundColor Yellow
# }
$Bin = @()
$Bin += @{
Name = 'Kubectl'
LocalVerCmd = { (((& kubectl version --client --short).Split(' '))[2]) }
RemoteVerCmd = { (Invoke-WebRequest "https://storage.googleapis.com/kubernetes-release/release/stable.txt").Content.Trim() }
}
$Bin += @{
Name = 'K9s'
LocalVerCmd = { (& k9s version --short | Select-String -Pattern '^Version\s+(.+)').Matches.Groups[1].Value }
RemoteVerCmd = { (Invoke-RestMethod "https://api.github.com/repos/derailed/k9s/releases/latest").tag_name }
}
# $Bin += @{
# Name = 'Saml2aws'
# LocalVerCmd = { "v" + (& saml2aws --version 2>&1) }
# RemoteVerCmd = { (Invoke-RestMethod "https://api.github.com/repos/versent/saml2aws/releases/latest").tag_name }
# }
# $Bin += @{
# Name = 'Helm'
# LocalVerCmd = { ((((& helm version --client --short).Split(' '))[1]).Split('+'))[0] }
# RemoteVerCmd = { (Invoke-RestMethod "https://api.github.com/repos/helm/helm/releases/latest").tag_name }
# }
Write-Host 'Check for tool updates: ' -NoNewline
Write-Host '$Bin | % { Check-BinVersion @_ }' -ForegroundColor Yellow
# --------------------------------------------------
# To do
# --------------------------------------------------
<#
Dynamic arguments with better auto-completion?
https://martin77s.wordpress.com/2014/06/09/dynamic-validateset-in-a-dynamic-parameter/
#>
# Write-Host "`n"