-
Notifications
You must be signed in to change notification settings - Fork 0
/
with-env.ps1
42 lines (33 loc) · 972 Bytes
/
with-env.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
$ori = @{}
Try {
$i = 0
# Loading .env files
if(Test-Path $args[0]) {
foreach($line in (Get-Content $args[0])) {
if($line -Match '^\s*$' -Or $line -Match '^#') {
continue
}
$key, $val = $line.Split("=")
$ori[$key] = if(Test-Path Env:\$key) { (Get-Item Env:\$key).Value } else { "" }
New-Item -Name $key -Value $val -ItemType Variable -Path Env: -Force > $null
}
$i++
}
while(1) {
if($i -ge $args.length) {
exit
}
if(!($args[$i] -Match '^[^ ]+=[^ ]+$')) {
break
}
$key, $val = $args[$i].Split("=")
$ori[$key] = if(Test-Path Env:\$key) { (Get-Item Env:\$key).Value } else { "" }
New-Item -Name $key -Value $val -ItemType Variable -Path Env: -Force > $null
$i++
}
Invoke-Expression ($args[$i..$args.length] -Join " ")
} Finally {
foreach($key in $ori.Keys) {
New-Item -Name $key -Value $ori.Item($key) -ItemType Variable -Path Env: -Force > $null
}
}