Created
February 7, 2019 21:18
-
-
Save nimzo6689/8857babc86006bce60f3a638ae286d86 to your computer and use it in GitHub Desktop.
Scoop+VSCode+PHPでの環境構築用PowerShell関数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://qiita.com/nimzo6689/items/e3ae6280f34539bb8680 用です。 | |
function replaceFileContentWithoutBOM($path, $regex, $replacement) { | |
Get-Content -Encoding UTF8 $path ` | |
| % {$_ -replace $regex, $replacement}` | |
| Out-String ` | |
| % { [Text.Encoding]::UTF8.GetBytes($_) } ` | |
| Set-Content -Path $path -Encoding Byte | |
} | |
function env($name,$global,$val='__get') { | |
$target = 'User'; if($global) {$target = 'Machine'} | |
if($val -eq '__get') { [environment]::getEnvironmentVariable($name,$target) } | |
else { [environment]::setEnvironmentVariable($name,$val,$target) } | |
} | |
function ensure_in_path($dir, $global) { | |
$path = env 'PATH' $global | |
$dir = (Resolve-Path $dir).Path | |
if($path -notmatch [regex]::escape($dir)) { | |
env 'PATH' $global "$dir;$path" # for future sessions... | |
$env:PATH = "$dir;$env:PATH" # for this session | |
} | |
} | |
function strip_path($orig_path, $dir) { | |
if($null -eq $orig_path) { $orig_path = '' } | |
$stripped = [string]::join(';', @( $orig_path.split(';') | Where-Object { $_ -and $_ -ne $dir } )) | |
return ($stripped -ne $orig_path), $stripped | |
} | |
function remove_from_path($dir,$global) { | |
$dir = (Resolve-Path $dir).Path | |
# future sessions | |
$was_in_path, $newpath = strip_path (env 'path' $global) $dir | |
if($was_in_path) { | |
env 'path' $global $newpath | |
} | |
# current session | |
$was_in_path, $newpath = strip_path $env:PATH $dir | |
if($was_in_path) { $env:PATH = $newpath } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment