Skip to content

Commit

Permalink
[support/deps] Improve collecting and filtering dependencies info
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Oct 9, 2024
1 parent 8e48de1 commit 3a42871
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### [13.6.0](https://kaos.sh/ek/13.6.0)

- `[setup]` Added package to install/uninstall application as a service
- `[support/deps]` Improved collecting and filtering dependencies info
- `[support/kernel]` Added simple globs support for parameter names

### [13.5.1](https://kaos.sh/ek/13.5.1)
Expand Down
53 changes: 49 additions & 4 deletions support/deps/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ package deps
// ////////////////////////////////////////////////////////////////////////////////// //

import (
"runtime/debug"
"strings"

"github.com/essentialkaos/ek/v13/support"

"github.com/essentialkaos/depsy"
Expand All @@ -17,16 +20,58 @@ import (
// ////////////////////////////////////////////////////////////////////////////////// //

// Extract extracts dependencies info from gomod data
func Extract(gomod []byte) []support.Dep {
func Extract(gomod []byte, withIndirect ...bool) []support.Dep {
if len(withIndirect) > 0 && withIndirect[0] {
return filterDeps(depsy.Extract(gomod, true))
}

return filterDeps(depsy.Extract(gomod, false))
}

// ////////////////////////////////////////////////////////////////////////////////// //

// filterDeps filters dependencies from gomod using information from bundled build info
func filterDeps(deps depsy.Dependencies) []support.Dep {
var result []support.Dep

for _, dep := range depsy.Extract(gomod, false) {
result = append(result, support.Dep{
buildInfo, _ := debug.ReadBuildInfo()

for _, dep := range deps {
depInfo := support.Dep{
Version: dep.Version,
Path: dep.PrettyPath(),
Extra: dep.Extra,
})
}

if buildInfo != nil {
hasDep, version := hasBuiltDep(dep, buildInfo)

if !hasDep {
continue
}

if version != "" && strings.Contains(version, "(") {
depInfo.Version = strings.Trim(version, "()")
}
}

result = append(result, depInfo)
}

return result
}

// hasBuiltDep checks if given dependency is present in build info
func hasBuiltDep(dep depsy.Dependency, buildInfo *debug.BuildInfo) (bool, string) {
for _, bDep := range buildInfo.Deps {
if bDep.Path == dep.Path {
if bDep.Replace != nil {
return true, bDep.Replace.Version
}

return true, ""
}
}

return false, ""
}

0 comments on commit 3a42871

Please sign in to comment.