yay/clean.go

249 lines
5.6 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/leonelquinteros/gotext"
"github.com/Jguer/go-alpm"
2020-07-10 00:36:45 +00:00
"github.com/Jguer/yay/v10/pkg/dep"
"github.com/Jguer/yay/v10/pkg/query"
2020-07-05 00:45:23 +00:00
"github.com/Jguer/yay/v10/pkg/settings"
"github.com/Jguer/yay/v10/pkg/stringset"
"github.com/Jguer/yay/v10/pkg/text"
)
// GetPkgbuild gets the pkgbuild of the package 'pkg' trying the ABS first and then the AUR trying the ABS first and then the AUR.
// RemovePackage removes package from VCS information
func removeVCSPackage(pkgs []string) {
updated := false
for _, pkgName := range pkgs {
2018-07-31 08:42:17 +00:00
if _, ok := savedInfo[pkgName]; ok {
delete(savedInfo, pkgName)
updated = true
}
}
if updated {
2020-07-05 01:26:00 +00:00
err := saveVCSInfo(config.Runtime.VCSPath)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
}
2017-10-14 16:11:47 +00:00
// CleanDependencies removes all dangling dependencies in system
2020-07-08 01:31:35 +00:00
func cleanDependencies(cmdArgs *settings.Arguments, alpmHandle *alpm.Handle, removeOptional bool) error {
hanging, err := hangingPackages(removeOptional, alpmHandle)
if err != nil {
return err
}
if len(hanging) != 0 {
2020-07-08 01:31:35 +00:00
return cleanRemove(cmdArgs, hanging)
}
2018-07-31 08:42:17 +00:00
return nil
}
// CleanRemove sends a full removal command to pacman with the pkgName slice
2020-07-08 01:31:35 +00:00
func cleanRemove(cmdArgs *settings.Arguments, pkgNames []string) error {
if len(pkgNames) == 0 {
return nil
}
2018-01-19 14:51:18 +00:00
2020-07-05 00:45:23 +00:00
arguments := cmdArgs.CopyGlobal()
_ = arguments.AddArg("R")
arguments.AddTarget(pkgNames...)
2018-07-31 08:42:17 +00:00
return show(passToPacman(arguments))
}
2020-07-08 01:22:01 +00:00
func syncClean(cmdArgs *settings.Arguments, alpmHandle *alpm.Handle) error {
keepInstalled := false
keepCurrent := false
2020-07-08 01:22:01 +00:00
_, removeAll, _ := cmdArgs.GetArg("c", "clean")
for _, v := range config.Runtime.PacmanConf.CleanMethod {
if v == "KeepInstalled" {
keepInstalled = true
} else if v == "KeepCurrent" {
keepCurrent = true
}
}
2020-07-05 00:45:23 +00:00
if config.Runtime.Mode == settings.ModeRepo || config.Runtime.Mode == settings.ModeAny {
2020-07-08 01:22:01 +00:00
if err := show(passToPacman(cmdArgs)); err != nil {
2018-07-24 01:49:45 +00:00
return err
}
}
2020-07-05 00:45:23 +00:00
if !(config.Runtime.Mode == settings.ModeAUR || config.Runtime.Mode == settings.ModeAny) {
2018-07-24 01:49:45 +00:00
return nil
}
var question string
if removeAll {
question = gotext.Get("Do you want to remove ALL AUR packages from cache?")
} else {
question = gotext.Get("Do you want to remove all other AUR packages from cache?")
}
fmt.Println(gotext.Get("\nBuild directory:"), config.BuildDir)
2020-07-10 22:48:30 +00:00
if text.ContinueTask(question, true, config.NoConfirm) {
if err := cleanAUR(keepInstalled, keepCurrent, removeAll, alpmHandle); err != nil {
return err
}
}
if removeAll {
return nil
}
2020-07-10 22:48:30 +00:00
if text.ContinueTask(gotext.Get("Do you want to remove ALL untracked AUR files?"), true, config.NoConfirm) {
2018-07-31 08:42:17 +00:00
return cleanUntracked()
}
2018-07-31 08:42:17 +00:00
return nil
}
func cleanAUR(keepInstalled, keepCurrent, removeAll bool, alpmHandle *alpm.Handle) error {
fmt.Println(gotext.Get("removing AUR packages from cache..."))
installedBases := make(stringset.StringSet)
inAURBases := make(stringset.StringSet)
remotePackages, _, err := query.GetRemotePackages(alpmHandle)
if err != nil {
return err
}
files, err := ioutil.ReadDir(config.BuildDir)
if err != nil {
return err
}
cachedPackages := make([]string, 0, len(files))
for _, file := range files {
if !file.IsDir() {
continue
}
cachedPackages = append(cachedPackages, file.Name())
}
// Most people probably don't use keep current and that is the only
// case where this is needed.
2018-05-12 16:16:48 +00:00
// Querying the AUR is slow and needs internet so don't do it if we
// don't need to.
if keepCurrent {
2020-07-10 00:36:45 +00:00
info, errInfo := query.AURInfo(cachedPackages, &query.AURWarnings{}, config.RequestSplitN)
if errInfo != nil {
return errInfo
}
for _, pkg := range info {
inAURBases.Set(pkg.PackageBase)
}
}
for _, pkg := range remotePackages {
if pkg.Base() != "" {
installedBases.Set(pkg.Base())
} else {
installedBases.Set(pkg.Name())
}
}
for _, file := range files {
if !file.IsDir() {
continue
}
if !removeAll {
if keepInstalled && installedBases.Get(file.Name()) {
continue
}
if keepCurrent && inAURBases.Get(file.Name()) {
continue
}
}
err = os.RemoveAll(filepath.Join(config.BuildDir, file.Name()))
if err != nil {
return nil
}
}
return nil
}
func cleanUntracked() error {
fmt.Println(gotext.Get("removing untracked AUR files from cache..."))
files, err := ioutil.ReadDir(config.BuildDir)
if err != nil {
return err
}
for _, file := range files {
if !file.IsDir() {
continue
}
dir := filepath.Join(config.BuildDir, file.Name())
if isGitRepository(dir) {
if err := show(passToGit(dir, "clean", "-fx")); err != nil {
return err
}
}
}
return nil
}
2018-09-27 13:10:36 +00:00
func isGitRepository(dir string) bool {
_, err := os.Stat(filepath.Join(dir, ".git"))
return !os.IsNotExist(err)
}
2020-07-10 00:36:45 +00:00
func cleanAfter(bases []dep.Base) {
2020-05-08 16:13:51 +00:00
fmt.Println(gotext.Get("removing untracked AUR files from cache..."))
2018-09-27 13:10:36 +00:00
for i, base := range bases {
dir := filepath.Join(config.BuildDir, base.Pkgbase())
if !isGitRepository(dir) {
continue
}
2018-09-27 13:10:36 +00:00
text.OperationInfoln(gotext.Get("Cleaning (%d/%d): %s", i+1, len(bases), cyan(dir)))
_, stderr, err := capture(passToGit(dir, "reset", "--hard", "HEAD"))
if err != nil {
text.Errorln(gotext.Get("error resetting %s: %s", base.String(), stderr))
}
2018-09-27 13:10:36 +00:00
if err := show(passToGit(dir, "clean", "-fx", "--exclude='*.pkg.*'")); err != nil {
fmt.Fprintln(os.Stderr, err)
2018-09-27 13:10:36 +00:00
}
}
}
2020-07-10 00:36:45 +00:00
func cleanBuilds(bases []dep.Base) {
2018-09-27 13:10:36 +00:00
for i, base := range bases {
dir := filepath.Join(config.BuildDir, base.Pkgbase())
text.OperationInfoln(gotext.Get("Deleting (%d/%d): %s", i+1, len(bases), cyan(dir)))
2018-09-27 13:10:36 +00:00
if err := os.RemoveAll(dir); err != nil {
fmt.Fprintln(os.Stderr, err)
2018-09-27 13:10:36 +00:00
}
}
}