Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embed data files in the binary #180

Merged
merged 2 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ When releasing a new version:

### New features:

- genqlient can now run as a portable binary (i.e. without a local checkout of the repository or `go run`).

### Bug fixes:

## v0.4.0
Expand Down
21 changes: 5 additions & 16 deletions generate/config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package generate

import (
_ "embed"
"go/token"
"io"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -123,22 +123,11 @@ func ReadAndValidateConfigFromDefaultLocations() (*Config, error) {
return ReadAndValidateConfig(cfgFile)
}

//go:embed default_genqlient.yaml
var defaultConfig []byte

func initConfig(filename string) error {
// TODO(benkraft): Embed this config file into the binary, see
// https://github.com/Khan/genqlient/issues/9.
r, err := os.Open(filepath.Join(thisDir, "default_genqlient.yaml"))
if err != nil {
return err
}
w, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o644)
if err != nil {
return errorf(nil, "unable to write default genqlient.yaml: %v", err)
}
_, err = io.Copy(w, r)
if err != nil {
return errorf(nil, "unable to write default genqlient.yaml: %v", err)
}
return nil
return os.WriteFile(filename, defaultConfig, 0o644)
}

// findCfg searches for the config file in this directory and all parents up the tree
Expand Down
4 changes: 2 additions & 2 deletions generate/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func TestGenerate(t *testing.T) {
}
}

func defaultConfig(t *testing.T) *Config {
func getDefaultConfig(t *testing.T) *Config {
// Parse the config that `genqlient --init` generates, to make sure that
// works.
var config Config
Expand All @@ -151,7 +151,7 @@ func TestGenerateWithConfig(t *testing.T) {
baseDir string // relative to dataDir
config *Config // omits Schema and Operations, set below.
}{
{"DefaultConfig", "", defaultConfig(t)},
{"DefaultConfig", "", getDefaultConfig(t)},
{"Subpackage", "", &Config{
Generated: "mypkg/myfile.go",
}},
Expand Down
16 changes: 5 additions & 11 deletions generate/template.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
package generate

import (
"embed"
"io"
"path/filepath"
"runtime"
"strings"
"text/template"
)

var (
// TODO(benkraft): Embed templates into the binary, see
// https://github.com/Khan/genqlient/issues/9.
_, thisFilename, _, _ = runtime.Caller(0)
thisDir = filepath.Dir(thisFilename)
)
//go:embed *.tmpl
var templates embed.FS

func repeat(n int, s string) string {
var builder strings.Builder
Expand All @@ -37,17 +32,16 @@ func sub(x, y int) int { return x - y }
func (g *generator) render(tmplRelFilename string, w io.Writer, data interface{}) error {
tmpl := g.templateCache[tmplRelFilename]
if tmpl == nil {
absFilename := filepath.Join(thisDir, tmplRelFilename)
funcMap := template.FuncMap{
"ref": g.ref,
"repeat": repeat,
"intRange": intRange,
"sub": sub,
}
var err error
tmpl, err = template.New(tmplRelFilename).Funcs(funcMap).ParseFiles(absFilename)
tmpl, err = template.New(tmplRelFilename).Funcs(funcMap).ParseFS(templates, tmplRelFilename)
if err != nil {
return errorf(nil, "could not load template %v: %v", absFilename, err)
return errorf(nil, "could not load template %v: %v", tmplRelFilename, err)
}
g.templateCache[tmplRelFilename] = tmpl
}
Expand Down