diff --git a/.golangci.yml b/.golangci.yml index 647998a..f627b90 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -11,6 +11,7 @@ linters: # they will be enabled - depguard - exhaustruct + - exportloopref - forbidigo - forcetypeassert - funlen diff --git a/pkg/apk/apk.go b/pkg/apk/apk.go index 60bf3be..2680bd1 100644 --- a/pkg/apk/apk.go +++ b/pkg/apk/apk.go @@ -19,10 +19,17 @@ type Apk struct { apkDir string } -// Build builds the APK package. -// -// It takes the artifactsPath as a parameter and returns an error. -func (a *Apk) Build(artifactsPath string) error { +// BuildPackage initiates the package building process for the Apk instance. +// It takes artifactsPath to specify where to store the build artifacts +// and calls the internal apkBuild method, returning any errors encountered. +func (a *Apk) BuildPackage(artifactsPath string) error { + return a.apkBuild(artifactsPath) +} + +// PrepareFakeroot sets up the environment for building an APK package in a fakeroot context. +// It initializes the apkDir, cleans up any existing directory, creates the necessary packer directory, +// and generates the APKBUILD and post-installation script files. The method returns an error if any step fails. +func (a *Apk) PrepareFakeroot(_ string) error { a.apkDir = filepath.Join(a.PKGBUILD.StartDir, "apk") if err := utils.RemoveAll(a.apkDir); err != nil { @@ -47,10 +54,6 @@ func (a *Apk) Build(artifactsPath string) error { return err } - if err := a.apkBuild(artifactsPath); err != nil { - return err - } - return nil } diff --git a/pkg/builder/builder.go b/pkg/builder/builder.go index 6fbcb09..8e33459 100644 --- a/pkg/builder/builder.go +++ b/pkg/builder/builder.go @@ -26,17 +26,17 @@ func (builder *Builder) Compile(noBuild bool) error { if !noBuild { if err := processFunction(builder.PKGBUILD.Prepare, - "preparing"); err != nil { + "preparing sources"); err != nil { return err } if err := processFunction(builder.PKGBUILD.Build, - "building"); err != nil { + "processing sources"); err != nil { return err } if err := processFunction(builder.PKGBUILD.Package, - "generating package"); err != nil { + "preparing fakeroot"); err != nil { return err } } diff --git a/pkg/dpkg/dpkg.go b/pkg/dpkg/dpkg.go index c494f24..ddacbaf 100644 --- a/pkg/dpkg/dpkg.go +++ b/pkg/dpkg/dpkg.go @@ -1,12 +1,10 @@ package dpkg import ( - "bytes" "fmt" "path/filepath" "regexp" "strings" - "text/template" "github.com/M0Rf30/yap/pkg/options" "github.com/M0Rf30/yap/pkg/pkgbuild" @@ -168,19 +166,17 @@ func (d *Deb) Prepare(makeDepends []string) error { // It does not take any parameters. // It returns an error if there is any issue during stripping. func (d *Deb) Strip() error { - var tmplBytesBuffer bytes.Buffer + var output strings.Builder - utils.Logger.Info("stripping binaries") - - tmpl := template.New("strip") + tmpl := d.PKGBUILD.RenderSpec(options.StripScript) - template.Must(tmpl.Parse(options.StripScript)) + utils.Logger.Info("stripping binaries") - if pkgbuild.Verbose { - return tmpl.Execute(&tmplBytesBuffer, d.PKGBUILD) + if err := tmpl.Execute(&output, d.PKGBUILD); err != nil { + return err } - return utils.RunScript(tmplBytesBuffer.String()) + return utils.RunScript(output.String()) } // Update updates the Deb package list. @@ -246,30 +242,38 @@ func (d *Deb) createDebResources() error { return nil } -// Build builds the Deb package. -// -// It takes the artifactsPath as a parameter and returns an error if any. -func (d *Deb) Build(artifactsPath string) error { - d.getArch() - d.getRelease() - - if err := utils.RemoveAll(d.debDir); err != nil { +// BuildPackage builds the Debian package and cleans up afterward. +// It takes artifactsPath to specify where to store the package. +// The method calls dpkgDeb to create the package and removes the +// package directory, returning an error if any step fails. +func (d *Deb) BuildPackage(artifactsPath string) error { + if err := d.dpkgDeb(artifactsPath); err != nil { return err } - if err := d.createDebResources(); err != nil { + if err := utils.RemoveAll(d.PKGBUILD.PackageDir); err != nil { return err } - if err := d.Strip(); err != nil { + return nil +} + +// PrepareFakeroot sets up the environment for building a Debian package in a fakeroot context. +// It retrieves architecture and release information, cleans up the debDir, creates necessary +// resources, and strips binaries. The method returns an error if any step fails. +func (d *Deb) PrepareFakeroot(_ string) error { + d.getArch() + d.getRelease() + + if err := utils.RemoveAll(d.debDir); err != nil { return err } - if err := d.dpkgDeb(artifactsPath); err != nil { + if err := d.createDebResources(); err != nil { return err } - if err := utils.RemoveAll(d.PKGBUILD.PackageDir); err != nil { + if err := d.Strip(); err != nil { return err } diff --git a/pkg/packer/packer.go b/pkg/packer/packer.go index f96d9c9..b4d7ca8 100644 --- a/pkg/packer/packer.go +++ b/pkg/packer/packer.go @@ -12,18 +12,21 @@ import ( // Packer is the common interface implemented by all package managers. type Packer interface { - // Prepare appends the dependencies required to build all the projects. It - // returns any error if encountered. - Prepare(depends []string) error - // Build reads the path where the final artifact will be written. It returns any - // error if encountered. - Build(output string) error + // BuildPackage starts the package building process and writes the final artifact + // to the specified output path. It returns an error if any issues occur during the build. + BuildPackage(output string) error // Install reads the path where the final artifact will be written. It returns // any error if encountered. Install(output string) error + // Prepare appends the dependencies required to build all the projects. It + // returns any error if encountered. + Prepare(depends []string) error // PrepareEnvironment reads a flag to install golang tools on request, on the // build machine. It returns any error if encountered. PrepareEnvironment(flag bool) error + // PrepareFakeroot sets up the environment for building the final artifact in a fakeroot context. + // It takes an output path where the artifact will be written and returns an error if any issues occur. + PrepareFakeroot(output string) error // Update performs a package manager update operation. It returns any error if // encountered. Update() error diff --git a/pkg/pacman/pacman.go b/pkg/pacman/pacman.go index 66d1f5c..1cfeb58 100644 --- a/pkg/pacman/pacman.go +++ b/pkg/pacman/pacman.go @@ -15,10 +15,24 @@ type Pacman struct { pacmanDir string } -// Build builds the Pacman package. +// BuildPackage initiates the package building process for the Pacman instance. // -// It takes the artifactsPath as a parameter and returns an error if any. -func (p *Pacman) Build(artifactsPath string) error { +// 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() +} + +// PrepareFakeroot sets up 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 p.PKGBUILD.PkgDest, _ = filepath.Abs(artifactsPath) @@ -41,10 +55,6 @@ func (p *Pacman) Build(artifactsPath string) error { return err } - if err := p.pacmanBuild(); err != nil { - return err - } - return nil } diff --git a/pkg/pkgbuild/pkgbuild.go b/pkg/pkgbuild/pkgbuild.go index 4086f9c..0856ff9 100644 --- a/pkg/pkgbuild/pkgbuild.go +++ b/pkg/pkgbuild/pkgbuild.go @@ -109,6 +109,14 @@ func (pkgBuild *PKGBUILD) CreateSpec(filePath string, tmpl *template.Template) e return tmpl.Execute(file, pkgBuild) } +// RenderSpec initializes a new template with custom functions and parses the provided script. +// It adds two custom functions to the template: +// 1. "join": Takes a slice of strings and joins them into a single string, separated by commas, +// while also trimming any leading or trailing spaces. +// 2. "multiline": Takes a string and replaces newline characters with a newline followed by a space, +// effectively formatting the string for better readability in multi-line contexts. +// +// The method returns the parsed template, which can be used for rendering with data. func (pkgBuild *PKGBUILD) RenderSpec(script string) *template.Template { tmpl := template.New("template").Funcs(template.FuncMap{ "join": func(strs []string) string { diff --git a/pkg/project/project.go b/pkg/project/project.go index 1ca8f10..d715ed4 100644 --- a/pkg/project/project.go +++ b/pkg/project/project.go @@ -267,7 +267,13 @@ func (mpc *MultipleProject) createPackages(proj *Project) error { return err } - if err := proj.PackageManager.Build(mpc.Output); err != nil { + if err := proj.PackageManager.PrepareFakeroot(mpc.Output); err != nil { + return err + } + + utils.Logger.Info("building resulting package") + + if err := proj.PackageManager.BuildPackage(mpc.Output); err != nil { return err } diff --git a/pkg/rpm/rpm.go b/pkg/rpm/rpm.go index 952617b..0bc41a2 100644 --- a/pkg/rpm/rpm.go +++ b/pkg/rpm/rpm.go @@ -28,10 +28,18 @@ type RPM struct { srpmsDir string } -// Build builds the RPM package. -// -// It takes the artifactsPath as a parameter and returns an error. -func (r *RPM) Build(artifactsPath string) error { +// BuildPackage initiates the package building process for the RPM instance. +// It calls the internal rpmBuild method and returns any errors encountered during the process. +func (r *RPM) BuildPackage(_ string) error { + return r.rpmBuild() +} + +// PrepareFakeroot sets up the environment for building an RPM package in a fakeroot context. +// It retrieves architecture, group, and release information, sets the package destination, +// cleans up the RPM directory, creates necessary directories, and gathers files. +// It also processes package dependencies and creates the RPM spec file, returning +// an error if any step fails. +func (r *RPM) PrepareFakeroot(artifactsPath string) error { r.getArch() r.getGroup() r.getRelease() @@ -71,10 +79,6 @@ func (r *RPM) Build(artifactsPath string) error { return err } - if err := r.rpmBuild(); err != nil { - return err - } - return nil }