-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: allow publishing to Brew via custom script
- Loading branch information
1 parent
0d76b3c
commit 07762c4
Showing
4 changed files
with
204 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"io" | ||
"log" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"text/template" | ||
) | ||
|
||
const ReleaseURL = "https://github.com/open-component-model/ocm/releases/download" | ||
|
||
const ClassName = "Ocm" | ||
|
||
func main() { | ||
version := flag.String("version", "", "version of the OCM formula") | ||
templateFile := flag.String("template", "ocm_formula_template.rb.tpl", "path to the template file") | ||
outputDir := flag.String("outputDirectory", ".", "path to the output directory") | ||
|
||
flag.Parse() | ||
|
||
if *version == "" { | ||
log.Fatalf("version is required") | ||
} | ||
|
||
architectures := []string{"amd64", "arm64"} | ||
oses := []string{"darwin", "linux"} | ||
values := map[string]string{ | ||
"Version": *version, | ||
} | ||
|
||
for _, targetOs := range oses { | ||
for _, arch := range architectures { | ||
url := fmt.Sprintf( | ||
"%s/v%[2]s/ocm-%[2]s-%s-%s.tar.gz.sha256", ReleaseURL, *version, targetOs, arch) | ||
rawDigest, err := http.Get(url) | ||
if err != nil { | ||
log.Fatalf("failed to get digest for %s/%s: %v", targetOs, arch, err) | ||
} | ||
digestBytes, err := io.ReadAll(rawDigest.Body) | ||
if err != nil { | ||
log.Fatalf("failed to read digest for %s/%s: %v", targetOs, arch, err) | ||
} | ||
if err := rawDigest.Body.Close(); err != nil { | ||
log.Fatalf("failed to close response body for %s/%s: %v", targetOs, arch, err) | ||
} | ||
digest := strings.TrimSpace(string(digestBytes)) | ||
|
||
values[fmt.Sprintf("%s_%s_sha256", targetOs, arch)] = digest | ||
} | ||
} | ||
|
||
// Parse and execute the template | ||
tmpl, err := template.New(filepath.Base(*templateFile)).Funcs(template.FuncMap{ | ||
"classname": func() string { | ||
return fmt.Sprintf("%sAT%s", ClassName, strings.ReplaceAll(*version, ".", "")) | ||
}, | ||
}).ParseFiles(*templateFile) | ||
if err != nil { | ||
log.Fatalf("failed to parse template: %v", err) | ||
} | ||
|
||
outputFile := fmt.Sprintf("ocm@%s.rb", *version) | ||
|
||
fi, err := os.Stat(*outputDir) | ||
if os.IsNotExist(err) { | ||
if err := os.MkdirAll(*outputDir, 0755); err != nil { | ||
log.Fatalf("failed to create output directory: %v", err) | ||
} | ||
} else if err != nil { | ||
log.Fatalf("failed to stat output directory: %v", err) | ||
} else if !fi.IsDir() { | ||
log.Fatalf("output directory is not a directory") | ||
} | ||
|
||
versionedFormula, err := os.Create(fmt.Sprintf(filepath.Join(*outputDir, outputFile))) | ||
if err != nil { | ||
log.Fatalf("failed to parse template: %v", err) | ||
} | ||
defer func() { | ||
if err := versionedFormula.Close(); err != nil { | ||
log.Fatalf("failed to close file: %v", err) | ||
} | ||
}() | ||
|
||
if err := tmpl.Execute(versionedFormula, values); err != nil { | ||
log.Fatalf("failed to execute template: %v", err) | ||
} | ||
|
||
println(versionedFormula.Name()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
{{- /* Go template for Homebrew Formula */ -}} | ||
# typed: false | ||
# frozen_string_literal: true | ||
|
||
class {{ classname }} < Formula | ||
desc "The OCM CLI makes it easy to create component versions and embed them in build processes." | ||
homepage "https://ocm.software/" | ||
version "{{ .Version }}" | ||
|
||
on_macos do | ||
on_intel do | ||
url "{{ .ReleaseURL }}/v{{ .Version }}/ocm-{{ .Version }}-darwin-amd64.tar.gz" | ||
sha256 "{{ .darwin_amd64_sha256 }}" | ||
|
||
def install | ||
bin.install "ocm" | ||
end | ||
end | ||
on_arm do | ||
url "{{ .ReleaseURL }}/v{{ .Version }}/ocm-{{ .Version }}-darwin-arm64.tar.gz" | ||
sha256 "{{ .darwin_arm64_sha256 }}" | ||
|
||
def install | ||
bin.install "ocm" | ||
end | ||
end | ||
end | ||
|
||
on_linux do | ||
on_intel do | ||
if Hardware::CPU.is_64_bit? | ||
url "{{ .ReleaseURL }}/v{{ .Version }}/ocm-{{ .Version }}-linux-amd64.tar.gz" | ||
sha256 "{{ .linux_amd64_sha256 }}" | ||
|
||
def install | ||
bin.install "ocm" | ||
end | ||
end | ||
end | ||
on_arm do | ||
if Hardware::CPU.is_64_bit? | ||
url "{{ .ReleaseURL }}/v{{ .Version }}/ocm-{{ .Version }}-linux-arm64.tar.gz" | ||
sha256 "{{ .linux_arm64_sha256 }}" | ||
|
||
def install | ||
bin.install "ocm" | ||
end | ||
end | ||
end | ||
end | ||
|
||
test do | ||
system "#{bin}/ocm --version" | ||
end | ||
end |