@@ -17,8 +17,11 @@ package publishcmd
1717import (
1818 "bytes"
1919 "context"
20+ "crypto/sha256"
21+ "encoding/hex"
2022 "flag"
2123 "fmt"
24+ "io"
2225 "os"
2326 "path/filepath"
2427 "strings"
@@ -187,6 +190,8 @@ type HomebrewCaskSettings struct {
187190 TemplateFilename string `mapstructure:"template_filename"`
188191 Description string `mapstructure:"description"`
189192 Homepage string `mapstructure:"homepage"`
193+ BinaryName string `mapstructure:"binary_name"`
194+ BinaryLocation string `mapstructure:"binary_location"`
190195}
191196
192197// HomebrewCaskContext holds data for the Homebrew cask template.
@@ -200,6 +205,9 @@ type HomebrewCaskContext struct {
200205 Homepage string
201206 PkgFilename string
202207 BundleIdentifier string
208+
209+ // TODO(bep) check how goreleaser does this.
210+ BinaryPath string // Full path to binary, e.g. /usr/local/bin/hugoreleaser
203211}
204212
205213func (p * Publisher ) updateHomebrewCask (
@@ -231,6 +239,12 @@ func (p *Publisher) updateHomebrewCask(
231239 if settings .CaskPath == "" {
232240 settings .CaskPath = fmt .Sprintf ("Casks/%s.rb" , settings .Name )
233241 }
242+ if settings .BinaryName == "" {
243+ settings .BinaryName = p .core .Config .BuildSettings .Binary
244+ }
245+ if settings .BinaryLocation == "" {
246+ settings .BinaryLocation = "/usr/local/bin"
247+ }
234248
235249 // Find the first .pkg archive matching the archive paths pattern.
236250 pkgInfo , err := p .findPkgArchive (release , pub .ArchivePathsCompiled )
@@ -249,6 +263,12 @@ func (p *Publisher) updateHomebrewCask(
249263 pkgInfo .Name ,
250264 )
251265
266+ // Build binary path if binary_name is configured.
267+ var binaryPath string
268+ if settings .BinaryName != "" {
269+ binaryPath = filepath .Join (settings .BinaryLocation , settings .BinaryName )
270+ }
271+
252272 // Build cask context.
253273 caskCtx := HomebrewCaskContext {
254274 Name : settings .Name ,
@@ -260,6 +280,7 @@ func (p *Publisher) updateHomebrewCask(
260280 Homepage : settings .Homepage ,
261281 PkgFilename : pkgInfo .Name ,
262282 BundleIdentifier : settings .BundleIdentifier ,
283+ BinaryPath : binaryPath ,
263284 }
264285
265286 // Render cask template.
@@ -337,12 +358,42 @@ func (p *Publisher) findPkgArchive(release *config.Release, archivePathsMatcher
337358
338359 // Check if it's a .pkg file.
339360 if strings .HasSuffix (archPath .Name , ".pkg" ) {
361+ // Calculate SHA256 from the actual file.
362+ archiveFile := filepath .Join (
363+ p .core .DistDir ,
364+ p .core .Config .Project ,
365+ p .core .Tag ,
366+ p .core .DistRootArchives ,
367+ filepath .FromSlash (archPath .Path ),
368+ archPath .Name ,
369+ )
370+
371+ checksum , err := calculateSHA256 (archiveFile )
372+ if err != nil {
373+ return pkgArchiveInfo {}, fmt .Errorf ("failed to calculate SHA256 for %s: %w" , archPath .Name , err )
374+ }
375+
340376 return pkgArchiveInfo {
341377 Name : archPath .Name ,
342- SHA256 : archPath . SHA256 ,
378+ SHA256 : checksum ,
343379 }, nil
344380 }
345381 }
346382
347383 return pkgArchiveInfo {}, fmt .Errorf ("no .pkg archive found for darwin" )
348384}
385+
386+ // calculateSHA256 calculates the SHA256 checksum of a file.
387+ func calculateSHA256 (filename string ) (string , error ) {
388+ f , err := os .Open (filename )
389+ if err != nil {
390+ return "" , err
391+ }
392+ defer f .Close ()
393+
394+ h := sha256 .New ()
395+ if _ , err := io .Copy (h , f ); err != nil {
396+ return "" , err
397+ }
398+ return hex .EncodeToString (h .Sum (nil )), nil
399+ }
0 commit comments