89 lines
2.3 KiB
Go
89 lines
2.3 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"os"
|
||
|
"os/exec"
|
||
|
"regexp"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
versionRe = regexp.MustCompile(`version = "[^"]+"`)
|
||
|
sha256Re = regexp.MustCompile(`sha256 = (?:"[^"]+"|lib.fakeSha256)`)
|
||
|
vendorSha256Re = regexp.MustCompile(`vendorSha256 = (?:"[^"]+"|lib.fakeSha256)`)
|
||
|
gotRe = regexp.MustCompile(`got:\s+(.+)`)
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
if err := mainErr(); err != nil {
|
||
|
fmt.Fprintln(os.Stderr, err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func mainErr() error {
|
||
|
if len(os.Args) < 3 {
|
||
|
return errors.New("hash <pkg> <version>")
|
||
|
}
|
||
|
|
||
|
pkg, version := os.Args[1], os.Args[2]
|
||
|
pkgFile := fmt.Sprintf("pkgs/%s/default.nix", pkg)
|
||
|
|
||
|
content, err := os.ReadFile(pkgFile)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// Set version and reset hashes
|
||
|
content = versionRe.ReplaceAll(content, []byte(fmt.Sprintf(`version = "%s"`, version)))
|
||
|
content = sha256Re.ReplaceAll(content, []byte("sha256 = lib.fakeSha256"))
|
||
|
content = vendorSha256Re.ReplaceAll(content, []byte("vendorSha256 = lib.fakeSha256"))
|
||
|
if err := os.WriteFile(pkgFile, content, os.ModePerm); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// Get sha256
|
||
|
out, _ := run(pkg)
|
||
|
match := gotRe.FindSubmatch(out)
|
||
|
if match == nil {
|
||
|
return errors.New("could not find expected sha256")
|
||
|
}
|
||
|
sha256 := match[1]
|
||
|
sha256Repl := fmt.Sprintf(`sha256 = "%s"`, sha256)
|
||
|
fmt.Printf("\n\n-----\n%s\n-----\n\n", sha256Repl)
|
||
|
content = sha256Re.ReplaceAll(content, []byte(sha256Repl))
|
||
|
if err := os.WriteFile(pkgFile, content, os.ModePerm); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// Get vendorSha256
|
||
|
out, _ = run(pkg)
|
||
|
match = gotRe.FindSubmatch(out)
|
||
|
if match == nil {
|
||
|
return errors.New("could not find expected vendorSha256")
|
||
|
}
|
||
|
vendorSha256 := match[1]
|
||
|
vendorSha256Repl := fmt.Sprintf(`vendorSha256 = "%s"`, vendorSha256)
|
||
|
fmt.Printf("\n\n-----\n%s\n-----\n\n", vendorSha256Repl)
|
||
|
content = vendorSha256Re.ReplaceAll(content, []byte(vendorSha256Repl))
|
||
|
if err := os.WriteFile(pkgFile, content, os.ModePerm); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// Make sure it builds
|
||
|
_, err = run(pkg)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
func run(pkg string) ([]byte, error) {
|
||
|
var buf bytes.Buffer
|
||
|
w := io.MultiWriter(&buf, os.Stdout)
|
||
|
cmd := exec.Command("nix-build", "-E", fmt.Sprintf("with import <nixpkgs> { }; callPackage ./pkgs/%s { }", pkg))
|
||
|
cmd.Stdout = w
|
||
|
cmd.Stderr = w
|
||
|
err := cmd.Run()
|
||
|
return buf.Bytes(), err
|
||
|
}
|