diff options
| author | ruki <[email protected]> | 2021-05-08 21:32:25 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2021-05-08 21:32:25 +0800 |
| commit | f21aa19d54be924764fc92f2dfda52af6198899c (patch) | |
| tree | c2296ad891ea87d4a2e6ca1f67ef2ee0f75783af /xmake/scripts | |
| parent | 6a803af51efe12c76a48da851dbdf69242c4eebe (diff) | |
fix psm1 script path
Diffstat (limited to 'xmake/scripts')
| -rw-r--r-- | xmake/scripts/xrepo-hook.psm1 | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/xmake/scripts/xrepo-hook.psm1 b/xmake/scripts/xrepo-hook.psm1 new file mode 100644 index 000000000..902ef3b16 --- /dev/null +++ b/xmake/scripts/xrepo-hook.psm1 @@ -0,0 +1,105 @@ + +## ENVIRONMENT MANAGEMENT ###################################################### + +<# + .SYNOPSIS + Enter a xrepo environment based on your current project. + + .EXAMPLE + Enter-XrepoEnvironment +#> +function Enter-XrepoEnvironment { + [CmdletBinding()] + param(); + + begin { + $script:xrepoOldEnvs = (Get-ChildItem -Path Env:); + + & $Env:XMAKE_EXE lua private.xrepo.action.env.info config; + if (-not $?) { + Exit 1; + } + + $xmakeColorTermBackup, $Env:XMAKE_COLORTERM = $Env:XMAKE_COLORTERM, "nocolor"; + $xrepoPrompt = (& $Env:XMAKE_EXE lua private.xrepo.action.env.info prompt | Out-String); + $Env:XMAKE_COLORTERM = $xmakeColorTermBackup; + if (-not $xrepoPrompt.StartsWith("[")) { + Write-Host $xrepoPrompt; + Exit 1; + } + + $activateCommand = (& $Env:XMAKE_EXE lua private.xrepo.action.env.info script.powershell | Out-String); + + Write-Verbose "[xrepo env script.powershell]`n$activateCommand"; + Invoke-Expression -Command $activateCommand; + + $Env:XMAKE_PROMPT_MODIFIER = $xrepoPrompt.Trim() + " "; + } + process {} + end {} +} + + +<# + .SYNOPSIS + Exit the current xrepo environment, if any. + + .EXAMPLE + Exit-XrepoEnvironment +#> +function Exit-XrepoEnvironment { + [CmdletBinding()] + param(); + + begin { + ForEach ($p in (Get-ChildItem Env:)) { + [Environment]::SetEnvironmentVariable($p.Name, $Null); + } + ForEach ($p in $script:xrepoOldEnvs) { + [Environment]::SetEnvironmentVariable($p.Name, $p.Value); + } + $Env:XMAKE_PROMPT_MODIFIER = ""; + } + process {} + end {} +} + + +if (Test-Path Function:\prompt) { + Rename-Item Function:\prompt XrepoPromptBackup +} else { + function XrepoPromptBackup() { + # Restore a basic prompt if the definition is missing. + "PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "; + } +} + + +<# + .SYNOPSIS + Modifies the current prompt to show the current xmake project. + + .EXAMPLE + Add-XrepoEnvironmentToPrompt + + Causes the current session's prompt to display the current xmake project name. +#> +function Add-XrepoEnvironmentToPrompt() { + function global:prompt() { + if ($Env:XMAKE_PROMPT_MODIFIER) { + $Env:XMAKE_PROMPT_MODIFIER | Write-Host -NoNewline + } + XrepoPromptBackup; + } +} + + +## EXPORTS ################################################################### + +Export-ModuleMember ` + -Alias * ` + -Function ` + Add-XrepoEnvironmentToPrompt, ` + Enter-XrepoEnvironment, ` + Exit-XrepoEnvironment, ` + prompt |
