From 8306264bdbdea549aa82b3072e7da93bdc7f4666 Mon Sep 17 00:00:00 2001 From: js0ny Date: Sun, 1 Dec 2024 07:02:12 +0000 Subject: [PATCH] feat(pwsh): Windows Migration and optimize powershell scripts --- .vscode/settings.json | 2 +- ...mblink_unix.bash => set_symlink_unix.bash} | 4 + bootstrap/set_symlink_win.ps1 | 36 +++++++ platforms/win/.wslconfig | 12 --- .../win/Microsoft.PowerShell_profile.ps1 | 94 ++++++------------- platforms/win/wslconfig | 19 ++++ {tools/powershell => scripts}/Completions.ps1 | 0 {tools/powershell => scripts}/Scripts.ps1 | 32 +++---- tools/powershell/Aliases.ps1 | 31 ++---- tools/powershell/Environment.ps1 | 10 ++ tools/powershell/Navigation.ps1 | 21 ++--- tools/powershell/Prompt.ps1 | 13 ++- 12 files changed, 141 insertions(+), 133 deletions(-) rename bootstrap/{set_symblink_unix.bash => set_symlink_unix.bash} (93%) create mode 100644 bootstrap/set_symlink_win.ps1 delete mode 100644 platforms/win/.wslconfig create mode 100644 platforms/win/wslconfig rename {tools/powershell => scripts}/Completions.ps1 (100%) rename {tools/powershell => scripts}/Scripts.ps1 (96%) create mode 100644 tools/powershell/Environment.ps1 diff --git a/.vscode/settings.json b/.vscode/settings.json index abc78ff..24eb11a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -12,7 +12,7 @@ "gitconfig": "git", "nvim/init.lua": "vim", "markdownlint.json": "markdownlint", - ".wslconfig": "settings", + "wslconfig": "settings", "skhdrc": "console" }, "material-icon-theme.folders.associations": { diff --git a/bootstrap/set_symblink_unix.bash b/bootstrap/set_symlink_unix.bash similarity index 93% rename from bootstrap/set_symblink_unix.bash rename to bootstrap/set_symlink_unix.bash index 0b056ac..78c6dbc 100644 --- a/bootstrap/set_symblink_unix.bash +++ b/bootstrap/set_symlink_unix.bash @@ -1,4 +1,8 @@ #! /bin/bash +# $DOTFILES/bootstrap/set_symlink_unix.bash +# Date: 2024-12-01 +# Author: contact@js0ny.net +# Set symlinks for Unix-like operating systems mkdir -p $XDG_CONFIG_HOME/conda $XDG_CONFIG_HOME/git $XDG_CONFIG_HOME/ideavim $XDG_CONFIG_HOME/markdownlint $XDG_CONFIG_HOME/pip $XDG_CONFIG_HOME/neovide $XDG_CONFIG_HOME/powershell $XDG_CONFIG_HOME/vscode $XDG_CONFIG_HOME/NuGet $XDG_CONFIG_HOME/vim $XDG_CONFIG_HOME/tmux $XDG_CONFIG_HOME/npm $XDG_CONFIG_HOME/readline $XDG_CONFIG_HOME/ipython mkdir -p ~/.config/zellij # Not support XDG_CONFIG_HOME but same directory # mkdir -p $WAKATIME_HOME diff --git a/bootstrap/set_symlink_win.ps1 b/bootstrap/set_symlink_win.ps1 new file mode 100644 index 0000000..fe3116e --- /dev/null +++ b/bootstrap/set_symlink_win.ps1 @@ -0,0 +1,36 @@ +# $DOTFILES\bootstrap\set_symlink_win.ps1 +# Date: 2024-12-01 +# Author: contact@js0ny.net +# Set symlinks for dotfiles on Windows +# 在 Windows 上设置 dotfiles 的符号链接 + +$DOTFILES = Join-Path $env:UserProfile ".dotfiles" +$BASE_COMMON = Join-Path $DOTFILES "common" +$BASE_TOOLS = Join-Path $DOTFILES "tools" +$BASE_WIN = Join-Path $DOTFILES "platforms" "win" +$linkDots = @{ + "$BASE_WIN\wslconfig" = "$env:UserProfile\.wslconfig" + "$BASE_WIN\neovide.toml" = "$Env:AppData\neovide\config.toml" + "$BASE_COMMON\condarc.yaml" = "$env:XDG_CONFIG_HOME\conda\.condarc" + "$BASE_COMMON\gitconfig" = "$env:UserProfile\.gitconfig" + "$BASE_COMMON\glow.yaml" = "$env:AppData\glow\glow.yml" + "$BASE_COMMON\haskeline" = "$env:UserProfile\.haskeline" + "$BASE_COMMON\ideavimrc" = "$env:XDG_CONFIG_HOME\ideavim\ideavimrc" + "$BASE_COMMON\lesskey" = "$env:LessKeyIn" + "$BASE_COMMON\npmrc" = "$env:NPM_CONFIG_USERCONFIG" + "$BASE_COMMON\NuGet.Config" = "$env:AppData\NuGet\NuGet.Config" + "$BASE_COMMON\obsidian.vimrc" = "$env:UserProfile\Obsidian\.obsidian.vimrc" + "$BASE_COMMON\pip.conf" = "$env:AppData\pip\pip.ini" + "$BASE_COMMON\vimrc" = "$env:Vim\_vimrc" + "$BASE_TOOLS\ipython" = "$env:IPYTHONDIR" + "$BASE_TOOLS\nvim" = "$env:XDG_CONFIG_HOME\nvim" +} + +# TODO: Auto create directories + +foreach ($target in $linkDots.Keys) { + $path = $linkDots[$target] + New-Item -ItemType SymbolicLink -Target $target -Path $path -Force +} + +New-Item -ItemType SymbolicLink -Target "$BASE_WIN\Microsoft.PowerShell_profile.ps1" -Path "$env:UserProfile\Documents\PowerShell\Microsoft.PowerShell_profile.ps1" -Force diff --git a/platforms/win/.wslconfig b/platforms/win/.wslconfig deleted file mode 100644 index 2166134..0000000 --- a/platforms/win/.wslconfig +++ /dev/null @@ -1,12 +0,0 @@ -# ~/.wslconfig -# New-Item -ItemType SymbolicLink -Path ~\.wslconfig -Target ~\.dotfiles\win\.wslconfig -[wsl2] -# networkingMode=mirrored -# dnsTunneling=true -# firewall=true -# autoProxy=true - -[experimental] -# requires dnsTunneling but are also OPTIONAL -bestEffortDnsParsing=true -hostAddressLoopback=true diff --git a/platforms/win/Microsoft.PowerShell_profile.ps1 b/platforms/win/Microsoft.PowerShell_profile.ps1 index f87965e..08a6cff 100644 --- a/platforms/win/Microsoft.PowerShell_profile.ps1 +++ b/platforms/win/Microsoft.PowerShell_profile.ps1 @@ -1,69 +1,30 @@ +# $DOTFILES/platforms\win\Microsoft.PowerShell_profile.ps1 +# Date: 2024-12-01 +# Author: contact@js0ny.net +# PowerShell profile for Windows + ### Load Configs ### -$DOTFILES = "$HOME\.dotfiles" -Get-ChildItem -Path $DOTFILES\powershell -Filter *.ps1 | ForEach-Object {. $_} -Get-ChildItem -Path $DOTFILES\powershell_private -Filter *.ps1 | ForEach-Object {. $_} +$DOTFILES = Join-Path $HOME ".dotfiles" +Get-ChildItem -Path $(Join-Path $DOTFILES "tools" "powershell") -Filter *.ps1 | ForEach-Object {. $_} ### Aliases ### -# Shell Equivalents # -Set-Alias "grep" "Select-String" -${function:which} = { (Get-Command $args[0]).Path } - -# Shell Configurations # -${function:shcfg} = { code $PROFILE } -${function:reload} = { & $PROFILE } -${function:pulldots} = { Set-Location -Path $DOTFILES && git pull } -Set-Alias "pwshcfg" "shcfg" - -# C & C++ # -# Set-Alias "cl" "clang" -# Set-Alias "clpp" "clang++" -# ${function:clang} = { clang -std=c99 $args[0] } -# ${function:clang++} = { clang -std=c++2b $args[0] } - -# WSL # -${function:wsl1} = {wsl.exe --distribution Arch} -${function:wsl2} = {wsl.exe --distribution Ubuntu-22.04} - -# Search Software # -function Find-AppPackageListRemote { - param( - [string]$Name - ) - if (-not $Name) { - Write-Host "Please provide a package name." - return - } - Write-Host "Searching for $Name..." - Write-Host "=== winget ===" - winget search $Name - Write-Host "=== choco ===" - choco search $Name -# Too slow! -# Write-Host "=== scoop ===" -# scoop search $Name -} -Set-Alias "pkgsearch" "Find-AppPackageListRemote" -function Get-AppPackageListLocal { - winget list - choco list - scoop list -} # Toggle Theme # -function Set-SystemTheme { - $regPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize" - $currentMode = Get-ItemProperty -Path $regPath -Name "AppsUseLightTheme" - if ($currentMode.AppsUseLightTheme -eq 1) { - Set-ItemProperty -Path $regPath -Name "AppsUseLightTheme" -Value 0 - Write-Host "已切换到深色模式" - } - else { - Set-ItemProperty -Path $regPath -Name "AppsUseLightTheme" -Value 1 - Write-Host "已切换到浅色模式" - } -} -Set-Alias "dark-mode" "Set-SystemTheme" # Consistent with macOS (`dark-mode`) +# TODO: Change to `bat` script implementation +# function Set-SystemTheme { +# $regPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize" +# $currentMode = Get-ItemProperty -Path $regPath -Name "AppsUseLightTheme" +# if ($currentMode.AppsUseLightTheme -eq 1) { +# Set-ItemProperty -Path $regPath -Name "AppsUseLightTheme" -Value 0 +# Write-Host "已切换到深色模式" +# } +# else { +# Set-ItemProperty -Path $regPath -Name "AppsUseLightTheme" -Value 1 +# Write-Host "已切换到浅色模式" +# } +# } +# Set-Alias "dark-mode" "Set-SystemTheme" # Consistent with macOS (`dark-mode`) # Miscs # @@ -98,13 +59,16 @@ If (Test-Path "$HOME\miniconda3\Scripts\conda.exe") { #Remove-Variable __lastStartup #Remove-Variable _currentDate -# Use some unix commands -${function:tree} = { wsl tree $args} -${function:ln} = { coreutils.exe ln $args} -${function:la} = { lsd.exe -la $args} - # Set default applications + $Env:PAGER = "less" $Env:EDITOR = "code --wait" $Env:VISUAL = "code --wait" $Env:FILE_MANAGER = "dopus.exe" + + +${function:wsl2} = {wsl.exe --distribution Ubuntu} +${function:wini} = { winget install $args } +${function:winr} = { winget uninstall $args } +${function:wins} = { winget search $args } +${function:winu} = { winget upgrade $args } diff --git a/platforms/win/wslconfig b/platforms/win/wslconfig new file mode 100644 index 0000000..bd68ee5 --- /dev/null +++ b/platforms/win/wslconfig @@ -0,0 +1,19 @@ +# $DOTFILES\platforms\win\wslconfig +# Date: 2024-12-01 +# Author: contact@js0ny.net +# Config Files (mainly for networking) for WSL2 (Windows Subsystem for Linux 2) +# 适用于 Windows 的 Linux 子系统 2 的配置文件(主要用于网络) + +# Location: ~/.wslconfig +# Linking: (PowerShell) +# New-Item -ItemType SymbolicLink -Path ~\.wslconfig -Target ~\.dotfiles\platforms\win\wslconfig +[wsl2] +# networkingMode=mirrored +# dnsTunneling=true +# firewall=true +# autoProxy=true + +[experimental] +# requires dnsTunneling but are also OPTIONAL +bestEffortDnsParsing=true +hostAddressLoopback=true diff --git a/tools/powershell/Completions.ps1 b/scripts/Completions.ps1 similarity index 100% rename from tools/powershell/Completions.ps1 rename to scripts/Completions.ps1 diff --git a/tools/powershell/Scripts.ps1 b/scripts/Scripts.ps1 similarity index 96% rename from tools/powershell/Scripts.ps1 rename to scripts/Scripts.ps1 index 2878059..49f47f0 100644 --- a/tools/powershell/Scripts.ps1 +++ b/scripts/Scripts.ps1 @@ -1,17 +1,17 @@ -function CsvToMarkdown { - param( [string]$csv) - $counter = 0 - $column = $csv.Split("`n")[0].Split(",").Length - $aHeader = " --- |" - $header = "|" + $aHeader * $column - $csv.replace(",", " | ").Split("`n") | ForEach-Object { - if ($counter -eq 0) { - Write-Output "| $_ |" - $counter++ - Write-Output $header - } - else { - Write-Output "| $_ |" - } - } +function CsvToMarkdown { + param( [string]$csv) + $counter = 0 + $column = $csv.Split("`n")[0].Split(",").Length + $aHeader = " --- |" + $header = "|" + $aHeader * $column + $csv.replace(",", " | ").Split("`n") | ForEach-Object { + if ($counter -eq 0) { + Write-Output "| $_ |" + $counter++ + Write-Output $header + } + else { + Write-Output "| $_ |" + } + } } \ No newline at end of file diff --git a/tools/powershell/Aliases.ps1 b/tools/powershell/Aliases.ps1 index 4f5741b..b835dc0 100644 --- a/tools/powershell/Aliases.ps1 +++ b/tools/powershell/Aliases.ps1 @@ -1,13 +1,16 @@ +# $DOTFILES/tools\powershell\Aliases.ps1 +# Date: 2024-12-01 +# Author: contact@js0ny.net +# Aliases for PowerShell ### VARIABLES ### $EDITOR = "code" # Shell Equivalents # Set-Alias "touch" "New-Item" -${function:ll} = { Get-ChildItem -Force } +${function:l} = { Get-ChildItem -Force } +${function:tree} = { lsd.exe --tree $args } +Set-Alias "which" "Get-Command" # Shell Configurations # -${function:shcfg} = { code $PROFILE } -Set-Alias "pwshcfg" "shcfg" -${function:reload} = { . $PROFILE } ${function:pulldots} = { Set-Location -Path $DOTFILES && git pull } # Editors # @@ -16,31 +19,13 @@ Set-Alias "c" "code" Set-Alias "gvi" "neovide" # File Creation # -function mkcd { param ( [string] $dirname) mkdir $dirname && Set-Location $dirname } +function mkcd { param ( [string] $dirname) New-Item -ItemType Directory -Name $dirname && Set-Location $dirname } function tc { param ( [string] $filename) New-Item $filename && code $filename } function tv { param ( [string] $filename) New-Item $filename && nvim $filename } function cdls { param( [string] $dirname) Set-Location $dirname && Get-ChildItem } ### Dev ### -# .NET # -${function:csi} = { dotnet repl --default-kernel csharp} -${function:fsi} = { dotnet repl --default-kernel fsharp} - # Python & Conda # Set-Alias "py" "python" Set-Alias "ipy" "ipython" -${function:pyact} = { conda activate $args[0] } -${function:pydact} = { conda deactivate } -${function:pylsenvs} = { conda env list } -${function:pymkenv} = { conda create --name $args[0] } -${function:pyrmenv} = { conda remove --name $args[0] --all } - -# Winget # - -if ($isWindows) { - ${function:wini} = { winget install $args } - ${function:winr} = { winget uninstall $args } - ${function:wins} = { winget search $args } - ${function:winu} = { winget upgrade $args } -} diff --git a/tools/powershell/Environment.ps1 b/tools/powershell/Environment.ps1 new file mode 100644 index 0000000..cf43c80 --- /dev/null +++ b/tools/powershell/Environment.ps1 @@ -0,0 +1,10 @@ +# $DOTFILES/tools\powershell\Environment.ps1 +# Date: 2024-12-01 +# Author: contact@js0ny.net +# Sourced by Microsoft.PowerShell_profile.ps1 + + +# This file stores only environment variables that only called by +# interactive session. + +$env:IPYTHONDIR = "$env:AppData\ipython" diff --git a/tools/powershell/Navigation.ps1 b/tools/powershell/Navigation.ps1 index c4f120a..c5ad86e 100644 --- a/tools/powershell/Navigation.ps1 +++ b/tools/powershell/Navigation.ps1 @@ -1,15 +1,14 @@ # ${function:~} = { Set-Location -Path ~ } cd is better -${function:...} = { Set-Location -Path ..\.. } -${function:....} = { Set-Location -Path ..\..\.. } -${function:.....} = { Set-Location -Path ..\..\..\.. } -${function:......} = { Set-Location -Path ..\..\..\..\.. } +${function:...} = { Set-Location -Path (Join-Path -Path (Join-Path -Path .. -ChildPath ..) -ChildPath ..) } +${function:....} = { Set-Location -Path (Join-Path -Path (Join-Path -Path (Join-Path -Path .. -ChildPath ..) -ChildPath ..) -ChildPath ..) } +${function:.....} = { Set-Location -Path (Join-Path -Path (Join-Path -Path (Join-Path -Path (Join-Path -Path .. -ChildPath ..) -ChildPath ..) -ChildPath ..) -ChildPath ..) } +${function:......} = { Set-Location -Path (Join-Path -Path (Join-Path -Path (Join-Path -Path (Join-Path -Path (Join-Path -Path .. -ChildPath ..) -ChildPath ..) -ChildPath ..) -ChildPath ..) -ChildPath ..) } # Absolute navigation -${function:docs} = { Set-Location -Path ~/Documents } -${function:down} = { Set-Location -Path ~/Downloads } -${function:dt} = { Set-Location -Path ~/Desktop } -${function:mytmp} = { Set-Location -Path ~/Temp } -${function:one} = { Set-Location -Path ~/OneDrive } -${function:doku} = { Set-Location -Path ~/doku && Get-ChildItem } +${function:docs} = { Set-Location -Path (Join-Path -Path $HOME -ChildPath "Documents") } +${function:down} = { Set-Location -Path (Join-Path -Path $HOME -ChildPath "Downloads") } +${function:dt} = { Set-Location -Path (Join-Path -Path $HOME -ChildPath "Desktop") } +${function:one} = { Set-Location -Path (Join-Path -Path $HOME -ChildPath "OneDrive") } -Invoke-Expression (& { (zoxide init powershell | Out-String) }) \ No newline at end of file + +Invoke-Expression (& { (zoxide init powershell | Out-String) }) diff --git a/tools/powershell/Prompt.ps1 b/tools/powershell/Prompt.ps1 index 40806a3..e34817b 100644 --- a/tools/powershell/Prompt.ps1 +++ b/tools/powershell/Prompt.ps1 @@ -1,7 +1,10 @@ +# $DOTFILES/tools\powershell\Prompt.ps1 +# Date: 2024-12-01 +# Author: contact@js0ny.net # Use starship to set prompt -$ENV:STARSHIP_CONFIG = "$DOTFILES/.config/starship/starship_pwsh.toml" +$ENV:STARSHIP_CONFIG = Join-Path $DOTFILES "tools" "starship" "starship_pwsh.toml" -Invoke-Expression (&starship init powershell) +# Invoke-Expression (&starship init powershell) # Below is the backup of original prompt function # $promptTime = $true @@ -16,7 +19,7 @@ Invoke-Expression (&starship init powershell) # } # # UserInfo # $prompt += " $Env:Username @ $Env:Userdomain" -# # Directory +# # Directory # $promptCurrentDirectory = $(PWD).Path # $promptCurrentDirectory = $promptCurrentDirectory.Replace("$HOME", "~") # $prompt += "`e[0m in `e[33m$promptCurrentDirectory " @@ -81,7 +84,7 @@ Invoke-Expression (&starship init powershell) # # if ( $global:promptWeather ) { # # $prompt += $(Write-WeatherCurrent -City "Edinburgh" -Country "UK" -Unit "metric" -Inline -Apikey $Env:WEATHER_API_KEY) # # } -# # Error on last command +# # Error on last command # ## TODO: Seems does not work # if ($?) { # $prompt += "`n`e[32m PS > `e[0m" @@ -89,4 +92,4 @@ Invoke-Expression (&starship init powershell) # $prompt += "`n`e[31m PS > `e[0m" # } # return $prompt -# } \ No newline at end of file +# }