Skip to content

Commit

Permalink
fix(engine-binary): adapt build tag generation (#1125)
Browse files Browse the repository at this point in the history
  • Loading branch information
steebchen authored Dec 11, 2023
1 parent 7ba1672 commit 5f40401
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
13 changes: 7 additions & 6 deletions binaries/bindata/bindata.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (
"os"

"github.com/steebchen/prisma-client-go/binaries"
"github.com/steebchen/prisma-client-go/binaries/platform"
)

// TODO go fmt files after creation

func WriteFile(name, pkg, platform, from, to string) error {
func WriteFile(name, pkg, from, to string, info platform.Info) error {
f, err := os.Create(to)
if err != nil {
return fmt.Errorf("generate open go file: %w", err)
Expand All @@ -20,7 +21,7 @@ func WriteFile(name, pkg, platform, from, to string) error {
//goland:noinspection GoUnhandledErrorResult
defer f.Close()

if err := writeHeader(f, pkg, name, platform); err != nil {
if err := writeHeader(f, pkg, name, info); err != nil {
return fmt.Errorf("write header: %w", err)
}

Expand All @@ -31,10 +32,10 @@ func WriteFile(name, pkg, platform, from, to string) error {
return nil
}

func writeHeader(w io.Writer, pkg, name, platform string) error {
func writeHeader(w io.Writer, pkg string, name string, info platform.Info) error {
_, err := fmt.Fprintf(w, `// Code generated by Prisma Client Go. DO NOT EDIT.
//go:build !codeanalysis && !prisma_ignore && %s
// +build !codeanalysis,!prisma_ignore,%s
//go:build !codeanalysis && !prisma_ignore && %s && %s
// +build !codeanalysis,!prisma_ignore,%s,%s
//nolint
package %s
Expand All @@ -46,7 +47,7 @@ import (
func init() {
unpack.Unpack(data, "%s", "%s")
}
`, platform, platform, pkg, name, binaries.EngineVersion)
`, info.Platform, info.Arch, info.Platform, info.Arch, pkg, name, binaries.EngineVersion)
return err
}

Expand Down
40 changes: 40 additions & 0 deletions binaries/platform/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package platform

import "strings"

type Info struct {
Platform string
Arch string
}

func MapBinaryTarget(name string) Info {
return Info{
Platform: mapBinaryTargetToPlatform(name),
Arch: mapBinaryTargetToArch(name),
}
}

func mapBinaryTargetToPlatform(name string) string {
switch {
case strings.Contains(name, "linux") ||
strings.Contains(name, "debian") ||
strings.Contains(name, "rhel") ||
strings.Contains(name, "musl"):
return "linux"
case strings.Contains(name, "darwin"):
return "darwin"
case strings.Contains(name, "windows"):
return "windows"
default:
return "linux"
}
}

func mapBinaryTargetToArch(name string) string {
switch {
case strings.Contains(name, "arm64"):
return "arm64"
default:
return "!arm64"
}
}
10 changes: 3 additions & 7 deletions generator/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"go/format"
"os"
"path"
"runtime"
"strings"
"text/template"

Expand Down Expand Up @@ -189,15 +188,12 @@ func generateBinaries(input *Root) error {

func generateQueryEngineFiles(binaryTargets []string, pkg, outputDir string) error {
for _, name := range binaryTargets {
pt := runtime.GOOS
if strings.Contains(name, "debian") || strings.Contains(name, "rhel") || strings.Contains(name, "musl") {
pt = "linux"
}

if name == "native" {
name = platform.BinaryPlatformNameStatic()
}

info := platform.MapBinaryTarget(name)

name = TransformBinaryTarget(name)

enginePath := binaries.GetEnginePath(binaries.GlobalCacheDir(), "query-engine", name)
Expand All @@ -206,7 +202,7 @@ func generateQueryEngineFiles(binaryTargets []string, pkg, outputDir string) err
to := path.Join(outputDir, filename)

// TODO check if already exists, but make sure version matches
if err := bindata.WriteFile(name, pkg, pt, enginePath, to); err != nil {
if err := bindata.WriteFile(name, pkg, enginePath, to, info); err != nil {
return fmt.Errorf("generate write go file: %w", err)
}

Expand Down

0 comments on commit 5f40401

Please sign in to comment.