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"
|
|
|
|
)
|
|
|
|
|
2019-02-11 10:08:24 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2019-02-11 10:08:24 +00:00
|
|
|
// 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) {
|
2019-02-13 19:04:07 +00:00
|
|
|
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),
|
2019-02-11 10:08:24 +00:00
|
|
|
PluginBuilder: builder.NewPluginBuilder(gc),
|
2019-02-02 22:15:09 +00:00
|
|
|
MigrationManager: manager.NewMigrationManager(gc, dbc),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-02-11 10:08:24 +00:00
|
|
|
// 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 {
|
2019-02-11 10:08:24 +00:00
|
|
|
// 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
|
|
|
|
}
|
2019-02-11 10:08:24 +00:00
|
|
|
// Clean up temporary files and directories when we are done
|
2019-02-02 22:15:09 +00:00
|
|
|
defer g.PluginBuilder.CleanUp()
|
2019-02-11 10:08:24 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2019-02-11 10:08:24 +00:00
|
|
|
// Perform db migrations for the transformers
|
2019-02-02 22:15:09 +00:00
|
|
|
return g.MigrationManager.RunMigrations()
|
|
|
|
}
|