2017-08-02 19:24:03 +02:00
|
|
|
package main
|
|
|
|
|
2018-04-16 21:26:18 +02:00
|
|
|
import (
|
2021-08-12 18:56:23 +02:00
|
|
|
"context"
|
2018-04-16 21:26:18 +02:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2019-10-05 19:39:31 +02:00
|
|
|
|
2020-05-04 09:24:32 +02:00
|
|
|
"github.com/leonelquinteros/gotext"
|
|
|
|
|
2023-03-07 22:04:06 +01:00
|
|
|
"github.com/Jguer/yay/v12/pkg/db"
|
|
|
|
"github.com/Jguer/yay/v12/pkg/query"
|
|
|
|
"github.com/Jguer/yay/v12/pkg/settings"
|
|
|
|
"github.com/Jguer/yay/v12/pkg/settings/exe"
|
|
|
|
"github.com/Jguer/yay/v12/pkg/settings/parser"
|
|
|
|
"github.com/Jguer/yay/v12/pkg/stringset"
|
|
|
|
"github.com/Jguer/yay/v12/pkg/text"
|
2018-04-16 21:26:18 +02:00
|
|
|
)
|
|
|
|
|
2021-08-11 20:13:28 +02:00
|
|
|
// CleanDependencies removes all dangling dependencies in system.
|
2023-03-05 22:58:18 +01:00
|
|
|
func cleanDependencies(ctx context.Context, cfg *settings.Configuration,
|
2023-03-31 23:22:57 +02:00
|
|
|
cmdBuilder exe.ICmdBuilder, cmdArgs *parser.Arguments, dbExecutor db.Executor,
|
|
|
|
removeOptional bool,
|
2023-03-05 22:58:18 +01:00
|
|
|
) error {
|
2020-08-07 18:55:19 +02:00
|
|
|
hanging := hangingPackages(removeOptional, dbExecutor)
|
2017-08-02 19:24:03 +02:00
|
|
|
if len(hanging) != 0 {
|
2023-03-31 23:22:57 +02:00
|
|
|
return cleanRemove(ctx, cfg, cmdBuilder, cmdArgs, hanging)
|
2017-08-02 19:24:03 +02:00
|
|
|
}
|
|
|
|
|
2018-07-31 10:42:17 +02:00
|
|
|
return nil
|
2017-08-02 19:24:03 +02:00
|
|
|
}
|
|
|
|
|
2021-08-11 20:13:28 +02:00
|
|
|
// CleanRemove sends a full removal command to pacman with the pkgName slice.
|
2023-03-31 23:22:57 +02:00
|
|
|
func cleanRemove(ctx context.Context, cfg *settings.Configuration,
|
|
|
|
cmdBuilder exe.ICmdBuilder, cmdArgs *parser.Arguments, pkgNames []string,
|
|
|
|
) error {
|
2018-01-04 01:59:57 +01:00
|
|
|
if len(pkgNames) == 0 {
|
2017-08-02 19:24:03 +02:00
|
|
|
return nil
|
|
|
|
}
|
2018-01-19 15:51:18 +01:00
|
|
|
|
2020-07-05 02:45:23 +02:00
|
|
|
arguments := cmdArgs.CopyGlobal()
|
2023-03-31 23:22:57 +02:00
|
|
|
if err := arguments.AddArg("R", "s", "u"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-05 02:45:23 +02:00
|
|
|
arguments.AddTarget(pkgNames...)
|
2018-07-31 10:42:17 +02:00
|
|
|
|
2023-03-31 23:22:57 +02:00
|
|
|
return cmdBuilder.Show(
|
|
|
|
cmdBuilder.BuildPacmanCmd(ctx,
|
|
|
|
arguments, cfg.Mode, settings.NoConfirm))
|
2017-08-02 19:24:03 +02:00
|
|
|
}
|
2018-04-16 21:26:18 +02:00
|
|
|
|
2023-03-05 22:58:18 +01:00
|
|
|
func syncClean(ctx context.Context, cfg *settings.Configuration, cmdArgs *parser.Arguments, dbExecutor db.Executor) error {
|
2018-04-16 21:26:18 +02:00
|
|
|
keepInstalled := false
|
|
|
|
keepCurrent := false
|
|
|
|
|
2020-07-08 03:22:01 +02:00
|
|
|
_, removeAll, _ := cmdArgs.GetArg("c", "clean")
|
2018-04-16 21:26:18 +02:00
|
|
|
|
2023-03-05 22:58:18 +01:00
|
|
|
for _, v := range cfg.Runtime.PacmanConf.CleanMethod {
|
2018-04-16 21:26:18 +02:00
|
|
|
if v == "KeepInstalled" {
|
|
|
|
keepInstalled = true
|
|
|
|
} else if v == "KeepCurrent" {
|
|
|
|
keepCurrent = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-13 09:48:39 +01:00
|
|
|
if cfg.Mode.AtLeastRepo() {
|
2023-03-05 22:58:18 +01:00
|
|
|
if err := cfg.Runtime.CmdBuilder.Show(cfg.Runtime.CmdBuilder.BuildPacmanCmd(ctx,
|
2023-03-13 09:48:39 +01:00
|
|
|
cmdArgs, cfg.Mode, settings.NoConfirm)); err != nil {
|
2018-07-24 03:49:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-13 09:48:39 +01:00
|
|
|
if !cfg.Mode.AtLeastAUR() {
|
2018-07-24 03:49:45 +02:00
|
|
|
return nil
|
2018-04-16 21:26:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var question string
|
|
|
|
if removeAll {
|
2020-05-04 09:24:32 +02:00
|
|
|
question = gotext.Get("Do you want to remove ALL AUR packages from cache?")
|
2018-04-16 21:26:18 +02:00
|
|
|
} else {
|
2020-05-04 09:24:32 +02:00
|
|
|
question = gotext.Get("Do you want to remove all other AUR packages from cache?")
|
2018-04-16 21:26:18 +02:00
|
|
|
}
|
|
|
|
|
2023-03-05 22:58:18 +01:00
|
|
|
fmt.Println(gotext.Get("\nBuild directory:"), cfg.BuildDir)
|
2018-04-16 21:26:18 +02:00
|
|
|
|
2022-08-14 00:56:23 +02:00
|
|
|
if text.ContinueTask(os.Stdin, question, true, settings.NoConfirm) {
|
2023-03-05 22:58:18 +01:00
|
|
|
if err := cleanAUR(ctx, cfg, keepInstalled, keepCurrent, removeAll, dbExecutor); err != nil {
|
2020-05-02 16:17:20 +02:00
|
|
|
return err
|
|
|
|
}
|
2018-04-16 21:26:18 +02:00
|
|
|
}
|
|
|
|
|
2020-05-02 16:17:20 +02:00
|
|
|
if removeAll {
|
|
|
|
return nil
|
2018-04-16 21:26:18 +02:00
|
|
|
}
|
|
|
|
|
2022-08-14 00:56:23 +02:00
|
|
|
if text.ContinueTask(os.Stdin, gotext.Get("Do you want to remove ALL untracked AUR files?"), true, settings.NoConfirm) {
|
2023-03-05 22:58:18 +01:00
|
|
|
return cleanUntracked(ctx, cfg)
|
2018-04-16 21:26:18 +02:00
|
|
|
}
|
|
|
|
|
2018-07-31 10:42:17 +02:00
|
|
|
return nil
|
2018-04-16 21:26:18 +02:00
|
|
|
}
|
|
|
|
|
2023-03-05 22:58:18 +01:00
|
|
|
func cleanAUR(ctx context.Context, config *settings.Configuration,
|
|
|
|
keepInstalled, keepCurrent, removeAll bool, dbExecutor db.Executor,
|
|
|
|
) error {
|
2020-05-04 09:24:32 +02:00
|
|
|
fmt.Println(gotext.Get("removing AUR packages from cache..."))
|
2018-04-16 21:26:18 +02:00
|
|
|
|
2019-10-16 23:25:40 +02:00
|
|
|
installedBases := make(stringset.StringSet)
|
|
|
|
inAURBases := make(stringset.StringSet)
|
2018-04-16 21:26:18 +02:00
|
|
|
|
2022-11-20 01:51:55 +01:00
|
|
|
remotePackages := dbExecutor.InstalledRemotePackages()
|
2018-04-16 21:26:18 +02:00
|
|
|
|
2021-08-26 09:31:14 +02:00
|
|
|
files, err := os.ReadDir(config.BuildDir)
|
2018-04-16 21:26:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cachedPackages := make([]string, 0, len(files))
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2018-04-16 21:26:18 +02:00
|
|
|
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 18:16:48 +02:00
|
|
|
// Querying the AUR is slow and needs internet so don't do it if we
|
2018-04-16 21:26:18 +02:00
|
|
|
// don't need to.
|
|
|
|
if keepCurrent {
|
2021-08-12 18:56:23 +02:00
|
|
|
info, errInfo := query.AURInfo(ctx, config.Runtime.AURClient, cachedPackages, &query.AURWarnings{}, config.RequestSplitN)
|
2020-05-02 16:17:20 +02:00
|
|
|
if errInfo != nil {
|
|
|
|
return errInfo
|
2018-04-16 21:26:18 +02:00
|
|
|
}
|
|
|
|
|
2022-12-29 19:42:43 +01:00
|
|
|
for i := range info {
|
|
|
|
inAURBases.Set(info[i].PackageBase)
|
2018-04-16 21:26:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pkg := range remotePackages {
|
2018-05-08 01:43:49 +02:00
|
|
|
if pkg.Base() != "" {
|
2019-10-05 19:39:31 +02:00
|
|
|
installedBases.Set(pkg.Base())
|
2018-05-08 01:43:49 +02:00
|
|
|
} else {
|
2019-10-05 19:39:31 +02:00
|
|
|
installedBases.Set(pkg.Name())
|
2018-05-08 01:43:49 +02:00
|
|
|
}
|
2018-04-16 21:26:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
if !file.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !removeAll {
|
2019-10-05 19:39:31 +02:00
|
|
|
if keepInstalled && installedBases.Get(file.Name()) {
|
2018-04-16 21:26:18 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-10-05 19:39:31 +02:00
|
|
|
if keepCurrent && inAURBases.Get(file.Name()) {
|
2018-04-16 21:26:18 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-21 10:09:59 +01:00
|
|
|
dir := filepath.Join(config.BuildDir, file.Name())
|
|
|
|
err = os.RemoveAll(dir)
|
2018-04-16 21:26:18 +02:00
|
|
|
if err != nil {
|
2022-12-21 10:09:59 +01:00
|
|
|
text.Warnln(gotext.Get("Unable to remove %s: %s", dir, err))
|
2018-04-16 21:26:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-05 22:58:18 +01:00
|
|
|
func cleanUntracked(ctx context.Context, cfg *settings.Configuration) error {
|
2020-05-04 09:24:32 +02:00
|
|
|
fmt.Println(gotext.Get("removing untracked AUR files from cache..."))
|
2018-04-16 21:26:18 +02:00
|
|
|
|
2023-03-05 22:58:18 +01:00
|
|
|
files, err := os.ReadDir(cfg.BuildDir)
|
2018-04-16 21:26:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
if !file.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-03-05 22:58:18 +01:00
|
|
|
dir := filepath.Join(cfg.BuildDir, file.Name())
|
2020-06-27 00:57:10 +02:00
|
|
|
if isGitRepository(dir) {
|
2023-03-05 22:58:18 +01:00
|
|
|
if err := cfg.Runtime.CmdBuilder.Show(cfg.Runtime.CmdBuilder.BuildGitCmd(ctx, dir, "clean", "-fx")); err != nil {
|
2020-08-19 00:58:10 +02:00
|
|
|
text.Warnln(gotext.Get("Unable to clean:"), dir)
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2020-06-27 00:57:10 +02:00
|
|
|
return err
|
|
|
|
}
|
2018-04-16 21:26:18 +02:00
|
|
|
}
|
|
|
|
}
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2018-04-16 21:26:18 +02:00
|
|
|
return nil
|
|
|
|
}
|
2018-09-27 15:10:36 +02:00
|
|
|
|
2020-06-27 00:57:10 +02:00
|
|
|
func isGitRepository(dir string) bool {
|
|
|
|
_, err := os.Stat(filepath.Join(dir, ".git"))
|
|
|
|
return !os.IsNotExist(err)
|
|
|
|
}
|
|
|
|
|
2023-03-05 22:58:18 +01:00
|
|
|
func cleanAfter(ctx context.Context, config *settings.Configuration,
|
|
|
|
cmdBuilder exe.ICmdBuilder, pkgbuildDirs map[string]string,
|
|
|
|
) {
|
2020-05-08 18:13:51 +02:00
|
|
|
fmt.Println(gotext.Get("removing untracked AUR files from cache..."))
|
2018-09-27 15:10:36 +02:00
|
|
|
|
2022-11-20 01:51:55 +01:00
|
|
|
i := 0
|
|
|
|
for _, dir := range pkgbuildDirs {
|
2022-11-14 01:14:13 +01:00
|
|
|
text.OperationInfoln(gotext.Get("Cleaning (%d/%d): %s", i+1, len(pkgbuildDirs), text.Cyan(dir)))
|
2018-09-27 15:10:36 +02:00
|
|
|
|
2022-11-14 01:14:13 +01:00
|
|
|
_, stderr, err := cmdBuilder.Capture(
|
|
|
|
cmdBuilder.BuildGitCmd(
|
2021-08-12 18:56:23 +02:00
|
|
|
ctx, dir, "reset", "--hard", "HEAD"))
|
2019-11-04 05:20:50 +01:00
|
|
|
if err != nil {
|
2022-11-14 01:14:13 +01:00
|
|
|
text.Errorln(gotext.Get("error resetting %s: %s", dir, stderr))
|
2019-11-04 05:20:50 +01:00
|
|
|
}
|
2018-09-27 15:10:36 +02:00
|
|
|
|
2021-08-12 18:56:23 +02:00
|
|
|
if err := config.Runtime.CmdBuilder.Show(
|
|
|
|
config.Runtime.CmdBuilder.BuildGitCmd(
|
2022-11-29 13:25:36 +01:00
|
|
|
ctx, dir, "clean", "-fx", "--exclude", "*.pkg.*")); err != nil {
|
2019-11-04 05:20:50 +01:00
|
|
|
fmt.Fprintln(os.Stderr, err)
|
2018-09-27 15:10:36 +02:00
|
|
|
}
|
2022-11-20 01:51:55 +01:00
|
|
|
|
|
|
|
i++
|
2018-09-27 15:10:36 +02:00
|
|
|
}
|
|
|
|
}
|