Skip to content

Commit

Permalink
Create install.ps1
Browse files Browse the repository at this point in the history
  • Loading branch information
steve02081504 committed Nov 26, 2023
1 parent 8fe58b6 commit 3247ff7
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 16 deletions.
15 changes: 2 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,12 @@
## 快速开始

将以下命令复制粘贴到powershell中以快速开始
这段脚本将会在你的powershell配置文件中添加esh并启动它
你可以跳过`add2profile.ps1`来避免对配置文件的修改

```powershell
$PwshProFiles = Split-Path $PROFILE
New-Item -ItemType Directory -Force -Path $PwshProFiles | Out-Null
Remove-Item $PwshProFiles/esh -Confirm -ErrorAction Ignore -Recurse
Remove-Item $PwshProFiles/esh-master -Force -ErrorAction Ignore -Confirm:$false -Recurse
Invoke-WebRequest https://github.com/steve02081504/esh/archive/refs/heads/master.zip -OutFile Eshell.zip
Expand-Archive Eshell.zip $PwshProFiles -Force
Remove-Item Eshell.zip -Force
Move-Item $PwshProFiles/esh-master $PwshProFiles/esh -Force
& $PwshProFiles/esh/add2profile.ps1
. $PwshProFiles/esh/run.ps1
Remove-Variable PwshProFiles
(Invoke-WebRequest https://github.com/steve02081504/esh/raw/master/install.ps1).Content | Invoke-Expression
```

![loading preview](https://github.com/steve02081504/esh/assets/31927825/c7ba3f3f-cdb2-4b93-8fdc-2f5901e0ce12)


Expand Down
89 changes: 89 additions & 0 deletions install.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/usr/bin/env pwsh
$profilesDir = Split-Path $PROFILE
# 遍历环境变量
$env:Path.Split(";") | ForEach-Object {
if ($_ -like "*[\\/]esh[\\/]path*") {
if(Test-Path $_ -PathType Container){
$eshDir = $_ -replace "[\\/]path[\\/]*$",''
$eshDirFromEnv = $true
}
else{
Write-Warning "检测到无效的esh环境变量于$_,请考虑删除"
}
}
}
if (-not $eshDir){
$eshDir =
if(Test-Path $profilesDir/esh) {
"$profilesDir/esh"
}
elseif(Test-Path $PWD/path/esh) {
$PWD
}
}
New-Item -ItemType Directory -Force -Path $profilesDir | Out-Null
if (-not $eshDir) {
Remove-Item $profilesDir/esh -Confirm -ErrorAction Ignore -Recurse
Remove-Item $profilesDir/esh-master -Force -ErrorAction Ignore -Confirm:$false -Recurse
Invoke-WebRequest https://github.com/steve02081504/esh/archive/refs/heads/master.zip -OutFile Eshell.zip
Expand-Archive Eshell.zip $profilesDir -Force
Remove-Item Eshell.zip -Force
Move-Item $profilesDir/esh-master $profilesDir/esh -Force
$eshDir = "$profilesDir/esh"
}
else{
Write-Host "检测到已安装 Esh 于 $eshDir"
}
function Choice($caption, $message) {
do {
$response = $Host.UI.PromptForChoice($caption, $message, @('&Yes', '&No'), 1)
} until ($response -ne -1)
$response -eq 0
}
if ((-not $eshDirFromEnv) -and (Choice("", "你想要安装 Eshell 到环境变量吗?"))) {
$env:Path += ";$profilesDir/esh/path"
$UserPath = [Environment]::GetEnvironmentVariable("Path", "User") + ";$profilesDir/esh/path"
[Environment]::SetEnvironmentVariable("Path", $UserPath, "User")
Write-Host "安装成功!`n你现在可以在任何地方使用 ``esh````EShell`` 命令了。"
}
$profilEshDir = $eshDir
if ($profilEshDir -like "$profilesDir[\\/]?*") {
$profilEshDir = $profilEshDir -replace "^$($profilesDir -replace "\\","\\")[\\/]?",'$PSScriptRoot/'
}
$startScript = ". $profilEshDir/run.ps1"
$universalProfile = "$profilesDir/profile.ps1"
function checkLoaded ($theProfile) {
(Get-Content $theProfile -ErrorAction Ignore) -ccontains $startScript
}
$added = $false
@(
$universalProfile
$profile
) | ForEach-Object {
$loaded = checkLoaded $_
if ($loaded) {
Write-Host "${_}中已经加载过esh"
$added = $true
}
}
if ((-not $added) -and (Choice("", "你想要添加 Eshell 到 PowerShell 配置文件吗?"))) {
@(
$universalProfile
$profile
) | ForEach-Object {
if (Test-Path $_) {
Write-Warning "${_}中添加了esh加载语句"
Add-Content $_ $startScript
$added = $true
}
}
if (-not $added) {
Write-Warning "未找到可用的profile文件,新建通用profile文件${universalProfile}"
Set-Content $universalProfile $startScript
}
}
if(-not $EshellUI) {
. $eshDir/run.ps1 -Invocation $MyInvocation
}
Remove-Variable @("profilesDir", "eshDir", "startScript", "universalProfile", "added", "profilEshDir", "eshDirFromEnv") -ErrorAction Ignore
Remove-Item @("function:Choice", "function:checkLoaded", "function:Choice") -ErrorAction Ignore
6 changes: 4 additions & 2 deletions run.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
if (-not $EshellUI) { . $PSScriptRoot/main.ps1 }
[CmdletBinding()]param($Invocation = $MyInvocation)

if (-not $EshellUI) { . $PSScriptRoot/main.ps1 }
if (-not $EshellUI) { exit 1 }
$EshellUI.RunFromScript($MyInvocation)
$EshellUI.RunFromScript($Invocation)
14 changes: 14 additions & 0 deletions src/commands/other.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,17 @@ function global:code {
Write-Host "VS Code not found."
}
}

function global:Clear-UserPath {
$UserPath = [Environment]::GetEnvironmentVariable("Path", "User").Split(';')
$SystemPath = [Environment]::GetEnvironmentVariable("Path", "Machine").Split(';')
$UserPath.Split(';') | ForEach-Object {
if ($SystemPath -contains $_) {
$UserPath = $UserPath -ne $_
Write-Warning "已自用户变量中移除在系统变量中的路径$_"
}
} | Out-Null
$UserPath = $UserPath | Select-Object -Unique
$UserPath = $UserPath -join ';'
[Environment]::SetEnvironmentVariable("Path", $UserPath, "User")
}
2 changes: 1 addition & 1 deletion src/main.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ $EshellUI = ValueEx @{
. $PSScriptRoot/system/UI/title.ps1
. $PSScriptRoot/system/UI/icon.ps1

. $PSScriptRoot/system/CodePageFixer.ps1
. $PSScriptRoot/system/Fixer.ps1
. $PSScriptRoot/system/linux.ps1

. $PSScriptRoot/system/UI/prompt/main.ps1
Expand Down
2 changes: 2 additions & 0 deletions src/system/CodePageFixer.ps1 → src/system/Fixer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@
TestAndSet $CodingBackUp
Write-Host $(' ' * $TestText.Length * 2)
$host.UI.RawUI.CursorPosition = $CursorPosBackUp
Remove-Variable @('CursorPosBackUp', 'CodingBackUp', 'TestText', 'TestAndSet')
Remove-Item function:TestAndSet
}

0 comments on commit 3247ff7

Please sign in to comment.