Skip to content

Commit

Permalink
Support calculating file hash when packing. close #7
Browse files Browse the repository at this point in the history
  • Loading branch information
fcharlie committed Aug 5, 2020
1 parent 4a89990 commit c67c533
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 49 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ Project file `bali.toml`:
```toml
# https://toml.io/en/
name = "bali"
version = "1.2.0"
version = "1.2.1"
dirs = [
"cmd/bali", # dirs
]
Expand Down Expand Up @@ -173,7 +173,7 @@ Program build file `balisrc.toml`:
name = "bali"
description = "Bali - Minimalist Golang build and packaging tool"
destination = "bin"
version = "1.2.0"
version = "1.2.1"
versioninfo = "res/versioninfo.json"
icon = "res/bali.ico"
manifest = "res/bali.manifest"
Expand Down
4 changes: 2 additions & 2 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ Bali 同时支持 TOML 或者 JSON 格式的项目文件,JSON 使用内置解
```toml
# https://toml.io/en/
name = "bali"
version = "1.2.0"
version = "1.2.1"
dirs = [
"cmd/bali", # dirs
]
Expand Down Expand Up @@ -155,7 +155,7 @@ norename = true
name = "bali"
description = "Bali - Minimalist Golang build and packaging tool"
destination = "bin"
version = "1.2.0"
version = "1.2.1"
versioninfo = "res/versioninfo.json"
icon = "res/bali.ico"
manifest = "res/bali.manifest"
Expand Down
2 changes: 1 addition & 1 deletion bali.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bali",
"version": "1.2.0",
"version": "1.2.1",
"files": [
{
"path": "LICENSE",
Expand Down
2 changes: 1 addition & 1 deletion bali.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# https://toml.io/en/
name = "bali"
version = "1.2.0"
version = "1.2.1"
dirs = [
"cmd/bali", # dirs
]
Expand Down
2 changes: 1 addition & 1 deletion cmd/bali/balisrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "bali",
"description": "Bali - Minimalist Golang build and packaging tool",
"destination": "bin",
"version": "1.2.0",
"version": "1.2.1",
"links": [
"bin/baligo"
],
Expand Down
2 changes: 1 addition & 1 deletion cmd/bali/balisrc.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "bali"
description = "Bali - Minimalist Golang build and packaging tool"
destination = "bin"
version = "1.2.0"
version = "1.2.1"
versioninfo = "res/versioninfo.json"
icon = "res/bali.ico"
manifest = "res/bali.manifest"
Expand Down
85 changes: 52 additions & 33 deletions cmd/bali/executor.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package main

import (
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -219,50 +222,59 @@ func (be *Executor) Compress() error {
var outfile string
var err error
var fd *os.File
var mw io.Writer
var pk pack.Packer
h := sha256.New()
var outname string
if be.target == "windows" {
outfile = filepath.Join(be.destination, base.StrCat(be.bm.Name, "-", be.target, "-", be.arch, "-", be.bm.Version, ".zip"))
fd, err = os.Create(outfile)
if err != nil {
outname = base.StrCat(be.bm.Name, "-", be.target, "-", be.arch, "-", be.bm.Version, ".zip")
outfile = filepath.Join(be.destination, outname)
if fd, err = os.Create(outfile); err != nil {
return err
}
if be.zipmethod != 0 {
pk = pack.NewZipPackerEx(fd, be.zipmethod)
} else {
pk = pack.NewZipPacker(fd)
}
mw = io.MultiWriter(fd, h)
pk = pack.NewZipPackerEx(mw, be.zipmethod)
} else {
outfile = filepath.Join(be.destination, base.StrCat(be.bm.Name, "-", be.target, "-", be.arch, "-", be.bm.Version, ".tar.gz"))
fd, err = os.Create(outfile)
if err != nil {
outname = base.StrCat(be.bm.Name, "-", be.target, "-", be.arch, "-", be.bm.Version, ".tar.gz")
outfile = filepath.Join(be.destination, outname)
if fd, err = os.Create(outfile); err != nil {
return err
}
pk = pack.NewTargzPacker(fd)
}
mw = io.MultiWriter(fd, h)
pk = pack.NewTargzPacker(mw)
}
// Please keep order
defer func() {
if err == nil && h != nil {
fmt.Fprintf(os.Stderr, "\x1b[34m%s %s\x1b[0m\n", hex.EncodeToString(h.Sum(nil)), outname)
fmt.Fprintf(os.Stderr, "bali create archive \x1b[32m%s\x1b[0m success\n", outname)
}
}()
defer fd.Close()
defer pk.Close()

for _, b := range be.binaries {
rel, err := filepath.Rel(be.out, b)
if err != nil {
var rel string
if rel, err = filepath.Rel(be.out, b); err != nil {
return err
}
fmt.Fprintf(os.Stderr, "compress target \x1b[32m%s\x1b[0m\n", rel)
if err := pk.AddFileEx(b, be.PathInArchive(rel), true); err != nil {
if err = pk.AddFileEx(b, be.PathInArchive(rel), true); err != nil {
return err
}
}
for name, lnkName := range be.linkmap {
nameInArchive := be.PathInArchive(name)
fmt.Fprintf(os.Stderr, "compress link \x1b[32m%s --> %s\x1b[0m\n", nameInArchive, lnkName)
if err := pk.AddTargetLink(nameInArchive, lnkName); err != nil {
if err = pk.AddTargetLink(nameInArchive, lnkName); err != nil {
return err
}
}
for _, f := range be.bm.Files {
file := filepath.Join(be.workdir, f.Path)
rel := filepath.Join(f.Destination, f.Base())
fmt.Fprintf(os.Stderr, "compress profile \x1b[32m%s\x1b[0m\n", f.Path)
if err := pk.AddFileEx(file, be.PathInArchive(rel), f.Executable); err != nil {
if err = pk.AddFileEx(file, be.PathInArchive(rel), f.Executable); err != nil {
return err
}
}
Expand All @@ -279,34 +291,43 @@ func (be *Executor) PackWin() error {
func (be *Executor) PackUNIX() error {
outfilename := base.StrCat(be.bm.Name, "-", be.target, "-", be.arch, "-", be.bm.Version, ".sh")
outfile := filepath.Join(be.destination, outfilename)
fd, err := pack.OpenFile(outfile)
hashfd, err := pack.OpenHashableFile(outfile)
if err != nil {
return err
}
defer fd.Close()
pk := pack.NewTargzPacker(fd)
// Keep order
defer func() {
if err == nil {
hashfd.Hashsum(outfilename)
fmt.Fprintf(os.Stderr, "create \x1b[32m%s\x1b[0m done\n", outfile)
fmt.Fprintf(os.Stderr, "Your can run '\x1b[32m./%s --prefix=/path/to/%s\x1b[0m' to install %s\n", outfilename, be.bm.Name, be.bm.Name)
fmt.Fprintf(os.Stderr, "bali create package \x1b[32m%s\x1b[0m success\n", outfilename)
}
}()
defer hashfd.Close()
pk := pack.NewTargzPacker(hashfd)
defer pk.Close()
var rw pack.RespondWriter
// bali post install script
if len(be.bm.Respond) != 0 {
if !base.PathExists(be.bm.Respond) {
return base.ErrorCat("respond file ", be.bm.Respond, " not found")
}
if err := pk.AddFileEx(be.bm.Respond, "bali_post_install", true); err != nil {
if err = pk.AddFileEx(be.bm.Respond, "bali_post_install", true); err != nil {
return err
}
} else if !be.norename {
if err := rw.Initialize(); err != nil {
if err = rw.Initialize(); err != nil {
return err
}
if err := rw.WriteBase(); err != nil {
if err = rw.WriteBase(); err != nil {
return err
}
}

for _, s := range be.binaries {
rel, err := filepath.Rel(be.out, s)
if err != nil {
var rel string
if rel, err = filepath.Rel(be.out, s); err != nil {
_ = rw.Close()
return err
}
Expand All @@ -315,7 +336,7 @@ func (be *Executor) PackUNIX() error {
if !be.norename {
nameInArchive = base.StrCat(nameInArchive, ".new")
}
if err := pk.AddFileEx(s, nameInArchive, true); err != nil {
if err = pk.AddFileEx(s, nameInArchive, true); err != nil {
_ = rw.Close()
return err
}
Expand All @@ -325,7 +346,7 @@ func (be *Executor) PackUNIX() error {
for name, lnkName := range be.linkmap {
nameInArchive := be.PathInArchive(name)
fmt.Fprintf(os.Stderr, "compress link \x1b[32m%s --> %s\x1b[0m\n", nameInArchive, lnkName)
if err := pk.AddTargetLink(nameInArchive, lnkName); err != nil {
if err = pk.AddTargetLink(nameInArchive, lnkName); err != nil {
_ = rw.Close()
return err
}
Expand All @@ -335,14 +356,14 @@ func (be *Executor) PackUNIX() error {
rel := filepath.Join(f.Destination, f.Base())
fmt.Fprintf(os.Stderr, "compress profile \x1b[32m%s\x1b[0m\n", rel)
if be.norename || f.NoRename {
if err := pk.AddFileEx(file, be.PathInArchive(rel), f.Executable); err != nil {
if err = pk.AddFileEx(file, be.PathInArchive(rel), f.Executable); err != nil {
_ = rw.Close()
return err
}
DbgPrint("Add profile %s (no rename)", f.Path)
} else {
nameInArchive := base.StrCat(be.PathInArchive(rel), ".template")
if err := pk.AddFileEx(file, nameInArchive, f.Executable); err != nil {
if err = pk.AddFileEx(file, nameInArchive, f.Executable); err != nil {
_ = rw.Close()
return err
}
Expand All @@ -353,12 +374,10 @@ func (be *Executor) PackUNIX() error {
_ = rw.Close()
if len(rw.Path) != 0 {
DbgPrint("Add post install script %s", rw.Path)
if err := pk.AddFileEx(rw.Path, pack.RespondName, true); err != nil {
if err = pk.AddFileEx(rw.Path, pack.RespondName, true); err != nil {
return err
}
}
fmt.Fprintf(os.Stderr, "create \x1b[32m%s\x1b[0m done\n", outfile)
fmt.Fprintf(os.Stderr, "Your can run '\x1b[32m./%s --prefix=/path/to/%s\x1b[0m' to install %s\n", outfilename, be.bm.Name, be.bm.Name)
return nil
}

Expand Down
3 changes: 1 addition & 2 deletions cmd/bali/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func (be *Executor) ParseArgv() error {
pa.Add("pack", base.NOARG, 'p')
pa.Add("cleanup", base.NOARG, 1001)
pa.Add("no-rename", base.NOARG, 1002)
be.zipmethod = pack.Deflate
if err := pa.Execute(os.Args, be); err != nil {
return err
}
Expand Down Expand Up @@ -194,13 +195,11 @@ func main() {
fmt.Fprintf(os.Stderr, "bali: compress: \x1b[31m%v\x1b[0m\n", err)
os.Exit(1)
}
fmt.Fprintf(os.Stderr, "bali create archive \x1b[32m%s\x1b[0m success\n", be.bm.Name)
}
if be.makepack {
if err := be.Pack(); err != nil {
fmt.Fprintf(os.Stderr, "bali: make pack: \x1b[31m%v\x1b[0m\n", err)
os.Exit(1)
}
fmt.Fprintf(os.Stderr, "bali create package \x1b[32m%s\x1b[0m success\n", be.bm.Name)
}
}
2 changes: 1 addition & 1 deletion cmd/bali/res/versioninfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"OriginalFilename": "bali.exe",
"PrivateBuild": "",
"ProductName": "Bali",
"ProductVersion": "1.0",
"ProductVersion": "1.2",
"SpecialBuild": ""
},
"VarFileInfo": {
Expand Down
52 changes: 47 additions & 5 deletions pack/stgz.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package pack

import (
"crypto/sha256"
"encoding/hex"
"fmt"
"hash"
"io"
"os"
"path/filepath"
)
Expand Down Expand Up @@ -111,18 +115,56 @@ bali_apply_config() {
`
)

// OpenFile todo
func OpenFile(name string) (*os.File, error) {
// HashableFile hash file
type HashableFile struct {
fd *os.File
h hash.Hash
mw io.Writer
}

// WriteString write string
func (f *HashableFile) WriteString(s string) (int, error) {
return f.mw.Write([]byte(s))
}

// Close close file
func (f *HashableFile) Close() error {
if f == nil || f.fd == nil {
return nil
}
return f.fd.Close()
}

// Hashsum hash sum
func (f *HashableFile) Hashsum(name string) {
if f == nil {
return
}
if f.h == nil {
return
}
fmt.Fprintf(os.Stderr, "\x1b[34m%s %s\x1b[0m\n", hex.EncodeToString(f.h.Sum(nil)), name)
}

// Write a file
func (f *HashableFile) Write(p []byte) (int, error) {
return f.mw.Write(p)
}

// OpenHashableFile todo
func OpenHashableFile(name string) (*HashableFile, error) {
fd, err := os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
return nil, err
}
if _, err := fd.WriteString(header); err != nil {
_ = fd.Close()
file := &HashableFile{h: sha256.New(), fd: fd}
file.mw = io.MultiWriter(file.fd, file.h)
if _, err := file.WriteString(header); err != nil {
_ = file.Close()
_ = os.Remove(name)
return nil, err
}
return fd, nil
return file, nil
}

// RespondWriter todo
Expand Down

0 comments on commit c67c533

Please sign in to comment.