2019-02-02 22:15:09 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2018 Vulcanize
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package writer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
. "github.com/dave/jennifer/jen"
|
|
|
|
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/config"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/plugin/helpers"
|
|
|
|
)
|
|
|
|
|
2019-02-11 10:08:24 +00:00
|
|
|
// Interface for writing a .go file for a simple
|
|
|
|
// plugin that exports the set of transformer
|
|
|
|
// initializers specified in the config
|
2019-02-02 22:15:09 +00:00
|
|
|
type PluginWriter interface {
|
|
|
|
WritePlugin() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type writer struct {
|
|
|
|
GenConfig config.Plugin
|
|
|
|
}
|
|
|
|
|
2019-02-11 10:08:24 +00:00
|
|
|
// Requires populated plugin config
|
2019-02-02 22:15:09 +00:00
|
|
|
func NewPluginWriter(gc config.Plugin) *writer {
|
|
|
|
return &writer{
|
|
|
|
GenConfig: gc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-11 10:08:24 +00:00
|
|
|
// Generates the plugin code according to config specification
|
2019-02-02 22:15:09 +00:00
|
|
|
func (w *writer) WritePlugin() error {
|
|
|
|
// Setup plugin file paths
|
|
|
|
goFile, err := w.setupFilePath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Begin code generation
|
|
|
|
f := NewFile("main")
|
|
|
|
f.HeaderComment("This is a plugin generated to export the configured transformer initializers")
|
|
|
|
|
2019-02-11 10:08:24 +00:00
|
|
|
// Import pkgs for generic TransformerInitializer interface and specific TransformerInitializers specified in config
|
2019-02-02 22:15:09 +00:00
|
|
|
f.ImportAlias("github.com/vulcanize/vulcanizedb/libraries/shared/transformer", "interface")
|
2019-02-13 19:04:07 +00:00
|
|
|
for name, transformer := range w.GenConfig.Transformers {
|
|
|
|
f.ImportAlias(transformer.RepositoryPath+"/"+transformer.Path, name)
|
2019-02-02 22:15:09 +00:00
|
|
|
}
|
|
|
|
|
2019-02-07 21:35:25 +00:00
|
|
|
// Collect initializer code
|
2019-02-13 19:04:07 +00:00
|
|
|
code, err := w.collectTransformers()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-02 22:15:09 +00:00
|
|
|
|
2019-02-07 21:35:25 +00:00
|
|
|
// Create Exporter variable with method to export the set of the imported storage and event transformer initializers
|
2019-02-02 22:15:09 +00:00
|
|
|
f.Type().Id("exporter").String()
|
|
|
|
f.Var().Id("Exporter").Id("exporter")
|
2019-02-11 10:08:24 +00:00
|
|
|
f.Func().Params(Id("e").Id("exporter")).Id("Export").Params().Parens(List(
|
|
|
|
Index().Qual("github.com/vulcanize/vulcanizedb/libraries/shared/transformer", "TransformerInitializer"),
|
|
|
|
Index().Qual("github.com/vulcanize/vulcanizedb/libraries/shared/transformer", "StorageTransformerInitializer"),
|
|
|
|
)).Block(Return(
|
|
|
|
Index().Qual(
|
2019-02-02 22:15:09 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/transformer",
|
2019-02-13 19:04:07 +00:00
|
|
|
"TransformerInitializer").Values(code[config.EthEvent]...),
|
2019-02-07 21:35:25 +00:00
|
|
|
Index().Qual(
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/transformer",
|
2019-02-13 19:04:07 +00:00
|
|
|
"StorageTransformerInitializer").Values(code[config.EthStorage]...))) // Exports the collected event and storage transformer initializers
|
2019-02-02 22:15:09 +00:00
|
|
|
|
|
|
|
// Write code to destination file
|
|
|
|
err = f.Save(goFile)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New(fmt.Sprintf("failed to save generated .go file: %s\r\n%s", goFile, err.Error()))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-11 10:08:24 +00:00
|
|
|
// Collect code for various types of initializers
|
2019-02-13 19:04:07 +00:00
|
|
|
func (w *writer) collectTransformers() (map[config.TransformerType][]Code, error) {
|
|
|
|
code := make(map[config.TransformerType][]Code)
|
|
|
|
for _, transformer := range w.GenConfig.Transformers {
|
|
|
|
path := transformer.RepositoryPath + "/" + transformer.Path
|
|
|
|
switch transformer.Type {
|
2019-02-07 21:35:25 +00:00
|
|
|
case config.EthEvent:
|
2019-02-13 19:04:07 +00:00
|
|
|
code[config.EthEvent] = append(code[config.EthEvent], Qual(path, "TransformerInitializer"))
|
2019-02-07 21:35:25 +00:00
|
|
|
case config.EthStorage:
|
2019-02-13 19:04:07 +00:00
|
|
|
code[config.EthStorage] = append(code[config.EthStorage], Qual(path, "StorageTransformerInitializer"))
|
|
|
|
default:
|
|
|
|
return nil, errors.New(fmt.Sprintf("invalid transformer type %s", transformer.Type))
|
2019-02-07 21:35:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-13 19:04:07 +00:00
|
|
|
return code, nil
|
2019-02-11 10:08:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the .go, clear old ones if present
|
2019-02-02 22:15:09 +00:00
|
|
|
func (w *writer) setupFilePath() (string, error) {
|
|
|
|
goFile, soFile, err := w.GenConfig.GetPluginPaths()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
// Clear .go and .so files of the same name if they exist
|
|
|
|
return goFile, helpers.ClearFiles(goFile, soFile)
|
|
|
|
}
|