2021-10-10 21:59:55 +00:00
|
|
|
// Clean Build Menu functions
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/leonelquinteros/gotext"
|
|
|
|
|
|
|
|
"github.com/Jguer/yay/v11/pkg/dep"
|
|
|
|
"github.com/Jguer/yay/v11/pkg/intrange"
|
|
|
|
"github.com/Jguer/yay/v11/pkg/settings"
|
|
|
|
"github.com/Jguer/yay/v11/pkg/stringset"
|
|
|
|
"github.com/Jguer/yay/v11/pkg/text"
|
|
|
|
)
|
|
|
|
|
2021-10-10 22:10:40 +00:00
|
|
|
func cleanNumberMenu(bases []dep.Base, installed stringset.StringSet) ([]dep.Base, error) {
|
2021-10-10 21:59:55 +00:00
|
|
|
toClean := make([]dep.Base, 0)
|
|
|
|
|
|
|
|
text.Infoln(gotext.Get("Packages to cleanBuild?"))
|
|
|
|
text.Infoln(gotext.Get("%s [A]ll [Ab]ort [I]nstalled [No]tInstalled or (1 2 3, 1-3, ^4)", text.Cyan(gotext.Get("[N]one"))))
|
|
|
|
|
|
|
|
cleanInput, err := getInput(config.AnswerClean)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cInclude, cExclude, cOtherInclude, cOtherExclude := intrange.ParseNumberMenu(cleanInput)
|
|
|
|
cIsInclude := len(cExclude) == 0 && len(cOtherExclude) == 0
|
|
|
|
|
|
|
|
if cOtherInclude.Get("abort") || cOtherInclude.Get("ab") {
|
|
|
|
return nil, settings.ErrUserAbort{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !cOtherInclude.Get("n") && !cOtherInclude.Get("none") {
|
|
|
|
for i, base := range bases {
|
|
|
|
pkg := base.Pkgbase()
|
2021-10-10 22:18:57 +00:00
|
|
|
anyInstalled := base.AnyIsInSet(installed)
|
2021-10-10 21:59:55 +00:00
|
|
|
|
|
|
|
dir := filepath.Join(config.BuildDir, pkg)
|
|
|
|
if _, err := os.Stat(dir); os.IsNotExist(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !cIsInclude && cExclude.Get(len(bases)-i) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if anyInstalled && (cOtherInclude.Get("i") || cOtherInclude.Get("installed")) {
|
|
|
|
toClean = append(toClean, base)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !anyInstalled && (cOtherInclude.Get("no") || cOtherInclude.Get("notinstalled")) {
|
|
|
|
toClean = append(toClean, base)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if cOtherInclude.Get("a") || cOtherInclude.Get("all") {
|
|
|
|
toClean = append(toClean, base)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if cIsInclude && (cInclude.Get(len(bases)-i) || cOtherInclude.Get(pkg)) {
|
|
|
|
toClean = append(toClean, base)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !cIsInclude && (!cExclude.Get(len(bases)-i) && !cOtherExclude.Get(pkg)) {
|
|
|
|
toClean = append(toClean, base)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return toClean, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func anyExistInCache(bases []dep.Base) bool {
|
|
|
|
for _, base := range bases {
|
|
|
|
pkg := base.Pkgbase()
|
|
|
|
dir := filepath.Join(config.BuildDir, pkg)
|
|
|
|
|
|
|
|
if _, err := os.Stat(dir); !os.IsNotExist(err) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanMenu(cleanMenuOption bool, aurBases []dep.Base, installed stringset.StringSet) error {
|
|
|
|
if !(cleanMenuOption && anyExistInCache(aurBases)) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-10 22:10:40 +00:00
|
|
|
pkgbuildNumberMenu(aurBases, installed)
|
2021-10-10 21:59:55 +00:00
|
|
|
|
2021-10-10 22:10:40 +00:00
|
|
|
toClean, errClean := cleanNumberMenu(aurBases, installed)
|
2021-10-10 21:59:55 +00:00
|
|
|
if errClean != nil {
|
|
|
|
return errClean
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, base := range toClean {
|
|
|
|
dir := filepath.Join(config.BuildDir, base.Pkgbase())
|
|
|
|
text.OperationInfoln(gotext.Get("Deleting (%d/%d): %s", i+1, len(toClean), text.Cyan(dir)))
|
|
|
|
|
|
|
|
if err := os.RemoveAll(dir); err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|