Merge pull request 'feat: add find-provides and find-requires (rpm only)' (#5) from Maks1mS/ALR:feat/auto-provides-requires into master

Reviewed-on: #5
This commit is contained in:
Евгений Храмов 2024-12-26 08:07:15 +00:00
commit 5d1d3d7c45
2 changed files with 155 additions and 64 deletions

@ -170,7 +170,7 @@ func BuildPackage(ctx context.Context, opts types.BuildOpts) ([]string, []string
pkgFormat := getPkgFormat(opts.Manager) // Получаем формат пакета pkgFormat := getPkgFormat(opts.Manager) // Получаем формат пакета
pkgInfo, err := buildPkgMetadata(vars, dirs, pkgFormat, info, append(repoDeps, builtNames...)) // Собираем метаданные пакета pkgInfo, err := buildPkgMetadata(ctx, vars, dirs, pkgFormat, info, append(repoDeps, builtNames...)) // Собираем метаданные пакета
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
@ -502,7 +502,7 @@ func executeFunctions(ctx context.Context, dec *decoder.Decoder, dirs types.Dire
} }
// Функция buildPkgMetadata создает метаданные для пакета, который будет собран. // Функция buildPkgMetadata создает метаданные для пакета, который будет собран.
func buildPkgMetadata(vars *types.BuildVars, dirs types.Directories, pkgFormat string, info *distro.OSRelease, deps []string) (*nfpm.Info, error) { func buildPkgMetadata(ctx context.Context, vars *types.BuildVars, dirs types.Directories, pkgFormat string, info *distro.OSRelease, deps []string) (*nfpm.Info, error) {
pkgInfo := getBasePkgInfo(vars) pkgInfo := getBasePkgInfo(vars)
pkgInfo.Description = vars.Description pkgInfo.Description = vars.Description
pkgInfo.Platform = "linux" pkgInfo.Platform = "linux"
@ -543,6 +543,17 @@ func buildPkgMetadata(vars *types.BuildVars, dirs types.Directories, pkgFormat s
} }
pkgInfo.Overridables.Contents = contents pkgInfo.Overridables.Contents = contents
if pkgFormat == "rpm" {
err = rpmFindProvides(ctx, pkgInfo, dirs)
if err != nil {
return nil, err
}
err = rpmFindRequires(ctx, pkgInfo, dirs)
if err != nil {
return nil, err
}
}
return pkgInfo, nil return pkgInfo, nil
} }

80
pkg/build/findDeps.go Normal file

@ -0,0 +1,80 @@
package build
import (
"bytes"
"context"
"os/exec"
"path"
"strings"
"github.com/goreleaser/nfpm/v2"
"plemya-x.ru/alr/internal/types"
"plemya-x.ru/alr/pkg/loggerctx"
)
func rpmFindDependencies(ctx context.Context, pkgInfo *nfpm.Info, dirs types.Directories, command string, updateFunc func(string)) error {
log := loggerctx.From(ctx)
if _, err := exec.LookPath(command); err != nil {
log.Info("Command not found on the system").Str("command", command).Send()
return nil
}
var paths []string
for _, content := range pkgInfo.Contents {
if content.Type != "dir" {
paths = append(paths,
path.Join(dirs.PkgDir, content.Destination),
)
}
}
if len(paths) == 0 {
return nil
}
cmd := exec.Command(command)
cmd.Stdin = bytes.NewBufferString(strings.Join(paths, "\n"))
cmd.Env = append(cmd.Env,
"RPM_BUILD_ROOT="+dirs.PkgDir,
"RPM_FINDPROV_METHOD=",
"RPM_FINDREQ_METHOD=",
"RPM_DATADIR=",
"RPM_SUBPACKAGE_NAME=",
)
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
log.Error(stderr.String())
return err
}
dependencies := strings.Split(strings.TrimSpace(out.String()), "\n")
for _, dep := range dependencies {
if dep != "" {
updateFunc(dep)
}
}
return nil
}
func rpmFindProvides(ctx context.Context, pkgInfo *nfpm.Info, dirs types.Directories) error {
log := loggerctx.From(ctx)
return rpmFindDependencies(ctx, pkgInfo, dirs, "/usr/lib/rpm/find-provides", func(dep string) {
log.Info("Provided dependency found").Str("dep", dep).Send()
pkgInfo.Overridables.Provides = append(pkgInfo.Overridables.Provides, dep)
})
}
func rpmFindRequires(ctx context.Context, pkgInfo *nfpm.Info, dirs types.Directories) error {
log := loggerctx.From(ctx)
return rpmFindDependencies(ctx, pkgInfo, dirs, "/usr/lib/rpm/find-requires", func(dep string) {
log.Info("Required dependency found").Str("dep", dep).Send()
pkgInfo.Overridables.Depends = append(pkgInfo.Overridables.Depends, dep)
})
}