diff options
| author | fallenink <[email protected]> | 2021-04-29 15:32:02 +0800 |
|---|---|---|
| committer | fallenink <[email protected]> | 2021-04-29 15:32:02 +0800 |
| commit | df6ca9ee326a838259e709804a403e0c2a2a8f51 (patch) | |
| tree | 5e22ebf87e6efcd4368fa81a07b927dc17c6394c /scripts | |
| parent | a2577fc01bf133f866b6c92b504b8991bb679034 (diff) | |
| parent | 9597a24c34cd2e47aab93c1a8a21914cff0c18a8 (diff) | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/installer.nsi | 1 | ||||
| -rw-r--r-- | scripts/xrepo-hook.psm1 | 90 | ||||
| -rw-r--r-- | scripts/xrepo.ps1 | 34 |
3 files changed, 125 insertions, 0 deletions
diff --git a/scripts/installer.nsi b/scripts/installer.nsi index 4f12aebea..a01b6feea 100644 --- a/scripts/installer.nsi +++ b/scripts/installer.nsi @@ -237,6 +237,7 @@ Section "XMake (required)" InstallExeutable File "..\*.md" File "..\core\build\xmake.exe" File "..\scripts\xrepo.bat" + File "..\scripts\xrepo.ps1" File /r /x ".DS_Store" "..\winenv" WriteUninstaller "uninstall.exe" diff --git a/scripts/xrepo-hook.psm1 b/scripts/xrepo-hook.psm1 new file mode 100644 index 000000000..ca63fb09b --- /dev/null +++ b/scripts/xrepo-hook.psm1 @@ -0,0 +1,90 @@ + +## 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:); + $xrepoPrompt = (& $Env:XMAKE_EXE lua private.xrepo.action.env.info prompt | Out-String); + $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 { + # Write-Host $script:xrepoOldEnvs; + 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 diff --git a/scripts/xrepo.ps1 b/scripts/xrepo.ps1 new file mode 100644 index 000000000..84bb4f000 --- /dev/null +++ b/scripts/xrepo.ps1 @@ -0,0 +1,34 @@ +$script:SCRIPT_PATH = $myinvocation.mycommand.path +$script:BASE_DIR = Split-Path $SCRIPT_PATH -Parent +$Env:XMAKE_EXE = Join-Path $BASE_DIR xmake.exe + + +if ($Args.Count -eq 0) { + # No args, just call the underlying conda executable. + & $Env:XMAKE_EXE lua private.xrepo; +} else { + $Command = $Args[0]; + if (($Command -eq "env") -and ($Args.Count -ge 2)) { + switch ($Args[1]) { + "shell" { + if (-not (Test-Path 'Env:XMAKE_ROOTDIR')) { + $Env:XMAKE_ROOTDIR = $BASE_DIR; + Import-Module "$Env:XMAKE_ROOTDIR\scripts\xrepo-hook.psm1"; + Add-XrepoEnvironmentToPrompt; + } + if ((Test-Path 'Env:XMAKE_PROMPT_MODIFIER') -and ($Env:XMAKE_PROMPT_MODIFIER -ne "")) { + Exit-XrepoEnvironment; + } + Enter-XrepoEnvironment; + return; + } + "quit" { + Exit-XrepoEnvironment; + return; + } + } + } + + $OtherArgs = @(); + & $Env:XMAKE_EXE lua private.xrepo $Command @OtherArgs; +} |
