~siegfriedehret/brrr

2735c74c88574f1ace9013951a38a986172fc621 — Siegfried Ehret 2 years ago ca949a7
🎁 allow to upgrade a package (stupid version: uninstall and install)
M src/commands/install.nim => src/commands/install.nim +17 -18
@@ 1,6 1,5 @@
import std/[algorithm, httpclient, osproc, sequtils, strutils,
    sugar, tables, times]
import ../lib/[common, repository, stash, types, utils]
import std/[httpclient, osproc, strutils, tables, times]
import ../lib/[common, repository, stash, types]

proc getFilename(url: string): string =
  let splits = rsplit(url, "/", maxsplit = 1)


@@ 39,10 38,7 @@ proc installPackage(pkg: PkgTuple, pkgDef: BrrrFile): string =
  if pkg.ver != star:
    result = pkg.ver
  else:
    let logs = execCmdEx(pkgDef.get_tags)
    var tags = logs[0].split("\n").filter(x => x != "")
    tags.sort(semverCmp)
    result = tags[tags.len - 1]
    result = getLastVersion(pkgDef.get_tags)

  let hasVersion = pkgDef.versions.hasKey(result)
  let hasStarVersion = pkgDef.versions.hasKey(star)


@@ 60,16 56,19 @@ proc installPackage(pkg: PkgTuple, pkgDef: BrrrFile): string =
  else:
    raise brrrError("Can't find version")

proc install*(pkg: PkgTuple) =
  let timeStart = now()
  try:
    var (url, pkgDef) = downloadPackageDefinition(pkg)
    let version = installPackage(pkg, pkgDef)
    addPackageToConfig(pkg.name, version, url)
    let timeEnd = now()
    let duration = timeEnd - timeStart
    log($pkg.name & "@" & version & " installed successfully (" & $duration & ")")
  except BrrrError as error:
    log("Error while installing " & $pkg)
    log(error.message, 2)

proc install*(packages: seq[PkgTuple]) =
  for pkg in packages.items:
    let timeStart = now()
    try:
      var (url, pkgDef) = downloadPackageDefinition(pkg)
      let version = installPackage(pkg, pkgDef)
      addPackageToConfig(pkg.name, version, url)
      let timeEnd = now()
      let duration = timeEnd - timeStart
      log($pkg.name & "@" & version & " installed successfully (" & $duration & ")")
    except BrrrError as error:
      log("Error while installing " & $pkg)
      log(error.message, 2)
    install(pkg)

