@@ -8,59 +8,64 @@ import (
8
8
)
9
9
10
10
const (
11
+ // PathStackEnv stores the string representation of the stack, as a ":" separated list.
12
+ // Each element in the list is also the key to the env-var that stores the
13
+ // nixEnvPath for that devbox-project. Except for the last element, which is InitPathEnv.
11
14
PathStackEnv = "DEVBOX_PATH_STACK"
12
15
13
16
// InitPathEnv stores the path prior to any devbox shellenv modifying the environment
14
17
InitPathEnv = "DEVBOX_INIT_PATH"
15
18
)
16
19
17
- // Stack has the following design:
18
- // 1. The PathStack enables tracking which sub-paths in PATH come from which devbox-project
19
- // 2. What it stores: The PathStack is an ordered-list of nixEnvPathKeys
20
- // 3. Each nixEnvPathKey is set as an env-var with the value of the nixEnvPath for that devbox-project.
21
- // 4. The final PATH is reconstructed by concatenating the env-var values of each nixEnvPathKey env-var.
22
- // 5. The Stack is stored in its own env-var shared by all devbox-projects in this shell.
23
- type Stack struct {
20
+ // stack has the following design:
21
+ // 1. The stack enables tracking which sub-paths in PATH come from which devbox-project
22
+ // 2. It is an ordered-list of keys to env-vars that store nixEnvPath values of devbox-projects.
23
+ // 3. The final PATH is reconstructed by concatenating the env-var values of each nixEnvPathKey.
24
+ // 5. The stack is stored in its own env-var PathStackEnv, shared by all devbox-projects in this shell.
25
+ type stack struct {
24
26
25
27
// keys holds the stack elements.
26
28
// Earlier (lower index number) keys get higher priority.
27
- // This keeps the string representation of the Stack aligned with the PATH value.
29
+ // This keeps the string representation of the stack aligned with the PATH value.
28
30
keys []string
29
31
}
30
32
31
- func NewStack (env map [string ]string ) * Stack {
33
+ func Stack (env map [string ]string ) * stack {
32
34
stackEnv , ok := env [PathStackEnv ]
33
35
if ! ok {
34
36
// if path stack is empty, then push the current PATH, which is the
35
37
// external environment prior to any devbox-shellenv being applied to it.
36
38
stackEnv = InitPathEnv
37
39
env [InitPathEnv ] = env ["PATH" ]
38
40
}
39
- return & Stack {
41
+ return & stack {
40
42
keys : strings .Split (stackEnv , ":" ),
41
43
}
42
44
}
43
45
44
- // String is the value of the Stack stored in its env-var.
45
- func (s * Stack ) String () string {
46
+ // String is the value of the stack stored in its env-var.
47
+ func (s * stack ) String () string {
46
48
return strings .Join (s .keys , ":" )
47
49
}
48
50
49
- // Key is the element stored in the Stack for a devbox-project. It represents
51
+ // Key is the element stored in the stack for a devbox-project. It represents
50
52
// a pointer to the nixEnvPath value stored in its own env-var, also using this same
51
53
// Key.
52
54
func Key (projectHash string ) string {
53
- return "DEVBOX_NIX_ " + projectHash
55
+ return "DEVBOX_NIX_ENV_PATH_ " + projectHash
54
56
}
55
57
56
- // AddToEnv adds the new nixEnvPath for the devbox-project identified by projectHash to the env.
57
- // It does so by modifying the following env-vars:
58
+ // PushAndUpdateEnv adds the new nixEnvPath for the devbox-project identified by projectHash.
59
+ // The nixEnvPath is pushed to the top of the stack (given highest priority), unless preservePathStack
60
+ // is enabled.
61
+ //
62
+ // It also updated the env by modifying the following env-vars:
58
63
// 1. nixEnvPath key
59
64
// 2. PathStack
60
65
// 3. PATH
61
66
//
62
67
// Returns the modified env map
63
- func (s * Stack ) AddToEnv (
68
+ func (s * stack ) PushAndUpdateEnv (
64
69
env map [string ]string ,
65
70
projectHash string ,
66
71
nixEnvPath string ,
@@ -81,19 +86,14 @@ func (s *Stack) AddToEnv(
81
86
}
82
87
env [PathStackEnv ] = s .String ()
83
88
84
- // Look up the paths-list for each Stack element, and join them together to get the final PATH.
89
+ // Look up the paths-list for each stack element, and join them together to get the final PATH.
85
90
pathLists := lo .Map (s .keys , func (part string , idx int ) string { return env [part ] })
86
91
env ["PATH" ] = JoinPathLists (pathLists ... )
87
92
return env
88
93
}
89
94
90
95
// Has tests if the stack has the specified key. Refer to the Key function for constructing
91
96
// the appropriate key for any devbox-project.
92
- func (s * Stack ) Has (key string ) bool {
93
- for _ , k := range s .keys {
94
- if k == key {
95
- return true
96
- }
97
- }
98
- return false
97
+ func (s * stack ) Has (key string ) bool {
98
+ return lo .Contains (s .keys , key )
99
99
}
0 commit comments