From e94b2a88166bc7a24176cd85abdb711b0224c07b Mon Sep 17 00:00:00 2001 From: EnumDev Date: Sat, 15 Mar 2025 11:20:33 +0200 Subject: [PATCH] Add replaces field to package information --- main.go | 14 ++++++++++++-- utils/operations.go | 33 ++++++++++++++++++++++++++++++--- utils/package_utils.go | 4 +++- utils/repo_utils.go | 17 +++++++++++++++++ 4 files changed, 62 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 709a344..a43e2c8 100644 --- a/main.go +++ b/main.go @@ -263,6 +263,9 @@ func resolveCommand() { } } + // Replace obsolete packages + operation.ReplaceObsoletePackages() + // Check for conflicts conflicts, err := operation.CheckForConflicts() if err != nil { @@ -343,10 +346,14 @@ func resolveCommand() { if slices.Contains(utils.BPMConfig.IgnorePackages, pkg) { continue } - entry, _, err := utils.GetRepositoryEntry(pkg) - if err != nil { + var entry *utils.RepositoryEntry + // Check if installed package can be replaced and install that instead + if e := utils.FindReplacement(pkg); e != nil { + entry = e + } else if entry, _, err = utils.GetRepositoryEntry(pkg); err != nil { continue } + installedInfo := utils.GetPackageInfo(pkg, rootDir) if installedInfo == nil { log.Fatalf("Error: could not get package info for (%s)\n", pkg) @@ -374,6 +381,9 @@ func resolveCommand() { } } + // Replace obsolete packages + operation.ReplaceObsoletePackages() + // Show operation summary operation.ShowOperationSummary() diff --git a/utils/operations.go b/utils/operations.go index 754a17a..fe5bef5 100644 --- a/utils/operations.go +++ b/utils/operations.go @@ -228,6 +228,30 @@ func (operation *BPMOperation) Cleanup(verbose bool) error { return nil } +func (operation *BPMOperation) ReplaceObsoletePackages() { + for _, value := range slices.Clone(operation.Actions) { + var pkgInfo *PackageInfo + if value.GetActionType() == "install" { + action := value.(*InstallPackageAction) + pkgInfo = action.BpmPackage.PkgInfo + + } else if value.GetActionType() == "fetch" { + action := value.(*FetchPackageAction) + pkgInfo = action.RepositoryEntry.Info + } else { + continue + } + + for _, r := range pkgInfo.Replaces { + if bpmpkg := GetPackage(r, operation.RootDir); bpmpkg != nil && !operation.ActionsContainPackage(bpmpkg.PkgInfo.Name) { + operation.InsertActionAt(0, &RemovePackageAction{ + BpmPackage: bpmpkg, + }) + } + } + } +} + func (operation *BPMOperation) CheckForConflicts() (map[string][]string, error) { conflicts := make(map[string][]string) installedPackages, err := GetInstalledPackages(operation.RootDir) @@ -256,9 +280,12 @@ func (operation *BPMOperation) CheckForConflicts() (map[string][]string, error) } else if value.GetActionType() == "remove" { action := value.(*RemovePackageAction) pkgInfo := action.BpmPackage.PkgInfo - slices.DeleteFunc(allPackages, func(info *PackageInfo) bool { - return info.Name == pkgInfo.Name - }) + for i := len(allPackages) - 1; i >= 0; i-- { + info := allPackages[i] + if info.Name == pkgInfo.Name { + allPackages = append(allPackages[:i], allPackages[i+1:]...) + } + } } } diff --git a/utils/package_utils.go b/utils/package_utils.go index 17fba2a..a0d2aeb 100644 --- a/utils/package_utils.go +++ b/utils/package_utils.go @@ -39,6 +39,7 @@ type PackageInfo struct { MakeDepends []string `yaml:"make_depends,omitempty"` OptionalDepends []string `yaml:"optional_depends,omitempty"` Conflicts []string `yaml:"conflicts,omitempty"` + Replaces []string `yaml:"replaces,omitempty"` Provides []string `yaml:"provides,omitempty"` } @@ -406,6 +407,7 @@ func ReadPackageInfo(contents string) (*PackageInfo, error) { MakeDepends: make([]string, 0), OptionalDepends: make([]string, 0), Conflicts: make([]string, 0), + Replaces: make([]string, 0), Provides: make([]string, 0), } err := yaml.Unmarshal([]byte(contents), &pkgInfo) @@ -468,7 +470,7 @@ func CreateReadableInfo(showArchitecture, showType, showPackageRelations bool, p } appendArray("Conflicting packages", pkgInfo.Conflicts) appendArray("Provided packages", pkgInfo.Provides) - + appendArray("Replaces packages", pkgInfo.Replaces) } ret = append(ret, "Installation Reason: "+string(GetInstallationReason(pkgInfo.Name, rootDir))) return strings.Join(ret, "\n") diff --git a/utils/repo_utils.go b/utils/repo_utils.go index 28fdaeb..4ea31ac 100644 --- a/utils/repo_utils.go +++ b/utils/repo_utils.go @@ -167,6 +167,23 @@ func GetRepositoryEntry(str string) (*RepositoryEntry, *Repository, error) { } } +func FindReplacement(pkg string) *RepositoryEntry { + for _, repo := range BPMConfig.Repositories { + for _, entry := range repo.Entries { + if entry.IsVirtualPackage { + continue + } + for _, replaced := range entry.Info.Replaces { + if replaced == pkg { + return entry + } + } + } + } + + return nil +} + func (repo *Repository) FetchPackage(pkg string) (string, error) { if !repo.ContainsPackage(pkg) { return "", errors.New("could not fetch package '" + pkg + "'")