M src/commands/uninstall.nim => src/commands/uninstall.nim +16 -13
@@ 29,18 29,21 @@ proc uninstallPackage(pkg: PkgTuple, pkgDef: BrrrFile,
  removePackageDir(pkg.name)
  removePackageFromConfig(pkg.name)

proc uninstall*(pkg: PkgTuple) =
  if isInstalled(pkg.name):
    let timeStart = now()
    let (version, url) = getPackageConfig(pkg.name)
    var (_, pkgDef) = downloadPackageDefinition(pkg)

    uninstallPackage(pkg, pkgDef, version)

    let timeEnd = now()
    let duration = timeEnd - timeStart
    log($pkg.name & "@" & version & " uninstalled successfully (" &
        $duration & ")")
  else:
    log($pkg.name & " is not installed.")

proc uninstall*(packages: seq[PkgTuple]) =
  for pkg in packages.items:
    if isInstalled(pkg.name):
      let timeStart = now()
      let (version, url) = getPackageConfig(pkg.name)
      var (_, pkgDef) = downloadPackageDefinition(pkg)

      uninstallPackage(pkg, pkgDef, version)

      let timeEnd = now()
      let duration = timeEnd - timeStart
      log($pkg.name & "@" & version & " uninstalled successfully (" &
          $duration & ")")
    else:
      log($pkg.name & " is not installed.")
    uninstall(pkg)

A src/commands/upgrade.nim => src/commands/upgrade.nim +8 -0
@@ 0,0 1,8 @@
import ./install, ./uninstall, ../lib/[common, stash, types]

proc upgrade*(packages: seq[PkgTuple]) =
  for pkg in getInstalledPackages():
    # TODO only if new version available
    uninstall(pkg)
    let nextPkg: PkgTuple = (name: pkg.name, ver: star)
    install(nextPkg)

M src/lib/repository.nim => src/lib/repository.nim +8 -2
@@ 1,6 1,6 @@
import std/[httpclient, streams, strutils]
import std/[algorithm, httpclient, osproc, sequtils, streams, strutils, sugar]
import yaml/serialization
import ./common, ./types
import ./common, ./types, ./utils

const baseUrl = "https://nyrst.github.io/freezer/{brrr_package_name}.yaml"



@@ 20,3 20,9 @@ proc getLocalPackageDefinition*(file: string): BrrrFile =
  load(s, brrrFile)
  s.close()
  return brrrFile

proc getLastVersion*(cmd: string): string =
  let logs = execCmdEx(cmd)
  var tags = logs.output.split("\n").filter(x => x != "")
  tags.sort(semverCmp)
  result = tags[tags.len - 1]

M src/lib/stash.nim => src/lib/stash.nim +9 -3
@@ 1,5 1,5 @@
import std/[os, parsecfg]
import ./common
import ./common, ./types

const brrrHome = getConfigDir() / "brrr"



@@ 36,13 36,19 @@ proc getPackageConfig*(name: string): tuple[version: string, url: string] =
  var url = config.getSectionValue(name, "url")
  return (version, url)

proc getInstalledPackages*(): seq[PkgTuple] =
  result = newSeq[PkgTuple](0)
  var config = getBrrrConfig()
  for name in config.sections:
    var pkg: PkgTuple
    pkg = (name: name, ver: config.getSectionValue(name, "version"))
    result.add(pkg)

proc isInstalled*(name: string): bool =
  var config = getBrrrConfig()
  let version = config.getSectionValue(name, "version", "")
  return version != ""



proc removePackageFromConfig*(name: string) =
  var config = getBrrrConfig()
  config.delSection(name)

M src/options.nim => src/options.nim +8 -4
@@ 1,15 1,15 @@
import std/[parseopt, strutils]
import commands/[help, install, uninstall, version], lib/types
import commands/[help, install, uninstall, upgrade, version], lib/types

# Many lines come from nimble.

type
  ActionType* = enum
    actionNil, actionHelp, actionInstall, actionUninstall, actionVersion
    actionNil, actionHelp, actionInstall, actionUninstall, actionUpgrade, actionVersion
  Action* = object
    case typ*: ActionType
    of actionNil, actionHelp, actionVersion: nil
    of actionInstall, actionUninstall:
    of actionInstall, actionUninstall, actionUpgrade:
      packages*: seq[PkgTuple]
  Options* = object
    action*: Action


@@ 22,6 22,8 @@ proc parseActionType*(action: string): ActionType =
    result = actionInstall
  of "uninstall":
    result = actionUninstall
  of "upgrade":
    result = actionUpgrade
  of "version":
    result = actionVersion
  else:


@@ 36,7 38,7 @@ proc parseArgument*(key: string, result: var Options) =
    assert false
  of actionHelp, actionVersion:
    discard
  of actionInstall, actionUninstall:
  of actionInstall, actionUninstall, actionUpgrade:
    if '@' in key:
      let i = find(key, '@')
      let (pkgName, pkgVer) = (key[0 .. i-1], key[i+1 .. key.len-1])


@@ 52,6 54,8 @@ proc doAction*(options: var Options) =
      install(options.action.packages)
    of actionUninstall:
      uninstall(options.action.packages)
    of actionUpgrade:
      upgrade(options.action.packages)
    of actionVersion:
      writeVersion()