Skip to content

Commit

Permalink
Merge pull request #74 from cloudspannerecosystem/use-go-embed
Browse files Browse the repository at this point in the history
use go1.16 for go:embed instead of go-assets-builder
  • Loading branch information
kazegusuri authored Jul 23, 2021
2 parents 9390163 + 7e3245b commit 20fce06
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 82 deletions.
8 changes: 1 addition & 7 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ jobs:
v2test:
docker:
- image: golang:1.15-buster
- image: golang:1.16-buster
environment:
SPANNER_EMULATOR_HOST: localhost:9010
SPANNER_EMULATOR_HOST_REST: localhost:9020
Expand All @@ -76,12 +76,6 @@ jobs:
command: |
make -C v2 build
- run:
name: regenerate templates and check diff
command: |
make -C v2 regen
git diff --quiet v2/module/builtin/tplbin/
- run:
name: regenerate template files and check diff
command: |
Expand Down
14 changes: 2 additions & 12 deletions v2/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,11 @@ help: ## show this help message.

all: build

build: regen ## build yo command and regenerate template bin
build: ## build yo command and regenerate template bin
go build

regen: module/builtin/tplbin/templates.go ## regenerate template bin

deps:
go get -u github.com/jessevdk/go-assets-builder

module/builtin/tplbin/templates.go: $(wildcard module/builtin/templates/*.tpl)
mkdir -p module/builtin/tplbin
go-assets-builder \
--package=tplbin \
--strip-prefix="/module/builtin/templates/" \
--output module/builtin/tplbin/templates.go \
module/builtin/templates/*.tpl
go mod download

.PHONY: test
test: ## run test
Expand Down
16 changes: 11 additions & 5 deletions v2/generator/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@
package generator

import (
"io"
"fmt"
"os"
"path/filepath"

"go.mercari.io/yo/v2/module/builtin/tplbin"
"go.mercari.io/yo/v2/module/builtin"
)

// CopyDefaultTemplates copies default templete files to dir.
func CopyDefaultTemplates(dir string) error {
for _, tf := range tplbin.Assets.Files {
for _, m := range builtin.All {
if err := func() (err error) {
file, err := os.OpenFile(filepath.Join(dir, tf.Name()), os.O_RDWR|os.O_CREATE, 0666)
filename := fmt.Sprintf("%s.go.tpl", m.Name())
file, err := os.OpenFile(filepath.Join(dir, filename), os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
return err
}
Expand All @@ -41,7 +42,12 @@ func CopyDefaultTemplates(dir string) error {
}
}()

_, err = io.Copy(file, tf)
b, err := m.Load()
if err != nil {
return fmt.Errorf("failed to load builtin module %q: %v", m.Name(), err)
}

_, err = file.Write(b)
return
}(); err != nil {
return err
Expand Down
3 changes: 1 addition & 2 deletions v2/go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
module go.mercari.io/yo/v2

go 1.15
go 1.16

require (
cloud.google.com/go v0.81.0
cloud.google.com/go/spanner v1.18.0
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/go-cmp v0.5.5
github.com/jessevdk/go-assets v0.0.0-20160921144138-4f4301a06e15
github.com/jinzhu/inflection v1.0.0
github.com/kenshaw/snaker v0.1.0
github.com/spf13/cobra v1.1.1
Expand Down
2 changes: 0 additions & 2 deletions v2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,6 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jessevdk/go-assets v0.0.0-20160921144138-4f4301a06e15 h1:cW/amwGEJK5MSKntPXRjX4dxs/nGxGT8gXKIsKFmHGc=
github.com/jessevdk/go-assets v0.0.0-20160921144138-4f4301a06e15/go.mod h1:Fdm/oWRW+CH8PRbLntksCNtmcCBximKPkVQYvmMl80k=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
Expand Down
9 changes: 9 additions & 0 deletions v2/module/builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ var (
Interface = newBuiltin(module.GlobalModule, "yo_db")
)

var All = []module.Module{
Header,
Type,
Operation,
Index,
LegacyIndex,
Interface,
}

var (
NullHeader = newNullModule(module.HeaderModule)
NullGlobal = newNullModule(module.GlobalModule)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020 Mercari, Inc.
// Copyright (c) 2021 Mercari, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
Expand All @@ -17,4 +17,42 @@
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package tplbin // import "go.mercari.io/yo/v2/module/builtin/tplbin"
package builtin

import (
"testing"

"go.mercari.io/yo/v2/module"
)

func TestBuiltin(t *testing.T) {
for _, m := range All {
b, err := m.Load()
if err != nil {
t.Fatalf("failed to load module %q: %v", m.Name(), err)
}

if len(b) == 0 {
t.Errorf("there is no contents in module %q", m.Name())
}
}
}

func TestNullModule(t *testing.T) {
nullModules := []module.Module{
NullHeader,
NullGlobal,
NullType,
}

for _, m := range nullModules {
b, err := m.Load()
if err != nil {
t.Fatalf("failed to load module %q: %v", m.Name(), err)
}

if len(b) != 0 {
t.Errorf("null module must have no contents %q: %s", m.Type(), b)
}
}
}
13 changes: 9 additions & 4 deletions v2/module/builtin/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@
package builtin

import (
"embed"
"fmt"
"io/ioutil"
"io"

"go.mercari.io/yo/v2/module"
templates "go.mercari.io/yo/v2/module/builtin/tplbin"
)

//go:embed templates/*.tpl
var templates embed.FS

type builtinMod struct {
typ module.ModuleType
name string
Expand All @@ -48,12 +51,14 @@ func (m *builtinMod) Type() module.ModuleType {
}

func (m *builtinMod) Load() ([]byte, error) {
f, err := templates.Assets.Open(fmt.Sprintf("%s.go.tpl", m.name))
f, err := templates.Open(fmt.Sprintf("templates/%s.go.tpl", m.name))
if err != nil {
return nil, fmt.Errorf("open %s: %w", m.name, err)
}

b, err := ioutil.ReadAll(f)
defer f.Close()

b, err := io.ReadAll(f)
if err != nil {
return nil, fmt.Errorf("failed to read file from assets: %w", err)
}
Expand Down
Loading

0 comments on commit 20fce06

Please sign in to comment.