From c6ad909074eb602cc3de0aa961bee425e1a625dd Mon Sep 17 00:00:00 2001 From: Siegfried Ehret Date: Wed, 29 Jun 2022 00:20:53 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=81=20update=20all=20the=20things?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 4 ++-- brrr.nimble | 6 +++--- src/brrr.nim | 2 +- src/commands/freezer.nim | 2 +- src/commands/help.nim | 12 +++++++----- src/commands/install.nim | 36 ++++++++++++++++++++---------------- src/commands/outdated.nim | 4 +++- src/commands/uninstall.nim | 7 ++++--- src/commands/upgrade.nim | 3 ++- src/commands/version.nim | 2 +- src/lib/common.nim | 13 +------------ src/lib/log.nim | 18 ++++++++++++++++++ src/lib/repository.nim | 5 +++-- src/lib/stash.nim | 2 +- src/lib/types.nim | 8 ++++++-- src/lib/utils.nim | 14 +++++++++----- src/options.nim | 26 +++++++++++++++++++------- 17 files changed, 101 insertions(+), 63 deletions(-) create mode 100644 src/lib/log.nim diff --git a/Makefile b/Makefile index 5fe7706..d0495be 100644 --- a/Makefile +++ b/Makefile @@ -10,8 +10,8 @@ format: ## Format files using nimpretty nimpretty src/**/*.nim run: build ## Run brrr - ./brrr freezer generate - #./brrr upgrade + #./brrr freezer generate + ./brrr upgrade #./brrr uninstall exa ;\ # ./brrr install exa diff --git a/brrr.nimble b/brrr.nimble index ee2878f..fb9d84d 100644 --- a/brrr.nimble +++ b/brrr.nimble @@ -1,8 +1,8 @@ # Package -version = "0.1.0" +version = "2022.06.28" author = "Siegfried Ehret" -description = "A new awesome nimble package" +description = "❄️ Yet another package manager" license = "MIT" srcDir = "src" bin = @["brrr"] @@ -10,6 +10,6 @@ bin = @["brrr"] # Dependencies -requires "nim >= 1.6.0" +requires "nim >= 1.6.6" requires "yaml >= 0.16.0" requires "semver >= 1.1.1" \ No newline at end of file diff --git a/src/brrr.nim b/src/brrr.nim index c00d4b6..a41c768 100644 --- a/src/brrr.nim +++ b/src/brrr.nim @@ -1,4 +1,4 @@ -import lib/[common, stash], options +import lib/[common, log, stash], options proc ctrlc() {.noconv.} = log("Bye!") diff --git a/src/commands/freezer.nim b/src/commands/freezer.nim index 80e0264..07c0334 100644 --- a/src/commands/freezer.nim +++ b/src/commands/freezer.nim @@ -1,5 +1,5 @@ import std/[os, parsecfg, sequtils, strutils, sugar, tables] -import ../lib/[common, repository, types] +import ../lib/[common, log, repository, types] const archs = @["linux", "macos"] diff --git a/src/commands/help.nim b/src/commands/help.nim index 795db36..593607f 100644 --- a/src/commands/help.nim +++ b/src/commands/help.nim @@ -3,14 +3,16 @@ import ../lib/common proc writeHelp*() = echo """ - brrr + brrr # Commands: - help Show this help - version Show the version - install Install a package + help Show this help + version Show the version - # TODO config info + install Install a package + uninstall Uninstall a package + upgrade Upgrade a package + outdated List outdated packages """.dedent() raise brrrQuit() diff --git a/src/commands/install.nim b/src/commands/install.nim index fcd4084..4712ea3 100644 --- a/src/commands/install.nim +++ b/src/commands/install.nim @@ -1,5 +1,5 @@ import std/[httpclient, osproc, strutils, tables, times] -import ../lib/[common, repository, stash, types] +import ../lib/[common, log, repository, stash, types] proc getFilename(url: string): string = let splits = rsplit(url, "/", maxsplit = 1) @@ -8,11 +8,8 @@ proc getFilename(url: string): string = proc downloadPackage(url, filename: string, targetDir: string = "/tmp/") = log("Downloading " & url) - try: - var client = newHttpClient() - writeFile(targetDir & "/" & filename, client.getContent(url)) - except IOError: - log("Failed to fetch package") + var client = newHttpClient() + writeFile(targetDir & "/" & filename, client.getContent(url)) proc downloadVersion(brrrFile: BrrrFile, packageVersion, installVersion, arch: string) = @@ -26,13 +23,14 @@ proc runScripts(pkgDef: BrrrFile, packageVersion, installVersion, arch: string) = let templateVersion = pkgDef.versions[installVersion] for script in pkgDef.templates[templateVersion][arch].install: - let runScript = script.multiReplace( + let runScript = script.command.multiReplace( ("{brrr_package_version}", packageVersion), ("{brrr_pkgdir}", getPackageDir(pkgDef.name)), ("{brrr_bin}", getBrrrBinDir())) - log("Running script: " & runScript) + log(script.name & "...") + log(runScript, 2, true) let logs = execCmdEx(runScript, workingDir = getPackageDir(pkgDef.name)) - log(logs[0], 2) + log(logs[0], 2, true) proc installPackage*(pkg: PkgTuple, pkgDef: BrrrFile): string = if pkg.ver != star: @@ -46,13 +44,19 @@ proc installPackage*(pkg: PkgTuple, pkgDef: BrrrFile): string = createPackageDir(pkg.getName()) if hasVersion: - downloadVersion(pkgDef, result, - result, osArch) - runScripts(pkgDef, result, result, osArch) + try: + downloadVersion(pkgDef, result, + result, osArch) + runScripts(pkgDef, result, result, osArch) + except IOError: + log("Failed to fetch package") elif hasStarVersion: - downloadVersion(pkgDef, result, - star, osArch) - runScripts(pkgDef, result, star, osArch) + try: + downloadVersion(pkgDef, result, + star, osArch) + runScripts(pkgDef, result, star, osArch) + except IOError: + log("Failed to fetch package") else: raise brrrError("Can't find version") @@ -60,7 +64,6 @@ proc install*(pkg: PkgTuple) = let timeStart = now() try: # TODO handle http(s) links (not freezer) and local packages - echo pkg.name var (name, url, pkgDef) = if pkg.name.startsWith("./"): getLocalPackageDefinition(pkg.name) else: @@ -78,4 +81,5 @@ proc install*(pkg: PkgTuple) = proc install*(packages: seq[PkgTuple]) = for pkg in packages.items: + # TODO check if already installed install(pkg) diff --git a/src/commands/outdated.nim b/src/commands/outdated.nim index af8664a..124cd14 100644 --- a/src/commands/outdated.nim +++ b/src/commands/outdated.nim @@ -1,4 +1,4 @@ -import ../lib/[common, repository, stash, types, utils] +import ../lib/[log, repository, stash, types, utils] proc outdated*() = for pkg in getInstalledPackages(): @@ -6,3 +6,5 @@ proc outdated*() = let lastVersion = getLastVersion(pkgDef.get_tags) if semverCmp(pkg.ver, lastVersion) == -1: log(name & " is outdated (" & pkg.ver & " => " & lastVersion & ")") + else: + log(name & " is up to date (" & pkg.ver & ")") diff --git a/src/commands/uninstall.nim b/src/commands/uninstall.nim index 8c0f203..2ec8414 100644 --- a/src/commands/uninstall.nim +++ b/src/commands/uninstall.nim @@ -1,15 +1,16 @@ import std/[osproc, strutils, tables, times] -import ../lib/[common, repository, stash, types] +import ../lib/[common, log, repository, stash, types] proc runScripts(pkgDef: BrrrFile, packageVersion, installedVersion, arch: string) = let templateVersion = pkgDef.versions[installedVersion] for script in pkgDef.templates[templateVersion][arch].uninstall: - let runScript = script.multiReplace( + let runScript = script.command.multiReplace( ("{brrr_package_version}", packageVersion), ("{brrr_pkgdir}", getPackageDir(pkgDef.name)), ("{brrr_bin}", getBrrrBinDir())) - log("Running script: " & runScript) + log(script.name & "...") + log(runScript, 2) let logs = execCmdEx(runScript, workingDir = getPackageDir(pkgDef.name)) log(logs[0], 2) diff --git a/src/commands/upgrade.nim b/src/commands/upgrade.nim index f036fc6..4184da5 100644 --- a/src/commands/upgrade.nim +++ b/src/commands/upgrade.nim @@ -1,5 +1,5 @@ import std/[times] -import ./install, ./uninstall, ../lib/[common, repository, stash, types, utils] +import ./install, ./uninstall, ../lib/[common, log, repository, stash, types, utils] proc upgrade*(packages: seq[PkgTuple]) = for pkg in getInstalledPackages(): @@ -16,4 +16,5 @@ proc upgrade*(packages: seq[PkgTuple]) = let timeEnd = now() let duration = timeEnd - timeStart log(name & "@" & version & " upgraded successfully (" & $duration & ")") + log("❄ ❄ ❄ ❄ ❄ ❄ ❄ ❄ ❄ ❄") diff --git a/src/commands/version.nim b/src/commands/version.nim index fe46836..5fe8c2c 100644 --- a/src/commands/version.nim +++ b/src/commands/version.nim @@ -1,4 +1,4 @@ -import ../lib/common +import ../lib/[common, log] proc writeVersion*() = log("brrr version " & brrrVersion) diff --git a/src/lib/common.nim b/src/lib/common.nim index c9ae8b0..313eddd 100644 --- a/src/lib/common.nim +++ b/src/lib/common.nim @@ -1,6 +1,4 @@ -import std/strutils - -const brrrVersion* = "0.1.0" +const brrrVersion* = "2011.11.30" const star* = "*" @@ -22,15 +20,6 @@ proc brrrQuit*(exitCode = QuitSuccess): ref BrrrQuit = result = newException(BrrrQuit, "") result.exitCode = exitCode -proc log*(message: string, padding = 0) = - var msg = message - stripLineEnd(msg) - if msg.len > 0: - if padding == 0: - echo indent("❄️ " & msg, padding) - else: - echo indent(msg, padding) - proc getOS*(): string = return # when defined(windows): "windows" diff --git a/src/lib/log.nim b/src/lib/log.nim new file mode 100644 index 0000000..a4f6992 --- /dev/null +++ b/src/lib/log.nim @@ -0,0 +1,18 @@ +import std/[strutils] + +var verbose* = false + +proc setVerbose*(val: bool) = + verbose = val + +proc log*(message: string, padding = 0, onlyVerbose = false) = + if onlyVerbose == true and verbose == false: + return + + var msg = message + stripLineEnd(msg) + if msg.len > 0: + if padding == 0: + echo indent("❄️ " & msg, padding) + else: + echo indent(msg, padding) diff --git a/src/lib/repository.nim b/src/lib/repository.nim index 9d5a1a7..d3c77c5 100644 --- a/src/lib/repository.nim +++ b/src/lib/repository.nim @@ -1,8 +1,9 @@ import std/[algorithm, httpclient, os, osproc, sequtils, streams, strutils, sugar] import yaml/serialization -import ./common, ./types, ./utils +import ./log, ./types, ./utils -const baseUrl = "https://nyrst.github.io/freezer/{brrr_package_name}.yaml" +# const baseUrl = "https://nyrst.github.io/freezer/{brrr_package_name}.yaml" +const baseUrl = "https://git.sr.ht/~siegfriedehret/freezer/blob/main/{brrr_package_name}.yaml" proc downloadPackageDefinition*(pkg: PkgTuple, quiet = false): (string, string, BrrrFile) = let url = baseUrl.replace("{brrr_package_name}", pkg.name) diff --git a/src/lib/stash.nim b/src/lib/stash.nim index 400c4ce..de0d0ac 100644 --- a/src/lib/stash.nim +++ b/src/lib/stash.nim @@ -1,5 +1,5 @@ import std/[os, parsecfg] -import ./common, ./types +import ./log, ./types const brrrHome = getConfigDir() / "brrr" diff --git a/src/lib/types.nim b/src/lib/types.nim index 0297704..d84a284 100644 --- a/src/lib/types.nim +++ b/src/lib/types.nim @@ -1,9 +1,13 @@ import std/[strutils, tables] +type BrrrScript = object + name*: string + command*: string + type BrrrTemplate = object url*: string - install*: seq[string] - uninstall*: seq[string] + install*: seq[BrrrScript] + uninstall*: seq[BrrrScript] type BrrrTemplates* = Table[string, Table[string, BrrrTemplate]] diff --git a/src/lib/utils.nim b/src/lib/utils.nim index 960b924..9393ee9 100644 --- a/src/lib/utils.nim +++ b/src/lib/utils.nim @@ -1,8 +1,12 @@ +import std/strutils import semver proc semverCmp*(a, b: string): int = - let aVersion = parseVersion(a) - let bVersion = parseVersion(b) - if aVersion == bVersion: 0 - elif aVersion > bVersion: 1 - else: -1 + try: + let aVersion = parseVersion(a) + let bVersion = parseVersion(b) + if aVersion == bVersion: 0 + elif aVersion > bVersion: 1 + else: -1 + except ParseError: + cmpIgnoreCase(a, b) diff --git a/src/options.nim b/src/options.nim index 61cfa9f..9c933bd 100644 --- a/src/options.nim +++ b/src/options.nim @@ -1,7 +1,8 @@ import std/[parseopt, strutils] -import commands/[freezer, help, install, outdated, uninstall, upgrade, version], lib/types +import commands/[freezer, help, install, outdated, uninstall, upgrade, version], lib/[log, types] # Many lines come from nimble. +# https://github.com/nim-lang/nimble/blob/master/src/nimblepkg/options.nim type ActionType* = enum @@ -15,6 +16,7 @@ type packages*: seq[PkgTuple] Options* = object action*: Action + verbose*: bool proc parseActionType*(action: string): ActionType = case action.normalize() @@ -22,13 +24,13 @@ proc parseActionType*(action: string): ActionType = result = actionFreezer of "help": result = actionHelp - of "install": + of "install", "i": result = actionInstall of "outdated": result = actionOutdated - of "uninstall": + of "uninstall", "u": result = actionUninstall - of "upgrade": + of "upgrade", "up": result = actionUpgrade of "version": result = actionVersion @@ -55,6 +57,8 @@ proc parseArgument*(key: string, result: var Options) = result.action.packages.add((key, "*")) proc doAction*(options: var Options) = + setVerbose(options.verbose) + case options.action.typ of actionHelp, actionNil: writeHelp() @@ -71,6 +75,16 @@ proc doAction*(options: var Options) = of actionVersion: writeVersion() +proc parseFlag*(flag, val: string, result: var Options, kind = cmdLongOption) = + + let f = flag.normalize() + + # Global flags. + var isGlobalFlag = true + case f + of "v": result.verbose = true + else: isGlobalFlag = false + proc initOptions*(): Options = Options( action: Action(typ: actionNil), @@ -79,8 +93,6 @@ proc initOptions*(): Options = proc parseCmdLine*(): Options = result = initOptions() - # Parse command line params first. A simple `--version` shouldn't require - # a config to be parsed. for kind, key, val in getOpt(): case kind of cmdArgument: @@ -89,5 +101,5 @@ proc parseCmdLine*(): Options = else: parseArgument(key, result) of cmdLongOption, cmdShortOption: - discard + parseFlag(key, val, result, kind) of cmdEnd: assert(false) # cannot happen \ No newline at end of file -- 2.45.2