Skip to content

Commit 28622fd

Browse files
committed
Misc fixes and improvements to the publish command
1 parent 95f6d43 commit 28622fd

File tree

5 files changed

+63
-2
lines changed

5 files changed

+63
-2
lines changed

cmd/publishcmd/publish.go

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ package publishcmd
1717
import (
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

205213
func (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+
}

hugoreleaser.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Next release
2-
HUGORELEASER_TAG=v0.61.0
2+
HUGORELEASER_TAG=v0.61.1
33
HUGORELEASER_COMMITISH=main

hugoreleaser.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,5 @@ publishers:
142142
format: homebrew_cask
143143
custom_settings:
144144
bundle_identifier: io.gohugo.hugoreleaser
145+
homepage: "https://github.com/gohugoio/hugoreleaser"
146+
description: "Build, archive and release Go programs."

staticfiles/templates/homebrew-cask.rb.gotmpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ cask "{{ .Name }}" do
88
homepage "{{ .Homepage }}"
99

1010
pkg "{{ .PkgFilename }}"
11+
{{- if .BinaryPath }}
12+
13+
binary "{{ .BinaryPath }}"
14+
{{- end }}
1115

1216
uninstall pkgutil: "{{ .BundleIdentifier }}"
1317
end

testscripts/commands/publish.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ publishers:
5858
-- dist/hugo/v1.2.0/releases/myrelease/hugo_1.2.0_checksums.txt --
5959
abc123def456 hugo_1.2.0_darwin-universal.pkg
6060

61+
# The actual pkg archive (needed for SHA256 calculation)
62+
-- dist/hugo/v1.2.0/archives/mac/darwin/universal/hugo_1.2.0_darwin-universal.pkg --
63+
dummy pkg content for testing
64+
6165
-- go.mod --
6266
module foo
6367
-- main.go --

0 commit comments

Comments
 (0)