ipld-eth-server/pkg/plugin/generator.go

69 lines
2.3 KiB
Go
Raw Normal View History

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 plugin
import (
"errors"
"github.com/vulcanize/vulcanizedb/pkg/config"
"github.com/vulcanize/vulcanizedb/pkg/plugin/builder"
"github.com/vulcanize/vulcanizedb/pkg/plugin/manager"
"github.com/vulcanize/vulcanizedb/pkg/plugin/writer"
)
// Generator is the top-level interface for creating transformer plugins
2019-02-02 22:15:09 +00:00
type Generator interface {
GenerateExporterPlugin() error
}
type generator struct {
writer.PluginWriter
builder.PluginBuilder
manager.MigrationManager
}
// Creates a new generator from a plugin and database config
2019-02-02 22:15:09 +00:00
func NewGenerator(gc config.Plugin, dbc config.Database) (*generator, error) {
if len(gc.Transformers) < 1 {
return nil, errors.New("plugin generator is not configured with any transformers")
2019-02-02 22:15:09 +00:00
}
return &generator{
PluginWriter: writer.NewPluginWriter(gc),
PluginBuilder: builder.NewPluginBuilder(gc),
2019-02-02 22:15:09 +00:00
MigrationManager: manager.NewMigrationManager(gc, dbc),
}, nil
}
// Generates plugin for the transformer initializers specified in the generator config
// Writes plugin code => Sets up build environment => Builds .so file => Performs db migrations for the plugin transformers => Clean up
2019-02-02 22:15:09 +00:00
func (g *generator) GenerateExporterPlugin() error {
// Use plugin writer interface to write the plugin code
2019-02-02 22:15:09 +00:00
err := g.PluginWriter.WritePlugin()
if err != nil {
return err
}
// Clean up temporary files and directories when we are done
2019-02-02 22:15:09 +00:00
defer g.PluginBuilder.CleanUp()
// Use plugin builder interface to setup build environment and compile .go file into a .so file
2019-02-02 22:15:09 +00:00
err = g.PluginBuilder.BuildPlugin()
if err != nil {
return err
}
// Perform db migrations for the transformers
2019-02-02 22:15:09 +00:00
return g.MigrationManager.RunMigrations()
}