-
Notifications
You must be signed in to change notification settings - Fork 12
/
Set-PsEnv.psm1
94 lines (80 loc) · 2.7 KB
/
Set-PsEnv.psm1
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
$localEnvFile = ".\.env"
<#
.Synopsis
Exports environment variable from the .env file to the current process.
.Description
This function looks for .env file in the current directoty, if present
it loads the environment variable mentioned in the file to the current process.
.Example
Set-PsEnv
.Example
#.env file format
#To Assign value, use "=" operator
<variable name>=<value>
#To Prefix value to an existing env variable, use ":=" operator
<variable name>:=<value>
#To Suffix value to an existing env variable, use "=:" operator
<variable name>=:<value>
#To comment a line, use "#" at the start of the line
#This is a comment, it will be skipped when parsing
.Example
# This is function is called by convention in PowerShell
# Auto exports the env variable at every prompt change
function prompt {
Set-PsEnv
}
#>
function Set-PsEnv {
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Low')]
param()
if($Global:PreviousDir -eq (Get-Location).Path){
Write-Verbose "Set-PsEnv:Skipping same dir"
return
} else {
$Global:PreviousDir = (Get-Location).Path
}
#return if no env file
if (!( Test-Path $localEnvFile)) {
Write-Verbose "No .env file"
return
}
#read the local env file
$content = Get-Content $localEnvFile -ErrorAction Stop
Write-Verbose "Parsed .env file"
#load the content to environment
foreach ($line in $content) {
if([string]::IsNullOrWhiteSpace($line)){
Write-Verbose "Skipping empty line"
continue
}
#ignore comments
if($line.StartsWith("#")){
Write-Verbose "Skipping comment: $line"
continue
}
#get the operator
if($line -like "*:=*"){
Write-Verbose "Prefix"
$kvp = $line -split ":=",2
$key = $kvp[0].Trim()
$value = "{0};{1}" -f $kvp[1].Trim(),[System.Environment]::GetEnvironmentVariable($key)
}
elseif ($line -like "*=:*"){
Write-Verbose "Suffix"
$kvp = $line -split "=:",2
$key = $kvp[0].Trim()
$value = "{1};{0}" -f $kvp[1].Trim(),[System.Environment]::GetEnvironmentVariable($key)
}
else {
Write-Verbose "Assign"
$kvp = $line -split "=",2
$key = $kvp[0].Trim()
$value = $kvp[1].Trim()
}
Write-Verbose "$key=$value"
if ($PSCmdlet.ShouldProcess("environment variable $key", "set value $value")) {
[Environment]::SetEnvironmentVariable($key, $value, "Process") | Out-Null
}
}
}
Export-ModuleMember -Function @('Set-PsEnv')