summary refs log tree commit diff stats
path: root/lib/std/private/nimbleutils.nim
blob: 297fb96958bbdce00be1d588abeabec5ef725adb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
##[
internal API for now, API subject to change
]##

import std/[os,osproc,sugar,strutils]

proc actionRetry*(maxRetry: int, backoffDuration: float, action: proc(): bool): bool =
  ## retry `action` up to `maxRetry` times with exponential backoff and initial
  ## duraton of `backoffDuration` seconds
  var t = backoffDuration
  for i in 0..<maxRetry:
    if action(): return true
    if i == maxRetry - 1: break
    sleep(int(t * 1000))
    t = t * 2 # exponential backoff
  return false

proc nimbleInstall*(name: string, message: var string): bool =
  let cmd = "nimble install -y " & name
  let (outp, status) = execCmdEx(cmd)
  if status != 0:
    message = "'$1' failed:\n$2" % [cmd, outp]
    result = false
  else: result = true

when isMainModule:
  block:
    var msg: string
    let ok = actionRetry(maxRetry = 2, backoffDuration = 0.1):
      (proc(): bool = nimbleInstall("nonexistant", msg))
    doAssert "Package not found" in msg
    doAssert not ok