# Licensed to the .NET Foundation under one or more agreements.# The .NET Foundation licenses this file to you under the Apache 2.0 License.# See the LICENSE file in the project root for more information.<#.SYNOPSISActivates an IronPython environment, placing its commands at the head of $Env:PATH..DESCRIPTIONThis script modifies the current PowerShell environment such that a given IronPython installation becomes the active one. By default, the installation containing this script is being activated, or, if the script is not part of an installation, the latest MSI-installed version of IronPython (Windows only).IronPython environments do not nest. Entering a new environment from within an active environment automatically exits the current one.The following modifications are made:* The Env:PATH variable is modified by adding the path to the environment commands at the beginning of the existing list of paths.* The PowerShell prompt is modified to display the name of the environment.* A new function Exit-IronPythonEnvironment is defined, with an alias "exipy". Use this function to undo all modifications done on entering.When switch -Isolated is used, additional modifications are performed:* Variable Env:IRONPYTHONPATH is cleared.* Alias:ipy, if exists, is deleted.* Function:ipy, if exists, is deleted.#>[CmdletBinding()]param(# Path to an IronPython environment to enter.[Parameter(Position=0)][SupportsWildcards()][string]$Path,# On enter, unset Env:IRONPYTHONPATH, Alias:ipy, and Function:ipy. Restore on exit.[switch]$Isolated,# Color of the environment name printed on the prompt.[ConsoleColor]$PromptColor=[ConsoleColor]::DarkGray)if(-not$Path){# Locate default environment to activateif(Test-Path(Join-Path$PSScriptRoot"IronPython.dll")){$Path=$PSScriptRoot}elseif($IsWindows-and(Test-PathHKLM:\SOFTWARE\IronPython\*\InstallPath)){$installKey=(Resolve-PathHKLM:\SOFTWARE\IronPython\*\InstallPath)[-1]# grab the latest$Path=Get-ItemPropertyValue-Path$installKey-Name'(default)'}else{Write-Error"Cannot locate default environment."exit1}}if(-not(Test-Path$Path-PathTypeContainer)){Write-Error"Environment not found: $Path"exit1}if((Resolve-Path$Path).Count-gt1){Write-Error"Environment path resolved to multiple locations:"Resolve-Path$Path|Write-Errorexit1}functionglobal:Exit-IronPythonEnvironment([switch]$NonDestructive){<#.SYNOPSISUndo all environment modifications done by Enter-IronPythonEnvironment.PARAMETER NonDestructiveKeep function Exit-IronPythonEnvironment and its alias. Subsequent invocations of this function while not within an IronPython environment will result in no-op.#>if(Test-PathFunction:IronPythonParentPrompt){Copy-ItemFunction:IronPythonParentPromptFunction:promptRemove-ItemFunction:IronPythonParentPrompt}if(Test-PathVariable:IronPythonParentEnvironment){$Env:IRONPYTHONPATH=$IronPythonParentEnvironment["libpath"]$Env:PATH=$IronPythonParentEnvironment["binpath"]$oldAlias=$IronPythonParentEnvironment["alias"]if($null-ne$oldAlias){Set-Alias-Nameipy`-Value$oldAlias.Definition`-Description$oldAlias.Description`-Option$oldAlias.Options`-Scopeglobal}if($null-ne$IronPythonParentEnvironment["function"]){Copy-Item$IronPythonParentEnvironment["function"]Function:ipy}Remove-ItemVariable:IronPythonParentEnvironment}Remove-Variable-NameIronPythonEnvironment*-Scopeglobalif(-not$NonDestructive){Remove-ItemFunction:Exit-IronPythonEnvironmentRemove-ItemAlias:exipy-ErrorActionIgnore}}Exit-IronPythonEnvironment-NonDestructive$global:IronPythonEnvironmentPath=Resolve-Path$Path$global:IronPythonEnvironmentName=Split-Path$IronPythonEnvironmentPath-Leaf$global:IronPythonEnvironmentColor="$PromptColor"# Save stuff that already exists$global:IronPythonParentEnvironment=@{"binpath"=$Env:PATH}if($Isolated){if(Test-PathAlias:ipy){$IronPythonParentEnvironment["alias"]=Get-ItemAlias:ipyRemove-ItemAlias:ipy}if(Test-PathFunction:ipy){$IronPythonParentEnvironment["function"]=Get-Itemfunction:ipyRemove-ItemFunction:ipy}if(Test-PathEnv:IRONPYTHONPATH){$IronPythonParentEnvironment["libpath"]=$Env:IRONPYTHONPATHRemove-ItemEnv:IRONPYTHONPATH}}functionglobal:IronPythonParentPrompt{""}Copy-ItemFunction:promptFunction:IronPythonParentPrompt# Set new stuff for the environment to be operationalfunctionglobal:prompt{Write-Host-NoNewline-ForegroundColor$IronPythonEnvironmentColor"«$IronPythonEnvironmentName» "IronPythonParentPrompt}[string[]]$newEnvPaths=@($IronPythonEnvironmentPath)if($IsWindows){$newEnvPaths+=Join-Path$IronPythonEnvironmentPath"Scripts"}else{$newEnvPaths+=Join-Path$IronPythonEnvironmentPath"bin"}$newEnvPaths+=$env:PATH$env:PATH=$newEnvPaths-join[IO.Path]::PathSeparatorSet-AliasexipyExit-IronPythonEnvironment-Scopeglobal
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。