Skip to content

Commit

Permalink
First draft of common patterns on codegens.
Browse files Browse the repository at this point in the history
Some are not tested, might have typos in templates.
  • Loading branch information
Alexandre Bourget committed Sep 11, 2024
1 parent 1208fec commit 7105f4d
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 120 deletions.
12 changes: 12 additions & 0 deletions common-templates/README.md.gotmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
This template holds chunks of common README.md

{{ define "readme_usage" -}}
## Usage

```bash
substreams build
substreams auth
substreams gui
```
{{- end }}

41 changes: 41 additions & 0 deletions common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package codegen

import (
"embed"
"io/fs"
"text/template"

"github.com/bmatcuk/doublestar/v4"
)

//go:embed common-templates/*.gotmpl
var commonTemplatesFS embed.FS

var commonTemplates *template.Template

func init() {
var err error
commonTemplates, err = parseCommonTemplates()
if err != nil {
panic(err)
}
}
func parseCommonTemplates() (*template.Template, error) {
t := template.New("").Funcs(templateFuncs)
filenames, err := doublestar.Glob(commonTemplatesFS, "**/*.gotmpl")
if err != nil {
return nil, err
}

for _, filename := range filenames {
b, err := fs.ReadFile(commonTemplatesFS, filename)
if err != nil {
return nil, err
}
_, err = t.New(filename).Parse(string(b))
if err != nil {
return nil, err
}
}
return t, nil
}
19 changes: 0 additions & 19 deletions ethfull/templates/triggers/subgraph.yaml.gotmpl

This file was deleted.

2 changes: 1 addition & 1 deletion evm-events-calls/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (p *Project) Render() (projectFiles map[string][]byte, err error) {
"rust-toolchain.toml": "rust-toolchain.toml",
".gitignore": ".gitignore",
"substreams.yaml.gotmpl": "substreams.yaml",
"README.md": "README.md",
"README.md.gotmpl": "README.md",
}

for templateFile, finalFileName := range templateFiles {
Expand Down
37 changes: 27 additions & 10 deletions evm-events-calls/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,24 @@ func TestTemplates(t *testing.T) {
_ = tpls
}

type match struct {
file string
contains string
}

func Test_Generate(t *testing.T) {
cases := []struct {
name string
generatorFile string
contains []match
}{
{
name: "uniswap_factory_track_calls",
generatorFile: "./testdata/uniswap_factory_track_calls.json",
contains: []match{
{"README.md", "## Usage\n\n```bash\nsubstreams build\n"},
{"README.md", "INSERT CONTRACT ADDRESS"},
},
},
{
name: "uniswap_factory_track_calls_events",
Expand Down Expand Up @@ -90,18 +100,25 @@ func Test_Generate(t *testing.T) {
require.NoError(t, err)
assert.NotEmpty(t, len(projFiles))

sourceTmpDir, err := os.MkdirTemp(os.TempDir(), "test_output_src.zip")
require.NoError(t, err)
for _, cont := range c.contains {
assert.Contains(t, projFiles, cont.file)
assert.Contains(t, string(projFiles[cont.file]), cont.contains)
}

projectTmpDir, err := os.MkdirTemp(os.TempDir(), "test_output_project.zip")
require.NoError(t, err)
// sourceTmpDir, err := os.MkdirTemp(os.TempDir(), "test_output_src.zip")
// require.NoError(t, err)

if os.Getenv("TEST_KEEP_TEST_OUTPUT") != "true" {
defer func() {
_ = os.RemoveAll(sourceTmpDir)
_ = os.RemoveAll(projectTmpDir)
}()
}
// projectTmpDir, err := os.MkdirTemp(os.TempDir(), "test_output_project.zip")
// require.NoError(t, err)

// if os.Getenv("TEST_KEEP_TEST_OUTPUT") != "true" {
// defer func() {
// _ = os.RemoveAll(sourceTmpDir)
// _ = os.RemoveAll(projectTmpDir)
// }()
// } else {
// fmt.Println("Produced", sourceTmpDir, projectTmpDir)
// }
})
}
}
Expand Down
21 changes: 0 additions & 21 deletions evm-events-calls/templates/README.md

This file was deleted.

27 changes: 7 additions & 20 deletions evm-minimal/templates/README.md.gotmpl
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
# EVM Minimal
# {{ .Name }} Substreams modules

## Build your Substreams
This package was initialized via `substreams init`, using the `evm-minimal` template.

```bash
substreams build
```

## Authenticate

To run your Substreams you will need to [authenticate](https://substreams.streamingfast.io/documentation/consume/authentication) yourself.

```bash
substreams auth
```

## Run your Substreams

```bash
substreams gui
```
{{ template "readme_usage" }}

## Modules

`map_my_data`: This module will do a simple computation of the number of **transactions** in each block.
### `map_my_data`

This module extracts small bits of block data, and does simple computations over the
number of **transactions** in each block.
2 changes: 1 addition & 1 deletion injective-events/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (p *Project) Render(outType outputType) (substreamsFiles map[string][]byte,

templateFiles := map[string]string{
".gitignore": ".gitignore",
"README.md": "README.md",
"README.md.gotmpl": "README.md",
"substreams.yaml.gotmpl": "substreams.yaml",
}

Expand Down
25 changes: 0 additions & 25 deletions injective-events/templates/README.md

This file was deleted.

12 changes: 12 additions & 0 deletions injective-events/templates/README.md.gotmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# {{ .Name }} Substreams modules

This package was initialized via `substreams init`, using the `injective-events` template.

{{ template "readme_usage" }}

## Modules

### `{{ .ModuleName }}`

This module uses the [Injective Foundational Modules](https://github.com/streamingfast/substreams-foundational-modules/)
and applies filters on the chosen events and attributes.
26 changes: 6 additions & 20 deletions injective-minimal/templates/README.md.gotmpl
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
# Injective Minimal
# {{ .Name }} Substreams modules

## Build your Substreams
This package was initialized via `substreams init`, using the `injective-minimal` template.

```bash
substreams build
```

## Authenticate

To run your Substreams you will need to [authenticate](https://substreams.streamingfast.io/documentation/consume/authentication) yourself.

```bash
substreams auth
```

## Run your Substreams

```bash
substreams gui
```
{{ template "readme_usage" }}

## Modules

`map_my_data`: This module will do a simple computation of the number of **transactions** in each block.
### `map_my_data`

This module will do a simple computation of the number of **transactions** in each block.
13 changes: 10 additions & 3 deletions templating.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package codegen

import (
"fmt"
"github.com/huandu/xstrings"
"github.com/iancoleman/strcase"
"io/fs"
"regexp"
"strings"
"text/template"

"github.com/huandu/xstrings"
"github.com/iancoleman/strcase"

"github.com/bmatcuk/doublestar/v4"
"github.com/golang-cz/textcase"
)
Expand All @@ -27,7 +28,12 @@ var templateFuncs = template.FuncMap{

// ParseFS reads the files from the embedded FS and parses them into named templates.
func ParseFS(myFuncs template.FuncMap, fsys fs.FS, pattern string) (*template.Template, error) {
t := template.New("").Funcs(templateFuncs).Funcs(myFuncs)
t, err := commonTemplates.Clone()
if err != nil {
return nil, err
}
t.Funcs(myFuncs)

filenames, err := doublestar.Glob(fsys, pattern)
if err != nil {
return nil, err
Expand All @@ -49,6 +55,7 @@ func ParseFS(myFuncs template.FuncMap, fsys fs.FS, pattern string) (*template.Te
return nil, err
}
}

return t, nil
}

Expand Down

0 comments on commit 7105f4d

Please sign in to comment.