Skip to content

Commit

Permalink
Remove bindata for go:embed
Browse files Browse the repository at this point in the history
  • Loading branch information
aarondl committed Sep 26, 2021
1 parent c6946db commit 213f396
Show file tree
Hide file tree
Showing 16 changed files with 90 additions and 2,786 deletions.
13 changes: 0 additions & 13 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,7 @@ with the Github code review tools. Then it will be merged into dev, and later go
## Developer getting started

1. Add a [Configuration files](https://github.com/volatiletech/sqlboiler#configuration).
1. You may need to install go-bindata first. Refer to this repo. https://github.com/kevinburke/go-bindata

```
# Make sure the version is tag v3.22.0
go get -u github.com/kevinburke/go-bindata/...
```

1. Write your changes
1. If you changed template files, run the following command to re-generate the binddata.

```
./boil.sh go-generate all
```

1. Generate executable. Run again if you have changed anything in core code or driver code.
```
./boil.sh build all
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ Flags:
--struct-tag-casing string Decides the casing for go structure tag names. camel, title, alias or snake (default "snake")
-t, --tag strings Struct tags to be included on your models in addition to json, yaml, toml
--tag-ignore strings List of column names that should have tags values set to '-' (ignored during parsing)
--templates strings A templates directory, overrides the bindata'd template folders in sqlboiler
--templates strings A templates directory, overrides the embedded template folders in sqlboiler
--version Print the version
--wipe Delete the output folder (rm -rf) before generation to ensure sanity
```
Expand Down Expand Up @@ -763,7 +763,7 @@ output_dir/
   └── jssingle.js
```

**Note**: Because the `--templates` flag overrides the internal bindata of `sqlboiler`, if you still
**Note**: Because the `--templates` flag overrides the embedded templates of `sqlboiler`, if you still
wish to generate the default templates it's recommended that you include the path to sqlboiler's templates
as well.

Expand Down
39 changes: 0 additions & 39 deletions boil.sh
Original file line number Diff line number Diff line change
Expand Up @@ -221,42 +221,6 @@ driver_test_db() {
esac
}

# ====================================
# Bindata
# ====================================

go_generate() {
if test -z "${1}" -o "all" = "${1}"; then
set -o xtrace
go generate
set +o xtrace

if test -z "${1}"; then
return
fi
fi

if test "all" = "${1}"; then
run="psql mysql mssql"
else
run="${1}"
fi

cwd="${PWD}"
for i in $run; do
run_go_generate "${cwd}" "${i}"
done
}

run_go_generate() {
baseDir="${1}"
driver="${2}"
set -o xtrace
cd "${baseDir}/drivers/sqlboiler-${driver}/driver"
go generate
set +o xtrace
}

# ====================================
# MSSQL stuff
# ====================================
Expand Down Expand Up @@ -317,8 +281,6 @@ case "${command}" in
driver-test-db) driver_test_db "$1" ;;
driver-test-user) driver_test_user "$1" ;;

go-generate) go_generate "$@" ;;

mssql-run) mssql_run "$@" ;;
mssql-stop) mssql_stop "$@" ;;
mssql-sqlcmd) mssql_sqlcmd "$@" ;;
Expand All @@ -337,7 +299,6 @@ case "${command}" in
echo " driver-test <driver> [args] - runs tests for the driver"
echo " driver-test-db <driver> - create driver db (run before driver-test-user)"
echo " driver-test-user <driver> - creates a user for the driver tests (unprivileged)"
echo " go-generate [all|driver] - runs go generate on packages, omit arg for sqlboiler itself"
echo " mssql-run [attach] - run mssql docker container, if attach is present will not daemonize"
echo " mssql-stop - stop mssql docker container"
echo " mssql-sqlcmd [args...] - run sql query using sqlcmd in docker container"
Expand Down
18 changes: 10 additions & 8 deletions boilingcore/boilingcore.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package boilingcore
import (
"encoding/json"
"fmt"
"io/fs"
"os"
"path/filepath"
"regexp"
Expand All @@ -14,7 +15,6 @@ import (
"github.com/friendsofgo/errors"
"github.com/volatiletech/sqlboiler/v4/drivers"
"github.com/volatiletech/sqlboiler/v4/importers"
"github.com/volatiletech/sqlboiler/v4/templatebin"
"github.com/volatiletech/strmangle"
)

Expand Down Expand Up @@ -49,7 +49,7 @@ type State struct {
}

// New creates a new state based off of the config
func New(config *Config) (*State, error) {
func New(config *Config, templatesBuiltin fs.FS) (*State, error) {
s := &State{
Config: config,
}
Expand Down Expand Up @@ -108,7 +108,7 @@ func New(config *Config) (*State, error) {
return nil, err
}

templates, err = s.initTemplates()
templates, err = s.initTemplates(templatesBuiltin)
if err != nil {
return nil, errors.Wrap(err, "unable to initialize templates")
}
Expand Down Expand Up @@ -225,7 +225,7 @@ func (s *State) Cleanup() error {
//
// Later, in order to properly look up imports the paths will
// be forced back to linux style paths.
func (s *State) initTemplates() ([]lazyTemplate, error) {
func (s *State) initTemplates(templatesBuiltin fs.FS) ([]lazyTemplate, error) {
var err error

templates := make(map[string]templateLoader)
Expand All @@ -246,10 +246,12 @@ func (s *State) initTemplates() ([]lazyTemplate, error) {
mergeTemplates(templates, tpls)
}
} else {
for _, a := range templatebin.AssetNames() {
if strings.HasSuffix(a, ".tpl") {
templates[normalizeSlashes(a)] = assetLoader(a)
}
assets, err := fs.Glob(templatesBuiltin, "*/*.tpl")
if err != nil {
return nil, err
}
for _, a := range assets {
templates[normalizeSlashes(a)] = assetLoader{fs: templatesBuiltin, name: a}
}
}

Expand Down
2 changes: 1 addition & 1 deletion boilingcore/boilingcore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func testNew(t *testing.T, aliases Aliases) {
Aliases: aliases,
}

state, err = New(config)
state, err = New(config, nil)
if err != nil {
t.Fatalf("Unable to create State using config: %s", err)
}
Expand Down
17 changes: 8 additions & 9 deletions boilingcore/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding"
"encoding/base64"
"fmt"
"io/fs"
"io/ioutil"
"path/filepath"
"sort"
Expand All @@ -13,7 +14,6 @@ import (

"github.com/friendsofgo/errors"
"github.com/volatiletech/sqlboiler/v4/drivers"
"github.com/volatiletech/sqlboiler/v4/templatebin"
"github.com/volatiletech/strmangle"
)

Expand Down Expand Up @@ -102,11 +102,7 @@ func (t templateNameList) Less(k, j int) bool {
}

res := strings.Compare(t[k], t[j])
if res <= 0 {
return true
}

return false
return res <= 0
}

// Templates returns the name of all the templates defined in the template list
Expand Down Expand Up @@ -205,18 +201,21 @@ func (b base64Loader) String() string {
return fmt.Sprintf("base64:(sha256 of content): %x", sha)
}

type assetLoader string
type assetLoader struct {
fs fs.FS
name string
}

func (a assetLoader) Load() ([]byte, error) {
return templatebin.Asset(string(a))
return fs.ReadFile(a.fs, string(a.name))
}

func (a assetLoader) MarshalText() ([]byte, error) {
return []byte(a.String()), nil
}

func (a assetLoader) String() string {
return "asset:" + string(a)
return "asset:" + string(a.name)
}

// set is to stop duplication from named enums, allowing a template loop
Expand Down
374 changes: 0 additions & 374 deletions drivers/sqlboiler-mssql/driver/bindata.go

This file was deleted.

27 changes: 19 additions & 8 deletions drivers/sqlboiler-mssql/driver/mssql.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package driver

import (
"database/sql"
"embed"
"encoding/base64"
"fmt"
"io/fs"
"net/url"
"strings"

Expand All @@ -15,12 +17,13 @@ import (
"github.com/volatiletech/strmangle"
)

//go:embed override
var templates embed.FS

func init() {
drivers.RegisterFromInit("mssql", &MSSQLDriver{})
}

//go:generate go-bindata -nometadata -pkg driver -prefix override override/...

// Assemble is more useful for calling into the library so you don't
// have to instantiate an empty type.
func Assemble(config drivers.Config) (dbinfo *drivers.DBInfo, err error) {
Expand All @@ -37,16 +40,24 @@ type MSSQLDriver struct {

// Templates that should be added/overridden
func (MSSQLDriver) Templates() (map[string]string, error) {
names := AssetNames()
tpls := make(map[string]string)
for _, n := range names {
b, err := Asset(n)
fs.WalkDir(templates, "override", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return nil, err
return err
}

tpls[n] = base64.StdEncoding.EncodeToString(b)
}
if d.IsDir() {
return nil
}

b, err := fs.ReadFile(templates, path)
if err != nil {
return err
}
tpls[strings.Replace(path, "override/", "", 1)] = base64.StdEncoding.EncodeToString(b)

return nil
})

return tpls, nil
}
Expand Down
Loading

0 comments on commit 213f396

Please sign in to comment.