Skip to content

Commit

Permalink
Embed data files in the binary (Khan#180)
Browse files Browse the repository at this point in the history
Now that we're on Go 1.16+ we can do this easily! The main advantage is
it means users can build a genqlient binary and use that portably (or we
could distribute one, or whatever). Plus the code is marginally simpler;
the `embed` API is really quite nice.

Fixes Khan#9.

Test plan:
```
make check
go build .
rm -rf generate               # pretend we have no checkout
./genqlient ./internal/integration/genqlient.yaml
./genqlient --init            # fails after generating a default config
```
  • Loading branch information
benjaminjkraft authored Mar 22, 2022
1 parent 13094c3 commit 36e86cf
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 29 deletions.
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

0 comments on commit 36e86cf

Please sign in to comment.