14
14
-- COPYING3. If not, go to http://www.gnu.org/licenses for a complete copy --
15
15
-- of the license. --
16
16
----------------------------------------------------------------------------*/
17
+ import { platform } from 'os' ;
18
+ import * as path from 'path' ;
17
19
import * as vscode from 'vscode' ;
18
- import * as path from 'path'
19
20
20
21
/**
21
22
* Substitue any variable reference present in the given string. VS Code
22
23
* variable references are listed here:
23
24
* https://code.visualstudio.com/docs/editor/variables-reference
24
- * @param str
25
- * @param recursive
26
- * @returns
25
+ * @param str - string to perform substitution on
26
+ * @param recursive - whether to perform substitution recursively on the result
27
+ * of each substitution until there are no variables to substitute.
28
+ *
29
+ * @returns string after applying substitutions
27
30
*/
28
- export function substituteVariables ( str : string , recursive = false ) {
29
-
30
- let workspaces = vscode . workspace . workspaceFolders ?? [ ] ;
31
- let workspace = workspaces . length ? workspaces [ 0 ] : null ;
32
- let activeEditor = vscode . window . activeTextEditor
33
- let activeFile = activeEditor ?. document ;
34
- let absoluteFilePath = activeFile ?. uri . fsPath ?? ""
31
+ export function substituteVariables ( str : string , recursive = false ) : string {
32
+ const workspaces = vscode . workspace . workspaceFolders ?? [ ] ;
33
+ const workspace = workspaces . length ? workspaces [ 0 ] : null ;
34
+ const activeEditor = vscode . window . activeTextEditor ;
35
+ const activeFile = activeEditor ?. document ;
36
+ const absoluteFilePath = activeFile ?. uri . fsPath ?? '' ;
35
37
36
38
if ( workspace != null ) {
37
39
str = str . replace ( / \$ { workspaceFolder} / g, workspace ?. uri . fsPath ) ;
@@ -41,43 +43,102 @@ export function substituteVariables(str: string, recursive = false) {
41
43
str = str . replace ( / \$ { file} / g, absoluteFilePath ) ;
42
44
let activeWorkspace = workspace ;
43
45
let relativeFilePath = absoluteFilePath ;
44
- for ( let workspace of workspaces ) {
46
+ for ( const workspace of workspaces ) {
45
47
if ( absoluteFilePath . replace ( workspace . uri . fsPath , '' ) !== absoluteFilePath ) {
46
48
activeWorkspace = workspace ;
47
- relativeFilePath = absoluteFilePath . replace ( workspace . uri . fsPath , '' ) . substr ( path . sep . length ) ;
49
+ relativeFilePath = absoluteFilePath
50
+ . replace ( workspace . uri . fsPath , '' )
51
+ . substr ( path . sep . length ) ;
48
52
break ;
49
53
}
50
54
}
51
- let parsedPath = path . parse ( absoluteFilePath ) ;
55
+ const parsedPath = path . parse ( absoluteFilePath ) ;
52
56
53
57
if ( activeWorkspace != null ) {
54
58
str = str . replace ( / \$ { fileWorkspaceFolder} / g, activeWorkspace ?. uri . fsPath ) ;
55
59
}
56
60
57
61
str = str . replace ( / \$ { relativeFile} / g, relativeFilePath ) ;
58
- str = str . replace ( / \$ { relativeFileDirname} / g, relativeFilePath . substr ( 0 , relativeFilePath . lastIndexOf ( path . sep ) ) ) ;
62
+ str = str . replace (
63
+ / \$ { relativeFileDirname} / g,
64
+ relativeFilePath . substr ( 0 , relativeFilePath . lastIndexOf ( path . sep ) )
65
+ ) ;
59
66
str = str . replace ( / \$ { fileBasename} / g, parsedPath . base ) ;
60
67
str = str . replace ( / \$ { fileBasenameNoExtension} / g, parsedPath . name ) ;
61
68
str = str . replace ( / \$ { fileExtname} / g, parsedPath . ext ) ;
62
- str = str . replace ( / \$ { fileDirname} / g, parsedPath . dir . substr ( parsedPath . dir . lastIndexOf ( path . sep ) + 1 ) ) ;
69
+ str = str . replace (
70
+ / \$ { fileDirname} / g,
71
+ parsedPath . dir . substr ( parsedPath . dir . lastIndexOf ( path . sep ) + 1 )
72
+ ) ;
63
73
str = str . replace ( / \$ { cwd} / g, parsedPath . dir ) ;
64
74
str = str . replace ( / \$ { pathSeparator} / g, path . sep ) ;
65
75
66
76
if ( activeEditor != null ) {
67
77
str = str . replace ( / \$ { lineNumber} / g, ( activeEditor . selection . start . line + 1 ) . toString ( ) ) ;
68
- str = str . replace ( / \$ { selectedText} / g, activeEditor . document . getText ( new vscode . Range ( activeEditor . selection . start , activeEditor . selection . end ) ) ) ;
78
+ str = str . replace (
79
+ / \$ { selectedText} / g,
80
+ activeEditor . document . getText (
81
+ new vscode . Range ( activeEditor . selection . start , activeEditor . selection . end )
82
+ )
83
+ ) ;
69
84
}
70
85
71
86
str = str . replace ( / \$ { env:( .* ?) } / g, function ( variable ) {
87
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
72
88
return process . env [ variable . match ( / \$ { env:( .* ?) } / ) ! [ 1 ] ] || '' ;
73
89
} ) ;
74
90
75
91
str = str . replace ( / \$ { config:( .* ?) } / g, function ( variable ) {
92
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
76
93
return vscode . workspace . getConfiguration ( ) . get ( variable . match ( / \$ { config:( .* ?) } / ) ! [ 1 ] , '' ) ;
77
94
} ) ;
78
95
79
- if ( recursive && str . match ( / \$ { ( w o r k s p a c e F o l d e r | w o r k s p a c e F o l d e r B a s e n a m e | f i l e W o r k s p a c e F o l d e r | r e l a t i v e F i l e | f i l e B a s e n a m e | f i l e B a s e n a m e N o E x t e n s i o n | f i l e E x t n a m e | f i l e D i r n a m e | c w d | p a t h S e p a r a t o r | l i n e N u m b e r | s e l e c t e d T e x t | e n v : ( .* ?) | c o n f i g : ( .* ?) ) } / ) ) {
96
+ if (
97
+ recursive &&
98
+ str . match (
99
+ // eslint-disable-next-line max-len
100
+ / \$ { ( w o r k s p a c e F o l d e r | w o r k s p a c e F o l d e r B a s e n a m e | f i l e W o r k s p a c e F o l d e r | r e l a t i v e F i l e | f i l e B a s e n a m e | f i l e B a s e n a m e N o E x t e n s i o n | f i l e E x t n a m e | f i l e D i r n a m e | c w d | p a t h S e p a r a t o r | l i n e N u m b e r | s e l e c t e d T e x t | e n v : ( .* ?) | c o n f i g : ( .* ?) ) } /
101
+ )
102
+ ) {
80
103
str = substituteVariables ( str , recursive ) ;
81
104
}
82
105
return str ;
83
- }
106
+ }
107
+
108
+ export function getCustomEnv ( ) {
109
+ const user_platform = platform ( ) ;
110
+ let env_config_name = 'terminal.integrated.env' ;
111
+
112
+ switch ( user_platform ) {
113
+ case 'darwin' :
114
+ env_config_name += '.osx' ;
115
+ break ;
116
+ case 'win32' :
117
+ env_config_name += '.windows' ;
118
+ break ;
119
+ default :
120
+ env_config_name += '.linux' ;
121
+ }
122
+
123
+ const custom_env = vscode . workspace
124
+ . getConfiguration ( )
125
+ . get < { [ name : string ] : string } > ( env_config_name ) ;
126
+
127
+ return custom_env ;
128
+ }
129
+
130
+ export function getEvaluatedCustomEnv ( ) {
131
+ const custom_env = getCustomEnv ( ) ;
132
+
133
+ if ( custom_env ) {
134
+ for ( const var_name in custom_env ) {
135
+ // Substitute VS Code variable references that might be present
136
+ // in the JSON settings configuration (e.g: "PATH": "${workspaceFolder}/obj")
137
+ custom_env [ var_name ] = custom_env [ var_name ] . replace ( / ( \$ \{ .* \} ) / , ( substring ) =>
138
+ substituteVariables ( substring , false )
139
+ ) ;
140
+ }
141
+ }
142
+
143
+ return custom_env ;
144
+ }
0 commit comments