Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/util/jupyter-api/setenv-commands.ts
Views: 687
/*1Code to set an environment variable to a given value in2several different languages.3*/45export default function createSetEnvCommand(6lang: string,7varName: string,8varValue: string9): string {10// throws exception if don't know how.11lang = lang.toLowerCase(); // xeus-cling is 'C++-17'12if (lang == "sparql") {13// there is no notion of environment variables for sparql.14return "";15}16if (lang.startsWith("python") || lang.startsWith("sage")) {17return createPythonSetEnvCode(varName, varValue);18} else if (lang == "r") {19return createRSetEnvCode(varName, varValue);20} else if (lang == "julia") {21return createJuliaSetEnvCode(varName, varValue);22} else if (lang == "octave" || lang == "matlab") {23return createOctaveSetEnvCode(varName, varValue);24} else if (lang == "javascript") {25return createNodeSetEnvCode(varName, varValue);26} else if (lang == "bash" || lang == "sh") {27return createBashSetEnvCommand(varName, varValue);28} else if (lang == "prolog") {29return createPrologSetEnvCode(varName, varValue);30} else if (lang == "c" || lang.startsWith("c++")) {31return createCppSetEnvCode(varName, varValue);32} else {33// e.g., "gap" -- I didn't try.34throw Error(35`unable to set environment variable: ${varName} for language ${lang} is not implemented.`36);37}38}3940// mostly written by ChatGPT441function createPythonSetEnvCode(varName: string, varValue: string): string {42const escapedVarName = varName.replace(/\\/g, "\\\\").replace(/'/g, "\\'");43const escapedVarValue = varValue.replace(/\\/g, "\\\\").replace(/'/g, "\\'");44return `import os; os.environ['${escapedVarName}'] = '${escapedVarValue}'`;45}4647function createRSetEnvCode(varName: string, varValue: string): string {48const escapedVarName = varName.replace(/\\/g, "\\\\").replace(/"/g, '\\"');49const escapedVarValue = varValue.replace(/\\/g, "\\\\").replace(/"/g, '\\"');50return `Sys.setenv("${escapedVarName}"="${escapedVarValue}")`;51}5253function createJuliaSetEnvCode(varName: string, varValue: string): string {54const escapedVarName = varName.replace(/\\/g, "\\\\").replace(/"/g, '\\"');55const escapedVarValue = varValue.replace(/\\/g, "\\\\").replace(/"/g, '\\"');56return `ENV["${escapedVarName}"] = "${escapedVarValue}"`;57}5859function createOctaveSetEnvCode(varName: string, varValue: string): string {60const escapedVarName = varName.replace(/\\/g, "\\\\").replace(/'/g, "\\'");61const escapedVarValue = varValue.replace(/\\/g, "\\\\").replace(/'/g, "\\'");62return `setenv('${escapedVarName}','${escapedVarValue}')`;63}6465function createNodeSetEnvCode(varName: string, varValue: string): string {66const escapedVarName = varName.replace(/\\/g, "\\\\").replace(/`/g, "\\`");67const escapedVarValue = varValue.replace(/\\/g, "\\\\").replace(/`/g, "\\`");68return `process.env.${escapedVarName} = \`${escapedVarValue}\``;69}7071function createBashSetEnvCommand(varName: string, varValue: string): string {72const escapedVarName = varName.replace(/(["'$`\\])/g, "\\$1");73const escapedVarValue = varValue.replace(/(["'$`\\])/g, "\\$1");74return `${escapedVarName}="${escapedVarValue}"`;75}7677function createPrologSetEnvCode(varName: string, varValue: string): string {78const escapedVarName = varName.replace(/\\/g, "\\\\").replace(/'/g, "\\'");79const escapedVarValue = varValue.replace(/\\/g, "\\\\").replace(/'/g, "\\'");80return `setenv('${escapedVarName}', '${escapedVarValue}').`;81}8283function createCppSetEnvCode(varName: string, varValue: string): string {84const escapedVarName = varName.replace(/\\/g, "\\\\").replace(/"/g, '\\"');85const escapedVarValue = varValue.replace(/\\/g, "\\\\").replace(/"/g, '\\"');86return `#include <cstdlib>\nstd::putenv("${escapedVarName}=${escapedVarValue}")\n`;87}888990