From f227d1088f6a8aee3654804b5fd0110e15532c5b Mon Sep 17 00:00:00 2001 From: kitsunyan Date: Thu, 7 Jun 2018 02:35:28 +0300 Subject: Add support for "-Sc" to clean package repositories --- src/common.nim | 13 ++++++++++++ src/feature/syncclean.nim | 51 +++++++++++++++++++++++++++++++++++++++++++++ src/feature/syncinstall.nim | 7 +------ src/main.nim | 7 +++++-- 4 files changed, 70 insertions(+), 8 deletions(-) create mode 100644 src/feature/syncclean.nim diff --git a/src/common.nim b/src/common.nim index 1749ed7..36aae38 100644 --- a/src/common.nim +++ b/src/common.nim @@ -53,6 +53,13 @@ proc checkAndRefresh*(color: bool, args: seq[Argument]): tuple[code: int, args: else: (0, args) +proc noconfirm*(args: seq[Argument]): bool = + args + .filter(arg => arg.matchOption(%%%"confirm") or + arg.matchOption(%%%"noconfirm")).optLast + .map(arg => arg.key == "noconfirm").get(false) or + args.check(%%%"ask") + proc packageTargets*(args: seq[Argument], parseDestination: bool): seq[PackageTarget] = args.targets.map(target => (block: let (noDestinationTarget, destination) = if parseDestination: (block: @@ -451,6 +458,12 @@ proc reloadPkgInfos*(config: Config, path: string, pkgInfos: seq[PackageInfo]): template bareFullName*(bareKind: BareKind, bareName: string): string = $bareKind & "-" & bareName & ".git" +proc bareFullNameDeconstruct*(bareKind: BareKind, bareFullName: string): Option[string] = + if bareFullName.find($bareKind & '-') == 0 and bareFullName.rfind(".git") == bareFullName.len - 4: + some(bareFullName[($bareKind).len + 1 .. ^5]) + else: + none(string) + proc cloneBareRepo(config: Config, bareKind: BareKind, bareName: string, url: string, branchOption: Option[string], dropPrivileges: bool): Option[string] = let fullName = bareFullName(bareKind, bareName) diff --git a/src/feature/syncclean.nim b/src/feature/syncclean.nim new file mode 100644 index 0000000..2e57908 --- /dev/null +++ b/src/feature/syncclean.nim @@ -0,0 +1,51 @@ +import + future, options, os, posix, sequtils, strutils, tables, + "../args", "../aur", "../common", "../config", "../format", "../lists", + "../package", "../pacman", "../utils", + "../wrapper/alpm" + +proc handleSyncClean*(args: seq[Argument], config: Config): int = + let code = pacmanRun(true, config.color, args) + if code != 0: + code + else: + let cleanCount = args.count(%%%"clean") + let noconfirm = args.noconfirm + + let reposCacheDir = config.userCacheCurrent.cache(CacheKind.repositories) + let homeDir = currentUser.home + let printReposCacheDir = if reposCacheDir.find(homeDir & "/") == 0: + '~' & reposCacheDir[homeDir.len .. ^1] + else: + reposCacheDir + echo() + echo(tr"Repositories directory: $#" % [printReposCacheDir]) + + if (cleanCount == 1 and + printColonUserChoice(config.color, tr"Do you want to remove unused repositories?", + ['y', 'n'], 'y', 'n', noconfirm, 'y') == 'y') or + (cleanCount >= 2 and + printColonUserChoice(config.color, tr"Do you want to remove ALL repositories?", + ['y', 'n'], 'n', 'n', noconfirm, 'n') == 'y'): + if cleanCount == 1: + echo(tr"removing unused package repositories...") + else: + echo(tr"removing all package repositories...") + + if existsDir(reposCacheDir): + withAlpmConfig(config, false, handle, dbs, errors): + for e in errors: printError(config.color, e) + + let local = handle.local + for file in walkDir(reposCacheDir): + if file.kind == pcDir: + let index = file.path.rfind('/') + let dirName = if index >= 0: file.path[index + 1 .. ^1] else: file.path + for name in bareFullNameDeconstruct(BareKind.pkg, dirName): + if cleanCount >= 2 or local[name] == nil: + removeDirQuiet(file.path) + + discard rmdir(reposCacheDir) + discard rmdir(config.userCacheCurrent) + + 0 diff --git a/src/feature/syncinstall.nim b/src/feature/syncinstall.nim index 1ca1083..7d638cc 100644 --- a/src/feature/syncinstall.nim +++ b/src/feature/syncinstall.nim @@ -1324,12 +1324,7 @@ proc handleSyncInstall*(args: seq[Argument], config: Config): int = else: none(string) - let noconfirm = args - .filter(arg => arg.matchOption(%%%"confirm") or - arg.matchOption(%%%"noconfirm")).optLast - .map(arg => arg.key == "noconfirm").get(false) or - args.check(%%%"ask") - + let noconfirm = args.noconfirm let targets = args.packageTargets(false) withAur(): diff --git a/src/main.nim b/src/main.nim index 54fbb08..a149000 100644 --- a/src/main.nim +++ b/src/main.nim @@ -3,11 +3,12 @@ import args, config, format, pacman, utils import + "feature/localquery", + "feature/syncclean", "feature/syncinfo", "feature/syncinstall", "feature/syncsearch", - "feature/syncsource", - "feature/localquery" + "feature/syncsource" proc execSudo*(args: seq[Argument]): int = execResult(sudoPrefix & getAppFilename() & @@ -81,6 +82,8 @@ proc handleSync(args: seq[Argument], config: Config): int = printError(config.color, trp("invalid option: '%s' and '%s' may not be used together\n") % ["--" & left, "--" & right]) 1 + elif syncArgs.check(%%%"clean"): + handleSyncClean(args, config) elif syncArgs.check(%%%"info") and syncArgs.checkOpGroup(OpGroup.syncQuery): handleSyncInfo(args, config) -- cgit v1.2.3-70-g09d2