From 332653b6827821420a8e104fcfcfa0b770bfa831 Mon Sep 17 00:00:00 2001 From: Christopher Arndt Date: Thu, 26 Sep 2024 08:26:17 +0200 Subject: [PATCH] feat: update proxy binaries on self-update Signed-off-by: Christopher Arndt --- src/choosenim.nim | 4 ++++ src/choosenimpkg/switcher.nim | 44 +++++++++++++++++++++-------------- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/choosenim.nim b/src/choosenim.nim index 3306e01..dd6c347 100644 --- a/src/choosenim.nim +++ b/src/choosenim.nim @@ -167,6 +167,10 @@ proc updateSelf(params: CliParams) = display("Info:", "Updated choosenim to version " & $version, Success, HighPriority) + # Any Nim installation currently activated by choosenim? + if $getCurrentVersion(params) != "": + discard installProxies(params) + proc update(params: CliParams) = if params.commands.len != 2: raise newException(ChooseNimError, diff --git a/src/choosenimpkg/switcher.nim b/src/choosenimpkg/switcher.nim index 97eb1f8..83d82d4 100644 --- a/src/choosenimpkg/switcher.nim +++ b/src/choosenimpkg/switcher.nim @@ -232,6 +232,31 @@ proc writeProxy(bin: string, params: CliParams) = display("Hint:", "Ensure that '$1' is before '$2' in the PATH env var." % [params.getBinDir(), fromPATH.splitFile.dir], Warning, HighPriority) +proc installProxies*(params: CliParams): bool = + ## Install or update proxy executables. + ## + ## Returns `true` when any proxies were newly installed or updated, + ## `false` otherwise. + var proxiesToInstall = @proxies + + # Handle MingW proxies. + when defined(windows): + if not isDefaultCCInPath(params): + let mingwBin = getMingwBin(params) + if not fileExists(mingwBin / "gcc".addFileExt(ExeExt)): + let msg = "No 'gcc' binary found in '$1'." % mingwBin + raise newException(ChooseNimError, msg) + + proxiesToInstall.add(mingwProxies) + + if not params.areProxiesInstalled(proxiesToInstall): + # Create the proxy executables. + for proxy in proxiesToInstall: + writeProxy(proxy, params) + return true + + return false + proc switchToPath(filepath: string, params: CliParams): bool = ## Switches to the specified file path that should point to the root of ## the Nim repo. @@ -254,30 +279,15 @@ proc switchToPath(filepath: string, params: CliParams): bool = "running `choosenim \"#v0.16.0\"` or similar.", Warning, HighPriority) - var proxiesToInstall = @proxies - # Handle MingW proxies. - when defined(windows): - if not isDefaultCCInPath(params): - let mingwBin = getMingwBin(params) - if not fileExists(mingwBin / "gcc".addFileExt(ExeExt)): - let msg = "No 'gcc' binary found in '$1'." % mingwBin - raise newException(ChooseNimError, msg) - - proxiesToInstall.add(mingwProxies) - # Return early if this version is already selected. let selectedPath = params.getSelectedPath() - let proxiesInstalled = params.areProxiesInstalled(proxiesToInstall) - if selectedPath == filepath and proxiesInstalled: + + if selectedPath == filepath and not installProxies(params): return false else: # Write selected path to "current file". writeFile(params.getCurrentFile(), filepath) - # Create the proxy executables. - for proxy in proxiesToInstall: - writeProxy(proxy, params) - when defined(windows): if not isNimbleBinInPath(params): display("Hint:", "Use 'choosenim --firstInstall' to add\n" &