@@ -8,76 +8,141 @@ param(
88
99. $PSScriptRoot / loadModule.ps1
1010
11- # 因为有些软件可能不支持这个环境变量,所以还是推荐直接用clash的tun模式。
12- if (Test-Path - Path " $PSScriptRoot \enableProxy" ) {
13- $Env: http_proxy = " http://127.0.0.1:7890" ; $Env: https_proxy = " http://127.0.0.1:7890" ;
14- }
15-
16-
17- if (Test-Path - Path " $PSScriptRoot /env.ps1" ){
18- . $PSScriptRoot / env.ps1
19- }
11+ function Init-Environment {
12+ <#
13+ . SYNOPSIS
14+ 初始化PowerShell环境配置
15+ . DESCRIPTION
16+ 初始化PowerShell环境配置,包括代理设置、编码配置、别名设置、工具初始化等。
17+ 这个函数封装了所有对环境变量有影响的配置,便于重复调用。
18+ . PARAMETER ScriptRoot
19+ 脚本根目录路径,默认为当前脚本所在目录
20+ . PARAMETER EnableProxy
21+ 是否启用代理设置,默认根据enableProxy文件存在性决定
22+ . PARAMETER ProxyUrl
23+ 代理服务器地址,默认为 http://127.0.0.1:7890
24+ . EXAMPLE
25+ Init-Environment
26+ 使用默认配置初始化环境
27+ . EXAMPLE
28+ Init-Environment -EnableProxy $false
29+ 初始化环境但不启用代理
30+ . EXAMPLE
31+ Init-Environment -ProxyUrl "http://127.0.0.1:8080"
32+ 使用自定义代理地址初始化环境
33+ . NOTES
34+ 此函数会影响当前PowerShell会话的环境变量和配置
35+ #>
2036
21-
22-
23-
24-
25- # & 运算符是调用运算符,它可以在新的作用域中执行脚本文件或命令,这样脚本文件或命令中定义的变量、函数、别名等都不会影响当前会话。
26- # . 运算符是点源运算符,它可以在当前会话中执行脚本文件或命令,这样脚本文件或命令中定义的变量、函数、别名等都可以在当前会话中使用。
27- # 所以应该用.点源运算符
28- # 这种执行的操作,似乎不能封装到模块里执行.
29- function Add-CondaEnv () {
30- $condaPath = " $env: USERPROFILE \anaconda3\shell\condabin\conda-hook.ps1"
31- if (Test-Path - Path $condaPath ) {
32- . $condaPath
37+ [CmdletBinding ()]
38+ param (
39+ [string ]$ScriptRoot = $PSScriptRoot ,
40+ [bool ]$EnableProxy = (Test-Path - Path " $PSScriptRoot \enableProxy" ),
41+ [string ]$ProxyUrl = " http://127.0.0.1:7890"
42+ )
43+
44+ Write-Verbose " 开始初始化PowerShell环境配置"
45+
46+ # 设置代理环境变量
47+ if ($EnableProxy ) {
48+ Write-Verbose " 启用代理设置: $ProxyUrl "
49+ $Env: http_proxy = $ProxyUrl
50+ $Env: https_proxy = $ProxyUrl
51+ Write-Host " 已设置代理: $ProxyUrl " - ForegroundColor Green
3352 }
34- }
35-
36- # powershell ise 的别名
37- Set-Alias - Name ise - Value powershell_ise
38- Set-Alias - Name ipython - Value Start-Ipython
39-
40- # powershell控制台编码设为utf8
41- $OutputEncoding = [console ]::InputEncoding = [console ]::OutputEncoding = New-Object System.Text.UTF8Encoding
42- # cmdlets 的默认参数
43- $PSDefaultParameterValues [" Out-File:Encoding" ] = " UTF8"
44-
45-
46- # 开启基于历史命令的命令补全
47- # Set-PSReadLineOption -PredictionSource History
48- # 直接导入模块就能生效
49- Import-Module PSReadLine
50-
51-
52- $tools = @ {
53- starship = { Invoke-Expression (& starship init powershell) }
54- sccache = {
55- # 设置sccache用于rust编译缓存,提高新启动项目的编译速度
56- $Env: RUSTC_WRAPPER = ' sccache'
53+ else {
54+ Write-Verbose " 跳过代理设置"
5755 }
58- zoxide = { Invoke-Expression (& { (zoxide init powershell | Out-String ) })
56+
57+ # 加载自定义环境变量脚本
58+ if (Test-Path - Path " $ScriptRoot /env.ps1" ) {
59+ Write-Verbose " 加载自定义环境变量脚本: $ScriptRoot /env.ps1"
60+ . " $ScriptRoot /env.ps1"
5961 }
60- fnm = { fnm env -- use-on - cd | Out-String | Invoke-Expression }
61- }
62-
63- foreach ($tool in $tools.GetEnumerator ()) {
64- if (Test-EXEProgram - Name $tool.Key ) {
65- & $tool.Value
62+
63+ # 内部函数:添加Conda环境
64+ function Add-CondaEnv {
65+ $condaPath = " $env: USERPROFILE \anaconda3\shell\condabin\conda-hook.ps1"
66+ if (Test-Path - Path $condaPath ) {
67+ Write-Verbose " 加载Conda环境: $condaPath "
68+ . $condaPath
69+ }
70+ }
71+
72+ # 设置PowerShell别名
73+ Write-Verbose " 设置PowerShell别名"
74+ Set-Alias - Name ise - Value powershell_ise - Scope Global
75+ Set-Alias - Name ipython - Value Start-Ipython - Scope Global
76+
77+ # 设置控制台编码为UTF8
78+ Write-Verbose " 设置控制台编码为UTF8"
79+ $Global :OutputEncoding = [console ]::InputEncoding = [console ]::OutputEncoding = New-Object System.Text.UTF8Encoding
80+ $Global :PSDefaultParameterValues [" Out-File:Encoding" ] = " UTF8"
81+
82+ # 导入PSReadLine模块
83+ Write-Verbose " 导入PSReadLine模块"
84+ try {
85+ Import-Module PSReadLine - ErrorAction Stop
86+ }
87+ catch {
88+ Write-Warning " 无法导入PSReadLine模块: $ ( $_.Exception.Message ) "
6689 }
67- else {
68- if ($tool.Key -eq ' starship' ) {
69- # 不存在starship的时候,提示用户进行安装
70- Write-Host - ForegroundColor Green ' 未安装startship(一款开源提示符美化),可以运行以下命令进行安装
71- 1.choco install starship
72- 2.Invoke-Expression (&starship init powershell)'
90+
91+ # 初始化开发工具
92+ Write-Verbose " 初始化开发工具"
93+ $tools = @ {
94+ starship = {
95+ Write-Verbose " 初始化Starship提示符"
96+ Invoke-Expression (& starship init powershell)
97+ }
98+ sccache = {
99+ Write-Verbose " 设置sccache用于Rust编译缓存"
100+ $Global :Env :RUSTC_WRAPPER = ' sccache'
101+ }
102+ zoxide = {
103+ Write-Verbose " 初始化zoxide目录跳转工具"
104+ Invoke-Expression (& { (zoxide init powershell | Out-String ) })
105+ }
106+ fnm = {
107+ Write-Verbose " 初始化fnm Node.js版本管理器"
108+ fnm env -- use-on - cd | Out-String | Invoke-Expression
109+ }
110+ }
111+
112+ foreach ($tool in $tools.GetEnumerator ()) {
113+ if (Test-EXEProgram - Name $tool.Key ) {
114+ try {
115+ & $tool.Value
116+ Write-Verbose " 成功初始化工具: $ ( $tool.Key ) "
117+ }
118+ catch {
119+ Write-Warning " 初始化工具 $ ( $tool.Key ) 时出错: $ ( $_.Exception.Message ) "
120+ }
121+ }
122+ else {
123+ if ($tool.Key -eq ' starship' ) {
124+ Write-Host - ForegroundColor Yellow ' 未安装starship(一款开源提示符美化工具),可以运行以下命令进行安装:
125+ 1. choco install starship
126+ 2. scoop install starship
127+ 3. winget install starship'
128+ }
129+ else {
130+ Write-Verbose " 工具 $ ( $tool.Key ) 未安装,跳过初始化"
131+ }
73132 }
74133 }
134+
135+ # 载入conda环境(如果环境变量中没有conda命令)
136+ if (-not (Test-EXEProgram - Name conda)) {
137+ Write-Verbose " 尝试加载Conda环境"
138+ Add-CondaEnv
139+ }
140+
141+ Write-Host " PowerShell环境初始化完成" - ForegroundColor Green
75142}
76143
77- # 载入conda环境,环境变量中没有conda命令时执行
78- if (-not (Test-EXEProgram - Name conda)) {
79- Add-CondaEnv
80- }
144+ # 调用环境初始化函数
145+ Init- Environment
81146
82147# 配置git,解决中文文件名不能正常显示的问题
83148# git config --global core.quotepath false
0 commit comments