2019-02-02 22:15:09 +00:00
|
|
|
// VulcanizeDB
|
2019-03-12 15:46:42 +00:00
|
|
|
// Copyright © 2019 Vulcanize
|
2019-02-02 22:15:09 +00:00
|
|
|
|
|
|
|
// 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 (
|
|
|
|
"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-10-18 17:34:22 +00:00
|
|
|
func NewPluginWriter(gc config.Plugin) PluginWriter {
|
2019-02-02 22:15:09 +00:00
|
|
|
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(
|
2019-03-14 16:59:39 +00:00
|
|
|
Index().Qual("github.com/vulcanize/vulcanizedb/libraries/shared/transformer", "EventTransformerInitializer"),
|
2019-02-11 10:08:24 +00:00
|
|
|
Index().Qual("github.com/vulcanize/vulcanizedb/libraries/shared/transformer", "StorageTransformerInitializer"),
|
2019-03-13 16:14:35 +00:00
|
|
|
Index().Qual("github.com/vulcanize/vulcanizedb/libraries/shared/transformer", "ContractTransformerInitializer"),
|
2019-02-11 10:08:24 +00:00
|
|
|
)).Block(Return(
|
|
|
|
Index().Qual(
|
2019-02-02 22:15:09 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/transformer",
|
2019-03-14 16:59:39 +00:00
|
|
|
"EventTransformerInitializer").Values(code[config.EthEvent]...),
|
2019-02-07 21:35:25 +00:00
|
|
|
Index().Qual(
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/transformer",
|
2019-03-11 16:43:43 +00:00
|
|
|
"StorageTransformerInitializer").Values(code[config.EthStorage]...),
|
|
|
|
Index().Qual(
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/transformer",
|
2019-03-13 16:14:35 +00:00
|
|
|
"ContractTransformerInitializer").Values(code[config.EthContract]...))) // 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 {
|
2019-10-18 15:20:53 +00:00
|
|
|
return fmt.Errorf("failed to save generated .go file: %s\r\n%s", goFile, err.Error())
|
2019-02-02 22:15:09 +00:00
|
|
|
}
|
|
|
|
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-03-14 16:59:39 +00:00
|
|
|
code[config.EthEvent] = append(code[config.EthEvent], Qual(path, "EventTransformerInitializer"))
|
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"))
|
2019-03-13 16:14:35 +00:00
|
|
|
case config.EthContract:
|
|
|
|
code[config.EthContract] = append(code[config.EthContract], Qual(path, "ContractTransformerInitializer"))
|
2019-02-13 19:04:07 +00:00
|
|
|
default:
|
2019-10-18 15:20:53 +00:00
|
|
|
return nil, fmt.Errorf("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)
|
|
|
|
}
|