Skip to content

Commit

Permalink
feat: rpm: create packages with pure go
Browse files Browse the repository at this point in the history
  • Loading branch information
M0Rf30 committed Oct 20, 2024
1 parent 8886636 commit 500a06a
Show file tree
Hide file tree
Showing 14 changed files with 143 additions and 336 deletions.
1 change: 0 additions & 1 deletion build/deploy/amazon-1/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ RUN \
yum -y install \
bash-completion \
ca-certificates \
rpm-build \
sudo; \
yap completion bash > /etc/bash_completion.d/yap; \
echo "source /usr/share/bash-completion/bash_completion" >> /etc/bashrc; \
Expand Down
1 change: 0 additions & 1 deletion build/deploy/amazon-2/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ RUN \
yum -y install \
bash-completion \
ca-certificates \
rpm-build \
sudo; \
yap completion bash > /etc/bash_completion.d/yap; \
echo "source /usr/share/bash-completion/bash_completion" >> /etc/bashrc; \
Expand Down
1 change: 0 additions & 1 deletion build/deploy/fedora-38/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ RUN \
dnf -y install \
bash-completion \
ca-certificates \
rpm-build \
sudo; \
yap completion bash > /etc/bash_completion.d/yap; \
echo "source /usr/share/bash-completion/bash_completion" >> /etc/bashrc; \
Expand Down
1 change: 0 additions & 1 deletion build/deploy/rocky-8/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ RUN \
bash-completion \
ca-certificates \
dnf-plugins-core \
rpm-build \
sudo; \
dnf config-manager --enable devel; \
dnf config-manager --enable powertools; \
Expand Down
1 change: 0 additions & 1 deletion build/deploy/rocky-9/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ RUN \
bash-completion \
ca-certificates \
dnf-plugins-core \
rpm-build \
sudo; \
dnf config-manager --enable crb; \
dnf config-manager --enable devel; \
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/github/go-spdx/v2 v2.3.2
github.com/go-git/go-git/v5 v5.12.0
github.com/go-playground/validator/v10 v10.22.1
github.com/google/rpmpack v0.6.1-0.20240413165640-60c43da513f7
github.com/mholt/archiver/v4 v4.0.0-alpha.8.0.20240924230722-275fd2ee4270
github.com/otiai10/copy v1.14.0
github.com/pkg/errors v0.9.1
Expand Down Expand Up @@ -35,6 +36,7 @@ require (
github.com/bodgit/plumbing v1.3.0 // indirect
github.com/bodgit/sevenzip v1.5.1 // indirect
github.com/bodgit/windows v1.0.1 // indirect
github.com/cavaliergopher/cpio v1.0.1 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ github.com/bodgit/sevenzip v1.5.1/go.mod h1:Q3YMySuVWq6pyGEolyIE98828lOfEoeWg5ze
github.com/bodgit/windows v1.0.1 h1:tF7K6KOluPYygXa3Z2594zxlkbKPAOvqr97etrGNIz4=
github.com/bodgit/windows v1.0.1/go.mod h1:a6JLwrB4KrTR5hBpp8FI9/9W9jJfeQ2h4XDXU74ZCdM=
github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
github.com/cavaliergopher/cpio v1.0.1 h1:KQFSeKmZhv0cr+kawA3a0xTQCU4QxXF1vhU7P7av2KM=
github.com/cavaliergopher/cpio v1.0.1/go.mod h1:pBdaqQjnvXxdS/6CvNDwIANIFSP0xRKI16PX4xejRQc=
github.com/cavaliergopher/grab/v3 v3.0.1 h1:4z7TkBfmPjmLAAmkkAZNX/6QJ1nNFdv3SdIHXju0Fr4=
github.com/cavaliergopher/grab/v3 v3.0.1/go.mod h1:1U/KNnD+Ft6JJiYoYBAimKH2XrYptb8Kl3DFGmsjpq4=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand Down Expand Up @@ -144,6 +146,8 @@ github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OI
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/google/pprof v0.0.0-20200212024743-f11f1df84d12/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/rpmpack v0.6.1-0.20240413165640-60c43da513f7 h1:B6QHhFc8R7+dG/Vny4uAkrAdKRAVLTWMVDpHD36JF7E=
github.com/google/rpmpack v0.6.1-0.20240413165640-60c43da513f7/go.mod h1:uqVAUVQLq8UY2hCDfmJ/+rtO3aw7qyhc90rCVEabEfI=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/gookit/color v1.4.2/go.mod h1:fqRyamkC1W8uxl+lxCQxOT09l/vYfZ+QeiX3rKQHCoQ=
Expand Down
12 changes: 1 addition & 11 deletions pkg/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,5 @@ func (builder *Builder) getSources() error {
// initDirs creates mandatory fakeroot folders (src, pkg) for a single project.
// It returns any error if occurred.
func (builder *Builder) initDirs() error {
dirs := []string{
builder.PKGBUILD.SourceDir,
builder.PKGBUILD.PackageDir}

for _, dir := range dirs {
if err := utils.ExistsMakeDir(dir); err != nil {
return err
}
}

return nil
return utils.ExistsMakeDir(builder.PKGBUILD.SourceDir)
}
14 changes: 6 additions & 8 deletions pkg/dpkg/dpkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/blakesmith/ar"
"github.com/mholt/archiver/v4"
"github.com/otiai10/copy"
"github.com/pkg/errors"
)

// Deb represents a Deb package.
Expand Down Expand Up @@ -244,9 +243,9 @@ func (d *Deb) createDebResources() error {
}

d.PKGBUILD.InstalledSize, _ = utils.GetDirSize(d.PKGBUILD.PackageDir)
d.PKGBUILD.Depends = d.processDepends(d.PKGBUILD.Depends)
d.PKGBUILD.MakeDepends = d.processDepends(d.PKGBUILD.MakeDepends)
d.PKGBUILD.OptDepends = d.processDepends(d.PKGBUILD.OptDepends)
d.PKGBUILD.Depends = processDepends(d.PKGBUILD.Depends)
d.PKGBUILD.MakeDepends = processDepends(d.PKGBUILD.MakeDepends)
d.PKGBUILD.OptDepends = processDepends(d.PKGBUILD.OptDepends)

tmpl := d.PKGBUILD.RenderSpec(specFile)
if err := d.PKGBUILD.CreateSpec(filepath.Join(d.debDir,
Expand Down Expand Up @@ -280,7 +279,7 @@ func (d *Deb) createDebResources() error {
// 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 {
debTemp, err := os.MkdirTemp(d.PKGBUILD.StartDir, "temp")
debTemp, err := os.MkdirTemp(d.PKGBUILD.SourceDir, "tmp")
if err != nil {
return err
}
Expand Down Expand Up @@ -381,7 +380,7 @@ func addArFile(writer *ar.Writer, name string, body []byte) error {
}

if err := writer.WriteHeader(&header); err != nil {
return errors.Errorf("cannot write file header")
return err
}

_, err := writer.Write(body)
Expand Down Expand Up @@ -409,13 +408,12 @@ func (d *Deb) getRelease() {
//
// Returns:
// - a new slice of strings with modified elements for deb syntax.
func (d *Deb) processDepends(depends []string) []string {
func processDepends(depends []string) []string {
pattern := `(?m)(<|<=|>=|=|>|<)`
regex := regexp.MustCompile(pattern)

for index, depend := range depends {
result := regex.Split(depend, -1)

if len(result) == 2 {
name := result[0]
operator := strings.Trim(depend, result[0]+result[1])
Expand Down
13 changes: 7 additions & 6 deletions pkg/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,11 @@ func ParseFile(distro, release, startDir, home string) (*pkgbuild.PKGBUILD, erro
}

pkgBuild := &pkgbuild.PKGBUILD{
Distro: distro,
Codename: release,
StartDir: startDir,
Home: home,
SourceDir: filepath.Join(startDir, "src"),
PackageDir: filepath.Join(startDir, "staging"),
Distro: distro,
Codename: release,
StartDir: startDir,
Home: home,
SourceDir: filepath.Join(startDir, "src"),
}

err = utils.ExistsMakeDir(startDir)
Expand All @@ -75,6 +74,8 @@ func ParseFile(distro, release, startDir, home string) (*pkgbuild.PKGBUILD, erro
return nil, err
}

pkgBuild.SetMainFolders()

err = parseSyntaxFile(pkgbuildSyntax, pkgBuild)
if err != nil {
return nil, err
Expand Down
7 changes: 5 additions & 2 deletions pkg/pkgbuild/pkgbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ func (pkgBuild *PKGBUILD) AddItem(key string, data any) error {

pkgBuild.priorities[key] = priority
pkgBuild.mapVariables(key, data)
pkgBuild.setMainFolders()
pkgBuild.mapArrays(key, data)
pkgBuild.mapFunctions(key, data)
pkgBuild.processOptions()
Expand Down Expand Up @@ -370,12 +369,16 @@ func (pkgBuild *PKGBUILD) parseDirective(input string) (string, int, error) {
//
// It takes no parameters.
// It does not return anything.
func (pkgBuild *PKGBUILD) setMainFolders() {
func (pkgBuild *PKGBUILD) SetMainFolders() {
switch pkgBuild.Distro {
case "arch":
pkgBuild.PackageDir = filepath.Join(pkgBuild.StartDir, "pkg", pkgBuild.PkgName)
case "alpine":
pkgBuild.PackageDir = filepath.Join(pkgBuild.StartDir, "apk", "pkg", pkgBuild.PkgName)
default:
pkgBuild.PackageDir, _ = os.MkdirTemp(
pkgBuild.StartDir,
pkgBuild.Distro)
}

if err := os.Setenv("pkgdir", pkgBuild.PackageDir); err != nil {
Expand Down
35 changes: 6 additions & 29 deletions pkg/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,6 @@ func (mpc *MultipleProject) BuildAll() error {
// returns an error if there was a problem removing the directories.
func (mpc *MultipleProject) Clean() error {
for _, project := range mpc.Projects {
if err := utils.RemoveAll(project.Builder.PKGBUILD.PackageDir); err != nil {
return err
}

if CleanBuild {
if err := utils.RemoveAll(project.Builder.PKGBUILD.SourceDir); err != nil {
return err
Expand Down Expand Up @@ -187,12 +183,7 @@ func (mpc *MultipleProject) MultiProject(distro, release, path string) error {
return err
}

err := mpc.validateAllProject(distro, release, path)
if err != nil {
return err
}

err = utils.ExistsMakeDir(mpc.BuildDir)
err := utils.ExistsMakeDir(mpc.BuildDir)
if err != nil {
return err
}
Expand Down Expand Up @@ -277,6 +268,10 @@ func (mpc *MultipleProject) createPackage(proj *Project) error {
return err
}

if err := utils.RemoveAll(proj.Builder.PKGBUILD.PackageDir); err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -337,6 +332,7 @@ func (mpc *MultipleProject) populateProjects(distro, release, path string) error
return err
}

pkgbuildFile.Validate()
pkgbuildFile.ValidateArchitecture()

packageManager = packer.GetPackageManager(pkgbuildFile, distro)
Expand Down Expand Up @@ -420,25 +416,6 @@ func (mpc *MultipleProject) setSingleProject(path string) {
singleProject = true
}

// validateAllProject validates all projects in the MultipleProject struct.
//
// It takes in the distro, release, and path as parameters and returns an error.
func (mpc *MultipleProject) validateAllProject(distro, release, path string) error {
for _, child := range mpc.Projects {
pkgbuildFile, err := parser.ParseFile(distro,
release,
filepath.Join(mpc.BuildDir, child.Name),
filepath.Join(path, child.Name))
if err != nil {
return err
}

pkgbuildFile.Validate()
}

return nil
}

// validateJSON validates the JSON of the MultipleProject struct.
//
// It uses the validator package to validate the struct and returns any errors encountered.
Expand Down
94 changes: 0 additions & 94 deletions pkg/rpm/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const (
var buildEnvironmentDeps = []string{
"autoconf",
"automake",
"createrepo",
"expect",
"gcc",
"gcc-c++",
Expand All @@ -20,7 +19,6 @@ var buildEnvironmentDeps = []string{
"make",
"openssl",
"pkgconf",
"rpm-sign",
"which",
}

Expand Down Expand Up @@ -78,95 +76,3 @@ var (
"rocky": ".el",
}
)

const specFile = `
{{- /* Mandatory fields */ -}}
Name: {{.PkgName}}
Summary: {{.PkgDesc}}
{{- if .Epoch}}
Epoch: {{.Epoch}}
{{- end }}
Version: {{.PkgVer}}
Release: {{.PkgRel}}
{{- if .ArchComputed}}
BuildArch: {{.ArchComputed}}
{{- end }}
{{- if .Section}}
Group: {{.Section}}
{{- end }}
{{- if .URL}}
URL: {{.URL}}
{{- end }}
{{- if .License}}
{{- with .License}}
License: {{join .}}
{{- end }}
{{- else }}
License: CUSTOM
{{- end }}
{{- if .Maintainer}}
Packager: {{.Maintainer}}
{{- end }}
{{- if .Copyright}}
Vendor: {{ range .Copyright}}{{ . }}; {{ end }}{{- end }}
{{- with .Provides}}
Provides: {{join .}}
{{- end }}
{{- with .Conflicts}}
Conflicts: {{join .}}
{{- end }}
{{- with .Depends}}
Requires: {{join .}}
{{- end }}
{{- with .MakeDepends}}
BuildRequires: {{join .}}
{{- end }}
{{- with .OptDepends}}
Recommends: {{join .}}
{{- end }}
{{- with .OptDepends}}
Suggests: {{join .}}
{{- end }}
AutoReqProv: no
{{- if .PkgDest}}
%define _rpmdir {{.PkgDest}}
{{- end }}
%global _build_id_links none
%global _python_bytecompile_extra 0
%global _python_bytecompile_errors_terminate_build 0
%undefine __brp_python_bytecompile
%description
{{.PkgDesc}}
%files
{{- range $key, $value := .Files }}
{{- if $value }}
{{$value}}
{{- end }}
{{- end }}
{{- if .PreInst}}
%pre
{{.PreInst}}
{{- end }}
{{- if .PostInst}}
%post
{{.PostInst}}
{{- end }}
{{- if .PreRm}}
%preun
if [[ "$1" -ne 0 ]]; then exit 0; fi
{{.PreRm}}
{{- end }}
{{- if .PostRm}}
%postun
if [[ "$1" -ne 0 ]]; then exit 0; fi
{{.PostRm}}
{{- end }}
`
Loading

0 comments on commit 500a06a

Please sign in to comment.