Fix - add ~/.nimble/bin to PATH on Windows ()

* Fix  - add ~/.nimble/bin to PATH on Windows

* Minor message changes

* Incorporate feedback

* Fix  - add ~/.nimble/bin to PATH on Windows

* Minor message changes

* Windows fixes

* Cleanup
This commit is contained in:
genotrance 2020-02-08 16:27:29 -06:00 committed by GitHub
parent defad989b2
commit 25f8648355
7 changed files with 106 additions and 14 deletions

View file

@ -9,6 +9,9 @@ from nimblepkg/packageinfo import getNameVersion
import choosenimpkg/[download, builder, switcher, common, cliparams, versions]
import choosenimpkg/[utils, channel, telemetry]
when defined(windows):
import choosenimpkg/env
proc installVersion(version: Version, params: CliParams) =
# Install the requested version.
let path = download(version, params)
@ -69,6 +72,11 @@ proc choose(params: CliParams) =
else:
chooseVersion(params.command, params)
when defined(windows):
# Check and add ~/.nimble/bin to PATH
if not isNimbleBinInPath(params) and params.firstInstall:
setNimbleBinPath(params)
proc updateSelf(params: CliParams) =
display("Updating", "choosenim", priority = HighPriority)

View file

@ -3,7 +3,10 @@ import os, times
import nimblepkg/[version, cli]
import nimblepkg/common as nimble_common
import cliparams, download, utils, common, switcher, telemetry
import cliparams, download, utils, common, telemetry
when defined(windows):
import switcher
proc buildFromCSources(params: CliParams) =
when defined(windows):

View file

@ -99,13 +99,8 @@ proc getCurrentChannelFile*(params: CliParams): string =
proc getAnalyticsFile*(params: CliParams): string =
return params.chooseNimDir / "analytics"
var cpuArch = 0
proc getCpuArch*(): int =
## Get CPU arch on Windows - get env var PROCESSOR_ARCHITECTURE
if cpuArch != 0:
return cpuArch
var failMsg = ""
let
@ -128,9 +123,6 @@ proc getCpuArch*(): int =
raise newException(ChooseNimError,
"Could not detect CPU architecture: " & failMsg)
# Only once
cpuArch = result
proc getMingwPath*(params: CliParams): string =
let arch = getCpuArch()
return params.getInstallDir() / "mingw" & $arch

View file

@ -1,4 +1,7 @@
import httpclient, strutils, os, terminal, times, math, json, uri
import httpclient, strutils, os, terminal, times, json, uri
when defined(macosx):
import math
import nimblepkg/[version, cli]
when defined(curl):

64
src/choosenimpkg/env.nim Normal file
View file

@ -0,0 +1,64 @@
import os, strutils
import nimblepkg/[cli, options]
import cliparams
when defined(windows):
# From finish.nim in nim-lang/Nim/tools
import registry
proc tryGetUnicodeValue(path, key: string, handle: HKEY): string =
# Get a unicode value from the registry or ""
try:
result = getUnicodeValue(path, key, handle)
except:
result = ""
proc addToPathEnv(path: string) =
# Append path to user PATH to registry
var paths = tryGetUnicodeValue(r"Environment", "Path", HKEY_CURRENT_USER)
let path = if path.contains(Whitespace): "\"" & path & "\"" else: path
if paths.len > 0:
if paths[^1] != PathSep:
paths.add PathSep
paths.add path
else:
paths = path
setUnicodeValue(r"Environment", "Path", paths, HKEY_CURRENT_USER)
proc setNimbleBinPath*(params: CliParams) =
# Ask the user and add nimble bin to PATH
let nimbleDesiredPath = params.getBinDir()
if prompt(params.nimbleOptions.forcePrompts,
nimbleDesiredPath & " is not in your PATH environment variable.\n" &
" Should it be added permanently?"):
addToPathEnv(nimbleDesiredPath)
display("Note:", "PATH changes will only take effect in new sessions.",
priority = HighPriority)
proc isNimbleBinInPath*(params: CliParams): bool =
# This proc searches the $PATH variable for the nimble bin directory,
# typically ~/.nimble/bin
result = false
let nimbleDesiredPath = params.getBinDir()
when defined(windows):
# Getting PATH from registry since it is the ultimate source of
# truth and session local $PATH can be changed.
let paths = tryGetUnicodeValue(r"Environment", "Path",
HKEY_CURRENT_USER) & PathSep & tryGetUnicodeValue(
r"System\CurrentControlSet\Control\Session Manager\Environment", "Path",
HKEY_LOCAL_MACHINE)
else:
let paths = getEnv("PATH")
for path in paths.split(PathSep):
if path.len == 0: continue
let path = path.strip(chars = {'"'})
let expandedPath =
try:
expandFilename(path)
except:
""
if expandedPath.cmpIgnoreCase(nimbleDesiredPath) == 0:
result = true
break

View file

@ -5,6 +5,9 @@ from nimblepkg/packageinfo import getNameVersion
import cliparams, common
when defined(windows):
import env
proc compileProxyexe() =
var cmd = "nim c"
when defined(release):
@ -118,7 +121,13 @@ proc writeProxy(bin: string, params: CliParams) =
# Don't write the file again if it already exists.
if fileExists(proxyPath) and readFile(proxyPath) == proxyExe: return
writeFile(proxyPath, proxyExe)
try:
writeFile(proxyPath, proxyExe)
except IOError:
display("Warning:", "component '$1' possibly in use, write failed" % bin, Warning,
priority = HighPriority)
return
# Make sure the exe has +x flag.
setFilePermissions(proxyPath,
getFilePermissions(proxyPath) + {fpUserExec})
@ -127,8 +136,13 @@ proc writeProxy(bin: string, params: CliParams) =
# Check whether this is in the user's PATH.
let fromPATH = findExe(bin)
if fromPATH == "" and not params.firstInstall:
display("Hint:", "Binary '$1' isn't in your PATH. Add '$2' to your PATH." %
[bin, params.getBinDir()], Warning, HighPriority)
let msg =
when defined(windows):
"Binary '$1' isn't in your PATH" % bin
else:
"Binary '$1' isn't in your PATH. Ensure that '$2' is in your PATH." %
[bin, params.getBinDir()]
display("Hint:", msg, Warning, HighPriority)
elif fromPATH != "" and fromPATH != proxyPath:
display("Warning:", "Binary '$1' is shadowed by '$2'." %
[bin, fromPATH], Warning, HighPriority)
@ -181,6 +195,11 @@ proc switchToPath(filepath: string, params: CliParams): bool =
for proxy in proxiesToInstall:
writeProxy(proxy, params)
when defined(windows):
if not isNimbleBinInPath(params):
display("Hint:", "Use 'choosenim <version/channel> --firstInstall' to add\n" &
"$1 to your PATH." % params.getBinDir(), Warning, HighPriority)
proc switchTo*(version: Version, params: CliParams) =
## Switches to the specified version by writing the appropriate proxy
## into $nimbleDir/bin.

View file

@ -3,7 +3,10 @@ import httpclient, os, strutils, osproc, uri
import nimblepkg/[cli, version]
import nimarchive
import switcher, cliparams, common
import cliparams, common
when defined(windows):
import switcher
proc parseVersion*(versionStr: string): Version =
if versionStr[0] notin {'#', '\0'} + Digits: