Skip to content

Commit cf30e2a

Browse files
committed
新增插件设置:全局环境变量
1 parent 419b759 commit cf30e2a

File tree

5 files changed

+63
-3
lines changed

5 files changed

+63
-3
lines changed

package.json

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@
218218
},
219219
"EIDE.Cpptools.ForceInclude": {
220220
"type": "array",
221+
"items": {
222+
"type": "string"
223+
},
221224
"scope": "resource",
222225
"markdownDescription": "Force include file path list for c/c++ plug-in intellisence",
223226
"default": []
@@ -285,9 +288,20 @@
285288
"EIDE.Builder.AdditionalCommandLine": {
286289
"type": "string",
287290
"scope": "resource",
288-
"markdownDescription": "Append additional commandline when invoke unify_builder",
291+
"markdownDescription": "%settings.builder.extraCommandLine%",
289292
"default": ""
290293
},
294+
"EIDE.Builder.EnvironmentVariables": {
295+
"type": "array",
296+
"items": {
297+
"type": "string"
298+
},
299+
"scope": "resource",
300+
"markdownDescription": "%settings.builder.presetEnvVars%",
301+
"default": [
302+
"# Name=Value"
303+
]
304+
},
291305
"EIDE.Option.ShowToolbarInEditerTitle": {
292306
"type": "boolean",
293307
"scope": "resource",

package.nls.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@
9898
"settings.enable.telemetry": "Enable telemetry for extension. We will collect information from the logs to help improve the functionality of this extension.",
9999

100100
"settings.builder.jobs": "The number of threads when build",
101+
"settings.builder.extraCommandLine": "Append additional commandline when invoke unify_builder",
102+
"settings.builder.presetEnvVars": "Preset Global Environment Variables when build project",
101103

102104
"settings.option.show.toolbar.in.editer.title": "Displays some toolbars in the editor title",
103105
"settings.option.show.statusbar": "Displays toolbar icons in the status bar",

package.nls.zh-CN.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@
9898
"settings.enable.telemetry": "启用遥测功能。我们将收集日志中的信息,帮助改进该扩展的功能",
9999

100100
"settings.builder.jobs": "构建时使用的线程数",
101+
"settings.builder.extraCommandLine": "构建时 unify_builder 的附加命令行参数",
102+
"settings.builder.presetEnvVars": "构建时预设的全局环境变量",
101103

102104
"settings.option.show.toolbar.in.editer.title": "在编辑器的标题栏显示工具栏图标",
103105
"settings.option.show.statusbar": "在状态栏显示工具栏图标",

src/EIDEProject.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,11 @@ export abstract class AbstractProject implements CustomConfigurationProvider, Pr
917917
}
918918

919919
public getProjectVariables(): { [key: string]: string } {
920-
const _env: { [key: string]: string } = this.getProjectUserEnv() || {};
920+
const _env: { [key: string]: string } = SettingManager.GetInstance().getGlobalEnvVariables() || {};
921+
/* set user env */
922+
const _uenv = this.getProjectUserEnv();
923+
for (const key in _uenv) _env[key] = _uenv[key];
924+
/* set built-in env */
921925
const _ienv = this.getBuiltinVarKvMap();
922926
for (const key in _ienv) _env[key] = _ienv[key];
923927
return _env;
@@ -1265,11 +1269,14 @@ export abstract class AbstractProject implements CustomConfigurationProvider, Pr
12651269
if (!File.isEnvPath(path))
12661270
break; // not have any env var, end
12671271

1268-
// replace stable env
1272+
// replace built-in env
12691273
path = this._replaceProjEnv(path);
12701274

12711275
// replace user env
12721276
path = this._replaceUserEnv(path, true);
1277+
1278+
// replace global envs
1279+
path = this._replaceGlobalEnv(path, true);
12731280
}
12741281

12751282
return path;
@@ -1301,6 +1308,20 @@ export abstract class AbstractProject implements CustomConfigurationProvider, Pr
13011308
return str;
13021309
}
13031310

1311+
// plug-in global env vars
1312+
private _replaceGlobalEnv(str: string, ignore_case_sensitivity: boolean = false): string {
1313+
const globEnv = SettingManager.GetInstance().getGlobalEnvVariables();
1314+
if (globEnv) {
1315+
for (const key in globEnv) {
1316+
let flag = 'g';
1317+
if (ignore_case_sensitivity) flag += 'i';
1318+
const reg = new RegExp(String.raw`\$\(${key}\)|\$\{${key}\}`, flag);
1319+
str = str.replace(reg, globEnv[key]);
1320+
}
1321+
}
1322+
return str;
1323+
}
1324+
13041325
ToAbsolutePath(path_: string, resolveEnv: boolean = true): string {
13051326
const path = resolveEnv ? this.replacePathEnv(path_.trim()) : path_.trim();
13061327
if (File.isAbsolute(path)) { return File.normalize(path); }

src/SettingManager.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,27 @@ export class SettingManager {
252252

253253
//--------------------- Global Option ------------------------
254254

255+
getGlobalEnvVariables(): { [key: string]: string } {
256+
const lines = this.getConfiguration().get<string[]>('Builder.EnvironmentVariables') || [];
257+
const result: { [key: string]: string } = {};
258+
for (const _line of lines) {
259+
const str = _line.trim();
260+
if (str.startsWith('#'))
261+
continue;
262+
const sep_idx = str.indexOf('=');
263+
if (sep_idx == -1)
264+
continue;
265+
const k = str.substring(0, sep_idx).trim();
266+
if (!/^\w+$/.test(k))
267+
continue;
268+
const v = str.substring(sep_idx + 1).trim();
269+
if (v) {
270+
result[k] = v;
271+
}
272+
}
273+
return result;
274+
}
275+
255276
getForceIncludeList(): string[] {
256277
return this.getConfiguration().get<string[]>('Cpptools.ForceInclude') || [];
257278
}

0 commit comments

Comments
 (0)