Skip to content

Commit

Permalink
feat: add preliminary copyright(deb) and vendor(rpm) support
Browse files Browse the repository at this point in the history
  • Loading branch information
M0Rf30 committed Nov 6, 2023
1 parent e83045a commit f018fdf
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 98 deletions.
9 changes: 6 additions & 3 deletions examples/yap/PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ pkgdesc__debian="Package software with ease for Debian"
pkgdesc__fedora="Package software with ease for Fedora"
pkgdesc__rocky="Package software with ease for Rocky"
pkgdesc__ubuntu="Package software with ease for Ubuntu"
maintainer="Example <[email protected]>"
maintainer="The Yap Packager <[email protected]>"
copyright=(
"2023, The Yap Project <https://github.com/M0Rf30/yap>"
)
arch=('x86_64')
license=('GPLv3')
license=('GPL-3.0-only')
section=misc
priority=optional
url="https://github.com/M0Rf30/${pkgname}"
Expand Down Expand Up @@ -57,7 +60,7 @@ package() {
"${pkgdir}/usr/share/fish/vendor_completions.d/${pkgname}.fish"

install -Dm 644 -t \
"${pkgdir}/usr/share/licenses/${pkgname}/GPL3" LICENSE.md
"${pkgdir}/usr/share/licenses/${pkgname}" LICENSE.md
install -Dm 644 -t \
"${pkgdir}/usr/share/doc/${pkgname}" README.md
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.19

require (
github.com/cavaliergopher/grab/v3 v3.0.1
github.com/github/go-spdx/v2 v2.2.0
github.com/go-git/go-git/v5 v5.10.0
github.com/go-playground/validator/v10 v10.16.0
github.com/mholt/archiver/v4 v4.0.0-alpha.8
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7
github.com/frankban/quicktest v1.14.5 h1:dfYrrRyLtiqT9GyKXgdh+k4inNeTvmGbuSgZ3lx3GhA=
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
github.com/github/go-spdx/v2 v2.2.0 h1:yBBLMasHA70Ujd35OpL/OjJOWWVNXcJGbars0GinGRI=
github.com/github/go-spdx/v2 v2.2.0/go.mod h1:hMCrsFgT0QnCwn7G8gxy/MxMpy67WgZrwFeISTn0o6w=
github.com/gliderlabs/ssh v0.3.5 h1:OcaySEmAQJgyYcArR+gGGTHCyE7nvhEMTlYY+Dp8CpY=
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI=
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376/go.mod h1:an3vInlBmSxCcxctByoQdvwPiA7DTK7jaaFDBTtu0ic=
Expand Down
21 changes: 18 additions & 3 deletions pkg/debian/constants.go → pkg/dpkg/constants.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package debian
package dpkg

var buildEnvironmentDeps = []string{
"build-essential",
Expand All @@ -7,7 +7,7 @@ var buildEnvironmentDeps = []string{
"ca-certificates",
}

var DebianArchs = map[string]string{
var DebArchs = map[string]string{
"any": "all",
"x86_64": "amd64",
"i686": "386",
Expand All @@ -21,7 +21,6 @@ const specFile = `
{{- /* Mandatory fields */ -}}
Package: {{.PkgName}}
Version: {{ if .Epoch}}{{ .Epoch }}:{{ end }}{{.PkgVer}}
{{- if .PreRelease}}~{{ .PreRelease }}{{- end }}
{{- if .PkgRel}}-{{ .PkgRel }}{{- end }}
Section: {{.Section}}
Priority: {{.Priority}}
Expand Down Expand Up @@ -55,3 +54,19 @@ case $1 in
*) exit;;
esac
`

const copyrightFile = `Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Upstream-Name: {{.PkgName}}
Upstream-Contact: {{.Maintainer}}
{{- if .URL}}
Source: {{.URL}}
{{- end }}
Files: *
{{- if .Copyright}}
Copyright: {{ range .Copyright}}{{ . }}
{{ end }}{{- end }}
{{- if .License}}
{{- range .License}}
License: {{ . }}{{- end }}
{{- end }}
`
87 changes: 51 additions & 36 deletions pkg/debian/debian.go → pkg/dpkg/dpkg.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package debian
package dpkg

import (
"bytes"
Expand All @@ -16,33 +16,33 @@ import (
"github.com/otiai10/copy"
)

// Debian represents a Debian package.
// Deb represents a Deb 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 {
type Deb struct {
debDir string
PKGBUILD *pkgbuild.PKGBUILD
}

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

// createConfFiles creates the conffiles file in the Debian package.
// createConfFiles creates the conffiles file in the Deb 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 {
func (d *Deb) createConfFiles() error {
var err error
if len(d.PKGBUILD.Backup) == 0 {
return err
Expand All @@ -68,11 +68,21 @@ func (d *Debian) createConfFiles() error {
return nil
}

// createDebconfTemplate creates a Debian package configuration file template.
func (d *Deb) createCopyrightFile() error {
if len(d.PKGBUILD.License) == 0 {
return nil
}

copyrightFilePath := filepath.Join(d.debDir, "copyright")

return d.PKGBUILD.CreateSpec(copyrightFilePath, copyrightFile)
}

// createDebconfTemplate creates a Deb 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 {
func (d *Deb) createDebconfTemplate() error {
var err error
if d.PKGBUILD.DebTemplate == "" {
return err
Expand All @@ -89,10 +99,10 @@ func (d *Debian) createDebconfTemplate() error {
return nil
}

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

// createScripts generates and writes the scripts for the Debian package.
// createScripts generates and writes the scripts for the Deb package.
//
// It takes no parameters.
// It returns an error if there was an issue generating or writing the scripts.
func (d *Debian) createScripts() error {
func (d *Deb) createScripts() error {
scripts := map[string]string{
"preinst": d.PKGBUILD.PreInst,
"postinst": d.PKGBUILD.PostInst,
Expand Down Expand Up @@ -147,14 +157,14 @@ func (d *Debian) createScripts() error {
return nil
}

// dpkgDeb generates Debian package files from the given artifact path.
// dpkgDeb generates Deb 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.
// Deb package files will be generated.
//
// The function returns an error if there was an issue generating the Debian package
// The function returns an error if there was an issue generating the Deb package
// files.
func (d *Debian) dpkgDeb(artifactPath string) error {
func (d *Deb) dpkgDeb(artifactPath string) error {
var err error

for _, arch := range d.PKGBUILD.Arch {
Expand All @@ -177,11 +187,11 @@ func (d *Debian) dpkgDeb(artifactPath string) error {
return nil
}

// Prepare prepares the Debian package by installing its dependencies using apt-get.
// Prepare prepares the Deb 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 {
func (d *Deb) Prepare(makeDepends []string) error {
args := []string{
"--assume-yes",
"install",
Expand All @@ -195,11 +205,11 @@ func (d *Debian) Prepare(makeDepends []string) error {
return nil
}

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

var tmplBytesBuffer bytes.Buffer
Expand Down Expand Up @@ -228,15 +238,15 @@ func (d *Debian) Strip() error {
return nil
}

// Update updates the Debian package list.
// Update updates the Deb package list.
//
// It calls the GetUpdates method of the PKGBUILD field of the Debian struct
// It calls the GetUpdates method of the PKGBUILD field of the Deb 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 {
func (d *Deb) Update() error {
err := d.PKGBUILD.GetUpdates("apt-get", "update")
if err != nil {
return err
Expand All @@ -245,16 +255,16 @@ func (d *Debian) Update() error {
return nil
}

// createDebResources creates the Debian package resources.
// createDebResources creates the Deb package resources.
//
// It creates the necessary directories and files for the Debian package.
// It creates the necessary directories and files for the Deb 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 {
func (d *Deb) createDebResources() error {
d.debDir = filepath.Join(d.PKGBUILD.PackageDir, "DEBIAN")
err := utils.ExistsMakeDir(d.debDir)

Expand All @@ -274,6 +284,11 @@ func (d *Debian) createDebResources() error {
return err
}

err = d.createCopyrightFile()
if err != nil {
return err
}

err = d.createScripts()
if err != nil {
return err
Expand All @@ -292,10 +307,10 @@ func (d *Debian) createDebResources() error {
return nil
}

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

d.getArch()
Expand Down Expand Up @@ -329,7 +344,7 @@ func (d *Debian) Build(artifactsPath string) error {
return nil
}

func (d *Debian) Install(artifactsPath string) error {
func (d *Deb) Install(artifactsPath string) error {
var err error

for _, arch := range d.PKGBUILD.Arch {
Expand All @@ -348,11 +363,11 @@ func (d *Debian) Install(artifactsPath string) error {
return nil
}

// PrepareEnvironment prepares the environment for the Debian package.
// PrepareEnvironment prepares the environment for the Deb 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 {
func (d *Deb) PrepareEnvironment(golang bool) error {
var err error

args := []string{
Expand All @@ -373,7 +388,7 @@ func (d *Debian) PrepareEnvironment(golang bool) error {
return nil
}

func (d *Debian) getRelease() {
func (d *Deb) getRelease() {
if d.PKGBUILD.Codename != "" {
d.PKGBUILD.PkgRel += d.PKGBUILD.Codename
} else {
Expand Down
8 changes: 4 additions & 4 deletions pkg/packer/packer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (

"github.com/M0Rf30/yap/pkg/apk"
"github.com/M0Rf30/yap/pkg/constants"
"github.com/M0Rf30/yap/pkg/debian"
"github.com/M0Rf30/yap/pkg/dpkg"
"github.com/M0Rf30/yap/pkg/pacman"
"github.com/M0Rf30/yap/pkg/pkgbuild"
"github.com/M0Rf30/yap/pkg/redhat"
"github.com/M0Rf30/yap/pkg/rpm"
)

// Packer is the common interface implemented by all package managers.
Expand Down Expand Up @@ -47,11 +47,11 @@ func GetPackageManager(pkgBuild *pkgbuild.PKGBUILD, distro string) Packer {
PKGBUILD: pkgBuild,
}
case "debian":
return &debian.Debian{
return &dpkg.Deb{
PKGBUILD: pkgBuild,
}
case "redhat":
return &redhat.Redhat{
return &rpm.RPM{
PKGBUILD: pkgBuild,
}
default:
Expand Down
Loading

0 comments on commit f018fdf

Please sign in to comment.