Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions DOCS/en/winux_shell_integration_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ To auto-activate in every new interactive PowerShell session, install the
if (Get-Command winux -ErrorAction SilentlyContinue) {
winux activate 6>$null
}

If `winux activate` reports `winux.ps1 not found`, the usual cause is a stale
older wrapper still left in `$PROFILE` from a previous install path such as an
old Scoop location. Re-run `winux-activate.ps1` from the current WinuxCmd
install to rewrite the wrapper against the latest `%LOCALAPPDATA%\WinuxCmd`
runtime.
```

### CMD / Windows Terminal
Expand All @@ -41,3 +47,5 @@ commands remain handled by cmd or PowerShell.
- Run `winux activate`, or add the auto-activation snippet above to `$PROFILE`.
2. Profile script not loaded:
- verify execution policy and profile path with `echo $PROFILE`.
3. `ls` seems to be hijacked even though `Get-Command ls` still shows the PowerShell alias:
- another tool may have installed a `PSConsoleHostReadLine` rewrite in `$PROFILE`; Microsoft `coreutils` does this, so inspect your profile for injected wrapper blocks.
7 changes: 7 additions & 0 deletions DOCS/zh/winux_shell_integration_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ wrapper,然后把下面这段放在 `$PROFILE` 里 wrapper 之后:
if (Get-Command winux -ErrorAction SilentlyContinue) {
winux activate 6>$null
}

如果 `winux activate` 提示 `winux.ps1 not found`,通常不是当前安装包漏文件,
而是 `$PROFILE` 里还残留着旧安装路径生成的 wrapper,例如早期 Scoop 路径。
请从当前 WinuxCmd 安装目录重新运行 `winux-activate.ps1`,让 wrapper 改为动态
查找最新的 `%LOCALAPPDATA%\WinuxCmd` 运行时目录。
```

### CMD / Windows Terminal
Expand All @@ -39,3 +44,5 @@ PowerShell 自己处理。
- 运行 `winux activate`,或把上面的自动激活片段加入 `$PROFILE`。
2. Profile 没生效:
- 检查执行策略与 profile 路径(`echo $PROFILE`)。
3. 明明 `Get-Command ls` 还是 PowerShell alias,但输入 `ls` 却跑到了别的实现:
- 说明可能有别的工具在 `$PROFILE` 里注入了 `PSConsoleHostReadLine` 改写逻辑;微软 `coreutils` 就会这样做。
52 changes: 34 additions & 18 deletions scripts/winux-activate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,36 @@ function Write-Color {
Write-Host "$($Markers[$Color]) $Text"
}

function Get-ProfileInstallPath {
if (-not [string]::IsNullOrWhiteSpace($env:WINUXCMD_PROFILE_PATH)) {
return $env:WINUXCMD_PROFILE_PATH
}

return $PROFILE.CurrentUserAllHosts
}

function Remove-LegacyProfileBlocks {
param([string]$Content)

$options = [System.Text.RegularExpressions.RegexOptions]::Singleline -bor
[System.Text.RegularExpressions.RegexOptions]::Multiline
$patterns = @(
'# WinuxCmd wrapper.*?(?=^# Find winuxcmd\.exe|\Z)',
'^# set alias\s*\r?\nUpdate-WinuxCmdAlias\s*\r?\n\r?\n',
'^function Update-WinuxCmdAlias\s*\{.*?^\}',
'^function global:winux\s*\{.*?^\}',
'^Set-Alias -Name winuxcmd -Value [^\r\n]+\r?\n?',
'^# =+[\r\n]+# WinuxCmd Integration.*?# =+[\r\n]+# End WinuxCmd Integration[\r\n]*',
'^Register-EngineEvent -SourceIdentifier PowerShell\.Exiting -Action \{.*?\} \| Out-Null\r?\n?'
)

foreach ($pattern in $patterns) {
$Content = [regex]::Replace($Content, $pattern, '', $options)
}

return $Content
}

# ========== Find WinuxCmd installation ==========
function Get-WinuxBinDir {
# Priority 1: Check current directory (Scoop installation scenario)
Expand Down Expand Up @@ -350,7 +380,7 @@ Register-EngineEvent -SourceIdentifier PowerShell.Exiting -Action {
} | Out-Null
'@

$profilePath = $PROFILE.CurrentUserAllHosts
$profilePath = Get-ProfileInstallPath

# Ensure profile directory exists
$profileDir = Split-Path $profilePath -Parent
Expand All @@ -365,20 +395,7 @@ Register-EngineEvent -SourceIdentifier PowerShell.Exiting -Action {
$currentContent = $currentContent.Trim()
}

# remove old configuration.
$patterns = @(
'(?s)# WinuxCmd wrapper.*?(?=^# Find winuxcmd\.exe|\Z)',
'(?s)^# set alias\s*\r?\nUpdate-WinuxCmdAlias\s*\r?\n\r?\n',
'(?s)^function Update-WinuxCmdAlias\s*\{.*?^\}',
'(?s)^function global:winux\s*\{.*?^\}',
'^Set-Alias -Name winuxcmd -Value [^\r\n]+\r?\n?',
'(?s)^# =+[\r\n]+# WinuxCmd Integration.*?# =+[\r\n]+# End WinuxCmd Integration[\r\n]+',
'(?s)^Register-EngineEvent -SourceIdentifier PowerShell\.Exiting -Action \{.*?\} \| Out-Null'
)

foreach ($pattern in $patterns) {
$currentContent = $currentContent -replace $pattern, ''
}
$currentContent = Remove-LegacyProfileBlocks -Content $currentContent

# remove extra space
$currentContent = $currentContent -replace '\n\n\n+', "`n`n"
Expand Down Expand Up @@ -466,7 +483,7 @@ if (-not $binDir) {
Write-Color "Cyan" "Found WinuxCmd at: $binDir"

# Get profile path for display
$profilePath = $PROFILE.CurrentUserAllHosts
$profilePath = Get-ProfileInstallPath

# Ask for confirmation
Write-Host ""
Expand All @@ -492,8 +509,7 @@ if (Install-WinuxToProfile -BinDir $binDir) {
Write-Color "Cyan" "Next steps:"
Write-Host "1. RESTART PowerShell or run: . `$PROFILE.CurrentUserAllHosts"
Write-Host "2. Test with: winux"
Write-Host "3. If you have winux.ps1, copy it to WinuxCmd bin directory"
Write-Host "4. Optional: winux activate (enables bare ls/rm/cat in PowerShell)"
Write-Host "3. Optional: winux activate (enables bare ls/rm/cat/man in PowerShell)"
Write-Host ""
Write-Color "Cyan" "Usage after restart:"
Write-Host " > winux # Show help and version info"
Expand Down
Loading
Loading