Skip to content

Commit

Permalink
Produce ZIP archives for Windows releases (#195)
Browse files Browse the repository at this point in the history
On Windows systems tar isn't available by default. This change creates
additional archives in the ZIP format. The tar.gz archive format on
Windows platform is kept for backwards compatibility as some users might
have written custom release scripts assuming this format.

Signed-off-by: Tobias Schmidt <tobidt@gmail.com>
  • Loading branch information
grobie committed Oct 27, 2020
1 parent 83ecadb commit ac7015b
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
52 changes: 52 additions & 0 deletions cmd/tarball.go
Expand Up @@ -15,9 +15,12 @@
package cmd

import (
"archive/zip"
"fmt"
"io"
"os"
"path/filepath"
"strings"

"github.com/pkg/errors"
kingpin "gopkg.in/alecthomas/kingpin.v2"
Expand Down Expand Up @@ -83,4 +86,53 @@ func runTarball(binariesLocation string) {
tar := fmt.Sprintf("%s.tar.gz", name)
fmt.Println(" > ", tar)
sh.RunCommand("tar", "zcf", filepath.Join(prefix, tar), "-C", tmpDir, name)

// Windows systems don't have tar available by default. Produce archives in
// the common zip format additionally.
if goos == "windows" {
archive := name + ".zip"
fmt.Println(" > ", archive)
if err := createZIP(filepath.Join(prefix, archive), dir); err != nil {
fatal(fmt.Errorf("Could not create ZIP archive: %w", err))
}
}
}

// createZIP creates a ZIP archive at the given path containing the specified
// directory.
func createZIP(path, dir string) error {
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()

w := zip.NewWriter(f)
defer w.Close()

prefix := filepath.Dir(dir)
walker := func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}

r, err := os.Open(path)
if err != nil {
return err
}
defer r.Close()

name := strings.TrimLeft(strings.TrimPrefix(path, prefix), "/")
f, err := w.Create(name)
if err != nil {
return err
}

_, err = io.Copy(f, r)
return err
}
return filepath.Walk(dir, walker)
}
1 change: 1 addition & 0 deletions doc/examples/crossbuild/.promu.yml
Expand Up @@ -8,6 +8,7 @@ crossbuild:
platforms:
- linux/amd64
- linux/386
- windows/amd64
tarball:
files:
- README.md
7 changes: 6 additions & 1 deletion doc/examples/crossbuild/Makefile
Expand Up @@ -14,8 +14,13 @@
# The crossbuild command sets the PREFIX env var for each platform
PREFIX ?= $(shell pwd)

# Workaround to mimic promu's build behavior which usually handles the extension.
ifeq ($(GOOS),windows)
EXTENSION = .exe
endif

build:
go build -o $(PREFIX)/crossbuild-example
go build -o $(PREFIX)/crossbuild-example$(EXTENSION)

test:
go test
2 changes: 1 addition & 1 deletion doc/examples/crossbuild/README.md
@@ -1,6 +1,6 @@
Crossbuild example

Run `promu crossbuild` to crossbuild for linux-386 and linux-amd64 platforms.
Run `promu crossbuild` to crossbuild for linux-386, linux-amd64, and windows-amd64 platforms.
Output will be in the `.build` directory.

Run `promu crossbuild tarballs` to build platform tarballs.
Expand Down
3 changes: 3 additions & 0 deletions main_test.go
Expand Up @@ -181,6 +181,7 @@ func TestPromuCrossbuild(t *testing.T) {
errcheck(t, err, string(output))
assertFileExists(t, path.Join(promuExamplesCrossbuild, ".build", "linux-386", "crossbuild-example"))
assertFileExists(t, path.Join(promuExamplesCrossbuild, ".build", "linux-amd64", "crossbuild-example"))
assertFileExists(t, path.Join(promuExamplesCrossbuild, ".build", "windows-amd64", "crossbuild-example.exe"))

cmd = exec.Command(promuBinaryAbsPath, "crossbuild", "tarballs")
cmd.Dir = promuExamplesCrossbuild
Expand All @@ -189,4 +190,6 @@ func TestPromuCrossbuild(t *testing.T) {
errcheck(t, err, string(output))
assertFileExists(t, path.Join(promuExamplesCrossbuild, ".tarballs", "promu-0.1.linux-386.tar.gz"))
assertFileExists(t, path.Join(promuExamplesCrossbuild, ".tarballs", "promu-0.1.linux-amd64.tar.gz"))
assertFileExists(t, path.Join(promuExamplesCrossbuild, ".tarballs", "promu-0.1.windows-amd64.tar.gz"))
assertFileExists(t, path.Join(promuExamplesCrossbuild, ".tarballs", "promu-0.1.windows-amd64.zip"))
}

0 comments on commit ac7015b

Please sign in to comment.