Skip to content

Commit

Permalink
bali 3.1.0: support crate alias
Browse files Browse the repository at this point in the history
  • Loading branch information
fcharlie committed Sep 1, 2024
1 parent 687b5c5 commit 99424ca
Show file tree
Hide file tree
Showing 14 changed files with 133 additions and 71 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ name = "bali"
summary = "Bali - Minimalist Golang build and packaging tool"
description = "Bali - Minimalist Golang build and packaging tool"
package-name = "bali-dev"
version = "3.0.1"
version = "3.1.0"
license = "MIT"
prefix = "/usr/local"
crates = [
Expand Down Expand Up @@ -166,7 +166,7 @@ Program build file `crate.toml`:
name = "bali"
description = "Bali - Minimalist Golang build and packaging tool"
destination = "bin"
version = "3.0.1"
version = "3.1.0"
goflags = [
"-ldflags",
"-X 'main.VERSION=$BUILD_VERSION' -X 'main.BUILD_TIME=$BUILD_TIME' -X 'main.BUILD_BRANCH=$BUILD_BRANCH' -X 'main.BUILD_COMMIT=$BUILD_COMMIT' -X 'main.BUILD_REFNAME=$BUILD_REFNAME' -X 'main.BUILD_GOVERSION=$BUILD_GOVERSION'",
Expand Down
4 changes: 2 additions & 2 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ name = "bali"
summary = "Bali - Minimalist Golang build and packaging tool"
description = "Bali - Minimalist Golang build and packaging tool"
package-name = "bali-dev"
version = "3.0.1"
version = "3.1.0"
license = "MIT"
prefix = "/usr/local"
crates = [
Expand All @@ -152,7 +152,7 @@ permissions = "0664"
name = "bali"
description = "Bali - Minimalist Golang build and packaging tool"
destination = "bin"
version = "3.0.1"
version = "3.1.0"
goflags = [
"-ldflags",
"-X 'main.VERSION=$BUILD_VERSION' -X 'main.BUILD_TIME=$BUILD_TIME' -X 'main.BUILD_BRANCH=$BUILD_BRANCH' -X 'main.BUILD_COMMIT=$BUILD_COMMIT' -X 'main.BUILD_REFNAME=$BUILD_REFNAME' -X 'main.BUILD_GOVERSION=$BUILD_GOVERSION'",
Expand Down
2 changes: 1 addition & 1 deletion bali.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "bali"
summary = "Bali - Minimalist Golang build and packaging tool"
description = "Bali - Minimalist Golang build and packaging tool"
package-name = "bali-dev"
version = "3.0.1"
version = "3.1.0"
license = "MIT"
prefix = "/usr/local"
crates = [
Expand Down
5 changes: 4 additions & 1 deletion cmd/bali/crate.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
name = "bali"
description = "Bali - Minimalist Golang build and packaging tool"
destination = "bin"
version = "3.0.1"
version = "3.1.0"
goflags = [
"-ldflags",
"-X 'main.VERSION=$BUILD_VERSION' -X 'main.BUILD_TIME=$BUILD_TIME' -X 'main.BUILD_BRANCH=$BUILD_BRANCH' -X 'main.BUILD_COMMIT=$BUILD_COMMIT' -X 'main.BUILD_REFNAME=$BUILD_REFNAME' -X 'main.BUILD_GOVERSION=$BUILD_GOVERSION'",
]
# alias = [
# "bin/bali-${BUILD_VERSION}-${BUILD_TARGET}-${BUILD_ARCH}",
# ]
2 changes: 1 addition & 1 deletion cmd/bali/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

// version info
var (
VERSION = "3.0.1"
VERSION = "3.1.0"
BUILD_TIME string = "NONE"
BUILD_COMMIT string = "NONE"
BUILD_BRANCH string = "NONE"
Expand Down
39 changes: 35 additions & 4 deletions pkg/barrow/barrow.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ func (b *BarrowCtx) makeEnv() {
}
}

func (b *BarrowCtx) binaryName(name string) string {
if b.Target == "windows" {
return name + ".exe"
}
return name
}

func (b *BarrowCtx) Initialize(ctx context.Context) error {
version, host := resolveGoVersion(ctx)
if !isDistSupported(ctx, b.Target, b.Arch) {
Expand All @@ -129,6 +136,8 @@ func (b *BarrowCtx) Initialize(ctx context.Context) error {
b.extraEnv["BUILD_HOST"] = host
b.extraEnv["GOOS"] = b.Target
b.extraEnv["GOARCH"] = b.Arch
b.extraEnv["BUILD_TARGET"] = b.Target
b.extraEnv["BUILD_ARCH"] = b.Arch
if err := b.resolveGit(ctx); err != nil {
return err
}
Expand Down Expand Up @@ -232,6 +241,20 @@ func (b *BarrowCtx) Run(ctx context.Context) error {
return nil
}

func (b *BarrowCtx) makeAlias(from, to string) error {
if !filepath.IsAbs(to) {
to = filepath.Join(b.Out, to)
}
if _, err := os.Lstat(to); err == nil {
_ = os.Remove(to)
}
if err := os.Symlink(from, to); err != nil {
fmt.Fprintf(os.Stderr, "create symlink error: %v\n", err)
return err
}
return nil
}

func (b *BarrowCtx) compile(ctx context.Context, location string) (*Crate, error) {
crate, err := b.LoadCrate(location)
if err != nil {
Expand All @@ -246,7 +269,7 @@ func (b *BarrowCtx) compile(ctx context.Context, location string) (*Crate, error
defer releaseFn() // remove it
}
b.DbgPrint("crate: %s\n", crate.Name)
baseName := crate.baseName(b.Target)
baseName := b.binaryName(crate.Name)
psArgs := make([]string, 0, 8)
psArgs = append(psArgs, "build", "-o", baseName)
for _, flag := range crate.GoFlags {
Expand All @@ -263,12 +286,20 @@ func (b *BarrowCtx) compile(ctx context.Context, location string) (*Crate, error
fmt.Fprintf(os.Stderr, "compile %s error \x1b[31m%s\x1b[0m\n", crate.Name, err)
return nil, err
}
destTo := filepath.Join(b.Out, crate.Destination, baseName)
_ = os.MkdirAll(filepath.Dir(destTo), 0755)
if err := os.Rename(filepath.Join(crate.cwd, baseName), destTo); err != nil {
crateDestination := filepath.Join(crate.Destination, baseName)
crateFullPath := filepath.Join(b.Out, crateDestination)
_ = os.MkdirAll(filepath.Dir(crateFullPath), 0755)
if err := os.Rename(filepath.Join(crate.cwd, baseName), crateFullPath); err != nil {
fmt.Fprintf(os.Stderr, "move out to dest error: %v\n", err)
return nil, err
}
for _, a := range crate.Alias {
aliasExpend := b.ExpandEnv(b.binaryName(a))
stage("compile", "Link \x1b[38;02;39;199;173m%s\x1b[0m --> \x1b[38;02;39;199;173m%s\x1b[0m ", filepath.ToSlash(crateDestination), filepath.ToSlash(aliasExpend))
if err := b.makeAlias(crateFullPath, aliasExpend); err != nil {
return nil, err
}
}
return crate, nil
}

Expand Down
21 changes: 21 additions & 0 deletions pkg/barrow/barrow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"path/filepath"
"strconv"
"testing"

"github.com/pelletier/go-toml/v2"
)

func TestParsePerm(t *testing.T) {
Expand All @@ -23,3 +25,22 @@ func TestNameInArchive(t *testing.T) {
nameInArchive := filepath.Join(prefix, dest, baseName)
fmt.Fprintf(os.Stderr, "%s %s\n", nameInArchive, ToNixPath(nameInArchive))
}

func TestEncodePackage(t *testing.T) {
p := &Package{
Name: "jack",
Include: []*FileItem{
{
Path: "LICENSE",
Destination: "share",
},
{
Path: "README.md",
Destination: "/usr/share",
},
},
}
if err := toml.NewEncoder(os.Stderr).Encode(p); err != nil {
fmt.Fprintf(os.Stderr, "encode error: %v\n", err)
}
}
4 changes: 2 additions & 2 deletions pkg/barrow/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func (b *BarrowCtx) cleanupItem(item *FileItem, force bool) error {
default:
destTo = filepath.Join(saveDir, filepath.Base(item.Path))
}
if si, err := os.Stat(destTo); err == nil {
o, err := os.Stat(source)
if si, err := os.Lstat(destTo); err == nil {
o, err := os.Lstat(source)
if err != nil {
return err
}
Expand Down
8 changes: 1 addition & 7 deletions pkg/barrow/crate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,10 @@ type Crate struct {
Destination string `toml:"destination,omitempty"`
GoFlags []string `toml:"goflags,omitempty"`
Version string `toml:"version,omitempty"`
Alias []string `toml:"alias,omitempty"` // with out suffix
cwd string `toml:"-"`
}

func (c *Crate) baseName(target string) string {
if target == "windows" {
return c.Name + ".exe"
}
return c.Name
}

func (b *BarrowCtx) LoadCrate(location string) (*Crate, error) {
cwd := filepath.Join(b.CWD, location)
file := filepath.Join(cwd, "crate.toml")
Expand Down
33 changes: 16 additions & 17 deletions pkg/barrow/nfpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,10 @@ func (b *BarrowCtx) addItem2Nfpm(info *nfpm.Info, item *FileItem, prefix string)
default:
nameInArchive = filepath.Join(prefix, item.Destination, filepath.Base(item.Path))
}
si, err := os.Lstat(itemPath)
si, err := os.Stat(itemPath)
if err != nil {
return err
}
if isSymlink(si) {
// FIXME: nfpm not write symlink content ?
info.Overridables.Contents = append(info.Overridables.Contents, &files.Content{
Type: files.TypeSymlink,
Source: itemPath,
Destination: nameInArchive,
FileInfo: &files.ContentFileInfo{
Mode: si.Mode(),
MTime: si.ModTime(),
Size: si.Size(),
},
})
return nil
}
mode := si.Mode().Perm()
if len(item.Permissions) != 0 {
if m, err := strconv.ParseInt(item.Permissions, 8, 64); err == nil {
Expand All @@ -64,7 +50,7 @@ func (b *BarrowCtx) addItem2Nfpm(info *nfpm.Info, item *FileItem, prefix string)
}

func (b *BarrowCtx) addCrate2Nfpm(info *nfpm.Info, crate *Crate, prefix string) error {
baseName := crate.baseName(b.Target)
baseName := b.binaryName(crate.Name)
out := filepath.Join(b.Out, crate.Destination, baseName)
si, err := os.Lstat(out)
if err != nil {
Expand All @@ -73,7 +59,7 @@ func (b *BarrowCtx) addCrate2Nfpm(info *nfpm.Info, crate *Crate, prefix string)
nameInArchive := filepath.Join(prefix, crate.Destination, baseName)
info.Overridables.Contents = append(info.Overridables.Contents, &files.Content{
Source: out,
Destination: nameInArchive,
Destination: AsExplicitRelativePath(nameInArchive),
FileInfo: &files.ContentFileInfo{
Owner: "root",
Group: "root",
Expand All @@ -82,6 +68,19 @@ func (b *BarrowCtx) addCrate2Nfpm(info *nfpm.Info, crate *Crate, prefix string)
Size: si.Size(),
},
})
for _, a := range crate.Alias {
aliasExpend := filepath.Join(prefix, b.ExpandEnv(b.binaryName(a)))
aliasPath, err := filepath.Rel(filepath.Dir(aliasExpend), filepath.Dir(nameInArchive))
if err != nil {
return err
}
aliasLink := filepath.Join(aliasPath, filepath.Base(nameInArchive))
info.Overridables.Contents = append(info.Overridables.Contents, &files.Content{
Source: AsExplicitRelativePath(aliasLink),
Destination: AsExplicitRelativePath(aliasExpend),
Type: files.TypeSymlink,
})
}
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/barrow/rpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (b *BarrowCtx) addItem2RPM(r *rpmpack.RPM, item *FileItem, prefix string) e
}

func (b *BarrowCtx) addCrate2RPM(r *rpmpack.RPM, crate *Crate, prefix string) error {
baseName := crate.baseName(b.Target)
baseName := b.binaryName(crate.Name)
out := filepath.Join(b.Out, crate.Destination, baseName)
fd, err := os.Open(out)
if err != nil {
Expand Down
30 changes: 21 additions & 9 deletions pkg/barrow/tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (

func (b *BarrowCtx) addItem2Tar(z *tar.Writer, item *FileItem, prefix string) error {
itemPath := filepath.Join(b.CWD, item.Path)
si, err := os.Lstat(itemPath)
si, err := os.Stat(itemPath)
if err != nil {
return err
}
Expand All @@ -31,13 +31,7 @@ func (b *BarrowCtx) addItem2Tar(z *tar.Writer, item *FileItem, prefix string) er
default:
nameInArchive = filepath.Join(prefix, item.Destination, filepath.Base(item.Path))
}
var linkTarget string
if isSymlink(si) {
if linkTarget, err = os.Readlink(itemPath); err != nil {
return err
}
}
hdr, err := tar.FileInfoHeader(si, linkTarget)
hdr, err := tar.FileInfoHeader(si, "")
if err != nil {
return err
}
Expand Down Expand Up @@ -68,7 +62,7 @@ func (b *BarrowCtx) addItem2Tar(z *tar.Writer, item *FileItem, prefix string) er
}

func (b *BarrowCtx) addCrate2Tar(z *tar.Writer, crate *Crate, prefix string) error {
baseName := crate.baseName(b.Target)
baseName := b.binaryName(crate.Name)
out := filepath.Join(b.Out, crate.Destination, baseName)
si, err := os.Lstat(out)
if err != nil {
Expand All @@ -92,6 +86,24 @@ func (b *BarrowCtx) addCrate2Tar(z *tar.Writer, crate *Crate, prefix string) err
if _, err := io.Copy(z, fd); err != nil {
return err
}
for _, a := range crate.Alias {
aliasExpend := filepath.Join(prefix, b.ExpandEnv(b.binaryName(a)))
aliasPath, err := filepath.Rel(filepath.Dir(aliasExpend), filepath.Dir(nameInArchive))
if err != nil {
return err
}
aliasLink := filepath.Join(aliasPath, filepath.Base(nameInArchive))
ah := &tar.Header{
Name: AsExplicitRelativePath(aliasExpend),
Linkname: AsExplicitRelativePath(aliasLink),
Typeflag: tar.TypeSymlink,
Format: tar.FormatGNU,
ModTime: si.ModTime(),
}
if err = z.WriteHeader(ah); err != nil {
return fmt.Errorf("write tar header error: %w", err)
}
}
return nil
}

Expand Down
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.Target)
vi.StringFileInfo.InternalName = b.binaryName(e.Name)
}
if len(vi.StringFileInfo.FileDescription) == 0 {
vi.StringFileInfo.FileDescription = e.Description
Expand Down
Loading

0 comments on commit 99424ca

Please sign in to comment.