forked from Plemya-x/ALR
refactor: move pkg/
to internal/
and update imports
Restructure project by relocating package contents from pkg/ to internal/ to better reflect internal-only usage. This commit is initial step to prepare project for public api
This commit is contained in:
738
internal/build/build.go
Normal file
738
internal/build/build.go
Normal file
@ -0,0 +1,738 @@
|
||||
// This file was originally part of the project "LURE - Linux User REpository", created by Elara Musayelyan.
|
||||
// It has been modified as part of "ALR - Any Linux Repository" by the ALR Authors.
|
||||
//
|
||||
// ALR - Any Linux Repository
|
||||
// Copyright (C) 2025 The ALR Authors
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package build
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/gob"
|
||||
"errors"
|
||||
"log/slog"
|
||||
|
||||
"github.com/leonelquinteros/gotext"
|
||||
"mvdan.cc/sh/v3/syntax"
|
||||
"mvdan.cc/sh/v3/syntax/typedjson"
|
||||
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/cliutils"
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/config"
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/db"
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/distro"
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/manager"
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/types"
|
||||
)
|
||||
|
||||
type BuildInput struct {
|
||||
opts *types.BuildOpts
|
||||
info *distro.OSRelease
|
||||
pkgFormat string
|
||||
script string
|
||||
repository string
|
||||
packages []string
|
||||
}
|
||||
|
||||
func (bi *BuildInput) GobEncode() ([]byte, error) {
|
||||
w := new(bytes.Buffer)
|
||||
encoder := gob.NewEncoder(w)
|
||||
|
||||
if err := encoder.Encode(bi.opts); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := encoder.Encode(bi.info); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := encoder.Encode(bi.pkgFormat); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := encoder.Encode(bi.script); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := encoder.Encode(bi.repository); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := encoder.Encode(bi.packages); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return w.Bytes(), nil
|
||||
}
|
||||
|
||||
func (bi *BuildInput) GobDecode(data []byte) error {
|
||||
r := bytes.NewBuffer(data)
|
||||
decoder := gob.NewDecoder(r)
|
||||
|
||||
if err := decoder.Decode(&bi.opts); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := decoder.Decode(&bi.info); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := decoder.Decode(&bi.pkgFormat); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := decoder.Decode(&bi.script); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := decoder.Decode(&bi.repository); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := decoder.Decode(&bi.packages); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *BuildInput) Repository() string {
|
||||
return b.repository
|
||||
}
|
||||
|
||||
func (b *BuildInput) BuildOpts() *types.BuildOpts {
|
||||
return b.opts
|
||||
}
|
||||
|
||||
func (b *BuildInput) OSRelease() *distro.OSRelease {
|
||||
return b.info
|
||||
}
|
||||
|
||||
func (b *BuildInput) PkgFormat() string {
|
||||
return b.pkgFormat
|
||||
}
|
||||
|
||||
type BuildOptsProvider interface {
|
||||
BuildOpts() *types.BuildOpts
|
||||
}
|
||||
|
||||
type OsInfoProvider interface {
|
||||
OSRelease() *distro.OSRelease
|
||||
}
|
||||
|
||||
type PkgFormatProvider interface {
|
||||
PkgFormat() string
|
||||
}
|
||||
|
||||
type RepositoryProvider interface {
|
||||
Repository() string
|
||||
}
|
||||
|
||||
// ================================================
|
||||
|
||||
type ScriptFile struct {
|
||||
File *syntax.File
|
||||
Path string
|
||||
}
|
||||
|
||||
func (s *ScriptFile) GobEncode() ([]byte, error) {
|
||||
var buf bytes.Buffer
|
||||
enc := gob.NewEncoder(&buf)
|
||||
if err := enc.Encode(s.Path); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var fileBuf bytes.Buffer
|
||||
if err := typedjson.Encode(&fileBuf, s.File); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fileData := fileBuf.Bytes()
|
||||
if err := enc.Encode(fileData); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
func (s *ScriptFile) GobDecode(data []byte) error {
|
||||
buf := bytes.NewBuffer(data)
|
||||
dec := gob.NewDecoder(buf)
|
||||
if err := dec.Decode(&s.Path); err != nil {
|
||||
return err
|
||||
}
|
||||
var fileData []byte
|
||||
if err := dec.Decode(&fileData); err != nil {
|
||||
return err
|
||||
}
|
||||
fileReader := bytes.NewReader(fileData)
|
||||
file, err := typedjson.Decode(fileReader)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.File = file.(*syntax.File)
|
||||
return nil
|
||||
}
|
||||
|
||||
type BuiltDep struct {
|
||||
Name string
|
||||
Path string
|
||||
}
|
||||
|
||||
func Map[T, R any](items []T, f func(T) R) []R {
|
||||
res := make([]R, len(items))
|
||||
for i, item := range items {
|
||||
res[i] = f(item)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func GetBuiltPaths(deps []*BuiltDep) []string {
|
||||
return Map(deps, func(dep *BuiltDep) string {
|
||||
return dep.Path
|
||||
})
|
||||
}
|
||||
|
||||
func GetBuiltName(deps []*BuiltDep) []string {
|
||||
return Map(deps, func(dep *BuiltDep) string {
|
||||
return dep.Name
|
||||
})
|
||||
}
|
||||
|
||||
type PackageFinder interface {
|
||||
FindPkgs(ctx context.Context, pkgs []string) (map[string][]db.Package, []string, error)
|
||||
}
|
||||
|
||||
type Config interface {
|
||||
GetPaths() *config.Paths
|
||||
PagerStyle() string
|
||||
}
|
||||
|
||||
type FunctionsOutput struct {
|
||||
Contents *[]string
|
||||
}
|
||||
|
||||
// EXECUTORS
|
||||
|
||||
type ScriptResolverExecutor interface {
|
||||
ResolveScript(ctx context.Context, pkg *db.Package) *ScriptInfo
|
||||
}
|
||||
|
||||
type ScriptExecutor interface {
|
||||
ReadScript(ctx context.Context, scriptPath string) (*ScriptFile, error)
|
||||
ExecuteFirstPass(ctx context.Context, input *BuildInput, sf *ScriptFile) (string, []*types.BuildVars, error)
|
||||
PrepareDirs(
|
||||
ctx context.Context,
|
||||
input *BuildInput,
|
||||
basePkg string,
|
||||
) error
|
||||
ExecuteSecondPass(
|
||||
ctx context.Context,
|
||||
input *BuildInput,
|
||||
sf *ScriptFile,
|
||||
varsOfPackages []*types.BuildVars,
|
||||
repoDeps []string,
|
||||
builtDeps []*BuiltDep,
|
||||
basePkg string,
|
||||
) ([]*BuiltDep, error)
|
||||
}
|
||||
|
||||
type CacheExecutor interface {
|
||||
CheckForBuiltPackage(ctx context.Context, input *BuildInput, vars *types.BuildVars) (string, bool, error)
|
||||
}
|
||||
|
||||
type ScriptViewerExecutor interface {
|
||||
ViewScript(ctx context.Context, input *BuildInput, sf *ScriptFile, basePkg string) error
|
||||
}
|
||||
|
||||
type CheckerExecutor interface {
|
||||
PerformChecks(
|
||||
ctx context.Context,
|
||||
input *BuildInput,
|
||||
vars *types.BuildVars,
|
||||
) (bool, error)
|
||||
}
|
||||
|
||||
type InstallerExecutor interface {
|
||||
InstallLocal(paths []string, opts *manager.Opts) error
|
||||
Install(pkgs []string, opts *manager.Opts) error
|
||||
RemoveAlreadyInstalled(pkgs []string) ([]string, error)
|
||||
}
|
||||
|
||||
type SourcesInput struct {
|
||||
Sources []string
|
||||
Checksums []string
|
||||
}
|
||||
|
||||
type SourceDownloaderExecutor interface {
|
||||
DownloadSources(
|
||||
ctx context.Context,
|
||||
input *BuildInput,
|
||||
basePkg string,
|
||||
si SourcesInput,
|
||||
) error
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
func NewBuilder(
|
||||
scriptResolver ScriptResolverExecutor,
|
||||
scriptExecutor ScriptExecutor,
|
||||
cacheExecutor CacheExecutor,
|
||||
scriptViewerExecutor ScriptViewerExecutor,
|
||||
checkerExecutor CheckerExecutor,
|
||||
installerExecutor InstallerExecutor,
|
||||
sourceExecutor SourceDownloaderExecutor,
|
||||
) *Builder {
|
||||
return &Builder{
|
||||
scriptResolver: scriptResolver,
|
||||
scriptExecutor: scriptExecutor,
|
||||
cacheExecutor: cacheExecutor,
|
||||
scriptViewerExecutor: scriptViewerExecutor,
|
||||
checkerExecutor: checkerExecutor,
|
||||
installerExecutor: installerExecutor,
|
||||
sourceExecutor: sourceExecutor,
|
||||
}
|
||||
}
|
||||
|
||||
type Builder struct {
|
||||
scriptResolver ScriptResolverExecutor
|
||||
scriptExecutor ScriptExecutor
|
||||
cacheExecutor CacheExecutor
|
||||
scriptViewerExecutor ScriptViewerExecutor
|
||||
checkerExecutor CheckerExecutor
|
||||
installerExecutor InstallerExecutor
|
||||
sourceExecutor SourceDownloaderExecutor
|
||||
repos PackageFinder
|
||||
// mgr manager.Manager
|
||||
}
|
||||
|
||||
type BuildArgs struct {
|
||||
Opts *types.BuildOpts
|
||||
Info *distro.OSRelease
|
||||
PkgFormat_ string
|
||||
}
|
||||
|
||||
func (b *BuildArgs) BuildOpts() *types.BuildOpts {
|
||||
return b.Opts
|
||||
}
|
||||
|
||||
func (b *BuildArgs) OSRelease() *distro.OSRelease {
|
||||
return b.Info
|
||||
}
|
||||
|
||||
func (b *BuildArgs) PkgFormat() string {
|
||||
return b.PkgFormat_
|
||||
}
|
||||
|
||||
type BuildPackageFromDbArgs struct {
|
||||
BuildArgs
|
||||
Package *db.Package
|
||||
Packages []string
|
||||
}
|
||||
|
||||
type BuildPackageFromScriptArgs struct {
|
||||
BuildArgs
|
||||
Script string
|
||||
Packages []string
|
||||
}
|
||||
|
||||
func (b *Builder) BuildPackageFromDb(
|
||||
ctx context.Context,
|
||||
args *BuildPackageFromDbArgs,
|
||||
) ([]*BuiltDep, error) {
|
||||
scriptInfo := b.scriptResolver.ResolveScript(ctx, args.Package)
|
||||
|
||||
return b.BuildPackage(ctx, &BuildInput{
|
||||
script: scriptInfo.Script,
|
||||
repository: scriptInfo.Repository,
|
||||
packages: args.Packages,
|
||||
pkgFormat: args.PkgFormat(),
|
||||
opts: args.Opts,
|
||||
info: args.Info,
|
||||
})
|
||||
}
|
||||
|
||||
func (b *Builder) BuildPackageFromScript(
|
||||
ctx context.Context,
|
||||
args *BuildPackageFromScriptArgs,
|
||||
) ([]*BuiltDep, error) {
|
||||
return b.BuildPackage(ctx, &BuildInput{
|
||||
script: args.Script,
|
||||
repository: "default",
|
||||
packages: args.Packages,
|
||||
pkgFormat: args.PkgFormat(),
|
||||
opts: args.Opts,
|
||||
info: args.Info,
|
||||
})
|
||||
}
|
||||
|
||||
func (b *Builder) BuildPackage(
|
||||
ctx context.Context,
|
||||
input *BuildInput,
|
||||
) ([]*BuiltDep, error) {
|
||||
scriptPath := input.script
|
||||
|
||||
slog.Debug("ReadScript")
|
||||
sf, err := b.scriptExecutor.ReadScript(ctx, scriptPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
slog.Debug("ExecuteFirstPass")
|
||||
basePkg, varsOfPackages, err := b.scriptExecutor.ExecuteFirstPass(ctx, input, sf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var builtDeps []*BuiltDep
|
||||
|
||||
if !input.opts.Clean {
|
||||
var remainingVars []*types.BuildVars
|
||||
for _, vars := range varsOfPackages {
|
||||
builtPkgPath, ok, err := b.cacheExecutor.CheckForBuiltPackage(ctx, input, vars)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if ok {
|
||||
builtDeps = append(builtDeps, &BuiltDep{
|
||||
Path: builtPkgPath,
|
||||
})
|
||||
} else {
|
||||
remainingVars = append(remainingVars, vars)
|
||||
}
|
||||
}
|
||||
|
||||
if len(remainingVars) == 0 {
|
||||
return builtDeps, nil
|
||||
}
|
||||
}
|
||||
|
||||
slog.Debug("ViewScript")
|
||||
err = b.scriptViewerExecutor.ViewScript(ctx, input, sf, basePkg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
slog.Info(gotext.Get("Building package"), "name", basePkg)
|
||||
|
||||
for _, vars := range varsOfPackages {
|
||||
cont, err := b.checkerExecutor.PerformChecks(ctx, input, vars)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !cont {
|
||||
return nil, errors.New("exit...")
|
||||
}
|
||||
}
|
||||
|
||||
buildDepends := []string{}
|
||||
optDepends := []string{}
|
||||
depends := []string{}
|
||||
sources := []string{}
|
||||
checksums := []string{}
|
||||
for _, vars := range varsOfPackages {
|
||||
buildDepends = append(buildDepends, vars.BuildDepends...)
|
||||
optDepends = append(optDepends, vars.OptDepends...)
|
||||
depends = append(depends, vars.Depends...)
|
||||
sources = append(sources, vars.Sources...)
|
||||
checksums = append(checksums, vars.Checksums...)
|
||||
}
|
||||
buildDepends = removeDuplicates(buildDepends)
|
||||
optDepends = removeDuplicates(optDepends)
|
||||
depends = removeDuplicates(depends)
|
||||
|
||||
if len(sources) != len(checksums) {
|
||||
slog.Error(gotext.Get("The checksums array must be the same length as sources"))
|
||||
return nil, errors.New("exit...")
|
||||
}
|
||||
sources, checksums = removeDuplicatesSources(sources, checksums)
|
||||
|
||||
slog.Debug("installBuildDeps")
|
||||
alrBuildDeps, err := b.installBuildDeps(ctx, input, buildDepends)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
slog.Debug("installOptDeps")
|
||||
_, err = b.installOptDeps(ctx, input, optDepends)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
depNames := make(map[string]struct{})
|
||||
for _, dep := range alrBuildDeps {
|
||||
depNames[dep.Name] = struct{}{}
|
||||
}
|
||||
|
||||
// We filter so as not to re-build what has already been built at the `installBuildDeps` stage.
|
||||
var filteredDepends []string
|
||||
for _, d := range depends {
|
||||
if _, found := depNames[d]; !found {
|
||||
filteredDepends = append(filteredDepends, d)
|
||||
}
|
||||
}
|
||||
|
||||
slog.Debug("BuildALRDeps")
|
||||
newBuiltDeps, repoDeps, err := b.BuildALRDeps(ctx, input, filteredDepends)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
slog.Debug("PrepareDirs")
|
||||
err = b.scriptExecutor.PrepareDirs(ctx, input, basePkg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
slog.Info(gotext.Get("Downloading sources"))
|
||||
slog.Debug("DownloadSources")
|
||||
err = b.sourceExecutor.DownloadSources(
|
||||
ctx,
|
||||
input,
|
||||
basePkg,
|
||||
SourcesInput{
|
||||
Sources: sources,
|
||||
Checksums: checksums,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
builtDeps = removeDuplicates(append(builtDeps, newBuiltDeps...))
|
||||
|
||||
slog.Debug("ExecuteSecondPass")
|
||||
res, err := b.scriptExecutor.ExecuteSecondPass(
|
||||
ctx,
|
||||
input,
|
||||
sf,
|
||||
varsOfPackages,
|
||||
repoDeps,
|
||||
builtDeps,
|
||||
basePkg,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
builtDeps = removeDuplicates(append(builtDeps, res...))
|
||||
|
||||
return builtDeps, nil
|
||||
}
|
||||
|
||||
type InstallPkgsArgs struct {
|
||||
BuildArgs
|
||||
AlrPkgs []db.Package
|
||||
NativePkgs []string
|
||||
}
|
||||
|
||||
func (b *Builder) InstallALRPackages(
|
||||
ctx context.Context,
|
||||
input interface {
|
||||
OsInfoProvider
|
||||
BuildOptsProvider
|
||||
PkgFormatProvider
|
||||
},
|
||||
alrPkgs []db.Package,
|
||||
) error {
|
||||
for _, pkg := range alrPkgs {
|
||||
res, err := b.BuildPackageFromDb(
|
||||
ctx,
|
||||
&BuildPackageFromDbArgs{
|
||||
Package: &pkg,
|
||||
Packages: []string{},
|
||||
BuildArgs: BuildArgs{
|
||||
Opts: input.BuildOpts(),
|
||||
Info: input.OSRelease(),
|
||||
PkgFormat_: input.PkgFormat(),
|
||||
},
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = b.installerExecutor.InstallLocal(
|
||||
GetBuiltPaths(res),
|
||||
&manager.Opts{
|
||||
NoConfirm: !input.BuildOpts().Interactive,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Builder) BuildALRDeps(
|
||||
ctx context.Context,
|
||||
input interface {
|
||||
OsInfoProvider
|
||||
BuildOptsProvider
|
||||
PkgFormatProvider
|
||||
},
|
||||
depends []string,
|
||||
) (buildDeps []*BuiltDep, repoDeps []string, err error) {
|
||||
if len(depends) > 0 {
|
||||
slog.Info(gotext.Get("Installing dependencies"))
|
||||
|
||||
found, notFound, err := b.repos.FindPkgs(ctx, depends) // Поиск зависимостей
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
repoDeps = notFound
|
||||
|
||||
// Если для некоторых пакетов есть несколько опций, упрощаем их все в один срез
|
||||
pkgs := cliutils.FlattenPkgs(
|
||||
ctx,
|
||||
found,
|
||||
"install",
|
||||
input.BuildOpts().Interactive,
|
||||
)
|
||||
type item struct {
|
||||
pkg *db.Package
|
||||
packages []string
|
||||
}
|
||||
pkgsMap := make(map[string]*item)
|
||||
for _, pkg := range pkgs {
|
||||
name := pkg.BasePkgName
|
||||
if name == "" {
|
||||
name = pkg.Name
|
||||
}
|
||||
if pkgsMap[name] == nil {
|
||||
pkgsMap[name] = &item{
|
||||
pkg: &pkg,
|
||||
}
|
||||
}
|
||||
pkgsMap[name].packages = append(
|
||||
pkgsMap[name].packages,
|
||||
pkg.Name,
|
||||
)
|
||||
}
|
||||
|
||||
for basePkgName := range pkgsMap {
|
||||
pkg := pkgsMap[basePkgName].pkg
|
||||
res, err := b.BuildPackageFromDb(
|
||||
ctx,
|
||||
&BuildPackageFromDbArgs{
|
||||
Package: pkg,
|
||||
Packages: pkgsMap[basePkgName].packages,
|
||||
BuildArgs: BuildArgs{
|
||||
Opts: input.BuildOpts(),
|
||||
Info: input.OSRelease(),
|
||||
PkgFormat_: input.PkgFormat(),
|
||||
},
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
buildDeps = append(buildDeps, res...)
|
||||
}
|
||||
}
|
||||
|
||||
repoDeps = removeDuplicates(repoDeps)
|
||||
buildDeps = removeDuplicates(buildDeps)
|
||||
|
||||
return buildDeps, repoDeps, nil
|
||||
}
|
||||
|
||||
func (i *Builder) installBuildDeps(
|
||||
ctx context.Context,
|
||||
input interface {
|
||||
OsInfoProvider
|
||||
BuildOptsProvider
|
||||
PkgFormatProvider
|
||||
},
|
||||
pkgs []string,
|
||||
) ([]*BuiltDep, error) {
|
||||
var builtDeps []*BuiltDep
|
||||
if len(pkgs) > 0 {
|
||||
deps, err := i.installerExecutor.RemoveAlreadyInstalled(pkgs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
builtDeps, err = i.InstallPkgs(ctx, input, deps) // Устанавливаем выбранные пакеты
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return builtDeps, nil
|
||||
}
|
||||
|
||||
func (i *Builder) installOptDeps(
|
||||
ctx context.Context,
|
||||
input interface {
|
||||
OsInfoProvider
|
||||
BuildOptsProvider
|
||||
PkgFormatProvider
|
||||
},
|
||||
pkgs []string,
|
||||
) ([]*BuiltDep, error) {
|
||||
var builtDeps []*BuiltDep
|
||||
optDeps, err := i.installerExecutor.RemoveAlreadyInstalled(pkgs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(optDeps) > 0 {
|
||||
optDeps, err := cliutils.ChooseOptDepends(
|
||||
ctx,
|
||||
optDeps,
|
||||
"install",
|
||||
input.BuildOpts().Interactive,
|
||||
) // Пользователя просят выбрать опциональные зависимости
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(optDeps) == 0 {
|
||||
return builtDeps, nil
|
||||
}
|
||||
|
||||
builtDeps, err = i.InstallPkgs(ctx, input, optDeps) // Устанавливаем выбранные пакеты
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return builtDeps, nil
|
||||
}
|
||||
|
||||
func (i *Builder) InstallPkgs(
|
||||
ctx context.Context,
|
||||
input interface {
|
||||
OsInfoProvider
|
||||
BuildOptsProvider
|
||||
PkgFormatProvider
|
||||
},
|
||||
pkgs []string,
|
||||
) ([]*BuiltDep, error) {
|
||||
builtDeps, repoDeps, err := i.BuildALRDeps(ctx, input, pkgs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(builtDeps) > 0 {
|
||||
err = i.installerExecutor.InstallLocal(GetBuiltPaths(builtDeps), &manager.Opts{
|
||||
NoConfirm: !input.BuildOpts().Interactive,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if len(repoDeps) > 0 {
|
||||
err = i.installerExecutor.Install(repoDeps, &manager.Opts{
|
||||
NoConfirm: !input.BuildOpts().Interactive,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return builtDeps, nil
|
||||
}
|
286
internal/build/build_internal_test.need-to-update
Normal file
286
internal/build/build_internal_test.need-to-update
Normal file
@ -0,0 +1,286 @@
|
||||
// ALR - Any Linux Repository
|
||||
// Copyright (C) 2025 The ALR Authors
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package build
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"mvdan.cc/sh/v3/syntax"
|
||||
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/config"
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/db"
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/types"
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/distro"
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/manager"
|
||||
)
|
||||
|
||||
type TestPackageFinder struct {
|
||||
FindPkgsFunc func(ctx context.Context, pkgs []string) (map[string][]db.Package, []string, error)
|
||||
}
|
||||
|
||||
func (pf *TestPackageFinder) FindPkgs(ctx context.Context, pkgs []string) (map[string][]db.Package, []string, error) {
|
||||
if pf.FindPkgsFunc != nil {
|
||||
return pf.FindPkgsFunc(ctx, pkgs)
|
||||
}
|
||||
return map[string][]db.Package{}, []string{}, nil
|
||||
}
|
||||
|
||||
type TestManager struct {
|
||||
NameFunc func() string
|
||||
FormatFunc func() string
|
||||
ExistsFunc func() bool
|
||||
SetRootCmdFunc func(cmd string)
|
||||
SyncFunc func(opts *manager.Opts) error
|
||||
InstallFunc func(opts *manager.Opts, pkgs ...string) error
|
||||
RemoveFunc func(opts *manager.Opts, pkgs ...string) error
|
||||
UpgradeFunc func(opts *manager.Opts, pkgs ...string) error
|
||||
InstallLocalFunc func(opts *manager.Opts, files ...string) error
|
||||
UpgradeAllFunc func(opts *manager.Opts) error
|
||||
ListInstalledFunc func(opts *manager.Opts) (map[string]string, error)
|
||||
IsInstalledFunc func(pkg string) (bool, error)
|
||||
}
|
||||
|
||||
func (m *TestManager) Name() string {
|
||||
if m.NameFunc != nil {
|
||||
return m.NameFunc()
|
||||
}
|
||||
return "TestManager"
|
||||
}
|
||||
|
||||
func (m *TestManager) Format() string {
|
||||
if m.FormatFunc != nil {
|
||||
return m.FormatFunc()
|
||||
}
|
||||
return "testpkg"
|
||||
}
|
||||
|
||||
func (m *TestManager) Exists() bool {
|
||||
if m.ExistsFunc != nil {
|
||||
return m.ExistsFunc()
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (m *TestManager) SetRootCmd(cmd string) {
|
||||
if m.SetRootCmdFunc != nil {
|
||||
m.SetRootCmdFunc(cmd)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *TestManager) Sync(opts *manager.Opts) error {
|
||||
if m.SyncFunc != nil {
|
||||
return m.SyncFunc(opts)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *TestManager) Install(opts *manager.Opts, pkgs ...string) error {
|
||||
if m.InstallFunc != nil {
|
||||
return m.InstallFunc(opts, pkgs...)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *TestManager) Remove(opts *manager.Opts, pkgs ...string) error {
|
||||
if m.RemoveFunc != nil {
|
||||
return m.RemoveFunc(opts, pkgs...)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *TestManager) Upgrade(opts *manager.Opts, pkgs ...string) error {
|
||||
if m.UpgradeFunc != nil {
|
||||
return m.UpgradeFunc(opts, pkgs...)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *TestManager) InstallLocal(opts *manager.Opts, files ...string) error {
|
||||
if m.InstallLocalFunc != nil {
|
||||
return m.InstallLocalFunc(opts, files...)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *TestManager) UpgradeAll(opts *manager.Opts) error {
|
||||
if m.UpgradeAllFunc != nil {
|
||||
return m.UpgradeAllFunc(opts)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *TestManager) ListInstalled(opts *manager.Opts) (map[string]string, error) {
|
||||
if m.ListInstalledFunc != nil {
|
||||
return m.ListInstalledFunc(opts)
|
||||
}
|
||||
return map[string]string{}, nil
|
||||
}
|
||||
|
||||
func (m *TestManager) IsInstalled(pkg string) (bool, error) {
|
||||
if m.IsInstalledFunc != nil {
|
||||
return m.IsInstalledFunc(pkg)
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
type TestConfig struct{}
|
||||
|
||||
func (c *TestConfig) PagerStyle() string {
|
||||
return "native"
|
||||
}
|
||||
|
||||
func (c *TestConfig) GetPaths() *config.Paths {
|
||||
return &config.Paths{
|
||||
CacheDir: "/tmp",
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecuteFirstPassIsSecure(t *testing.T) {
|
||||
cfg := &TestConfig{}
|
||||
pf := &TestPackageFinder{}
|
||||
info := &distro.OSRelease{}
|
||||
m := &TestManager{}
|
||||
|
||||
opts := types.BuildOpts{
|
||||
Manager: m,
|
||||
Interactive: false,
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
b := NewBuilder(
|
||||
ctx,
|
||||
opts,
|
||||
pf,
|
||||
info,
|
||||
cfg,
|
||||
)
|
||||
|
||||
tmpFile, err := os.CreateTemp("", "testfile-")
|
||||
assert.NoError(t, err)
|
||||
tmpFilePath := tmpFile.Name()
|
||||
defer os.Remove(tmpFilePath)
|
||||
|
||||
_, err = os.Stat(tmpFilePath)
|
||||
assert.NoError(t, err)
|
||||
|
||||
testScript := fmt.Sprintf(`name='test'
|
||||
version=1.0.0
|
||||
release=1
|
||||
rm -f %s`, tmpFilePath)
|
||||
|
||||
fl, err := syntax.NewParser().Parse(strings.NewReader(testScript), "alr.sh")
|
||||
assert.NoError(t, err)
|
||||
|
||||
_, _, err = b.executeFirstPass(fl)
|
||||
assert.NoError(t, err)
|
||||
|
||||
_, err = os.Stat(tmpFilePath)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestExecuteFirstPassIsCorrect(t *testing.T) {
|
||||
type testCase struct {
|
||||
Name string
|
||||
Script string
|
||||
Opts types.BuildOpts
|
||||
Expected func(t *testing.T, vars []*types.BuildVars)
|
||||
}
|
||||
|
||||
for _, tc := range []testCase{{
|
||||
Name: "single package",
|
||||
Script: `name='test'
|
||||
version='1.0.0'
|
||||
release=1
|
||||
epoch=2
|
||||
desc="Test package"
|
||||
homepage='https://example.com'
|
||||
maintainer='Ivan Ivanov'
|
||||
`,
|
||||
Opts: types.BuildOpts{
|
||||
Manager: &TestManager{},
|
||||
Interactive: false,
|
||||
},
|
||||
Expected: func(t *testing.T, vars []*types.BuildVars) {
|
||||
assert.Equal(t, 1, len(vars))
|
||||
assert.Equal(t, vars[0].Name, "test")
|
||||
assert.Equal(t, vars[0].Version, "1.0.0")
|
||||
assert.Equal(t, vars[0].Release, int(1))
|
||||
assert.Equal(t, vars[0].Epoch, uint(2))
|
||||
assert.Equal(t, vars[0].Description, "Test package")
|
||||
},
|
||||
}, {
|
||||
Name: "multiple packages",
|
||||
Script: `name=(
|
||||
foo
|
||||
bar
|
||||
)
|
||||
|
||||
version='0.0.1'
|
||||
release=1
|
||||
epoch=2
|
||||
desc="Test package"
|
||||
|
||||
meta_foo() {
|
||||
desc="Foo package"
|
||||
}
|
||||
|
||||
meta_bar() {
|
||||
|
||||
}
|
||||
`,
|
||||
Opts: types.BuildOpts{
|
||||
Packages: []string{"foo"},
|
||||
Manager: &TestManager{},
|
||||
Interactive: false,
|
||||
},
|
||||
Expected: func(t *testing.T, vars []*types.BuildVars) {
|
||||
assert.Equal(t, 1, len(vars))
|
||||
assert.Equal(t, vars[0].Name, "foo")
|
||||
assert.Equal(t, vars[0].Description, "Foo package")
|
||||
},
|
||||
}} {
|
||||
t.Run(tc.Name, func(t *testing.T) {
|
||||
cfg := &TestConfig{}
|
||||
pf := &TestPackageFinder{}
|
||||
info := &distro.OSRelease{}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
b := NewBuilder(
|
||||
ctx,
|
||||
tc.Opts,
|
||||
pf,
|
||||
info,
|
||||
cfg,
|
||||
)
|
||||
|
||||
fl, err := syntax.NewParser().Parse(strings.NewReader(tc.Script), "alr.sh")
|
||||
assert.NoError(t, err)
|
||||
|
||||
_, allVars, err := b.scriptExecutor.ExecuteSecondPass(fl)
|
||||
assert.NoError(t, err)
|
||||
|
||||
tc.Expected(t, allVars)
|
||||
})
|
||||
}
|
||||
}
|
69
internal/build/cache.go
Normal file
69
internal/build/cache.go
Normal file
@ -0,0 +1,69 @@
|
||||
// ALR - Any Linux Repository
|
||||
// Copyright (C) 2025 The ALR Authors
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package build
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/goreleaser/nfpm/v2"
|
||||
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/types"
|
||||
)
|
||||
|
||||
type Cache struct {
|
||||
cfg Config
|
||||
}
|
||||
|
||||
func (c *Cache) CheckForBuiltPackage(
|
||||
ctx context.Context,
|
||||
input *BuildInput,
|
||||
vars *types.BuildVars,
|
||||
) (string, bool, error) {
|
||||
filename, err := pkgFileName(input, vars)
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
|
||||
pkgPath := filepath.Join(getBaseDir(c.cfg, vars.Name), filename)
|
||||
|
||||
_, err = os.Stat(pkgPath)
|
||||
if err != nil {
|
||||
return "", false, nil
|
||||
}
|
||||
|
||||
return pkgPath, true, nil
|
||||
}
|
||||
|
||||
func pkgFileName(
|
||||
input interface {
|
||||
OsInfoProvider
|
||||
PkgFormatProvider
|
||||
RepositoryProvider
|
||||
},
|
||||
vars *types.BuildVars,
|
||||
) (string, error) {
|
||||
pkgInfo := getBasePkgInfo(vars, input)
|
||||
|
||||
packager, err := nfpm.Get(input.PkgFormat())
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return packager.ConventionalFileName(pkgInfo), nil
|
||||
}
|
74
internal/build/checker.go
Normal file
74
internal/build/checker.go
Normal file
@ -0,0 +1,74 @@
|
||||
// ALR - Any Linux Repository
|
||||
// Copyright (C) 2025 The ALR Authors
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package build
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
|
||||
"github.com/leonelquinteros/gotext"
|
||||
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/cliutils"
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/cpu"
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/manager"
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/types"
|
||||
)
|
||||
|
||||
type Checker struct {
|
||||
mgr manager.Manager
|
||||
}
|
||||
|
||||
func (c *Checker) PerformChecks(
|
||||
ctx context.Context,
|
||||
input *BuildInput,
|
||||
vars *types.BuildVars,
|
||||
) (bool, error) {
|
||||
if !cpu.IsCompatibleWith(cpu.Arch(), vars.Architectures) { // Проверяем совместимость архитектуры
|
||||
cont, err := cliutils.YesNoPrompt(
|
||||
ctx,
|
||||
gotext.Get("Your system's CPU architecture doesn't match this package. Do you want to build anyway?"),
|
||||
input.opts.Interactive,
|
||||
true,
|
||||
)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if !cont {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
|
||||
installed, err := c.mgr.ListInstalled(nil)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
filename, err := pkgFileName(input, vars)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if instVer, ok := installed[filename]; ok { // Если пакет уже установлен, выводим предупреждение
|
||||
slog.Warn(gotext.Get("This package is already installed"),
|
||||
"name", vars.Name,
|
||||
"version", instVer,
|
||||
)
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
71
internal/build/dirs.go
Normal file
71
internal/build/dirs.go
Normal file
@ -0,0 +1,71 @@
|
||||
// ALR - Any Linux Repository
|
||||
// Copyright (C) 2025 The ALR Authors
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package build
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/types"
|
||||
)
|
||||
|
||||
type BaseDirProvider interface {
|
||||
BaseDir() string
|
||||
}
|
||||
|
||||
type SrcDirProvider interface {
|
||||
SrcDir() string
|
||||
}
|
||||
|
||||
type PkgDirProvider interface {
|
||||
PkgDir() string
|
||||
}
|
||||
|
||||
type ScriptDirProvider interface {
|
||||
ScriptDir() string
|
||||
}
|
||||
|
||||
func getDirs(
|
||||
cfg Config,
|
||||
scriptPath string,
|
||||
basePkg string,
|
||||
) (types.Directories, error) {
|
||||
pkgsDir := cfg.GetPaths().PkgsDir
|
||||
|
||||
scriptPath, err := filepath.Abs(scriptPath)
|
||||
if err != nil {
|
||||
return types.Directories{}, err
|
||||
}
|
||||
baseDir := filepath.Join(pkgsDir, basePkg)
|
||||
return types.Directories{
|
||||
BaseDir: getBaseDir(cfg, basePkg),
|
||||
SrcDir: getSrcDir(cfg, basePkg),
|
||||
PkgDir: filepath.Join(baseDir, "pkg"),
|
||||
ScriptDir: getScriptDir(scriptPath),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func getBaseDir(cfg Config, basePkg string) string {
|
||||
return filepath.Join(cfg.GetPaths().PkgsDir, basePkg)
|
||||
}
|
||||
|
||||
func getSrcDir(cfg Config, basePkg string) string {
|
||||
return filepath.Join(getBaseDir(cfg, basePkg), "src")
|
||||
}
|
||||
|
||||
func getScriptDir(scriptPath string) string {
|
||||
return filepath.Dir(scriptPath)
|
||||
}
|
96
internal/build/find_deps/alt_linux.go
Normal file
96
internal/build/find_deps/alt_linux.go
Normal file
@ -0,0 +1,96 @@
|
||||
// ALR - Any Linux Repository
|
||||
// Copyright (C) 2025 The ALR Authors
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package finddeps
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"log/slog"
|
||||
"os/exec"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/goreleaser/nfpm/v2"
|
||||
"github.com/leonelquinteros/gotext"
|
||||
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/types"
|
||||
)
|
||||
|
||||
func rpmFindDependenciesALTLinux(ctx context.Context, pkgInfo *nfpm.Info, dirs types.Directories, command string, envs []string, updateFunc func(string)) error {
|
||||
if _, err := exec.LookPath(command); err != nil {
|
||||
slog.Info(gotext.Get("Command not found on the system"), "command", command)
|
||||
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.CommandContext(ctx, command)
|
||||
cmd.Stdin = bytes.NewBufferString(strings.Join(paths, "\n") + "\n")
|
||||
cmd.Env = append(cmd.Env,
|
||||
"RPM_BUILD_ROOT="+dirs.PkgDir,
|
||||
"RPM_FINDPROV_METHOD=",
|
||||
"RPM_FINDREQ_METHOD=",
|
||||
"RPM_DATADIR=",
|
||||
"RPM_SUBPACKAGE_NAME=",
|
||||
)
|
||||
cmd.Env = append(cmd.Env, envs...)
|
||||
var out bytes.Buffer
|
||||
var stderr bytes.Buffer
|
||||
cmd.Stdout = &out
|
||||
cmd.Stderr = &stderr
|
||||
if err := cmd.Run(); err != nil {
|
||||
slog.Error(stderr.String())
|
||||
return err
|
||||
}
|
||||
slog.Debug(stderr.String())
|
||||
|
||||
dependencies := strings.Split(strings.TrimSpace(out.String()), "\n")
|
||||
for _, dep := range dependencies {
|
||||
if dep != "" {
|
||||
updateFunc(dep)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type ALTLinuxFindProvReq struct{}
|
||||
|
||||
func (o *ALTLinuxFindProvReq) FindProvides(ctx context.Context, pkgInfo *nfpm.Info, dirs types.Directories, skiplist []string) error {
|
||||
return rpmFindDependenciesALTLinux(ctx, pkgInfo, dirs, "/usr/lib/rpm/find-provides", []string{"RPM_FINDPROV_SKIPLIST=" + strings.Join(skiplist, "\n")}, func(dep string) {
|
||||
slog.Info(gotext.Get("Provided dependency found"), "dep", dep)
|
||||
pkgInfo.Overridables.Provides = append(pkgInfo.Overridables.Provides, dep)
|
||||
})
|
||||
}
|
||||
|
||||
func (o *ALTLinuxFindProvReq) FindRequires(ctx context.Context, pkgInfo *nfpm.Info, dirs types.Directories, skiplist []string) error {
|
||||
return rpmFindDependenciesALTLinux(ctx, pkgInfo, dirs, "/usr/lib/rpm/find-requires", []string{"RPM_FINDREQ_SKIPLIST=" + strings.Join(skiplist, "\n")}, func(dep string) {
|
||||
slog.Info(gotext.Get("Required dependency found"), "dep", dep)
|
||||
pkgInfo.Overridables.Depends = append(pkgInfo.Overridables.Depends, dep)
|
||||
})
|
||||
}
|
39
internal/build/find_deps/empty.go
Normal file
39
internal/build/find_deps/empty.go
Normal file
@ -0,0 +1,39 @@
|
||||
// ALR - Any Linux Repository
|
||||
// Copyright (C) 2025 The ALR Authors
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package finddeps
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
|
||||
"github.com/goreleaser/nfpm/v2"
|
||||
"github.com/leonelquinteros/gotext"
|
||||
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/types"
|
||||
)
|
||||
|
||||
type EmptyFindProvReq struct{}
|
||||
|
||||
func (o *EmptyFindProvReq) FindProvides(ctx context.Context, pkgInfo *nfpm.Info, dirs types.Directories, skiplist []string) error {
|
||||
slog.Info(gotext.Get("AutoProv is not implemented for this package format, so it's skipped"))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (o *EmptyFindProvReq) FindRequires(ctx context.Context, pkgInfo *nfpm.Info, dirs types.Directories, skiplist []string) error {
|
||||
slog.Info(gotext.Get("AutoReq is not implemented for this package format, so it's skipped"))
|
||||
return nil
|
||||
}
|
118
internal/build/find_deps/fedora.go
Normal file
118
internal/build/find_deps/fedora.go
Normal file
@ -0,0 +1,118 @@
|
||||
// ALR - Any Linux Repository
|
||||
// Copyright (C) 2025 The ALR Authors
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package finddeps
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os/exec"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/goreleaser/nfpm/v2"
|
||||
"github.com/leonelquinteros/gotext"
|
||||
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/types"
|
||||
)
|
||||
|
||||
type FedoraFindProvReq struct{}
|
||||
|
||||
func rpmFindDependenciesFedora(ctx context.Context, pkgInfo *nfpm.Info, dirs types.Directories, command string, args []string, updateFunc func(string)) error {
|
||||
if _, err := exec.LookPath(command); err != nil {
|
||||
slog.Info(gotext.Get("Command not found on the system"), "command", command)
|
||||
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.CommandContext(ctx, command, args...)
|
||||
cmd.Stdin = bytes.NewBufferString(strings.Join(paths, "\n") + "\n")
|
||||
cmd.Env = append(cmd.Env,
|
||||
"RPM_BUILD_ROOT="+dirs.PkgDir,
|
||||
)
|
||||
var out bytes.Buffer
|
||||
var stderr bytes.Buffer
|
||||
cmd.Stdout = &out
|
||||
cmd.Stderr = &stderr
|
||||
if err := cmd.Run(); err != nil {
|
||||
slog.Error(stderr.String())
|
||||
return err
|
||||
}
|
||||
slog.Debug(stderr.String())
|
||||
|
||||
dependencies := strings.Split(strings.TrimSpace(out.String()), "\n")
|
||||
for _, dep := range dependencies {
|
||||
if dep != "" {
|
||||
updateFunc(dep)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (o *FedoraFindProvReq) FindProvides(ctx context.Context, pkgInfo *nfpm.Info, dirs types.Directories, skiplist []string) error {
|
||||
return rpmFindDependenciesFedora(
|
||||
ctx,
|
||||
pkgInfo,
|
||||
dirs,
|
||||
"/usr/lib/rpm/rpmdeps",
|
||||
[]string{
|
||||
"--define=_use_internal_dependency_generator 1",
|
||||
"--provides",
|
||||
fmt.Sprintf(
|
||||
"--define=__provides_exclude_from %s\"",
|
||||
strings.Join(skiplist, "|"),
|
||||
),
|
||||
},
|
||||
func(dep string) {
|
||||
slog.Info(gotext.Get("Provided dependency found"), "dep", dep)
|
||||
pkgInfo.Overridables.Provides = append(pkgInfo.Overridables.Provides, dep)
|
||||
})
|
||||
}
|
||||
|
||||
func (o *FedoraFindProvReq) FindRequires(ctx context.Context, pkgInfo *nfpm.Info, dirs types.Directories, skiplist []string) error {
|
||||
return rpmFindDependenciesFedora(
|
||||
ctx,
|
||||
pkgInfo,
|
||||
dirs,
|
||||
"/usr/lib/rpm/rpmdeps",
|
||||
[]string{
|
||||
"--define=_use_internal_dependency_generator 1",
|
||||
"--requires",
|
||||
fmt.Sprintf(
|
||||
"--define=__requires_exclude_from %s",
|
||||
strings.Join(skiplist, "|"),
|
||||
),
|
||||
},
|
||||
func(dep string) {
|
||||
slog.Info(gotext.Get("Required dependency found"), "dep", dep)
|
||||
pkgInfo.Overridables.Depends = append(pkgInfo.Overridables.Depends, dep)
|
||||
})
|
||||
}
|
58
internal/build/find_deps/find_deps.go
Normal file
58
internal/build/find_deps/find_deps.go
Normal file
@ -0,0 +1,58 @@
|
||||
// ALR - Any Linux Repository
|
||||
// Copyright (C) 2025 The ALR Authors
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package finddeps
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/goreleaser/nfpm/v2"
|
||||
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/distro"
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/types"
|
||||
)
|
||||
|
||||
type ProvReqFinder interface {
|
||||
FindProvides(ctx context.Context, pkgInfo *nfpm.Info, dirs types.Directories, skiplist []string) error
|
||||
FindRequires(ctx context.Context, pkgInfo *nfpm.Info, dirs types.Directories, skiplist []string) error
|
||||
}
|
||||
|
||||
type ProvReqService struct {
|
||||
finder ProvReqFinder
|
||||
}
|
||||
|
||||
func New(info *distro.OSRelease, pkgFormat string) *ProvReqService {
|
||||
s := &ProvReqService{
|
||||
finder: &EmptyFindProvReq{},
|
||||
}
|
||||
if pkgFormat == "rpm" {
|
||||
switch info.ID {
|
||||
case "altlinux":
|
||||
s.finder = &ALTLinuxFindProvReq{}
|
||||
case "fedora":
|
||||
s.finder = &FedoraFindProvReq{}
|
||||
}
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *ProvReqService) FindProvides(ctx context.Context, pkgInfo *nfpm.Info, dirs types.Directories, skiplist []string) error {
|
||||
return s.finder.FindProvides(ctx, pkgInfo, dirs, skiplist)
|
||||
}
|
||||
|
||||
func (s *ProvReqService) FindRequires(ctx context.Context, pkgInfo *nfpm.Info, dirs types.Directories, skiplist []string) error {
|
||||
return s.finder.FindRequires(ctx, pkgInfo, dirs, skiplist)
|
||||
}
|
54
internal/build/installer.go
Normal file
54
internal/build/installer.go
Normal file
@ -0,0 +1,54 @@
|
||||
// ALR - Any Linux Repository
|
||||
// Copyright (C) 2025 The ALR Authors
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package build
|
||||
|
||||
import (
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/manager"
|
||||
)
|
||||
|
||||
func NewInstaller(mgr manager.Manager) *Installer {
|
||||
return &Installer{
|
||||
mgr: mgr,
|
||||
}
|
||||
}
|
||||
|
||||
type Installer struct{ mgr manager.Manager }
|
||||
|
||||
func (i *Installer) InstallLocal(paths []string, opts *manager.Opts) error {
|
||||
return i.mgr.InstallLocal(opts, paths...)
|
||||
}
|
||||
|
||||
func (i *Installer) Install(pkgs []string, opts *manager.Opts) error {
|
||||
return i.mgr.Install(opts, pkgs...)
|
||||
}
|
||||
|
||||
func (i *Installer) RemoveAlreadyInstalled(pkgs []string) ([]string, error) {
|
||||
filteredPackages := []string{}
|
||||
|
||||
for _, dep := range pkgs {
|
||||
installed, err := i.mgr.IsInstalled(dep)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if installed {
|
||||
continue
|
||||
}
|
||||
filteredPackages = append(filteredPackages, dep)
|
||||
}
|
||||
|
||||
return filteredPackages, nil
|
||||
}
|
52
internal/build/main_build.go
Normal file
52
internal/build/main_build.go
Normal file
@ -0,0 +1,52 @@
|
||||
// ALR - Any Linux Repository
|
||||
// Copyright (C) 2025 The ALR Authors
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package build
|
||||
|
||||
import (
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/manager"
|
||||
)
|
||||
|
||||
func NewMainBuilder(
|
||||
cfg Config,
|
||||
mgr manager.Manager,
|
||||
repos PackageFinder,
|
||||
scriptExecutor ScriptExecutor,
|
||||
installerExecutor InstallerExecutor,
|
||||
) (*Builder, error) {
|
||||
builder := &Builder{
|
||||
scriptExecutor: scriptExecutor,
|
||||
cacheExecutor: &Cache{
|
||||
cfg,
|
||||
},
|
||||
scriptResolver: &ScriptResolver{
|
||||
cfg,
|
||||
},
|
||||
scriptViewerExecutor: &ScriptViewer{
|
||||
config: cfg,
|
||||
},
|
||||
checkerExecutor: &Checker{
|
||||
mgr,
|
||||
},
|
||||
installerExecutor: installerExecutor,
|
||||
sourceExecutor: &SourceDownloader{
|
||||
cfg,
|
||||
},
|
||||
repos: repos,
|
||||
}
|
||||
|
||||
return builder, nil
|
||||
}
|
40
internal/build/safe_common.go
Normal file
40
internal/build/safe_common.go
Normal file
@ -0,0 +1,40 @@
|
||||
// ALR - Any Linux Repository
|
||||
// Copyright (C) 2025 The ALR Authors
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package build
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func setCommonCmdEnv(cmd *exec.Cmd) {
|
||||
cmd.Env = []string{
|
||||
"HOME=/var/cache/alr",
|
||||
"LOGNAME=alr",
|
||||
"USER=alr",
|
||||
"PATH=/usr/bin:/bin:/usr/local/bin",
|
||||
}
|
||||
for _, env := range os.Environ() {
|
||||
if strings.HasPrefix(env, "LANG=") ||
|
||||
strings.HasPrefix(env, "LANGUAGE=") ||
|
||||
strings.HasPrefix(env, "LC_") ||
|
||||
strings.HasPrefix(env, "ALR_LOG_LEVEL=") {
|
||||
cmd.Env = append(cmd.Env, env)
|
||||
}
|
||||
}
|
||||
}
|
150
internal/build/safe_installer.go
Normal file
150
internal/build/safe_installer.go
Normal file
@ -0,0 +1,150 @@
|
||||
// ALR - Any Linux Repository
|
||||
// Copyright (C) 2025 The ALR Authors
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package build
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/rpc"
|
||||
"os"
|
||||
"os/exec"
|
||||
"sync"
|
||||
"syscall"
|
||||
|
||||
"github.com/hashicorp/go-plugin"
|
||||
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/logger"
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/manager"
|
||||
)
|
||||
|
||||
type InstallerPlugin struct {
|
||||
Impl InstallerExecutor
|
||||
}
|
||||
|
||||
type InstallerRPC struct {
|
||||
client *rpc.Client
|
||||
}
|
||||
|
||||
type InstallerRPCServer struct {
|
||||
Impl InstallerExecutor
|
||||
}
|
||||
|
||||
type InstallArgs struct {
|
||||
PackagesOrPaths []string
|
||||
Opts *manager.Opts
|
||||
}
|
||||
|
||||
func (r *InstallerRPC) InstallLocal(paths []string, opts *manager.Opts) error {
|
||||
return r.client.Call("Plugin.InstallLocal", &InstallArgs{
|
||||
PackagesOrPaths: paths,
|
||||
Opts: opts,
|
||||
}, nil)
|
||||
}
|
||||
|
||||
func (s *InstallerRPCServer) InstallLocal(args *InstallArgs, reply *struct{}) error {
|
||||
return s.Impl.InstallLocal(args.PackagesOrPaths, args.Opts)
|
||||
}
|
||||
|
||||
func (r *InstallerRPC) Install(pkgs []string, opts *manager.Opts) error {
|
||||
return r.client.Call("Plugin.Install", &InstallArgs{
|
||||
PackagesOrPaths: pkgs,
|
||||
Opts: opts,
|
||||
}, nil)
|
||||
}
|
||||
|
||||
func (s *InstallerRPCServer) Install(args *InstallArgs, reply *struct{}) error {
|
||||
return s.Impl.Install(args.PackagesOrPaths, args.Opts)
|
||||
}
|
||||
|
||||
func (r *InstallerRPC) RemoveAlreadyInstalled(paths []string) ([]string, error) {
|
||||
var val []string
|
||||
err := r.client.Call("Plugin.RemoveAlreadyInstalled", paths, &val)
|
||||
return val, err
|
||||
}
|
||||
|
||||
func (s *InstallerRPCServer) RemoveAlreadyInstalled(pkgs []string, res *[]string) error {
|
||||
vars, err := s.Impl.RemoveAlreadyInstalled(pkgs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*res = vars
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *InstallerPlugin) Client(b *plugin.MuxBroker, c *rpc.Client) (interface{}, error) {
|
||||
return &InstallerRPC{client: c}, nil
|
||||
}
|
||||
|
||||
func (p *InstallerPlugin) Server(*plugin.MuxBroker) (interface{}, error) {
|
||||
return &InstallerRPCServer{Impl: p.Impl}, nil
|
||||
}
|
||||
|
||||
func GetSafeInstaller() (InstallerExecutor, func(), error) {
|
||||
var err error
|
||||
|
||||
executable, err := os.Executable()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
cmd := exec.Command(executable, "_internal-installer")
|
||||
setCommonCmdEnv(cmd)
|
||||
|
||||
slog.Debug("safe installer setup", "uid", syscall.Getuid(), "gid", syscall.Getgid())
|
||||
|
||||
client := plugin.NewClient(&plugin.ClientConfig{
|
||||
HandshakeConfig: HandshakeConfig,
|
||||
Plugins: pluginMap,
|
||||
Cmd: cmd,
|
||||
Logger: logger.GetHCLoggerAdapter(),
|
||||
SkipHostEnv: true,
|
||||
UnixSocketConfig: &plugin.UnixSocketConfig{
|
||||
Group: "alr",
|
||||
},
|
||||
SyncStderr: os.Stderr,
|
||||
})
|
||||
rpcClient, err := client.Client()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
var cleanupOnce sync.Once
|
||||
cleanup := func() {
|
||||
cleanupOnce.Do(func() {
|
||||
client.Kill()
|
||||
})
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err != nil {
|
||||
slog.Debug("close installer")
|
||||
cleanup()
|
||||
}
|
||||
}()
|
||||
|
||||
raw, err := rpcClient.Dispense("installer")
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
executor, ok := raw.(InstallerExecutor)
|
||||
if !ok {
|
||||
err = fmt.Errorf("dispensed object is not a ScriptExecutor (got %T)", raw)
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return executor, cleanup, nil
|
||||
}
|
273
internal/build/safe_script_executor.go
Normal file
273
internal/build/safe_script_executor.go
Normal file
@ -0,0 +1,273 @@
|
||||
// ALR - Any Linux Repository
|
||||
// Copyright (C) 2025 The ALR Authors
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package build
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/rpc"
|
||||
"os"
|
||||
"os/exec"
|
||||
"sync"
|
||||
|
||||
"github.com/hashicorp/go-plugin"
|
||||
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/logger"
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/types"
|
||||
)
|
||||
|
||||
var HandshakeConfig = plugin.HandshakeConfig{
|
||||
ProtocolVersion: 1,
|
||||
MagicCookieKey: "ALR_PLUGIN",
|
||||
MagicCookieValue: "-",
|
||||
}
|
||||
|
||||
type ScriptExecutorPlugin struct {
|
||||
Impl ScriptExecutor
|
||||
}
|
||||
|
||||
type ScriptExecutorRPCServer struct {
|
||||
Impl ScriptExecutor
|
||||
}
|
||||
|
||||
// =============================
|
||||
//
|
||||
// ReadScript
|
||||
//
|
||||
|
||||
func (s *ScriptExecutorRPC) ReadScript(ctx context.Context, scriptPath string) (*ScriptFile, error) {
|
||||
var resp *ScriptFile
|
||||
err := s.client.Call("Plugin.ReadScript", scriptPath, &resp)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func (s *ScriptExecutorRPCServer) ReadScript(scriptPath string, resp *ScriptFile) error {
|
||||
file, err := s.Impl.ReadScript(context.Background(), scriptPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*resp = *file
|
||||
return nil
|
||||
}
|
||||
|
||||
// =============================
|
||||
//
|
||||
// ExecuteFirstPass
|
||||
//
|
||||
|
||||
type ExecuteFirstPassArgs struct {
|
||||
Input *BuildInput
|
||||
Sf *ScriptFile
|
||||
}
|
||||
|
||||
type ExecuteFirstPassResp struct {
|
||||
BasePkg string
|
||||
VarsOfPackages []*types.BuildVars
|
||||
}
|
||||
|
||||
func (s *ScriptExecutorRPC) ExecuteFirstPass(ctx context.Context, input *BuildInput, sf *ScriptFile) (string, []*types.BuildVars, error) {
|
||||
var resp *ExecuteFirstPassResp
|
||||
err := s.client.Call("Plugin.ExecuteFirstPass", &ExecuteFirstPassArgs{
|
||||
Input: input,
|
||||
Sf: sf,
|
||||
}, &resp)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
return resp.BasePkg, resp.VarsOfPackages, nil
|
||||
}
|
||||
|
||||
func (s *ScriptExecutorRPCServer) ExecuteFirstPass(args *ExecuteFirstPassArgs, resp *ExecuteFirstPassResp) error {
|
||||
basePkg, varsOfPackages, err := s.Impl.ExecuteFirstPass(context.Background(), args.Input, args.Sf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*resp = ExecuteFirstPassResp{
|
||||
BasePkg: basePkg,
|
||||
VarsOfPackages: varsOfPackages,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// =============================
|
||||
//
|
||||
// PrepareDirs
|
||||
//
|
||||
|
||||
type PrepareDirsArgs struct {
|
||||
Input *BuildInput
|
||||
BasePkg string
|
||||
}
|
||||
|
||||
func (s *ScriptExecutorRPC) PrepareDirs(
|
||||
ctx context.Context,
|
||||
input *BuildInput,
|
||||
basePkg string,
|
||||
) error {
|
||||
err := s.client.Call("Plugin.PrepareDirs", &PrepareDirsArgs{
|
||||
Input: input,
|
||||
BasePkg: basePkg,
|
||||
}, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *ScriptExecutorRPCServer) PrepareDirs(args *PrepareDirsArgs, reply *struct{}) error {
|
||||
err := s.Impl.PrepareDirs(
|
||||
context.Background(),
|
||||
args.Input,
|
||||
args.BasePkg,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// =============================
|
||||
//
|
||||
// ExecuteSecondPass
|
||||
//
|
||||
|
||||
type ExecuteSecondPassArgs struct {
|
||||
Input *BuildInput
|
||||
Sf *ScriptFile
|
||||
VarsOfPackages []*types.BuildVars
|
||||
RepoDeps []string
|
||||
BuiltDeps []*BuiltDep
|
||||
BasePkg string
|
||||
}
|
||||
|
||||
func (s *ScriptExecutorRPC) ExecuteSecondPass(
|
||||
ctx context.Context,
|
||||
input *BuildInput,
|
||||
sf *ScriptFile,
|
||||
varsOfPackages []*types.BuildVars,
|
||||
repoDeps []string,
|
||||
builtDeps []*BuiltDep,
|
||||
basePkg string,
|
||||
) ([]*BuiltDep, error) {
|
||||
var resp []*BuiltDep
|
||||
err := s.client.Call("Plugin.ExecuteSecondPass", &ExecuteSecondPassArgs{
|
||||
Input: input,
|
||||
Sf: sf,
|
||||
VarsOfPackages: varsOfPackages,
|
||||
RepoDeps: repoDeps,
|
||||
BuiltDeps: builtDeps,
|
||||
BasePkg: basePkg,
|
||||
}, &resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *ScriptExecutorRPCServer) ExecuteSecondPass(args *ExecuteSecondPassArgs, resp *[]*BuiltDep) error {
|
||||
res, err := s.Impl.ExecuteSecondPass(
|
||||
context.Background(),
|
||||
args.Input,
|
||||
args.Sf,
|
||||
args.VarsOfPackages,
|
||||
args.RepoDeps,
|
||||
args.BuiltDeps,
|
||||
args.BasePkg,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*resp = res
|
||||
return err
|
||||
}
|
||||
|
||||
//
|
||||
// ============================
|
||||
//
|
||||
|
||||
func (p *ScriptExecutorPlugin) Server(*plugin.MuxBroker) (interface{}, error) {
|
||||
return &ScriptExecutorRPCServer{Impl: p.Impl}, nil
|
||||
}
|
||||
|
||||
func (p *ScriptExecutorPlugin) Client(b *plugin.MuxBroker, c *rpc.Client) (interface{}, error) {
|
||||
return &ScriptExecutorRPC{client: c}, nil
|
||||
}
|
||||
|
||||
type ScriptExecutorRPC struct {
|
||||
client *rpc.Client
|
||||
}
|
||||
|
||||
var pluginMap = map[string]plugin.Plugin{
|
||||
"script-executor": &ScriptExecutorPlugin{},
|
||||
"installer": &InstallerPlugin{},
|
||||
}
|
||||
|
||||
func GetSafeScriptExecutor() (ScriptExecutor, func(), error) {
|
||||
var err error
|
||||
|
||||
executable, err := os.Executable()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
cmd := exec.Command(executable, "_internal-safe-script-executor")
|
||||
setCommonCmdEnv(cmd)
|
||||
|
||||
client := plugin.NewClient(&plugin.ClientConfig{
|
||||
HandshakeConfig: HandshakeConfig,
|
||||
Plugins: pluginMap,
|
||||
Cmd: cmd,
|
||||
Logger: logger.GetHCLoggerAdapter(),
|
||||
SkipHostEnv: true,
|
||||
UnixSocketConfig: &plugin.UnixSocketConfig{
|
||||
Group: "alr",
|
||||
},
|
||||
SyncStderr: os.Stderr,
|
||||
})
|
||||
rpcClient, err := client.Client()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
var cleanupOnce sync.Once
|
||||
cleanup := func() {
|
||||
cleanupOnce.Do(func() {
|
||||
client.Kill()
|
||||
})
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err != nil {
|
||||
slog.Debug("close script-executor")
|
||||
cleanup()
|
||||
}
|
||||
}()
|
||||
|
||||
raw, err := rpcClient.Dispense("script-executor")
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
executor, ok := raw.(ScriptExecutor)
|
||||
if !ok {
|
||||
err = fmt.Errorf("dispensed object is not a ScriptExecutor (got %T)", raw)
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return executor, cleanup, nil
|
||||
}
|
445
internal/build/script_executor.go
Normal file
445
internal/build/script_executor.go
Normal file
@ -0,0 +1,445 @@
|
||||
// ALR - Any Linux Repository
|
||||
// Copyright (C) 2025 The ALR Authors
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package build
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/shlex"
|
||||
"github.com/goreleaser/nfpm/v2"
|
||||
"github.com/leonelquinteros/gotext"
|
||||
"mvdan.cc/sh/v3/expand"
|
||||
"mvdan.cc/sh/v3/interp"
|
||||
"mvdan.cc/sh/v3/syntax"
|
||||
|
||||
finddeps "gitea.plemya-x.ru/Plemya-x/ALR/internal/build/find_deps"
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/distro"
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/shutils/decoder"
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/shutils/handlers"
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/shutils/helpers"
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/types"
|
||||
)
|
||||
|
||||
type LocalScriptExecutor struct {
|
||||
cfg Config
|
||||
}
|
||||
|
||||
func NewLocalScriptExecutor(cfg Config) *LocalScriptExecutor {
|
||||
return &LocalScriptExecutor{
|
||||
cfg,
|
||||
}
|
||||
}
|
||||
|
||||
func (e *LocalScriptExecutor) ReadScript(ctx context.Context, scriptPath string) (*ScriptFile, error) {
|
||||
fl, err := readScript(scriptPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &ScriptFile{
|
||||
Path: scriptPath,
|
||||
File: fl,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (e *LocalScriptExecutor) ExecuteFirstPass(ctx context.Context, input *BuildInput, sf *ScriptFile) (string, []*types.BuildVars, error) {
|
||||
varsOfPackages := []*types.BuildVars{}
|
||||
|
||||
scriptDir := filepath.Dir(sf.Path)
|
||||
env := createBuildEnvVars(input.info, types.Directories{ScriptDir: scriptDir})
|
||||
|
||||
runner, err := interp.New(
|
||||
interp.Env(expand.ListEnviron(env...)), // Устанавливаем окружение
|
||||
interp.StdIO(os.Stdin, os.Stderr, os.Stderr), // Устанавливаем стандартный ввод-вывод
|
||||
interp.ExecHandler(helpers.Restricted.ExecHandler(handlers.NopExec)), // Ограничиваем выполнение
|
||||
interp.ReadDirHandler2(handlers.RestrictedReadDir(scriptDir)), // Ограничиваем чтение директорий
|
||||
interp.StatHandler(handlers.RestrictedStat(scriptDir)), // Ограничиваем доступ к статистике файлов
|
||||
interp.OpenHandler(handlers.RestrictedOpen(scriptDir)), // Ограничиваем открытие файлов
|
||||
interp.Dir(scriptDir),
|
||||
)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
|
||||
err = runner.Run(ctx, sf.File) // Запускаем скрипт
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
|
||||
dec := decoder.New(input.info, runner) // Создаём новый декодер
|
||||
|
||||
type packages struct {
|
||||
BasePkgName string `sh:"basepkg_name"`
|
||||
Names []string `sh:"name"`
|
||||
}
|
||||
|
||||
var pkgs packages
|
||||
err = dec.DecodeVars(&pkgs)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
|
||||
if len(pkgs.Names) == 0 {
|
||||
return "", nil, errors.New("package name is missing")
|
||||
}
|
||||
|
||||
var vars types.BuildVars
|
||||
|
||||
if len(pkgs.Names) == 1 {
|
||||
err = dec.DecodeVars(&vars) // Декодируем переменные
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
varsOfPackages = append(varsOfPackages, &vars)
|
||||
|
||||
return vars.Name, varsOfPackages, nil
|
||||
}
|
||||
|
||||
var pkgNames []string
|
||||
|
||||
if len(input.packages) != 0 {
|
||||
pkgNames = input.packages
|
||||
} else {
|
||||
pkgNames = pkgs.Names
|
||||
}
|
||||
|
||||
for _, pkgName := range pkgNames {
|
||||
var preVars types.BuildVarsPre
|
||||
funcName := fmt.Sprintf("meta_%s", pkgName)
|
||||
meta, ok := dec.GetFuncWithSubshell(funcName)
|
||||
if !ok {
|
||||
return "", nil, fmt.Errorf("func %s is missing", funcName)
|
||||
}
|
||||
r, err := meta(ctx)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
d := decoder.New(&distro.OSRelease{}, r)
|
||||
err = d.DecodeVars(&preVars)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
vars := preVars.ToBuildVars()
|
||||
vars.Name = pkgName
|
||||
vars.Base = pkgs.BasePkgName
|
||||
|
||||
varsOfPackages = append(varsOfPackages, &vars)
|
||||
}
|
||||
|
||||
return pkgs.BasePkgName, varsOfPackages, nil
|
||||
}
|
||||
|
||||
func (e *LocalScriptExecutor) PrepareDirs(
|
||||
ctx context.Context,
|
||||
input *BuildInput,
|
||||
basePkg string,
|
||||
) error {
|
||||
dirs, err := getDirs(
|
||||
e.cfg,
|
||||
input.script,
|
||||
basePkg,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = prepareDirs(dirs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *LocalScriptExecutor) ExecuteSecondPass(
|
||||
ctx context.Context,
|
||||
input *BuildInput,
|
||||
sf *ScriptFile,
|
||||
varsOfPackages []*types.BuildVars,
|
||||
repoDeps []string,
|
||||
builtDeps []*BuiltDep,
|
||||
basePkg string,
|
||||
) ([]*BuiltDep, error) {
|
||||
dirs, err := getDirs(e.cfg, sf.Path, basePkg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
env := createBuildEnvVars(input.info, dirs)
|
||||
|
||||
fakeroot := handlers.FakerootExecHandler(2 * time.Second)
|
||||
runner, err := interp.New(
|
||||
interp.Env(expand.ListEnviron(env...)), // Устанавливаем окружение
|
||||
interp.StdIO(os.Stdin, os.Stderr, os.Stderr), // Устанавливаем стандартный ввод-вывод
|
||||
interp.ExecHandlers(func(next interp.ExecHandlerFunc) interp.ExecHandlerFunc {
|
||||
return helpers.Helpers.ExecHandler(fakeroot)
|
||||
}), // Обрабатываем выполнение через fakeroot
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = runner.Run(ctx, sf.File)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dec := decoder.New(input.info, runner)
|
||||
|
||||
// var builtPaths []string
|
||||
|
||||
err = e.ExecuteFunctions(ctx, dirs, dec)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, vars := range varsOfPackages {
|
||||
packageName := ""
|
||||
if vars.Base != "" {
|
||||
packageName = vars.Name
|
||||
}
|
||||
|
||||
pkgFormat := input.pkgFormat
|
||||
|
||||
funcOut, err := e.ExecutePackageFunctions(
|
||||
ctx,
|
||||
dec,
|
||||
dirs,
|
||||
packageName,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
slog.Info(gotext.Get("Building package metadata"), "name", basePkg)
|
||||
|
||||
pkgInfo, err := buildPkgMetadata(
|
||||
ctx,
|
||||
input,
|
||||
vars,
|
||||
dirs,
|
||||
append(
|
||||
repoDeps,
|
||||
GetBuiltName(builtDeps)...,
|
||||
),
|
||||
funcOut.Contents,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
packager, err := nfpm.Get(pkgFormat) // Получаем упаковщик для формата пакета
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pkgName := packager.ConventionalFileName(pkgInfo) // Получаем имя файла пакета
|
||||
pkgPath := filepath.Join(dirs.BaseDir, pkgName) // Определяем путь к пакету
|
||||
|
||||
pkgFile, err := os.Create(pkgPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = packager.Package(pkgInfo, pkgFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
builtDeps = append(builtDeps, &BuiltDep{
|
||||
Name: vars.Name,
|
||||
Path: pkgPath,
|
||||
})
|
||||
}
|
||||
|
||||
return builtDeps, nil
|
||||
}
|
||||
|
||||
func buildPkgMetadata(
|
||||
ctx context.Context,
|
||||
input interface {
|
||||
OsInfoProvider
|
||||
BuildOptsProvider
|
||||
PkgFormatProvider
|
||||
RepositoryProvider
|
||||
},
|
||||
vars *types.BuildVars,
|
||||
dirs types.Directories,
|
||||
deps []string,
|
||||
preferedContents *[]string,
|
||||
) (*nfpm.Info, error) {
|
||||
pkgInfo := getBasePkgInfo(vars, input)
|
||||
pkgInfo.Description = vars.Description
|
||||
pkgInfo.Platform = "linux"
|
||||
pkgInfo.Homepage = vars.Homepage
|
||||
pkgInfo.License = strings.Join(vars.Licenses, ", ")
|
||||
pkgInfo.Maintainer = vars.Maintainer
|
||||
pkgInfo.Overridables = nfpm.Overridables{
|
||||
Conflicts: append(vars.Conflicts, vars.Name),
|
||||
Replaces: vars.Replaces,
|
||||
Provides: append(vars.Provides, vars.Name),
|
||||
Depends: deps,
|
||||
}
|
||||
pkgInfo.Section = vars.Group
|
||||
|
||||
pkgFormat := input.PkgFormat()
|
||||
info := input.OSRelease()
|
||||
|
||||
if pkgFormat == "apk" {
|
||||
// Alpine отказывается устанавливать пакеты, которые предоставляют сами себя, поэтому удаляем такие элементы
|
||||
pkgInfo.Overridables.Provides = slices.DeleteFunc(pkgInfo.Overridables.Provides, func(s string) bool {
|
||||
return s == pkgInfo.Name
|
||||
})
|
||||
}
|
||||
|
||||
if pkgFormat == "rpm" {
|
||||
pkgInfo.RPM.Group = vars.Group
|
||||
|
||||
if vars.Summary != "" {
|
||||
pkgInfo.RPM.Summary = vars.Summary
|
||||
} else {
|
||||
lines := strings.SplitN(vars.Description, "\n", 2)
|
||||
pkgInfo.RPM.Summary = lines[0]
|
||||
}
|
||||
}
|
||||
|
||||
if vars.Epoch != 0 {
|
||||
pkgInfo.Epoch = strconv.FormatUint(uint64(vars.Epoch), 10)
|
||||
}
|
||||
|
||||
setScripts(vars, pkgInfo, dirs.ScriptDir)
|
||||
|
||||
if slices.Contains(vars.Architectures, "all") {
|
||||
pkgInfo.Arch = "all"
|
||||
}
|
||||
|
||||
contents, err := buildContents(vars, dirs, preferedContents)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pkgInfo.Overridables.Contents = contents
|
||||
|
||||
if len(vars.AutoProv) == 1 && decoder.IsTruthy(vars.AutoProv[0]) {
|
||||
f := finddeps.New(info, pkgFormat)
|
||||
err = f.FindProvides(ctx, pkgInfo, dirs, vars.AutoProvSkipList)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if len(vars.AutoReq) == 1 && decoder.IsTruthy(vars.AutoReq[0]) {
|
||||
f := finddeps.New(info, pkgFormat)
|
||||
err = f.FindRequires(ctx, pkgInfo, dirs, vars.AutoReqSkipList)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return pkgInfo, nil
|
||||
}
|
||||
|
||||
func (e *LocalScriptExecutor) ExecuteFunctions(ctx context.Context, dirs types.Directories, dec *decoder.Decoder) error {
|
||||
prepare, ok := dec.GetFunc("prepare")
|
||||
if ok {
|
||||
slog.Info(gotext.Get("Executing prepare()"))
|
||||
|
||||
err := prepare(ctx, interp.Dir(dirs.SrcDir))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
build, ok := dec.GetFunc("build")
|
||||
if ok {
|
||||
slog.Info(gotext.Get("Executing build()"))
|
||||
|
||||
err := build(ctx, interp.Dir(dirs.SrcDir))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *LocalScriptExecutor) ExecutePackageFunctions(
|
||||
ctx context.Context,
|
||||
dec *decoder.Decoder,
|
||||
dirs types.Directories,
|
||||
packageName string,
|
||||
) (*FunctionsOutput, error) {
|
||||
output := &FunctionsOutput{}
|
||||
var packageFuncName string
|
||||
var filesFuncName string
|
||||
|
||||
if packageName == "" {
|
||||
packageFuncName = "package"
|
||||
filesFuncName = "files"
|
||||
} else {
|
||||
packageFuncName = fmt.Sprintf("package_%s", packageName)
|
||||
filesFuncName = fmt.Sprintf("files_%s", packageName)
|
||||
}
|
||||
packageFn, ok := dec.GetFunc(packageFuncName)
|
||||
if ok {
|
||||
slog.Info(gotext.Get("Executing %s()", packageFuncName))
|
||||
err := packageFn(ctx, interp.Dir(dirs.SrcDir))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
files, ok := dec.GetFuncP(filesFuncName, func(ctx context.Context, s *interp.Runner) error {
|
||||
// It should be done via interp.RunnerOption,
|
||||
// but due to the issues below, it cannot be done.
|
||||
// - https://github.com/mvdan/sh/issues/962
|
||||
// - https://github.com/mvdan/sh/issues/1125
|
||||
script, err := syntax.NewParser().Parse(strings.NewReader("cd $pkgdir && shopt -s globstar"), "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.Run(ctx, script)
|
||||
})
|
||||
|
||||
if ok {
|
||||
slog.Info(gotext.Get("Executing %s()", filesFuncName))
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
|
||||
err := files(
|
||||
ctx,
|
||||
interp.Dir(dirs.PkgDir),
|
||||
interp.StdIO(os.Stdin, buf, os.Stderr),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
contents, err := shlex.Split(buf.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
output.Contents = &contents
|
||||
}
|
||||
|
||||
return output, nil
|
||||
}
|
53
internal/build/script_resolver.go
Normal file
53
internal/build/script_resolver.go
Normal file
@ -0,0 +1,53 @@
|
||||
// ALR - Any Linux Repository
|
||||
// Copyright (C) 2025 The ALR Authors
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package build
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/db"
|
||||
)
|
||||
|
||||
type ScriptResolver struct {
|
||||
cfg Config
|
||||
}
|
||||
|
||||
type ScriptInfo struct {
|
||||
Script string
|
||||
Repository string
|
||||
}
|
||||
|
||||
func (s *ScriptResolver) ResolveScript(
|
||||
ctx context.Context,
|
||||
pkg *db.Package,
|
||||
) *ScriptInfo {
|
||||
var repository, script string
|
||||
|
||||
repodir := s.cfg.GetPaths().RepoDir
|
||||
repository = pkg.Repository
|
||||
if pkg.BasePkgName != "" {
|
||||
script = filepath.Join(repodir, repository, pkg.BasePkgName, "alr.sh")
|
||||
} else {
|
||||
script = filepath.Join(repodir, repository, pkg.Name, "alr.sh")
|
||||
}
|
||||
|
||||
return &ScriptInfo{
|
||||
Repository: repository,
|
||||
Script: script,
|
||||
}
|
||||
}
|
46
internal/build/script_view.go
Normal file
46
internal/build/script_view.go
Normal file
@ -0,0 +1,46 @@
|
||||
// ALR - Any Linux Repository
|
||||
// Copyright (C) 2025 The ALR Authors
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package build
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/cliutils"
|
||||
)
|
||||
|
||||
type ScriptViewerConfig interface {
|
||||
PagerStyle() string
|
||||
}
|
||||
|
||||
type ScriptViewer struct {
|
||||
config ScriptViewerConfig
|
||||
}
|
||||
|
||||
func (s *ScriptViewer) ViewScript(
|
||||
ctx context.Context,
|
||||
input *BuildInput,
|
||||
sf *ScriptFile,
|
||||
basePkg string,
|
||||
) error {
|
||||
return cliutils.PromptViewScript(
|
||||
ctx,
|
||||
sf.Path,
|
||||
basePkg,
|
||||
s.config.PagerStyle(),
|
||||
input.opts.Interactive,
|
||||
)
|
||||
}
|
86
internal/build/source_downloader.go
Normal file
86
internal/build/source_downloader.go
Normal file
@ -0,0 +1,86 @@
|
||||
// ALR - Any Linux Repository
|
||||
// Copyright (C) 2025 The ALR Authors
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package build
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/dl"
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/dlcache"
|
||||
)
|
||||
|
||||
type SourceDownloader struct {
|
||||
cfg Config
|
||||
}
|
||||
|
||||
func NewSourceDownloader(cfg Config) *SourceDownloader {
|
||||
return &SourceDownloader{
|
||||
cfg,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SourceDownloader) DownloadSources(
|
||||
ctx context.Context,
|
||||
input *BuildInput,
|
||||
basePkg string,
|
||||
si SourcesInput,
|
||||
) error {
|
||||
for i, src := range si.Sources {
|
||||
|
||||
opts := dl.Options{
|
||||
Name: fmt.Sprintf("[%d]", i),
|
||||
URL: src,
|
||||
Destination: getSrcDir(s.cfg, basePkg),
|
||||
Progress: os.Stderr,
|
||||
LocalDir: getScriptDir(input.script),
|
||||
}
|
||||
|
||||
if !strings.EqualFold(si.Checksums[i], "SKIP") {
|
||||
// Если контрольная сумма содержит двоеточие, используйте часть до двоеточия
|
||||
// как алгоритм, а часть после как фактическую контрольную сумму.
|
||||
// В противном случае используйте sha256 по умолчанию с целой строкой как контрольной суммой.
|
||||
algo, hashData, ok := strings.Cut(si.Checksums[i], ":")
|
||||
if ok {
|
||||
checksum, err := hex.DecodeString(hashData)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
opts.Hash = checksum
|
||||
opts.HashAlgorithm = algo
|
||||
} else {
|
||||
checksum, err := hex.DecodeString(si.Checksums[i])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
opts.Hash = checksum
|
||||
}
|
||||
}
|
||||
|
||||
opts.DlCache = dlcache.New(s.cfg)
|
||||
|
||||
err := dl.Download(ctx, opts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
324
internal/build/utils.go
Normal file
324
internal/build/utils.go
Normal file
@ -0,0 +1,324 @@
|
||||
// ALR - Any Linux Repository
|
||||
// Copyright (C) 2025 The ALR Authors
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package build
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
// Импортируем пакеты для поддержки различных форматов пакетов (APK, DEB, RPM и ARCH).
|
||||
|
||||
_ "github.com/goreleaser/nfpm/v2/apk"
|
||||
_ "github.com/goreleaser/nfpm/v2/arch"
|
||||
_ "github.com/goreleaser/nfpm/v2/deb"
|
||||
_ "github.com/goreleaser/nfpm/v2/rpm"
|
||||
"mvdan.cc/sh/v3/syntax"
|
||||
|
||||
"github.com/goreleaser/nfpm/v2"
|
||||
"github.com/goreleaser/nfpm/v2/files"
|
||||
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/cpu"
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/distro"
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/manager"
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/overrides"
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/types"
|
||||
)
|
||||
|
||||
// Функция readScript анализирует скрипт сборки с использованием встроенной реализации bash
|
||||
func readScript(script string) (*syntax.File, error) {
|
||||
fl, err := os.Open(script) // Открываем файл скрипта
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer fl.Close() // Закрываем файл после выполнения
|
||||
|
||||
file, err := syntax.NewParser().Parse(fl, "alr.sh") // Парсим скрипт с помощью синтаксического анализатора
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return file, nil // Возвращаем синтаксическое дерево
|
||||
}
|
||||
|
||||
// Функция prepareDirs подготавливает директории для сборки.
|
||||
func prepareDirs(dirs types.Directories) error {
|
||||
err := os.RemoveAll(dirs.BaseDir) // Удаляем базовую директорию, если она существует
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = os.MkdirAll(dirs.SrcDir, 0o755) // Создаем директорию для источников
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.MkdirAll(dirs.PkgDir, 0o755) // Создаем директорию для пакетов
|
||||
}
|
||||
|
||||
// Функция buildContents создает секцию содержимого пакета, которая содержит файлы,
|
||||
// которые будут включены в конечный пакет.
|
||||
func buildContents(vars *types.BuildVars, dirs types.Directories, preferedContents *[]string) ([]*files.Content, error) {
|
||||
contents := []*files.Content{}
|
||||
|
||||
processPath := func(path, trimmed string, prefered bool) error {
|
||||
fi, err := os.Lstat(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if fi.IsDir() {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
if !prefered {
|
||||
_, err = f.Readdirnames(1)
|
||||
if err != io.EOF {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
contents = append(contents, &files.Content{
|
||||
Source: path,
|
||||
Destination: trimmed,
|
||||
Type: "dir",
|
||||
FileInfo: &files.ContentFileInfo{
|
||||
MTime: fi.ModTime(),
|
||||
},
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
if fi.Mode()&os.ModeSymlink != 0 {
|
||||
link, err := os.Readlink(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
link = strings.TrimPrefix(link, dirs.PkgDir)
|
||||
|
||||
contents = append(contents, &files.Content{
|
||||
Source: link,
|
||||
Destination: trimmed,
|
||||
Type: "symlink",
|
||||
FileInfo: &files.ContentFileInfo{
|
||||
MTime: fi.ModTime(),
|
||||
Mode: fi.Mode(),
|
||||
},
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
fileContent := &files.Content{
|
||||
Source: path,
|
||||
Destination: trimmed,
|
||||
FileInfo: &files.ContentFileInfo{
|
||||
MTime: fi.ModTime(),
|
||||
Mode: fi.Mode(),
|
||||
Size: fi.Size(),
|
||||
},
|
||||
}
|
||||
|
||||
if slices.Contains(vars.Backup, trimmed) {
|
||||
fileContent.Type = "config|noreplace"
|
||||
}
|
||||
|
||||
contents = append(contents, fileContent)
|
||||
return nil
|
||||
}
|
||||
|
||||
if preferedContents != nil {
|
||||
for _, trimmed := range *preferedContents {
|
||||
path := filepath.Join(dirs.PkgDir, trimmed)
|
||||
if err := processPath(path, trimmed, true); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
} else {
|
||||
err := filepath.Walk(dirs.PkgDir, func(path string, fi os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
trimmed := strings.TrimPrefix(path, dirs.PkgDir)
|
||||
return processPath(path, trimmed, false)
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return contents, nil
|
||||
}
|
||||
|
||||
var RegexpALRPackageName = regexp.MustCompile(`^(?P<package>[^+]+)\+alr-(?P<repo>.+)$`)
|
||||
|
||||
func getBasePkgInfo(vars *types.BuildVars, input interface {
|
||||
RepositoryProvider
|
||||
OsInfoProvider
|
||||
},
|
||||
) *nfpm.Info {
|
||||
return &nfpm.Info{
|
||||
Name: fmt.Sprintf("%s+alr-%s", vars.Name, input.Repository()),
|
||||
Arch: cpu.Arch(),
|
||||
Version: vars.Version,
|
||||
Release: overrides.ReleasePlatformSpecific(vars.Release, input.OSRelease()),
|
||||
Epoch: strconv.FormatUint(uint64(vars.Epoch), 10),
|
||||
}
|
||||
}
|
||||
|
||||
// Функция getPkgFormat возвращает формат пакета из менеджера пакетов,
|
||||
// или ALR_PKG_FORMAT, если он установлен.
|
||||
func GetPkgFormat(mgr manager.Manager) string {
|
||||
pkgFormat := mgr.Format()
|
||||
if format, ok := os.LookupEnv("ALR_PKG_FORMAT"); ok {
|
||||
pkgFormat = format
|
||||
}
|
||||
return pkgFormat
|
||||
}
|
||||
|
||||
// Функция createBuildEnvVars создает переменные окружения, которые будут установлены
|
||||
// в скрипте сборки при его выполнении.
|
||||
func createBuildEnvVars(info *distro.OSRelease, dirs types.Directories) []string {
|
||||
env := os.Environ()
|
||||
|
||||
env = append(
|
||||
env,
|
||||
"DISTRO_NAME="+info.Name,
|
||||
"DISTRO_PRETTY_NAME="+info.PrettyName,
|
||||
"DISTRO_ID="+info.ID,
|
||||
"DISTRO_VERSION_ID="+info.VersionID,
|
||||
"DISTRO_ID_LIKE="+strings.Join(info.Like, " "),
|
||||
"ARCH="+cpu.Arch(),
|
||||
"NCPU="+strconv.Itoa(runtime.NumCPU()),
|
||||
)
|
||||
|
||||
if dirs.ScriptDir != "" {
|
||||
env = append(env, "scriptdir="+dirs.ScriptDir)
|
||||
}
|
||||
|
||||
if dirs.PkgDir != "" {
|
||||
env = append(env, "pkgdir="+dirs.PkgDir)
|
||||
}
|
||||
|
||||
if dirs.SrcDir != "" {
|
||||
env = append(env, "srcdir="+dirs.SrcDir)
|
||||
}
|
||||
|
||||
return env
|
||||
}
|
||||
|
||||
// Функция setScripts добавляет скрипты-перехватчики к метаданным пакета.
|
||||
func setScripts(vars *types.BuildVars, info *nfpm.Info, scriptDir string) {
|
||||
if vars.Scripts.PreInstall != "" {
|
||||
info.Scripts.PreInstall = filepath.Join(scriptDir, vars.Scripts.PreInstall)
|
||||
}
|
||||
|
||||
if vars.Scripts.PostInstall != "" {
|
||||
info.Scripts.PostInstall = filepath.Join(scriptDir, vars.Scripts.PostInstall)
|
||||
}
|
||||
|
||||
if vars.Scripts.PreRemove != "" {
|
||||
info.Scripts.PreRemove = filepath.Join(scriptDir, vars.Scripts.PreRemove)
|
||||
}
|
||||
|
||||
if vars.Scripts.PostRemove != "" {
|
||||
info.Scripts.PostRemove = filepath.Join(scriptDir, vars.Scripts.PostRemove)
|
||||
}
|
||||
|
||||
if vars.Scripts.PreUpgrade != "" {
|
||||
info.ArchLinux.Scripts.PreUpgrade = filepath.Join(scriptDir, vars.Scripts.PreUpgrade)
|
||||
info.APK.Scripts.PreUpgrade = filepath.Join(scriptDir, vars.Scripts.PreUpgrade)
|
||||
}
|
||||
|
||||
if vars.Scripts.PostUpgrade != "" {
|
||||
info.ArchLinux.Scripts.PostUpgrade = filepath.Join(scriptDir, vars.Scripts.PostUpgrade)
|
||||
info.APK.Scripts.PostUpgrade = filepath.Join(scriptDir, vars.Scripts.PostUpgrade)
|
||||
}
|
||||
|
||||
if vars.Scripts.PreTrans != "" {
|
||||
info.RPM.Scripts.PreTrans = filepath.Join(scriptDir, vars.Scripts.PreTrans)
|
||||
}
|
||||
|
||||
if vars.Scripts.PostTrans != "" {
|
||||
info.RPM.Scripts.PostTrans = filepath.Join(scriptDir, vars.Scripts.PostTrans)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
// Функция setVersion изменяет переменную версии в скрипте runner.
|
||||
// Она используется для установки версии на вывод функции version().
|
||||
func setVersion(ctx context.Context, r *interp.Runner, to string) error {
|
||||
fl, err := syntax.NewParser().Parse(strings.NewReader("version='"+to+"'"), "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return r.Run(ctx, fl)
|
||||
}
|
||||
*/
|
||||
|
||||
// Функция packageNames возвращает имена всех предоставленных пакетов.
|
||||
/*
|
||||
func packageNames(pkgs []db.Package) []string {
|
||||
names := make([]string, len(pkgs))
|
||||
for i, p := range pkgs {
|
||||
names[i] = p.Name
|
||||
}
|
||||
return names
|
||||
}
|
||||
*/
|
||||
|
||||
// Функция removeDuplicates убирает любые дубликаты из предоставленного среза.
|
||||
func removeDuplicates[T comparable](slice []T) []T {
|
||||
seen := map[T]struct{}{}
|
||||
result := []T{}
|
||||
|
||||
for _, item := range slice {
|
||||
if _, ok := seen[item]; !ok {
|
||||
seen[item] = struct{}{}
|
||||
result = append(result, item)
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func removeDuplicatesSources(sources, checksums []string) ([]string, []string) {
|
||||
seen := map[string]string{}
|
||||
keys := make([]string, 0)
|
||||
for i, s := range sources {
|
||||
if val, ok := seen[s]; !ok || strings.EqualFold(val, "SKIP") {
|
||||
if !ok {
|
||||
keys = append(keys, s)
|
||||
}
|
||||
seen[s] = checksums[i]
|
||||
}
|
||||
}
|
||||
|
||||
newSources := make([]string, len(keys))
|
||||
newChecksums := make([]string, len(keys))
|
||||
for i, k := range keys {
|
||||
newSources[i] = k
|
||||
newChecksums[i] = seen[k]
|
||||
}
|
||||
return newSources, newChecksums
|
||||
}
|
47
internal/build/utils_internal_test.go
Normal file
47
internal/build/utils_internal_test.go
Normal file
@ -0,0 +1,47 @@
|
||||
// ALR - Any Linux Repository
|
||||
// Copyright (C) 2025 The ALR Authors
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package build
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestRemoveDuplicatesSources(t *testing.T) {
|
||||
type testCase struct {
|
||||
Name string
|
||||
Sources []string
|
||||
Checksums []string
|
||||
NewSources []string
|
||||
NewChecksums []string
|
||||
}
|
||||
|
||||
for _, tc := range []testCase{{
|
||||
Name: "prefer non-skip values",
|
||||
Sources: []string{"a", "b", "c", "a"},
|
||||
Checksums: []string{"skip", "skip", "skip", "1"},
|
||||
NewSources: []string{"a", "b", "c"},
|
||||
NewChecksums: []string{"1", "skip", "skip"},
|
||||
}} {
|
||||
t.Run(tc.Name, func(t *testing.T) {
|
||||
s, c := removeDuplicatesSources(tc.Sources, tc.Checksums)
|
||||
assert.Equal(t, s, tc.NewSources)
|
||||
assert.Equal(t, c, tc.NewChecksums)
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user