From 94cc4c723a2feb64d6a7da6383ff37a63dba4c70 Mon Sep 17 00:00:00 2001 From: develar Date: Fri, 19 Jul 2019 09:41:32 +0200 Subject: [PATCH] fix(dmg): cannot build DMG on macOS 10.15 --- app-builder-bin/package.json | 2 +- go.mod | 4 +- go.sum | 8 +-- main.go | 2 +- pkg/node-modules/tree.go | 2 +- pkg/package-format/dmg/dmg.go | 87 ++++++++++++++++++++++++++---- pkg/package-format/dmg/dmg_test.go | 19 +++++++ 7 files changed, 105 insertions(+), 19 deletions(-) create mode 100644 pkg/package-format/dmg/dmg_test.go diff --git a/app-builder-bin/package.json b/app-builder-bin/package.json index d0fab1a..b236ddf 100644 --- a/app-builder-bin/package.json +++ b/app-builder-bin/package.json @@ -1,7 +1,7 @@ { "name": "app-builder-bin", "description": "app-builder precompiled binaries", - "version": "3.4.0", + "version": "3.4.1", "files": [ "*.js", "mac", diff --git a/go.mod b/go.mod index b2aee4f..27086f1 100644 --- a/go.mod +++ b/go.mod @@ -3,10 +3,10 @@ module github.com/develar/app-builder require ( github.com/aclements/go-rabin v0.0.0-20170911142644-d0b643ea1a4c github.com/alecthomas/kingpin v2.2.6+incompatible - github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc // indirect + github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4 // indirect github.com/alessio/shellescape v0.0.0-20190409004728-b115ca0f9053 // indirect - github.com/aws/aws-sdk-go v1.20.20 + github.com/aws/aws-sdk-go v1.21.0 github.com/biessek/golang-ico v0.0.0-20180326222316-d348d9ea4670 github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect github.com/davecgh/go-spew v1.1.1 // indirect diff --git a/go.sum b/go.sum index 68a65ee..50c3722 100644 --- a/go.sum +++ b/go.sum @@ -2,14 +2,14 @@ github.com/aclements/go-rabin v0.0.0-20170911142644-d0b643ea1a4c h1:YOZwrMKo75ZY github.com/aclements/go-rabin v0.0.0-20170911142644-d0b643ea1a4c/go.mod h1:x5RmfBtNWHpxyhZledMnt/vFb6z5y+fadAiinzuLYpo= github.com/alecthomas/kingpin v2.2.6+incompatible h1:5svnBTFgJjZvGKyYBtMB0+m5wvrbUHiqye8wRJMlnYI= github.com/alecthomas/kingpin v2.2.6+incompatible/go.mod h1:59OFYbFVLKQKq+mqrL6Rw5bR0c3ACQaawgXx0QYndlE= -github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc h1:cAKDfWh5VpdgMhJosfJnn5/FoN2SRZ4p7fJNX58YPaU= -github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= +github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM= +github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4 h1:Hs82Z41s6SdL1CELW+XaDYmOH4hkBN4/N9og/AsOv7E= github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/alessio/shellescape v0.0.0-20190409004728-b115ca0f9053 h1:H/GMMKYPkEIC3DF/JWQz8Pdd+Feifov2EIgGfNpeogI= github.com/alessio/shellescape v0.0.0-20190409004728-b115ca0f9053/go.mod h1:xW8sBma2LE3QxFSzCnH9qe6gAE2yO9GvQaWwX89HxbE= -github.com/aws/aws-sdk-go v1.20.20 h1:OAR/GtjMOhenkp1NNKr1N1FgIP3mQXHeGbRhvVIAQp0= -github.com/aws/aws-sdk-go v1.20.20/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo= +github.com/aws/aws-sdk-go v1.21.0 h1:dRGzi4XZe5GFSJssHkRNUf/hRD/HL8bdqTYa9hpAO8c= +github.com/aws/aws-sdk-go v1.21.0/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo= github.com/biessek/golang-ico v0.0.0-20180326222316-d348d9ea4670 h1:FQPKKjDhzG0T4ew6dm6MGrXb4PRAi8ZmTuYuxcF62BM= github.com/biessek/golang-ico v0.0.0-20180326222316-d348d9ea4670/go.mod h1:iRWAFbKXMMkVQyxZ1PfGlkBr1TjATx1zy2MRprV7A3Q= github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4YnC6+E63dPcxHo2sUxDIu8g3QgEJdRY= diff --git a/main.go b/main.go index d49f3b5..eebaf1e 100644 --- a/main.go +++ b/main.go @@ -47,7 +47,7 @@ func main() { return } - var app = kingpin.New("app-builder", "app-builder").Version("3.4.0") + var app = kingpin.New("app-builder", "app-builder").Version("3.4.1") node_modules.ConfigureCommand(app) node_modules.ConfigureRebuildCommand(app) diff --git a/pkg/node-modules/tree.go b/pkg/node-modules/tree.go index 6b75ab6..f731e01 100644 --- a/pkg/node-modules/tree.go +++ b/pkg/node-modules/tree.go @@ -43,7 +43,7 @@ func ConfigureCommand(app *kingpin.Application) { return err } - jsonWriter := jsoniter.NewStream(jsoniter.ConfigDefault, os.Stdout, 32*1024) + jsonWriter := jsoniter.NewStream(jsoniter.ConfigFastest, os.Stdout, 32*1024) writeResult(jsonWriter, collector) err = jsonWriter.Flush() if err != nil { diff --git a/pkg/package-format/dmg/dmg.go b/pkg/package-format/dmg/dmg.go index 0170250..4c25a16 100644 --- a/pkg/package-format/dmg/dmg.go +++ b/pkg/package-format/dmg/dmg.go @@ -3,10 +3,12 @@ package dmg import ( + "bytes" "os" "os/exec" "path/filepath" "regexp" + "strconv" "strings" "github.com/alecthomas/kingpin" @@ -14,6 +16,7 @@ import ( "github.com/develar/app-builder/pkg/log" "github.com/develar/app-builder/pkg/util" "github.com/develar/errors" + "github.com/json-iterator/go" "github.com/pkg/xattr" "go.uber.org/zap" ) @@ -26,47 +29,111 @@ func ConfigureCommand(app *kingpin.Application) { background := command.Flag("background", "").String() command.Action(func(context *kingpin.ParseContext) error { - err := BuildDmg(*volumePath, *icon, *background) + backgroundFileInImage, err := BuildDmg(*volumePath, *icon, *background) if err != nil { - return errors.WithStack(err) + return err + } + + jsonWriter := jsoniter.NewStream(jsoniter.ConfigFastest, os.Stdout, 32*1024) + jsonWriter.WriteObjectStart() + + if *background != "" { + pixelWidth, pixelHeight, err := getImageSizeUsingSips(*background) + if err != nil { + return err + } + + jsonWriter.WriteObjectField("backgroundWidth") + jsonWriter.WriteInt(pixelWidth) + jsonWriter.WriteMore() + jsonWriter.WriteObjectField("backgroundHeight") + jsonWriter.WriteInt(pixelHeight) + + jsonWriter.WriteMore() + jsonWriter.WriteObjectField("backgroundFile") + jsonWriter.WriteString(backgroundFileInImage) + } + + jsonWriter.WriteObjectEnd() + err = jsonWriter.Flush() + if err != nil { + return err } return nil }) } -func BuildDmg(volumePath string, icon string, backgroundPath string) error { +func getImageSizeUsingSips(background string) (int, int, error) { + command := exec.Command("sips", "-g", "pixelHeight", "-g", "pixelWidth", background) + result, err := util.Execute(command) + if err != nil { + return 0, 0, err + } + + pixelWidth := 0 + pixelHeight := 0 + re := regexp.MustCompile(`([a-zA-Z]+):\s*(\d+)`) + lines := bytes.Split(result, []byte("\n")) + for _, value := range lines { + if len(value) == 0 { + continue + } + + nameAndValue := re.FindStringSubmatch(string(value)) + if nameAndValue == nil { + continue + } + + size, err := strconv.Atoi(nameAndValue[2]) + if err != nil { + return 0, 0, errors.WithStack(err) + } + + switch nameAndValue[1] { + case "pixelWidth": + pixelWidth = size + case "pixelHeight": + pixelHeight = size + } + } + return pixelWidth, pixelHeight, nil +} + +func BuildDmg(volumePath string, icon string, backgroundPath string) (string, error) { if icon != "" { // cannot use hard link because volume uses different disk iconPath := filepath.Join(volumePath, ".VolumeIcon.icns") err := fs.CopyDirOrFile(icon, iconPath) if err != nil { - return errors.WithStack(err) + return "", errors.WithStack(err) } err = setHasCustomIconAttribute(volumePath) if err != nil { - return errors.WithStack(err) + return "", errors.WithStack(err) } err = setIsInvisibleAttribute(iconPath) if err != nil { - return errors.WithStack(err) + return "", errors.WithStack(err) } } + backgroundFileInImage := "" if backgroundPath != "" { backgroundPath, err := GetEffectiveBackgroundPath(backgroundPath) if err != nil { - return errors.WithStack(err) + return "", err } - err = fs.CopyDirOrFile(backgroundPath, filepath.Join(volumePath, ".background", filepath.Base(backgroundPath))) + backgroundFileInImage = filepath.Join(volumePath, ".background", filepath.Base(backgroundPath)) + err = fs.CopyDirOrFile(backgroundPath, backgroundFileInImage) if err != nil { - return errors.WithStack(err) + return "", errors.WithStack(err) } } - return nil + return backgroundFileInImage, nil } func GetEffectiveBackgroundPath(path string) (string, error) { diff --git a/pkg/package-format/dmg/dmg_test.go b/pkg/package-format/dmg/dmg_test.go new file mode 100644 index 0000000..10b6e1b --- /dev/null +++ b/pkg/package-format/dmg/dmg_test.go @@ -0,0 +1,19 @@ +package dmg + +import ( + "testing" + + "github.com/develar/app-builder/pkg/log" + . "github.com/onsi/gomega" +) + +func TestSize(t *testing.T) { + g := NewGomegaWithT(t) + + log.InitLogger() + + w, h, err := getImageSizeUsingSips("/Volumes/data/Desktop/test.png") + g.Expect(err).To(BeNil()) + g.Expect(w).To(Equal(1316)) + g.Expect(h).To(Equal(894)) +}