From 605c83a6d1ee5f5b99f078f0daf74d94baf7248b Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Fri, 20 Oct 2017 14:03:31 +0900 Subject: [PATCH] support powershell env --- emsdk | 11 ++++++++--- emsdk.bat | 6 +----- emsdk.ps1 | 37 +++++++++++++++++++++++++++++++++++++ emsdk_env.ps1 | 2 ++ 4 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 emsdk.ps1 create mode 100644 emsdk_env.ps1 diff --git a/emsdk b/emsdk index 8ef70ef..236ef71 100755 --- a/emsdk +++ b/emsdk @@ -31,6 +31,8 @@ binaryen_git_repo = 'https://github.com/WebAssembly/binaryen.git' # Enable this to do very verbose printing about the different steps that are being run. Useful for debugging. VERBOSE = bool(os.getenv('EMSDK_VERBOSE')) if os.getenv('EMSDK_VERBOSE') != None else False +POWERSHELL = bool(os.getenv('EMSDK_POWERSHELL')) + WINDOWS = False if os.name == 'nt' or (os.getenv('SYSTEMROOT') != None and 'WINDOWS' in os.getenv('SYSTEMROOT')) or (os.getenv('COMSPEC') != None and 'WINDOWS' in os.getenv('COMSPEC')): WINDOWS = True @@ -80,7 +82,7 @@ emscripten_config_directory = os.path.expanduser("~/") if os.path.exists(os.path.join(emsdk_path(), '.emscripten')): emscripten_config_directory = emsdk_path() -EMSDK_SET_ENV = 'emsdk_set_env.bat' if (WINDOWS and not MSYS) else 'emsdk_set_env.sh' +EMSDK_SET_ENV = 'emsdk_set_env.ps1' if POWERSHELL else 'emsdk_set_env.bat' if (WINDOWS and not MSYS) else 'emsdk_set_env.sh' # Finds the given executable 'program' in PATH. Operates like the Unix tool 'which'. def which(program): @@ -1880,7 +1882,8 @@ def construct_env(tools_to_activate, permanent): # else: if os.environ['PATH'] != newpath: # Don't bother setting the path if there are no changes. - if WINDOWS and not MSYS: env_string += 'SET PATH=' + newpath + '\n' + if POWERSHELL: env_string += '$env:PATH="' + newpath + '"\n' + elif WINDOWS and not MSYS: env_string += 'SET PATH=' + newpath + '\n' else: env_string += 'export PATH="' + newpath + '"\n' if len(added_path) > 0: print('Adding directories to PATH:') @@ -1912,7 +1915,9 @@ def construct_env(tools_to_activate, permanent): if len(env_vars_to_add) > 0: print('Setting environment variables:') for key, value in env_vars_to_add: - if WINDOWS and not MSYS: + if POWERSHELL: + env_string += '$env:' + key + '="' + value + '"\n' + elif WINDOWS and not MSYS: if permanent: env_string += 'SETX ' + key + ' "' + value + '"\n' else: diff --git a/emsdk.bat b/emsdk.bat index 54635dc..b6cf95c 100644 --- a/emsdk.bat +++ b/emsdk.bat @@ -1,5 +1,3 @@ -@SET PREVPATH=%PATH% - :: Find python from an explicit location relative to the Emscripten SDK. @IF EXIST "%~dp0python\2.7.5.3_64bit\python.exe" ( @SET EMSDK_PY="%~dp0python\2.7.5.3_64bit\python.exe" @@ -21,15 +19,13 @@ @GOTO end ) -:: As last resort, access from PATH. +:: As a last resort, access from PATH. @SET EMSDK_PY=python :end @call %EMSDK_PY% "%~dp0\emsdk" %* @set EMSDK_PY= -@set PATH=%PREVPATH% -@set PREVPATH= :: python is not able to set environment variables to the parent calling process, so :: therefore have it craft a .bat file, which we invoke after finishing python execution, diff --git a/emsdk.ps1 b/emsdk.ps1 new file mode 100644 index 0000000..c1d1488 --- /dev/null +++ b/emsdk.ps1 @@ -0,0 +1,37 @@ +$ScriptDirectory = Split-Path -parent $PSCommandPath + +$PythonLocations = $( + "python\2.7.5.3_64bit\python.exe", + "python\2.7.5.3_32bit\python.exe", + "python\2.7.5_64bit\python.exe", + "python\2.7.5.1_32bit\python.exe" +) + +# Find python from an explicit location relative to the Emscripten SDK. +foreach ($Location in $PythonLocations) { + $FullLocation = Join-Path $ScriptDirectory $Location + if (Test-Path $FullLocation) { + $EMSDK_PY = $FullLocation + break + } +} + +# As a last resort, access from PATH. +if (-Not $EMSDK_PY) { + $EMSDK_PY = "python" +} + +# Tell EMSDK to create environment variable setter as a .ps1 file +$env:EMSDK_POWERSHELL = 1 + +& $EMSDK_PY "$ScriptDirectory/emsdk" $args + +# python is not able to set environment variables to the parent calling process, so +# therefore have it craft a .ps1 file, which we invoke after finishing python execution, +# to set up the environment variables +if (Test-Path "emsdk_set_env.ps1") { + & emsdk_set_env.ps1 + Remove-Item emsdk_set_env.ps1 +} + +Remove-Item Env:\EMSDK_POWERSHELL diff --git a/emsdk_env.ps1 b/emsdk_env.ps1 new file mode 100644 index 0000000..fc7f2c7 --- /dev/null +++ b/emsdk_env.ps1 @@ -0,0 +1,2 @@ +$ScriptDirectory = Split-Path -parent $PSCommandPath +& "$ScriptDirectory/emsdk.ps1" construct_env $args