-
Notifications
You must be signed in to change notification settings - Fork 216
/
setenv-commands.ts
88 lines (79 loc) · 3.69 KB
/
setenv-commands.ts
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
/*
Code to set an environment variable to a given value in
several different languages.
*/
export default function createSetEnvCommand(
lang: string,
varName: string,
varValue: string
): string {
// throws exception if don't know how.
lang = lang.toLowerCase(); // xeus-cling is 'C++-17'
if (lang == "sparql") {
// there is no notion of environment variables for sparql.
return "";
}
if (lang.startsWith("python") || lang.startsWith("sage")) {
return createPythonSetEnvCode(varName, varValue);
} else if (lang == "r") {
return createRSetEnvCode(varName, varValue);
} else if (lang == "julia") {
return createJuliaSetEnvCode(varName, varValue);
} else if (lang == "octave" || lang == "matlab") {
return createOctaveSetEnvCode(varName, varValue);
} else if (lang == "javascript") {
return createNodeSetEnvCode(varName, varValue);
} else if (lang == "bash" || lang == "sh") {
return createBashSetEnvCommand(varName, varValue);
} else if (lang == "prolog") {
return createPrologSetEnvCode(varName, varValue);
} else if (lang == "c" || lang.startsWith("c++")) {
return createCppSetEnvCode(varName, varValue);
} else {
// e.g., "gap" -- I didn't try.
throw Error(
`unable to set environment variable: ${varName} for language ${lang} is not implemented.`
);
}
}
// mostly written by ChatGPT4
function createPythonSetEnvCode(varName: string, varValue: string): string {
const escapedVarName = varName.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
const escapedVarValue = varValue.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
return `import os; os.environ['${escapedVarName}'] = '${escapedVarValue}'`;
}
function createRSetEnvCode(varName: string, varValue: string): string {
const escapedVarName = varName.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
const escapedVarValue = varValue.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
return `Sys.setenv("${escapedVarName}"="${escapedVarValue}")`;
}
function createJuliaSetEnvCode(varName: string, varValue: string): string {
const escapedVarName = varName.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
const escapedVarValue = varValue.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
return `ENV["${escapedVarName}"] = "${escapedVarValue}"`;
}
function createOctaveSetEnvCode(varName: string, varValue: string): string {
const escapedVarName = varName.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
const escapedVarValue = varValue.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
return `setenv('${escapedVarName}','${escapedVarValue}')`;
}
function createNodeSetEnvCode(varName: string, varValue: string): string {
const escapedVarName = varName.replace(/\\/g, "\\\\").replace(/`/g, "\\`");
const escapedVarValue = varValue.replace(/\\/g, "\\\\").replace(/`/g, "\\`");
return `process.env.${escapedVarName} = \`${escapedVarValue}\``;
}
function createBashSetEnvCommand(varName: string, varValue: string): string {
const escapedVarName = varName.replace(/(["'$`\\])/g, "\\$1");
const escapedVarValue = varValue.replace(/(["'$`\\])/g, "\\$1");
return `${escapedVarName}="${escapedVarValue}"`;
}
function createPrologSetEnvCode(varName: string, varValue: string): string {
const escapedVarName = varName.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
const escapedVarValue = varValue.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
return `setenv('${escapedVarName}', '${escapedVarValue}').`;
}
function createCppSetEnvCode(varName: string, varValue: string): string {
const escapedVarName = varName.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
const escapedVarValue = varValue.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
return `#include <cstdlib>\nstd::putenv("${escapedVarName}=${escapedVarValue}")\n`;
}