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"
|
|
|
|
|
2021-09-08 22:28:08 +02:00
|
|
|
"github.com/Jguer/yay/v11/pkg/db"
|
|
|
|
"github.com/Jguer/yay/v11/pkg/query"
|
|
|
|
"github.com/Jguer/yay/v11/pkg/settings"
|
2022-11-14 01:14:13 +01:00
|
|
|
"github.com/Jguer/yay/v11/pkg/settings/exe"
|
2021-09-08 22:28:08 +02:00
|
|
|
"github.com/Jguer/yay/v11/pkg/settings/parser"
|
|
|
|
"github.com/Jguer/yay/v11/pkg/stringset"
|
|
|
|
"github.com/Jguer/yay/v11/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.
|
2021-08-12 18:56:23 +02:00
|
|
|
func cleanDependencies(ctx context.Context, cmdArgs *parser.Arguments, dbExecutor db.Executor, removeOptional bool) 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 {
|
2021-08-12 18:56:23 +02:00
|
|
|
return cleanRemove(ctx, 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.
|
2021-08-12 18:56:23 +02:00
|
|
|
func cleanRemove(ctx context.Context, 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()
|
|
|
|
_ = arguments.AddArg("R")
|
|
|
|
arguments.AddTarget(pkgNames...)
|
2018-07-31 10:42:17 +02:00
|
|
|
|
2021-08-08 20:22:39 +02:00
|
|
|
return config.Runtime.CmdBuilder.Show(
|
2021-08-12 18:56:23 +02:00
|
|
|
config.Runtime.CmdBuilder.BuildPacmanCmd(ctx,
|
2021-08-08 00:57:55 +02:00
|
|
|
arguments, config.Runtime.Mode, settings.NoConfirm))
|
2017-08-02 19:24:03 +02:00
|
|
|
}
|
2018-04-16 21:26:18 +02:00
|
|
|
|
2021-08-12 18:56:23 +02:00
|
|
|
func syncClean(ctx context.Context, 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
|
|
|
|
2020-07-05 15:34:27 +02:00
|
|
|
for _, v := range config.Runtime.PacmanConf.CleanMethod {
|
2018-04-16 21:26:18 +02:00
|
|
|
if v == "KeepInstalled" {
|
|
|
|
keepInstalled = true
|
|
|
|
} else if v == "KeepCurrent" {
|
|
|
|
keepCurrent = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-09 13:26:32 +02:00
|
|
|
if config.Runtime.Mode.AtLeastRepo() {
|
2021-08-12 18:56:23 +02:00
|
|
|
if err := config.Runtime.CmdBuilder.Show(config.Runtime.CmdBuilder.BuildPacmanCmd(ctx,
|
2021-08-08 00:57:55 +02:00
|
|
|
cmdArgs, config.Runtime.Mode, settings.NoConfirm)); err != nil {
|
2018-07-24 03:49:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-09 13:26:32 +02:00
|
|
|
if !config.Runtime.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
|
|
|
}
|
|
|
|
|
2020-05-04 09:24:32 +02:00
|
|
|
fmt.Println(gotext.Get("\nBuild directory:"), config.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) {
|
2021-08-12 18:56:23 +02:00
|
|
|
if err := cleanAUR(ctx, 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) {
|
2021-08-12 18:56:23 +02:00
|
|
|
return cleanUntracked(ctx)
|
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
|
|
|
}
|
|
|
|
|
2021-08-12 18:56:23 +02:00
|
|
|
func cleanAUR(ctx context.Context, 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
|
|
|
|
2020-08-01 09:55:08 +02:00
|
|
|
remotePackages, _ := query.GetRemotePackages(dbExecutor)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
for _, pkg := range info {
|
2019-10-05 19:39:31 +02:00
|
|
|
inAURBases.Set(pkg.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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.RemoveAll(filepath.Join(config.BuildDir, file.Name()))
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-12 18:56:23 +02:00
|
|
|
func cleanUntracked(ctx context.Context) 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
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
if !file.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
dir := filepath.Join(config.BuildDir, file.Name())
|
2020-06-27 00:57:10 +02:00
|
|
|
if isGitRepository(dir) {
|
2021-08-12 18:56:23 +02:00
|
|
|
if err := config.Runtime.CmdBuilder.Show(config.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)
|
|
|
|
}
|
|
|
|
|
2022-11-14 01:14:13 +01:00
|
|
|
func cleanAfter(ctx context.Context, cmdBuilder exe.ICmdBuilder, pkgbuildDirs []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-14 01:14:13 +01:00
|
|
|
for i, dir := range pkgbuildDirs {
|
|
|
|
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(
|
2021-10-10 11:01:59 +02: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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|