Add replaces field to package information

This commit is contained in:
EnumDev 2025-03-15 11:20:33 +02:00
parent 4f9d2cdecd
commit e94b2a8816
4 changed files with 62 additions and 6 deletions

14
main.go
View File

@ -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()

View File

@ -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:]...)
}
}
}
}

View File

@ -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")

View File

@ -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 + "'")