@@ -24,6 +24,16 @@ type HostSubContext struct {
2424
2525// SubstituteHost resolves ${localEnv:X}, ${localWorkspaceFolder}, etc.
2626func SubstituteHost (ctx HostSubContext , value interface {}) interface {} {
27+ replace := hostReplacer (ctx )
28+ return substituteRecursive (replace , value )
29+ }
30+
31+ // SubstituteHostString is the typed string variant of SubstituteHost.
32+ func SubstituteHostString (ctx HostSubContext , value string ) string {
33+ return resolveString (hostReplacer (ctx ), value )
34+ }
35+
36+ func hostReplacer (ctx HostSubContext ) replaceFn {
2737 env := ctx .Env
2838 if ctx .Platform == "win32" {
2939 env = normalizeEnvKeys (env )
@@ -37,9 +47,9 @@ func SubstituteHost(ctx HostSubContext, value interface{}) interface{} {
3747 }, ctx .ContainerWorkspaceFolder )
3848 }
3949
40- return substituteRecursive ( func (match , variable string , args []string ) string {
50+ return func (match , variable string , args []string ) string {
4151 return replaceHostVar (isWin , env , ctx , match , variable , args )
42- }, value )
52+ }
4353}
4454
4555// SubstituteDevContainerID resolves ${devcontainerId} from id-labels.
@@ -50,9 +60,33 @@ func SubstituteDevContainerID(idLabels map[string]string, value interface{}) int
5060 if cachedID == "" && idLabels != nil {
5161 cachedID = ComputeDevContainerID (idLabels )
5262 }
53- if cachedID != "" {
54- return cachedID
55- }
63+ return substituteDevContainerID (match , cachedID )
64+ }
65+ return match
66+ }, value )
67+ }
68+
69+ // SubstituteDevContainerIDString resolves ${devcontainerId} when the ID has
70+ // already been computed. Keeping the parsing here prevents callers from
71+ // implementing variable substitution with ad-hoc string replacements.
72+ func SubstituteDevContainerIDString (devcontainerID , value string ) string {
73+ return resolveString (func (match , variable string , _ []string ) string {
74+ if variable == "devcontainerId" {
75+ return substituteDevContainerID (match , devcontainerID )
76+ }
77+ return match
78+ }, value )
79+ }
80+
81+ // SubstituteTemplateOptions resolves ${templateOption:name} using the supplied
82+ // option values. Unknown options remain unchanged.
83+ func SubstituteTemplateOptions (options map [string ]string , value string ) string {
84+ return resolveString (func (match , variable string , args []string ) string {
85+ if variable != "templateOption" || len (args ) == 0 {
86+ return match
87+ }
88+ if replacement , ok := options [strings .TrimSpace (args [0 ])]; ok {
89+ return replacement
5690 }
5791 return match
5892 }, value )
@@ -99,6 +133,13 @@ func ComputeDevContainerID(idLabels map[string]string) string {
99133
100134type replaceFn func (match , variable string , args []string ) string
101135
136+ func substituteDevContainerID (match , devcontainerID string ) string {
137+ if devcontainerID == "" {
138+ return match
139+ }
140+ return devcontainerID
141+ }
142+
102143func substituteRecursive (replace replaceFn , value interface {}) interface {} {
103144 switch v := value .(type ) {
104145 case string :
0 commit comments