Skip to content

Commit

Permalink
fix: unevaluated variables in constants package
Browse files Browse the repository at this point in the history
  • Loading branch information
M0Rf30 committed Nov 9, 2024
1 parent e0a93f7 commit 0485267
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Steps to reproduce the behavior
A clear and concise description of what you expected to happen.

**Logs**
Run the commands with `--verbose` and post the log here as a file upload
Run the commands and post the log here as a file upload
Attach also the output of `podman logs` or `docker logs`, possibly with `--latest` flag

**Desktop (please complete the following information):**
Expand Down
3 changes: 0 additions & 3 deletions cmd/yap/command/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"strings"

"github.com/M0Rf30/yap/pkg/parser"
"github.com/M0Rf30/yap/pkg/pkgbuild"
"github.com/M0Rf30/yap/pkg/project"
"github.com/M0Rf30/yap/pkg/source"
"github.com/M0Rf30/yap/pkg/utils"
Expand Down Expand Up @@ -74,8 +73,6 @@ func init() {
"from", "", "", "Build starting from a defined package name")
buildCmd.Flags().StringVarP(&project.ToPkgName,
"to", "", "", "Build until a defined package name")
buildCmd.PersistentFlags().BoolVarP(&pkgbuild.Verbose,
"verbose", "v", false, "Verbose output")
buildCmd.Flags().BoolVarP(&project.Zap,
"zap", "z", false, "Remove entire staging dir before building the package")
}
2 changes: 1 addition & 1 deletion examples/yap/PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ url="https://github.com/M0Rf30/${pkgname}"
makedepends=(
'upx'
)
makedepends__apt=(
makedepends__dpkg=(
'upx-ucl'
)
source=(
Expand Down
4 changes: 2 additions & 2 deletions pkg/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ func (builder *Builder) Compile(noBuild bool) error {
}

if err := processFunction(builder.PKGBUILD.Build,
"processing sources"); err != nil {
"building"); err != nil {
return err
}

if err := processFunction(builder.PKGBUILD.Package,
"preparing fakeroot"); err != nil {
"generating package"); err != nil {
return err
}
}
Expand Down
37 changes: 35 additions & 2 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package constants

import (
"strings"

"github.com/M0Rf30/yap/pkg/set"
)

Expand Down Expand Up @@ -37,7 +39,7 @@ var (
"almalinux": "rpm",
"alpine": "apk",
"amzn": "rpm",
"arch": "pacman",
"arch": "makepkg",
"centos": "rpm",
"debian": "dpkg",
"fedora": "rpm",
Expand All @@ -50,10 +52,41 @@ var (
"ubuntu": "dpkg",
}

ReleasesSet = set.NewSet()
Packers = [...]string{
"apk",
"dpkg",
"makepkg",
"rpm",
}

Distros = []string{}
DistrosSet = set.NewSet()
DistroPackageManager = map[string]string{}
PackagersSet = set.NewSet()
CleanPrevious = false
)

// init initializes the package.
//
// It iterates over the Releases slice and adds each release to the ReleasesSet set.
// It also extracts the distribution name from each release and adds it to the Distros slice.
// The function then iterates over the Distros slice and assigns the corresponding package manager
// to each distribution in the DistroPackageManager map.
// If a distribution does not have a supported package manager, the function prints an error message
// and exits the program.
// Finally, it adds each package manager to the PackagersSet set.
func init() {
for _, release := range Releases {
distro := strings.Split(release, "_")[0]
Distros = append(Distros, distro)
DistrosSet.Add(distro)
}

for _, distro := range Distros {
DistroPackageManager[distro] = DistroToPackageManager[distro]
}

for _, packageManager := range Packers {
PackagersSet.Add(packageManager)
}
}
2 changes: 1 addition & 1 deletion pkg/pacman/constants.go → pkg/makepkg/constants.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pacman
package makepkg

var buildEnvironmentDeps = []string{
"base-devel",
Expand Down
64 changes: 32 additions & 32 deletions pkg/pacman/pacman.go → pkg/makepkg/makepkg.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pacman
package makepkg

import (
"path/filepath"
Expand All @@ -7,49 +7,49 @@ import (
"github.com/M0Rf30/yap/pkg/utils"
)

// Pacman represents a package manager for the Pacman distribution.
// Makepkg represents a package manager for the Makepkg distribution.
//
// It contains methods for building, installing, and updating packages.
type Pacman struct {
type Makepkg struct {
PKGBUILD *pkgbuild.PKGBUILD
pacmanDir string
}

// BuildPackage initiates the package building process for the Pacman instance.
// BuildPackage initiates the package building process for the Makepkg instance.
//
// It takes a single parameter:
// - artifactsPath: a string representing the path where the build artifacts will be stored.
//
// The method calls the internal pacmanBuild function to perform the actual build process.
// It returns an error if the build process encounters any issues.
func (p *Pacman) BuildPackage(_ string) error {
return p.pacmanBuild()
func (m *Makepkg) BuildPackage(_ string) error {
return m.pacmanBuild()
}

// PrepareFakeroot sets up the environment for building a package in a fakeroot context.
// PrepareFakeroot sets um the environment for building a package in a fakeroot context.
//
// It takes an artifactsPath parameter, which specifies where to store build artifacts.
// The method initializes the pacmanDir, resolves the package destination, and creates
// the PKGBUILD and post-installation script files if necessary. It returns an error
// if any step fails.
func (p *Pacman) PrepareFakeroot(artifactsPath string) error {
p.pacmanDir = p.PKGBUILD.StartDir
// if any stem fails.
func (m *Makepkg) PrepareFakeroot(artifactsPath string) error {
m.pacmanDir = m.PKGBUILD.StartDir

p.PKGBUILD.PkgDest, _ = filepath.Abs(artifactsPath)
m.PKGBUILD.PkgDest, _ = filepath.Abs(artifactsPath)

tmpl := p.PKGBUILD.RenderSpec(specFile)
tmpl := m.PKGBUILD.RenderSpec(specFile)

if p.PKGBUILD.Home != p.PKGBUILD.StartDir {
err := p.PKGBUILD.CreateSpec(filepath.Join(p.pacmanDir,
if m.PKGBUILD.Home != m.PKGBUILD.StartDir {
err := m.PKGBUILD.CreateSpec(filepath.Join(m.pacmanDir,
"PKGBUILD"), tmpl)
if err != nil {
return err
}
}

tmpl = p.PKGBUILD.RenderSpec(postInstall)
err := p.PKGBUILD.CreateSpec(filepath.Join(p.pacmanDir,
p.PKGBUILD.PkgName+".install"), tmpl)
tmpl = m.PKGBUILD.RenderSpec(postInstall)
err := m.PKGBUILD.CreateSpec(filepath.Join(m.pacmanDir,
m.PKGBUILD.PkgName+".install"), tmpl)

if err != nil {
return err
Expand All @@ -62,13 +62,13 @@ func (p *Pacman) PrepareFakeroot(artifactsPath string) error {
//
// artifactsPath: the path where the package artifacts are located.
// error: an error if the installation fails.
func (p *Pacman) Install(artifactsPath string) error {
pkgName := p.PKGBUILD.PkgName + "-" +
p.PKGBUILD.PkgVer +
func (m *Makepkg) Install(artifactsPath string) error {
pkgName := m.PKGBUILD.PkgName + "-" +
m.PKGBUILD.PkgVer +
"-" +
p.PKGBUILD.PkgRel +
m.PKGBUILD.PkgRel +
"-" +
p.PKGBUILD.ArchComputed +
m.PKGBUILD.ArchComputed +
".pkg.tar.zst"

pkgFilePath := filepath.Join(artifactsPath, pkgName)
Expand All @@ -84,24 +84,24 @@ func (p *Pacman) Install(artifactsPath string) error {
return nil
}

// Prepare prepares the Pacman package by getting the dependencies using the PKGBUILD.
// Prepare prepares the Makepkg package by getting the dependencies using the PKGBUILD.
//
// makeDepends is a slice of strings representing the dependencies to be included.
// It returns an error if there is any issue getting the dependencies.
func (p *Pacman) Prepare(makeDepends []string) error {
func (m *Makepkg) Prepare(makeDepends []string) error {
args := []string{
"-S",
"--noconfirm",
}

return p.PKGBUILD.GetDepends("pacman", args, makeDepends)
return m.PKGBUILD.GetDepends("pacman", args, makeDepends)
}

// PrepareEnvironment prepares the environment for the Pacman.
// PrepareEnvironment prepares the environment for the Makepkg.
//
// It takes a boolean parameter `golang` which indicates whether the environment should be prepared for Golang.
// It returns an error if there is any issue in preparing the environment.
func (p *Pacman) PrepareEnvironment(golang bool) error {
func (m *Makepkg) PrepareEnvironment(golang bool) error {
args := []string{
"-S",
"--noconfirm",
Expand All @@ -117,12 +117,12 @@ func (p *Pacman) PrepareEnvironment(golang bool) error {
return utils.Exec(false, "", "pacman", args...)
}

// Update updates the Pacman package manager.
// Update updates the Makepkg package manager.
//
// It retrieves the updates using the GetUpdates method of the PKGBUILD struct.
// It returns an error if there is any issue during the update process.
func (p *Pacman) Update() error {
return p.PKGBUILD.GetUpdates("pacman", "-Sy")
func (m *Makepkg) Update() error {
return m.PKGBUILD.GetUpdates("pacman", "-Sy")
}

// pacmanBuild builds the package using makepkg command.
Expand All @@ -131,6 +131,6 @@ func (p *Pacman) Update() error {
// The error is returned as is.
// Returns:
// - error: An error if any occurred during the execution of the makepkg command.
func (p *Pacman) pacmanBuild() error {
return utils.Exec(true, p.pacmanDir, "makepkg", "-ef")
func (m *Makepkg) pacmanBuild() error {
return utils.Exec(true, m.pacmanDir, "makepkg", "-ef")
}
6 changes: 3 additions & 3 deletions pkg/packer/packer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/M0Rf30/yap/pkg/apk"
"github.com/M0Rf30/yap/pkg/constants"
"github.com/M0Rf30/yap/pkg/dpkg"
"github.com/M0Rf30/yap/pkg/pacman"
"github.com/M0Rf30/yap/pkg/makepkg"
"github.com/M0Rf30/yap/pkg/pkgbuild"
"github.com/M0Rf30/yap/pkg/rpm"
"github.com/M0Rf30/yap/pkg/utils"
Expand Down Expand Up @@ -44,8 +44,8 @@ func GetPackageManager(pkgBuild *pkgbuild.PKGBUILD, distro string) Packer {
return &apk.Apk{
PKGBUILD: pkgBuild,
}
case "pacman":
return &pacman.Pacman{
case "makepkg":
return &makepkg.Makepkg{
PKGBUILD: pkgBuild,
}
case "dpkg":
Expand Down
9 changes: 0 additions & 9 deletions pkg/pkgbuild/pkgbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ import (
"golang.org/x/exp/rand"
)

// Verbose is a flag to enable verbose output.
var Verbose bool

// PKGBUILD defines all the fields accepted by the yap specfile (variables,
// arrays, functions). It adds some exotics fields to manage debconfig
// templating and other rpm/deb descriptors.
Expand Down Expand Up @@ -105,12 +102,6 @@ func (pkgBuild *PKGBUILD) CreateSpec(filePath string, tmpl *template.Template) e
}
defer file.Close()

if Verbose {
if err := tmpl.Execute(utils.MultiPrinter.Writer, pkgBuild); err != nil {
return err
}
}

return tmpl.Execute(file, pkgBuild)
}

Expand Down

0 comments on commit 0485267

Please sign in to comment.