Skip to content

Commit

Permalink
build: fixed packaging tool reporting total instead of compressed size
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasklaen committed Nov 4, 2023
1 parent a8f040a commit 8d2dfc4
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 8 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ module uosc/bins
go 1.21.3

require (
github.com/atotto/clipboard v0.1.4
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
k8s.io/apimachinery v0.28.3
)

require github.com/atotto/clipboard v0.1.4 // indirect
26 changes: 21 additions & 5 deletions src/tools/lib/zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ import (
"path/filepath"
)

// CountingWriter wraps an io.Writer and counts the number of bytes written.
type CountingWriter struct {
written int64
writer io.Writer
}

// Write writes bytes and counts them.
func (cw *CountingWriter) Write(p []byte) (int, error) {
n, err := cw.writer.Write(p)
cw.written += int64(n)
return n, err
}

/*
`files` format:
Expand Down Expand Up @@ -39,7 +52,8 @@ func ZipFilesWithHeaders(files map[string]string, outputFile string, headerMod H
return ZipStats{}, err
}
defer f.Close()
zw := zip.NewWriter(f)
countedF := &CountingWriter{writer: f}
zw := zip.NewWriter(countedF)
defer zw.Close()

var filesNum, bytes int64
Expand Down Expand Up @@ -127,15 +141,17 @@ func ZipFilesWithHeaders(files map[string]string, outputFile string, headerMod H
}

return ZipStats{
FilesNum: filesNum,
Bytes: bytes,
FilesNum: filesNum,
TotalBytes: bytes,
CompressedBytes: countedF.written,
}, nil
}

// If `HeaderModFn` function sets `header.Name` to empty string, file will be skipped.
type HeaderModFn func(header *zip.FileHeader) *zip.FileHeader

type ZipStats struct {
FilesNum int64
Bytes int64
FilesNum int64
TotalBytes int64
CompressedBytes int64
}
2 changes: 1 addition & 1 deletion src/tools/tools/packager.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func Packager(args []string) {
fmt.Printf(
"Packaging into: %s\n- uosc.zip: %.2f MB, %d files\n- uosc.conf: %.1f KB",
filepath.ToSlash(must(filepath.Rel(cwd, releaseRoot)))+"/",
float64(stats.Bytes)/1024/1024,
float64(stats.CompressedBytes)/1024/1024,
stats.FilesNum,
float64(confSize)/1024,
)
Expand Down
Binary file modified tools/tools-darwin
Binary file not shown.
Binary file modified tools/tools-linux
Binary file not shown.
Binary file modified tools/tools.exe
Binary file not shown.

0 comments on commit 8d2dfc4

Please sign in to comment.