Skip to content

Commit

Permalink
v3 install files
Browse files Browse the repository at this point in the history
  • Loading branch information
fcharlie committed Aug 25, 2024
1 parent 437232d commit 9a014af
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
*.zip
/*.sh
cmd/bali/bali
cmd/bali/balistub
*.rpm
7 changes: 6 additions & 1 deletion bali.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
# https://toml.io/en/
name = "bali"
summary = "Bali - Minimalist Golang build and packaging tool"
description = "Bali - Minimalist Golang build and packaging tool"
package-name = "bali-name"
version = "3.0.0"
license = "MIT"
prefix = "/usr/local"
crates = [
"cmd/bali", # crates
"cmd/peassets",
]
description = "Bali - Minimalist Golang build and packaging tool"


dirs = [
"cmd/bali", # targets
Expand Down
25 changes: 20 additions & 5 deletions pkg/barrow/barrow.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type BarrowCtx struct {
Verbose bool
extraEnv map[string]string
environ []string
// TODO signature
}

func (b *BarrowCtx) DbgPrint(format string, a ...any) {
Expand Down Expand Up @@ -139,7 +140,8 @@ func (b *BarrowCtx) Initialize(ctx context.Context) error {
}

func (b *BarrowCtx) Run(ctx context.Context) error {
p, err := LoadPackage(b.CWD)
b.DbgPrint("target: %s arch: %s", b.Target, b.Arch)
p, err := b.LoadPackage(b.CWD)
if err != nil {
fmt.Fprintf(os.Stderr, "parse package metadata error: %v\n", err)
return err
Expand All @@ -162,16 +164,29 @@ func (b *BarrowCtx) Run(ctx context.Context) error {
}
crates = append(crates, crate)
}
fmt.Fprintf(os.Stderr, "crates: %d\n", len(crates))
if len(b.Pack) == 0 {
return nil
}
switch strings.ToLower(b.Pack) {
case "zip":
if err := b.zip(ctx, p, crates); err != nil {
return err
}
case "rpm":
if err := b.rpm(ctx, p, crates); err != nil {
return err
}
case "sh", "stgz":
if err := b.stgz(ctx, p, crates); err != nil {
return err
}
case "tar.gz", "tgz":
case "":
return nil
if err := b.tgz(ctx, p, crates); err != nil {
return err
}
default:
return errors.New("bad format")
fmt.Fprintf(os.Stderr, "unsupported pack format '%s'\n", b.Pack)
return fmt.Errorf("unsupported pack format '%s'", b.Pack)
}
return nil
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/barrow/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ type FileItem struct {

type Package struct {
Name string `toml:"name"`
PackageName string `toml:"package-name,omitempty"`
Summary string `toml:"summary,omitempty"` // Is a short description of the software
Description string `toml:"description,omitempty"` // description is a longer piece of software information than Summary, consisting of one or more paragraphs
Version string `toml:"version,omitempty"`
Authors []string `toml:"authors,omitempty"`
Vendor string `toml:"vendor,omitempty"`
URL string `toml:"url,omitempty"`
Packager string `toml:"url,omitempty"` // BALI_RPM_PACKAGER
Packager string `toml:"packager,omitempty"` // BALI_RPM_PACKAGER
Group string `toml:"group,omitempty"`
License string `toml:"license,omitempty"`
LicenseFile string `toml:"license-file,omitempty"`
Expand All @@ -47,12 +48,13 @@ func LoadMetadata(file string, v any) error {
return nil
}

func LoadPackage(cwd string) (*Package, error) {
func (b *BarrowCtx) LoadPackage(cwd string) (*Package, error) {
file := filepath.Join(cwd, "bali.toml")
var p Package
if err := LoadMetadata(file, &p); err != nil {
return nil, err
}
p.PackageName = b.Getenv("PACKAGE_NAME")
return &p, nil
}

Expand Down
151 changes: 151 additions & 0 deletions pkg/barrow/rpm.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,155 @@
package barrow

import (
"context"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"strconv"
"time"

"github.com/google/rpmpack"
)

// https://docs.redhat.com/zh_hans/documentation/red_hat_enterprise_linux/7/html/rpm_packaging_guide/working-with-spec-files
//

func (b *BarrowCtx) addItem(r *rpmpack.RPM, item *FileItem, prefix string) error {
fd, err := os.Open(filepath.Join(b.CWD, item.Path))
if err != nil {
return err
}
defer fd.Close()
si, err := fd.Stat()
if err != nil {
return err
}
payload, err := io.ReadAll(fd)
if err != nil {
return err
}
var saveTo string
switch {
case len(item.Rename) != 0:
saveTo = filepath.Join(prefix, item.Destination, item.Rename)
default:
saveTo = filepath.Join(prefix, item.Destination, filepath.Base(item.Path))
}
mode := si.Mode().Perm()
if len(item.Permissions) != 0 {
if m, err := strconv.ParseInt(item.Permissions, 8, 64); err == nil {
mode = fs.FileMode(m)
}
}
r.AddFile(rpmpack.RPMFile{
Name: saveTo,
Body: payload,
Mode: uint(mode),
Group: "root",
Owner: "root",
MTime: uint32(si.ModTime().Unix()),
})
return nil
}

func (b *BarrowCtx) addCrate(r *rpmpack.RPM, crate *Crate, prefix string) error {
baseName := crate.baseName(b.Target)
out := filepath.Join(b.Out, crate.Destination, baseName)
fd, err := os.Open(out)
if err != nil {
return err
}
defer fd.Close()
si, err := fd.Stat()
if err != nil {
return err
}
payload, err := io.ReadAll(fd)
if err != nil {
return err
}
r.AddFile(rpmpack.RPMFile{
Name: filepath.Join(prefix, crate.Destination, baseName),
Body: payload,
Mode: 0755,
Group: "root",
Owner: "root",
MTime: uint32(si.ModTime().Unix()),
})
return nil
}

// var (
// archList = map[string]string{
// "arm64": "aarch64",
// "amd64": "x86_64",
// "riscv64": "riscv64",
// "loong64": "loong64",
// }
// )

// func convertArch(arch string) string {
// if a, ok := archList[arch]; ok {
// return a
// }
// return arch
// }

func (b *BarrowCtx) rpm(ctx context.Context, p *Package, crates []*Crate) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
packageName := p.PackageName
if len(packageName) == 0 {
packageName = p.Name
}
// arch := convertArch(b.Arch)
r, err := rpmpack.NewRPM(rpmpack.RPMMetaData{
Name: packageName,
Summary: p.Summary,
Description: p.Description,
Version: p.Version,
Release: b.Release,
Arch: b.Arch,
Vendor: p.Vendor,
URL: p.URL,
Packager: p.Packager,
Group: p.Group,
Licence: p.License,
BuildHost: b.Getenv("BUILD_HOST"),
BuildTime: time.Now(),
})
if err != nil {
return err
}
for _, item := range p.Include {
if err := b.addItem(r, item, p.Prefix); err != nil {
return err
}
}
for _, crate := range crates {
if err := b.addCrate(r, crate, p.Prefix); err != nil {
return err
}
}
var rpmName string
switch {
case len(r.Release) == 0:
rpmName = fmt.Sprintf("%s-%s.%s.rpm", r.Name, r.Version, r.Arch)
default:
rpmName = fmt.Sprintf("%s-%s-%s.%s.rpm", r.Name, r.Version, r.Release, r.Arch)
}
fd, err := os.Create(filepath.Join(b.CWD, rpmName))
if err != nil {
return err
}
defer fd.Close()
if err := r.Write(fd); err != nil {
return err
}
return nil
}
21 changes: 21 additions & 0 deletions pkg/barrow/tgz.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package barrow

import "context"

func (b *BarrowCtx) stgz(ctx context.Context, p *Package, crates []*Crate) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
return nil
}

func (b *BarrowCtx) tgz(ctx context.Context, p *Package, crates []*Crate) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
return nil
}
2 changes: 1 addition & 1 deletion pkg/barrow/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (b *BarrowCtx) makeResources(e *Crate, saveTo string) error {
vi.StringFileInfo.ProductName = e.Name
}
if len(vi.StringFileInfo.InternalName) == 0 {
vi.StringFileInfo.InternalName = e.baseName(b.Arch)
vi.StringFileInfo.InternalName = e.baseName(b.Target)
}
if len(vi.StringFileInfo.FileDescription) == 0 {
vi.StringFileInfo.FileDescription = e.Description
Expand Down
12 changes: 12 additions & 0 deletions pkg/barrow/zip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package barrow

import "context"

func (b *BarrowCtx) zip(ctx context.Context, p *Package, crates []*Crate) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
return nil
}

0 comments on commit 9a014af

Please sign in to comment.