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 builder
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/config"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/plugin/helpers"
|
|
|
|
)
|
|
|
|
|
2019-02-11 10:08:24 +00:00
|
|
|
// Interface for compile Go code written by the
|
|
|
|
// PluginWriter into a shared object (.so file)
|
|
|
|
// which can be used loaded as a plugin
|
2019-02-02 22:15:09 +00:00
|
|
|
type PluginBuilder interface {
|
|
|
|
BuildPlugin() error
|
|
|
|
CleanUp() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type builder struct {
|
2019-02-13 19:04:07 +00:00
|
|
|
GenConfig config.Plugin
|
|
|
|
dependencies []string
|
|
|
|
tmpVenDirs []string // Keep track of temp vendor directories
|
|
|
|
goFile string // Keep track of goFile name
|
2019-02-02 22:15:09 +00:00
|
|
|
}
|
|
|
|
|
2019-02-11 10:08:24 +00:00
|
|
|
// Requires populated plugin config
|
2019-10-18 17:34:22 +00:00
|
|
|
func NewPluginBuilder(gc config.Plugin) PluginBuilder {
|
2019-02-02 22:15:09 +00:00
|
|
|
return &builder{
|
|
|
|
GenConfig: gc,
|
2019-02-13 19:04:07 +00:00
|
|
|
tmpVenDirs: make([]string, 0),
|
2019-02-02 22:15:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *builder) BuildPlugin() error {
|
|
|
|
// Get plugin .go and .so file paths
|
|
|
|
var err error
|
|
|
|
var soFile string
|
|
|
|
b.goFile, soFile, err = b.GenConfig.GetPluginPaths()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup env to build plugin
|
2019-03-05 20:49:31 +00:00
|
|
|
setupErr := b.setupBuildEnv()
|
|
|
|
if setupErr != nil {
|
|
|
|
return setupErr
|
2019-02-02 22:15:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build the .go file into a .so plugin
|
2019-03-05 20:49:31 +00:00
|
|
|
execErr := exec.Command("go", "build", "-buildmode=plugin", "-o", soFile, b.goFile).Run()
|
|
|
|
if execErr != nil {
|
2019-10-18 15:20:53 +00:00
|
|
|
return fmt.Errorf("unable to build .so file: %s", execErr.Error())
|
2019-02-02 22:15:09 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets up temporary vendor libs needed for plugin build
|
2019-02-11 10:08:24 +00:00
|
|
|
// This is to work around a conflict between plugins and vendoring (https://github.com/golang/go/issues/20481)
|
2019-02-02 22:15:09 +00:00
|
|
|
func (b *builder) setupBuildEnv() error {
|
|
|
|
// TODO: Less hacky way of handling plugin build deps
|
2019-02-18 11:23:36 +00:00
|
|
|
vendorPath, err := helpers.CleanPath(filepath.Join("$GOPATH/src", b.GenConfig.Home, "vendor"))
|
2019-02-02 22:15:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-13 19:04:07 +00:00
|
|
|
repoPaths := b.GenConfig.GetRepoPaths()
|
|
|
|
|
2019-02-02 22:15:09 +00:00
|
|
|
// Import transformer dependencies so that we can build our plugin
|
2019-02-13 19:04:07 +00:00
|
|
|
for importPath := range repoPaths {
|
2019-03-04 22:59:38 +00:00
|
|
|
dst := filepath.Join(vendorPath, importPath)
|
2019-03-05 20:49:31 +00:00
|
|
|
src, cleanErr := helpers.CleanPath(filepath.Join("$GOPATH/src", importPath))
|
|
|
|
if cleanErr != nil {
|
|
|
|
return cleanErr
|
2019-03-04 22:59:38 +00:00
|
|
|
}
|
|
|
|
|
2019-03-05 20:49:31 +00:00
|
|
|
copyErr := helpers.CopyDir(src, dst, "vendor")
|
|
|
|
if copyErr != nil {
|
2019-10-18 15:20:53 +00:00
|
|
|
return fmt.Errorf("unable to copy transformer dependency from %s to %s: %v", src, dst, copyErr)
|
2019-02-02 22:15:09 +00:00
|
|
|
}
|
2019-03-04 22:59:38 +00:00
|
|
|
|
2019-02-18 11:23:36 +00:00
|
|
|
// Have to clear out the copied over vendor lib or plugin won't build (see issue above)
|
2019-03-05 20:49:31 +00:00
|
|
|
removeErr := os.RemoveAll(filepath.Join(dst, "vendor"))
|
|
|
|
if removeErr != nil {
|
|
|
|
return removeErr
|
2019-02-02 22:15:09 +00:00
|
|
|
}
|
2019-02-11 10:08:24 +00:00
|
|
|
// Keep track of this vendor directory to clear later
|
2019-03-04 22:59:38 +00:00
|
|
|
b.tmpVenDirs = append(b.tmpVenDirs, dst)
|
2019-02-02 22:15:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-11 10:08:24 +00:00
|
|
|
// Used to clear all of the tmp vendor libs used to build the plugin
|
|
|
|
// Also clears the go file if saving it has not been specified in the config
|
|
|
|
// Do not call until after the MigrationManager has performed its operations
|
|
|
|
// as it needs to pull the db migrations from the tmpVenDirs
|
2019-02-02 22:15:09 +00:00
|
|
|
func (b *builder) CleanUp() error {
|
|
|
|
if !b.GenConfig.Save {
|
|
|
|
err := helpers.ClearFiles(b.goFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, venDir := range b.tmpVenDirs {
|
|
|
|
err := os.RemoveAll(venDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|