diff --git a/src/babel.nim b/src/babel.nim index f5ff823..e174987 100644 --- a/src/babel.nim +++ b/src/babel.nim @@ -6,6 +6,9 @@ import httpclient, parseopt, os, strutils, osproc, pegs, tables, parseutils, import babelpkg/packageinfo, babelpkg/version, babelpkg/tools, babelpkg/download +when not defined(windows): + from posix import getpid + type TOptions = object forcePrompts: TForcePrompt @@ -388,17 +391,32 @@ proc installFromDir(dir: string, latest: bool, options: TOptions, echo(pkgInfo.name & " installed successfully.") +proc getBabelTempDir(): string = + ## Returns a path to a temporary directory. + ## + ## The returned path will be the same for the duration of the process but + ## different for different runs of it. You have to make sure to create it + ## first. In release builds the directory will be removed when babel finishes + ## its work. + result = getTempDir() / "babel_" + when defined(windows): + proc GetCurrentProcessId(): int32 {.stdcall, dynlib: "kernel32", + importc: "GetCurrentProcessId".} + result.add($GetCurrentProcessId()) + else: + result.add($getpid()) + proc downloadPkg(url: string, verRange: PVersionRange, downMethod: TDownloadMethod): string = - let downloadDir = (getTempDir() / "babel" / getDownloadDirName(url, verRange)) - if not existsDir(getTempDir() / "babel"): createDir(getTempDir() / "babel") + let downloadDir = (getBabelTempDir() / getDownloadDirName(url, verRange)) + createDir(downloadDir.extractFilename) echo("Downloading ", url, " into ", downloadDir, " using ", downMethod, "...") doDownload(url, downloadDir, verRange, downMethod) result = downloadDir proc downloadPkg(pkg: TPackage, verRange: PVersionRange): string = - let downloadDir = (getTempDir() / "babel" / getDownloadDirName(pkg, verRange)) - if not existsDir(getTempDir() / "babel"): createDir(getTempDir() / "babel") + let downloadDir = (getBabelTempDir() / getDownloadDirName(pkg, verRange)) + createDir(downloadDir.extractFilename) let downMethod = pkg.downloadMethod.getDownloadMethod() echo("Downloading ", pkg.name, " into ", downloadDir, " using ", downMethod, "...") doDownload(pkg.url, downloadDir, verRange, downMethod) @@ -432,7 +450,8 @@ proc install(packages: seq[tuple[name: string, verRange: PVersionRange]], else: # If package is not found give the user a chance to update package.json if doPrompt and - options.prompt(pv.name & " not found in local packages.json, check internet for updated packages?"): + options.prompt(pv.name & " not found in local packages.json, " & + "check internet for updated packages?"): update() install(@[pv], options, false) else: @@ -561,5 +580,7 @@ when isMainModule: parseCmdLine().doAction() except EBabel: quit("FAILURE: " & getCurrentExceptionMsg()) + finally: + removeDir(getBabelTempDir()) else: parseCmdLine().doAction()