Skip to content

Commit

Permalink
documentation: add more documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
M0Rf30 committed Oct 30, 2023
1 parent 6c1365c commit 01b7d2b
Show file tree
Hide file tree
Showing 18 changed files with 508 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = false
insert_final_newline = true
31 changes: 30 additions & 1 deletion pkg/apk/apk.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,21 @@ import (
"github.com/M0Rf30/yap/pkg/utils"
)

// Apk represents the APK package manager.
//
// It contains the directory path of the package and the PKGBUILD struct, which
// contains the metadata and build instructions for the package.
type Apk struct {
// PKGBUILD is a pointer to the pkgbuild.PKGBUILD struct, which contains information about the package being built.
PKGBUILD *pkgbuild.PKGBUILD
apkDir string

// apkDir is a string representing the directory where the APK package files are stored.
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 {
a.apkDir = filepath.Join(a.PKGBUILD.StartDir, "apk")

Expand Down Expand Up @@ -43,6 +53,10 @@ func (a *Apk) Build(artifactsPath string) error {
return err
}

// Install installs the APK package to the specified artifacts path.
//
// It takes a string parameter `artifactsPath` which specifies the path where the artifacts are located.
// It returns an error if there was an error during the installation process.
func (a *Apk) Install(artifactsPath string) error {
var err error

Expand All @@ -69,6 +83,10 @@ func (a *Apk) Install(artifactsPath string) error {
return err
}

// Prepare prepares the Apk by adding dependencies to the PKGBUILD file.
//
// makeDepends is a slice of strings representing the dependencies to be added.
// It returns an error if there is any issue with adding the dependencies.
func (a *Apk) Prepare(makeDepends []string) error {
args := []string{
"add",
Expand All @@ -82,6 +100,9 @@ func (a *Apk) Prepare(makeDepends []string) error {
return err
}

// PrepareEnvironment prepares the build environment for APK packaging.
// It installs requested Go tools if 'golang' is true.
// It returns an error if any step fails.
func (a *Apk) PrepareEnvironment(golang bool) error {
var err error

Expand All @@ -104,6 +125,8 @@ func (a *Apk) PrepareEnvironment(golang bool) error {
return err
}

// Update updates the APK package manager's package database.
// It returns an error if the update process fails.
func (a *Apk) Update() error {
err := a.PKGBUILD.GetUpdates("apk", "update")
if err != nil {
Expand All @@ -113,6 +136,8 @@ func (a *Apk) Update() error {
return err
}

// apkBuild compiles the APK package using 'abuild-keygen' and 'abuild'.
// It returns an error if any compilation step fails.
func (a *Apk) apkBuild(artifactsPath string) error {
err := utils.Exec(a.apkDir,
"abuild-keygen",
Expand All @@ -135,6 +160,10 @@ func (a *Apk) apkBuild(artifactsPath string) error {
return err
}

// makePackerDir creates the necessary directories for the Apk struct.
//
// It does not take any parameters.
// It returns an error if any of the directory operations fail.
func (a *Apk) makePackerDir() error {
err := utils.ExistsMakeDir(a.apkDir)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ func (builder *Builder) Package() error {
return err
}

// build executes the instructions provided by a single project build()
// function. It returns any error if occurred.
// build executes a set of instructions provided by a project build function. If
// there is an error during execution, it returns the error.
func (builder *Builder) build() error {
err := RunScript(builder.PKGBUILD.Build)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions pkg/builder/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
"mvdan.cc/sh/v3/syntax"
)

// RunScript runs a shell script.
//
// It takes a string parameter `cmds` which represents the shell script to be executed.
// The function returns an error if there was an issue running the script.
func RunScript(cmds string) error {
script, _ := syntax.NewParser().Parse(strings.NewReader(cmds), "")

Expand Down
9 changes: 9 additions & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ var (
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() {
var packageManager string

Expand Down
65 changes: 65 additions & 0 deletions pkg/debian/debian.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,32 @@ import (
"github.com/otiai10/copy"
)

// Debian represents a Debian package.
//
// It contains the directory path of the package and the PKGBUILD struct, which
// contains the metadata and build instructions for the package.
type Debian struct {
debDir string
PKGBUILD *pkgbuild.PKGBUILD
}

// getArch updates the architecture field in the Debian struct.
//
// It iterates over the architecture values in the PKGBUILD field of the Debian struct
// and replaces them with the corresponding values from the DebianArchs map.
func (d *Debian) getArch() {
for index, arch := range d.PKGBUILD.Arch {
d.PKGBUILD.Arch[index] = DebianArchs[arch]
}
}

// createConfFiles creates the conffiles file in the Debian package.
//
// It iterates over the Backup field of the PKGBUILD struct and adds each name
// to the data string. The data string is then written to the conffiles file
// located at the debDir path.
//
// Returns an error if there was a problem creating or writing to the file.
func (d *Debian) createConfFiles() error {
var err error
if len(d.PKGBUILD.Backup) == 0 {
Expand All @@ -53,6 +68,10 @@ func (d *Debian) createConfFiles() error {
return err
}

// createDebconfTemplate creates a Debian package configuration file template.
//
// It does not take any parameters.
// It returns an error if there was an issue creating the template.
func (d *Debian) createDebconfTemplate() error {
var err error
if d.PKGBUILD.DebTemplate == "" {
Expand All @@ -70,6 +89,9 @@ func (d *Debian) createDebconfTemplate() error {
return err
}

// createDebconfConfig creates a Debian configuration file.
//
// It takes no parameters and returns an error.
func (d *Debian) createDebconfConfig() error {
var err error
if d.PKGBUILD.DebConfig == "" {
Expand All @@ -87,6 +109,10 @@ func (d *Debian) createDebconfConfig() error {
return err
}

// createScripts generates and writes the scripts for the Debian package.
//
// It takes no parameters.
// It returns an error if there was an issue generating or writing the scripts.
func (d *Debian) createScripts() error {
var err error

Expand Down Expand Up @@ -123,6 +149,13 @@ func (d *Debian) createScripts() error {
return err
}

// dpkgDeb generates Debian package files from the given artifact path.
//
// It takes a string parameter `artifactPath` which represents the path where the
// Debian package files will be generated.
//
// The function returns an error if there was an issue generating the Debian package
// files.
func (d *Debian) dpkgDeb(artifactPath string) error {
var err error

Expand All @@ -145,6 +178,10 @@ func (d *Debian) dpkgDeb(artifactPath string) error {
return err
}

// Prepare prepares the Debian package by installing its dependencies using apt-get.
//
// makeDepends: a slice of strings representing the dependencies to be installed.
// Returns an error if there was a problem installing the dependencies.
func (d *Debian) Prepare(makeDepends []string) error {
args := []string{
"--assume-yes",
Expand All @@ -159,6 +196,10 @@ func (d *Debian) Prepare(makeDepends []string) error {
return err
}

// Strip strips binaries from the Debian package.
//
// It does not take any parameters.
// It returns an error if there is any issue during stripping.
func (d *Debian) Strip() error {
var err error

Expand Down Expand Up @@ -188,6 +229,14 @@ func (d *Debian) Strip() error {
return err
}

// Update updates the Debian package list.
//
// It calls the GetUpdates method of the PKGBUILD field of the Debian struct
// to retrieve any updates using the "apt-get" command and the "update" argument.
// If an error occurs during the update, it is returned.
//
// Returns:
// - error: An error if the update fails.
func (d *Debian) Update() error {
err := d.PKGBUILD.GetUpdates("apt-get", "update")
if err != nil {
Expand All @@ -197,6 +246,15 @@ func (d *Debian) Update() error {
return err
}

// createDebResources creates the Debian package resources.
//
// It creates the necessary directories and files for the Debian package.
// It also sets the installed size of the package based on the size of the package directory.
// It generates the control file for the package.
// It creates the scripts for the package.
// It creates the debconf template file.
// It creates the debconf config file.
// It returns an error if any of the operations fail.
func (d *Debian) createDebResources() error {
d.debDir = filepath.Join(d.PKGBUILD.PackageDir, "DEBIAN")
err := utils.ExistsMakeDir(d.debDir)
Expand Down Expand Up @@ -235,6 +293,9 @@ func (d *Debian) createDebResources() error {
return err
}

// Build builds the Debian package.
//
// It takes the artifactsPath as a parameter and returns an error if any.
func (d *Debian) Build(artifactsPath string) error {
var err error

Expand Down Expand Up @@ -288,6 +349,10 @@ func (d *Debian) Install(artifactsPath string) error {
return err
}

// PrepareEnvironment prepares the environment for the Debian package.
//
// It takes a boolean parameter `golang` which indicates whether or not to set up Go.
// It returns an error if there was a problem during the environment preparation.
func (d *Debian) PrepareEnvironment(golang bool) error {
var err error

Expand Down
8 changes: 5 additions & 3 deletions pkg/packer/packer.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ type Packer interface {
Update() error
}

// GetPackageManager reads the pkgBuild structure and the distro name. It
// returns a Packer interface representing the specialized package manager for
// that distro.
// GetPackageManager returns a Packer interface based on the given package build and distribution.
//
// pkgBuild: A pointer to a pkgbuild.PKGBUILD struct.
// distro: A string representing the distribution.
// Returns a Packer interface.
func GetPackageManager(pkgBuild *pkgbuild.PKGBUILD, distro string) Packer {
var packageManager Packer

Expand Down
28 changes: 28 additions & 0 deletions pkg/pacman/pacman.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ import (
"github.com/M0Rf30/yap/pkg/utils"
)

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

// Build builds the Pacman package.
//
// It takes the artifactsPath as a parameter and returns an error if any.
func (p *Pacman) Build(artifactsPath string) error {
p.pacmanDir = p.PKGBUILD.StartDir

Expand All @@ -37,6 +43,10 @@ func (p *Pacman) Build(artifactsPath string) error {
return nil
}

// Install installs the package using the given artifacts path.
//
// artifactsPath: the path where the package artifacts are located.
// error: an error if the installation fails.
func (p *Pacman) Install(artifactsPath string) error {
var err error

Expand All @@ -63,6 +73,10 @@ func (p *Pacman) Install(artifactsPath string) error {
return err
}

// Prepare prepares the Pacman 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 {
args := []string{
"-S",
Expand All @@ -77,6 +91,10 @@ func (p *Pacman) Prepare(makeDepends []string) error {
return err
}

// PrepareEnvironment prepares the environment for the Pacman.
//
// 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 {
var err error

Expand All @@ -100,6 +118,10 @@ func (p *Pacman) PrepareEnvironment(golang bool) error {
return err
}

// Update updates the Pacman 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 {
err := p.PKGBUILD.GetUpdates("pacman", "-Sy")
if err != nil {
Expand All @@ -109,6 +131,12 @@ func (p *Pacman) Update() error {
return err
}

// pacmanBuild builds the package using makepkg command.
//
// It executes the makepkg command in the pacman directory and returns an error if any.
// 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 {
err := utils.Exec(p.pacmanDir, "makepkg", "-f")
if err != nil {
Expand Down
Loading

0 comments on commit 01b7d2b

Please sign in to comment.