From bee23e2f56979770087c818094a14a96f1f268fc Mon Sep 17 00:00:00 2001 From: EnumDev Date: Sat, 18 Oct 2025 09:51:36 +0300 Subject: [PATCH] Improve local package querying --- src/bpm/main.go | 6 ++---- src/bpmlib/dependencies.go | 27 +++++++++---------------- src/bpmlib/general.go | 6 +----- src/bpmlib/operations.go | 5 +---- src/bpmlib/packages.go | 41 +++++++++++++++++++------------------- 5 files changed, 34 insertions(+), 51 deletions(-) diff --git a/src/bpm/main.go b/src/bpm/main.go index 097469b..b70fc59 100644 --- a/src/bpm/main.go +++ b/src/bpm/main.go @@ -207,7 +207,6 @@ func showPackageInfo() { var info *bpmlib.PackageInfo isFile := false - showInstallationReason := false if stat, err := os.Stat(pkg); err == nil && !stat.IsDir() { bpmpkg, err := bpmlib.ReadPackage(pkg) if err != nil { @@ -223,7 +222,6 @@ func showPackageInfo() { } else { info = bpmlib.GetPackageInfo(pkg, rootDir) } - showInstallationReason = true } if info == nil { log.Printf("Error: package (%s) is not installed\n", pkg) @@ -242,7 +240,7 @@ func showPackageInfo() { } fmt.Println("File: " + abs) } - fmt.Println(bpmlib.CreateReadableInfo(true, true, true, showInstallationReason, info, rootDir)) + fmt.Println(info.CreateReadableInfo(rootDir)) } } @@ -286,7 +284,7 @@ func showPackageList() { if n != 0 { fmt.Println() } - fmt.Println(bpmlib.CreateReadableInfo(true, true, true, true, info, rootDir)) + fmt.Println(info.CreateReadableInfo(rootDir)) } } } diff --git a/src/bpmlib/dependencies.go b/src/bpmlib/dependencies.go index 68c143c..5c4b220 100644 --- a/src/bpmlib/dependencies.go +++ b/src/bpmlib/dependencies.go @@ -1,7 +1,6 @@ package bpmlib import ( - "errors" "fmt" "slices" ) @@ -170,19 +169,11 @@ func resolvePackageDependenciesFromDatabase(resolved *[]pkgInstallationReason, u } } -func GetPackageDependants(pkgName string, rootDir string) ([]string, error) { - ret := make([]string, 0) - - // Get BPM package - pkg := GetPackage(pkgName, rootDir) - if pkg == nil { - return nil, errors.New("package not found: " + pkgName) - } - +func (pkgInfo *PackageInfo) GetPackageDependants(rootDir string) (dependants []string) { // Get installed package names pkgs, err := GetInstalledPackages(rootDir) if err != nil { - return nil, errors.New("could not get installed packages") + return nil } // Loop through all installed packages @@ -190,33 +181,33 @@ func GetPackageDependants(pkgName string, rootDir string) ([]string, error) { // Get installed BPM package installedPkg := GetPackage(installedPkgName, rootDir) if installedPkg == nil { - return nil, errors.New("package not found: " + installedPkgName) + return nil } // Skip iteration if comparing the same packages - if installedPkg.PkgInfo.Name == pkgName { + if installedPkg.PkgInfo.Name == pkgInfo.Name { continue } // Add installed package to list if its dependencies include pkgName if slices.ContainsFunc(installedPkg.PkgInfo.Depends, func(n string) bool { - return n == pkgName + return n == pkgInfo.Name }) { - ret = append(ret, installedPkgName) + dependants = append(dependants, installedPkgName) continue } // Loop through each virtual package - for _, vpkg := range pkg.PkgInfo.Provides { + for _, vpkg := range pkgInfo.Provides { // Add installed package to list if its dependencies contain a provided virtual package if slices.ContainsFunc(installedPkg.PkgInfo.Depends, func(n string) bool { return n == vpkg }) { - ret = append(ret, installedPkgName) + dependants = append(dependants, installedPkgName) break } } } - return ret, nil + return dependants } diff --git a/src/bpmlib/general.go b/src/bpmlib/general.go index 9ef3cbe..7472a5e 100644 --- a/src/bpmlib/general.go +++ b/src/bpmlib/general.go @@ -221,11 +221,7 @@ func RemovePackages(rootDir string, force, cleanupDependencies bool, packages .. // Get packages and their dependants packageDepndants := make(map[string][]string, 0) for _, action := range operation.Actions { - dependants, err := GetPackageDependants(action.(*RemovePackageAction).BpmPackage.PkgInfo.Name, rootDir) - if err != nil { - return nil, fmt.Errorf("could not get package dependants: %s", err) - } - + dependants := action.(*RemovePackageAction).BpmPackage.PkgInfo.GetPackageDependants(rootDir) packageDepndants[action.(*RemovePackageAction).BpmPackage.PkgInfo.Name] = dependants } diff --git a/src/bpmlib/operations.go b/src/bpmlib/operations.go index 6ba8c77..fae8bf7 100644 --- a/src/bpmlib/operations.go +++ b/src/bpmlib/operations.go @@ -176,10 +176,7 @@ func (operation *BPMOperation) RemoveNeededPackages() error { } for pkg, action := range removeActions { - dependants, err := GetPackageDependants(action.BpmPackage.PkgInfo.Name, operation.RootDir) - if err != nil { - return errors.New("could not get dependant packages for package (" + pkg + ")") - } + dependants := action.BpmPackage.PkgInfo.GetPackageDependants(operation.RootDir) dependants = slices.DeleteFunc(dependants, func(d string) bool { if _, ok := removeActions[d]; ok { return true diff --git a/src/bpmlib/packages.go b/src/bpmlib/packages.go index 6be21d5..d20100d 100644 --- a/src/bpmlib/packages.go +++ b/src/bpmlib/packages.go @@ -507,39 +507,39 @@ func ReadPackageInfo(contents string) (*PackageInfo, error) { return pkgInfo, nil } -func CreateReadableInfo(showArchitecture, showType, showPackageRelations, showInstallationReason bool, pkgInfo *PackageInfo, rootDir string) string { +func (pkgInfo *PackageInfo) CreateReadableInfo(rootDir string) string { ret := make([]string, 0) appendArray := func(label string, array []string) { if len(array) == 0 { return } + + // Sort array + slices.Sort(array) + ret = append(ret, fmt.Sprintf("%s: %s", label, strings.Join(array, ", "))) } + ret = append(ret, "Name: "+pkgInfo.Name) ret = append(ret, "Description: "+pkgInfo.Description) ret = append(ret, "Version: "+pkgInfo.GetFullVersion()) ret = append(ret, "URL: "+pkgInfo.Url) ret = append(ret, "License: "+pkgInfo.License) - if showArchitecture { - ret = append(ret, "Architecture: "+pkgInfo.Arch) + ret = append(ret, "Architecture: "+pkgInfo.Arch) + ret = append(ret, "Type: "+pkgInfo.Type) + appendArray("Dependencies", pkgInfo.Depends) + if pkgInfo.Type == "source" { + appendArray("Make Dependencies", pkgInfo.MakeDepends) } - if showType { - ret = append(ret, "Type: "+pkgInfo.Type) - } - if showPackageRelations { - appendArray("Dependencies", pkgInfo.Depends) - if pkgInfo.Type == "source" { - appendArray("Make Dependencies", pkgInfo.MakeDepends) - } - appendArray("Optional dependencies", pkgInfo.OptionalDepends) - dependants, err := GetPackageDependants(pkgInfo.Name, rootDir) - if err == nil { - appendArray("Dependant packages", dependants) - } - appendArray("Conflicting packages", pkgInfo.Conflicts) - appendArray("Provided packages", pkgInfo.Provides) - appendArray("Replaces packages", pkgInfo.Replaces) + appendArray("Optional dependencies", pkgInfo.OptionalDepends) + dependants := pkgInfo.GetPackageDependants(rootDir) + if len(dependants) > 0 { + appendArray("Dependant packages", dependants) } + appendArray("Conflicting packages", pkgInfo.Conflicts) + appendArray("Provided packages", pkgInfo.Provides) + appendArray("Replaces packages", pkgInfo.Replaces) + if pkgInfo.Type == "source" && len(pkgInfo.SplitPackages) != 0 { splitPkgs := make([]string, len(pkgInfo.SplitPackages)) for i, splitPkgInfo := range pkgInfo.SplitPackages { @@ -547,7 +547,8 @@ func CreateReadableInfo(showArchitecture, showType, showPackageRelations, showIn } appendArray("Split Packages", splitPkgs) } - if IsPackageInstalled(pkgInfo.Name, rootDir) && showInstallationReason { + + if rootDir != "" && IsPackageInstalled(pkgInfo.Name, rootDir) { installationReason := GetInstallationReason(pkgInfo.Name, rootDir) var installationReasonString string switch installationReason {