-
Notifications
You must be signed in to change notification settings - Fork 2
/
profile.ps1
316 lines (285 loc) · 8.75 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
try {
# Very slow
# Provides 'Write-Prompt'
# Import-Module posh-git;
}
catch {
}
$_pathIntrinsics = $ExecutionContext.SessionState.Path;
function _path([array] $pathList) {
return $_pathIntrinsics.GetUnresolvedProviderPathFromPSPath(
[io.path]::combine([string[]]$pathList));
}
# Used for Elixir repl.
Remove-Alias -Force -Name iex
# For git to correctly show unicode files content.
$env:LANG = "en_US.UTF-8";
# Always install dependencies in .venv for pipenv.
$env:PIPENV_VENV_IN_PROJECT = 1
# Do not lock dependencies (very slow).
$env:PIPENV_SKIP_LOCK = 1
# Disable lockfile generation for pipenv (much faster install).
$env:PIPENV_SKIP_LOCK = 1
# Enable Python 2.7 apps to write into PowerShell console.
$env:PYTHONIOENCODING = "UTF-8"
$COLOR_DGRAY = ([ConsoleColor]::DarkGray);
$COLOR_DYELLOW = ([ConsoleColor]::DarkYellow);
$COLOR_GREEN = ([ConsoleColor]::Green);
$COLOR_BLUE = ([ConsoleColor]::Blue);
$COLOR_MAGENTA = ([ConsoleColor]::Magenta);
function cdd() { Set-Location ~/Documents; }
function cdc() { Set-Location ~/dotfiles; }
function cdx() { Set-Location ~/.xi; }
function cdh() { Set-Location ~; }
function g() { & git $Args }
function gst() {
$dirNameList = Get-ChildItem -Name -Directory;
foreach ($dirName in $dirNameList) {
if (!(Test-Path "$dirName\.git")) {
Write-Host "not a git repo; " -NoNewline -ForegroundColor DarkCyan;
Write-Host $dirName;
continue;
}
Set-Location $dirName;
$ret = & git status;
$UP_MARKER = "Your branch is up to date with";
$CLEAN_MARKER = "nothing to commit, working tree clean";
if ($ret[1].Contains($UP_MARKER)) {
Write-Host "up to date; " -NoNewline;
}
else {
Write-Host "out of sync" -NoNewline -ForegroundColor Green;
Write-Host "; " -NoNewline;
}
if ($ret[3].Contains($CLEAN_MARKER) -or $ret[4].Contains($CLEAN_MARKER)) {
Write-Host "clean; " -NoNewline;
}
else {
Write-Host "working tree changes" -NoNewline -ForegroundColor Red;
Write-Host "; " -NoNewline;
}
Write-Host $dirName;
Set-Location ..;
}
}
function ahk() {
# Restart if already running
# if (Get-Process "AutoHotkey" -ErrorAction SilentlyContinue) { return; }
Start-Process `
autohotkey.exe `
-ArgumentList "$($env:USERPROFILE)\dotfiles\keyboard.ahk" `
-WindowStyle Hidden `
-Verb RunAs;
}
# ============================================================================
# Windows-OSX-Linux consistency
# ============================================================================
function rmf($dst) {
Remove-Item $dst -Recurse -Force -ErrorAction SilentlyContinue
}
function ll($dst) {
lsd -l $dst
}
function grep() {
Select-String -Path $Args[1] -Pattern $Args[0]
}
function vec() {
python3 -m virtualenv .venv
}
function vea() {
.\.venv\Scripts\activate.ps1
}
function ved() {
deactivate
}
function ver() {
Remove-Item .venv -Recurse -Force -ErrorAction SilentlyContinue
}
# ============================================================================
# Tools
# ============================================================================
function Update-VscodeExt() {
$cfgFileName = "package.json";
if (-not (Test-Path $cfgFileName)) {
Write-Error "$cfgFileName not found";
return;
}
$cfg = Get-Content $cfgFileName | ConvertFrom-Json;
$name = $cfg.name;
if (-not $name) {
Write-Error "'name' property not found in the $cfgFileName";
return;
}
$publisher = $cfg.publisher;
if (-not $publisher) {
Write-Error "'publisher' property not found in the $cfgFileName";
return;
}
$version = $cfg.version;
if (-not $version) {
Write-Error "'version' property not found in the $cfgFileName";
return;
}
$verPartList = $version.split(".");
$extRoot = "$env:USERPROFILE\.vscode\extensions"
$extDir = "";
# Try current version and all older build semver since installed
# extension is often older than the development one.
for ($i = [int]$verPartList[-1]; $i -ge 0; $i --) {
$verPartList[-1] = $i
$curVer = [System.String]::Join(".", $verPartList);
$extDir = "$extRoot\$publisher.$name-$curVer";
if (Test-Path $extDir) {
break;
}
}
if (-not (Test-Path $extDir)) {
Write-Error "'$extRoot\$publisher.$name-$version...0' dir not found";
return;
}
if (-not (Test-Path -Path $extDir/src)) {
New-Item -Path $extDir/src -ItemType Directory | Out-Null;
}
Copy-Item *.js $extDir;
Copy-Item *.json $extDir;
Write-Output "Copied into $extDir";
if (Test-Path -Path src) {
Copy-Item src/*.js $extDir/src;
Copy-Item src/*.json $extDir/src;
Write-Output "Copied ./src into $extDir/src";
}
}
function Start-Srv() {
$name = "srv";
$job = Get-Job -Name $name -ErrorAction SilentlyContinue;
if ($job) {
Write-Host "Already started";
return;
}
$job = {
Set-Location $Args[0];
$driveName = 'site';
$_Args = @{
Name = $driveName
PSProvider = 'FileSystem'
Root = $PWD.Path
};
New-PSDrive @_Args;
$listener = New-Object System.Net.HttpListener;
$listener.Prefixes.Add("http://localhost:8080/");
$listener.Start();
while($listener.IsListening) {
$context = $listener.GetContext();
$url = $Context.Request.Url.LocalPath;
if ($url -eq '/favicon.ico') {
$Context.Response.Close();
continue;
}
if ($url -eq '/_stop') {
$listener.Stop();
Remove-PSDrive -Name $driveName;
return;
}
$ext = [System.IO.Path]::GetExtension($url);
$context.Response.ContentType = @{
'.htm' = 'text/html'
'.html' = 'text/html'
'.css' = 'text/css'
'.svg' = 'image/svg+xml'
'.png' = 'image/png'
'.jpg' = 'image/jpeg'
'.jepg' = 'image/jpeg'
}[$ext];
try {
$data = Get-Content -AsByteStream -Path "$($driveName):$url";
$context.Response.OutputStream.Write($data, 0, $data.Length);
}
catch {
$context.Response.StatusCode = 404;
}
$Context.Response.Close();
}
};
$job = Start-Job -Name $name -ArgumentList $PWD -ScriptBlock $job;
Write-Host "Server job started";
}
function Stop-Srv() {
$name = "srv";
$job = Get-Job -Name $name -ErrorAction SilentlyContinue;
if ($job) {
try {
Invoke-WebRequest -Uri 'http://localhost:8080/_stop';
}
catch {
}
Stop-Job -Name $name;
Remove-Job -Name $name;
Write-Host "Server job stopped";
}
else {
Write-Host "No server job found";
}
}
$promptMsg = $null;
function Add-PromptMsg($msg) {
Set-Variable -Name "promptMsg" -Value $msg -Scope Global;
[Microsoft.PowerShell.PSConsoleReadLine]::InvokePrompt();
$timer = New-Object System.Timers.Timer
$timer.AutoReset = $false
$timer.Interval = 1000
Register-ObjectEvent -InputObject $timer -EventName Elapsed -Action {
Set-Variable -Name "promptMsg" -Value $null -Scope Global;
[Microsoft.PowerShell.PSConsoleReadLine]::InvokePrompt();
}
$timer.Enabled = $true
}
# Without this, returns cleared screen after any input.
Set-PSReadlineKeyHandler -Key Ctrl+l -ScriptBlock {
[Microsoft.PowerShell.PSConsoleReadLine]::ClearScreen();
}
Set-PSReadlineKeyHandler -Key Ctrl+d -ScriptBlock {
Add-PromptMsg "dbg";
}
# Very slow
# if (Get-Command "conda.exe" -ErrorAction SilentlyContinue) {
# (& conda.exe shell.powershell hook) | Out-String | Invoke-Expression
# }
# After conda, which tries to replace prompt.
function prompt {
Write-Host "[" -NoNewLine -ForegroundColor $COLOR_DGRAY;
if ($promptMsg) {
Write-Host $promptMsg -NoNewLine -ForegroundColor $COLOR_GREEN;
}
else {
Write-Host "..." -NoNewLine -ForegroundColor $COLOR_DYELLOW;
}
Write-Host "] " -NoNewLine -ForegroundColor $COLOR_DGRAY;
if ($env:USERPROFILE) {
$location = $(Get-Location).ToString().Replace($env:USERPROFILE, "~");
}
else {
$location = $(Get-Location).ToString().Replace($env:HOME, "~");
}
$locationItemList = $location.Replace("\", "/").Split("/");
foreach ($locationItem in $locationItemList) {
Write-Host $locationItem -NoNewLine -ForegroundColor $COLOR_MAGENTA;
Write-Host "/" -NoNewLine -ForegroundColor $COLOR_BLUE;
}
Write-Host " $" -NoNewLine -ForegroundColor $COLOR_DGRAY;
# Return something to replace default prompt.
return " ";
}
if ($IsMacOS) {
# Homebrew will refuse to link Ruby 2.7 over os-provided 2.3 version.
$Env:PATH = "/usr/local/opt/ruby/bin:$Env:PATH";
# Homebrew can --link Python, but modify path for consistancy with Ruby.
$Env:PATH = "/usr/local/opt/[email protected]/bin:$Env:PATH";
}
if ($IsWindows) {
# Not added by winget installed to path
$path = _path(@("~", "apps", "AutoHotkey.AutoHotkey", "v2"));
$Env:PATH = "$Env:PATH;$path";
# Installed by cloning from git
$path = _path(@("~", ".pyenv", "pyenv-win", "bin"));
$Env:PATH = "$Env:PATH;$path";
}