From 085854301967241a8f64da72b6b0fd02c7d615f9 Mon Sep 17 00:00:00 2001 From: Aaron Delasy Date: Sun, 2 Feb 2025 10:56:29 +0200 Subject: [PATCH] Change the way environment variables are set --- src/offline | 4 ++-- src/utils | 58 +++++++++++++++++++++++++++++------------------------ 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/src/offline b/src/offline index db46df9..3048aac 100644 --- a/src/offline +++ b/src/offline @@ -4,7 +4,7 @@ */ import * as Config from "./config" -import pathParam, profileFileAppend from "./utils" +import pathParam, setEnvSystem from "./utils" fn installDependencies (tempDirectory: str) { fs_mkdirSync(tempDirectory) @@ -27,7 +27,7 @@ fn installDependencies (tempDirectory: str) { installPath := Config.getDependenciesPath() fs_copyDirectorySync(platformDependenciesPath, installPath) - profileFileAppend("export THE_DEPS_DIR=" + pathParam(installPath) + " # Added by `the offline`") + setEnvSystem("THE_DEPS_DIR", installPath) } fn installCompiler (tempDirectory: str) { diff --git a/src/utils b/src/utils index 4dfd03d..acc269d 100644 --- a/src/utils +++ b/src/utils @@ -35,32 +35,6 @@ export fn excludePaths (paths: str[], exclude: str[]) str[] { return result } -export fn profileFileAppend (text: str) { - mut filePath := process_home + path_SEP + ".zprofile" - - if !fs_existsSync(filePath) { - filePath = process_home + path_SEP + ".bash_profile" - } - - if !fs_existsSync(filePath) { - filePath = process_home + path_SEP + ".profile" - } - - mut content := fs_existsSync(filePath) ? fs_readFileSync(filePath).str() : "" - - if content[content.len - 1] != '\n' { - content += os_EOL - } - - content += text - - if text[text.len - 1] != '\n' { - content += os_EOL - } - - fs_writeFileSync(filePath, content.toBuffer()) -} - export fn mapStr (arr: str[], cb: (str, int) -> str) str[] { l := arr.len mut result := arr @@ -82,6 +56,38 @@ export fn removeDirectorySync (path: str) { } } +export fn setEnvSystem (name: str, value: str) { + if os_NAME == "Windows" { + process_runSync( + "powershell '[System.Environment]::SetEnvironmentVariable(" + + "\"" + name + "\", \"" + value + "\", " + + "[System.EnvironmentVariableTarget]::User" + + ")'" + ) + + return + } + + mut filePath := process_home + path_SEP + ".zprofile" + + if !fs_existsSync(filePath) { + filePath = process_home + path_SEP + ".bash_profile" + } + + if !fs_existsSync(filePath) { + filePath = process_home + path_SEP + ".profile" + } + + mut content := fs_existsSync(filePath) ? fs_readFileSync(filePath).str() : "" + + if !content.empty && content[content.len - 1] != '\n' { + content += os_EOL + } + + content += "export " + name + "=\"" + value + "\" # Added by The CLI\n" + fs_writeFileSync(filePath, content.toBuffer()) +} + export fn scandirDeepSync (path: str) str[] { files := fs_scandirSync(path) filesLen := files.len