Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 9bb2f27

Browse files
committed
option to write the plugin .go file and run the plugin migrations without building the .so file so that it can be done from the shell instead to afford complete error messages
1 parent ba4e79f commit 9bb2f27

File tree

8 files changed

+124
-10
lines changed

8 files changed

+124
-10
lines changed

cmd/compose.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,5 +188,6 @@ func prepConfig() {
188188
FileName: viper.GetString("exporter.name"),
189189
Save: viper.GetBool("exporter.save"),
190190
Home: viper.GetString("exporter.home"),
191+
WriteOnly: viper.GetBool("exporter.writeOnly"),
191192
}
192193
}

cmd/contractWatcher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ package cmd
1818

1919
import (
2020
"fmt"
21-
"github.com/vulcanize/vulcanizedb/pkg/config"
2221
"time"
2322

2423
log "github.com/sirupsen/logrus"
2524
"github.com/spf13/cobra"
2625

2726
st "github.com/vulcanize/vulcanizedb/libraries/shared/transformer"
27+
"github.com/vulcanize/vulcanizedb/pkg/config"
2828
ft "github.com/vulcanize/vulcanizedb/pkg/contract_watcher/full/transformer"
2929
ht "github.com/vulcanize/vulcanizedb/pkg/contract_watcher/header/transformer"
3030
"github.com/vulcanize/vulcanizedb/utils"

pkg/config/contract.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
package config
1818

1919
import (
20+
"strings"
21+
2022
log "github.com/sirupsen/logrus"
2123
"github.com/spf13/viper"
24+
2225
"github.com/vulcanize/vulcanizedb/pkg/geth"
23-
"strings"
2426
)
2527

2628
// Config struct for generic contract transformer

pkg/config/plugin.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type Plugin struct {
3131
FileName string
3232
Save bool
3333
Home string
34+
WriteOnly bool
3435
}
3536

3637
type Transformer struct {

pkg/config/plugin_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
package config_test
1818

1919
import (
20+
"os"
21+
"path/filepath"
22+
2023
. "github.com/onsi/ginkgo"
2124
. "github.com/onsi/gomega"
25+
2226
"github.com/vulcanize/vulcanizedb/pkg/config"
23-
"os"
24-
"path/filepath"
2527
)
2628

2729
var allDifferentPathsConfig = config.Plugin{

pkg/plugin/composer/composer.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// VulcanizeDB
2+
// Copyright © 2019 Vulcanize
3+
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package composer
18+
19+
import (
20+
"errors"
21+
"fmt"
22+
"os"
23+
"path/filepath"
24+
25+
"github.com/vulcanize/vulcanizedb/pkg/config"
26+
"github.com/vulcanize/vulcanizedb/pkg/plugin/helpers"
27+
)
28+
29+
type composer struct {
30+
GenConfig config.Plugin
31+
dependencies []string
32+
tmpVenDirs []string // Keep track of temp vendor directories
33+
goFile string // Keep track of goFile name
34+
}
35+
36+
// Requires populated plugin config
37+
func NewPluginComposer(gc config.Plugin) *composer {
38+
return &composer{
39+
GenConfig: gc,
40+
tmpVenDirs: make([]string, 0),
41+
}
42+
}
43+
44+
func (b *composer) BuildPlugin() error {
45+
// Get plugin .go and .so file paths
46+
var err error
47+
b.goFile, _, err = b.GenConfig.GetPluginPaths()
48+
if err != nil {
49+
return err
50+
}
51+
52+
// setup env to build plugin
53+
return b.setupComposeEnv()
54+
}
55+
56+
// Sets up temporary vendor libs needed for plugin build
57+
// This is to work around a conflict between plugins and vendoring (https://github.com/golang/go/issues/20481)
58+
func (b *composer) setupComposeEnv() error {
59+
// TODO: Less hacky way of handling plugin build deps
60+
vendorPath, err := helpers.CleanPath(filepath.Join("$GOPATH/src", b.GenConfig.Home, "vendor"))
61+
if err != nil {
62+
return err
63+
}
64+
65+
repoPaths := b.GenConfig.GetRepoPaths()
66+
67+
// Import transformer dependencies so that we can build our plugin
68+
for importPath := range repoPaths {
69+
dst := filepath.Join(vendorPath, importPath)
70+
src, cleanErr := helpers.CleanPath(filepath.Join("$GOPATH/src", importPath))
71+
if cleanErr != nil {
72+
return cleanErr
73+
}
74+
75+
copyErr := helpers.CopyDir(src, dst, "vendor")
76+
if copyErr != nil {
77+
return errors.New(fmt.Sprintf("unable to copy transformer dependency from %s to %s: %v", src, dst, copyErr))
78+
}
79+
80+
// Have to clear out the copied over vendor lib or plugin won't build (see issue above)
81+
removeErr := os.RemoveAll(filepath.Join(dst, "vendor"))
82+
if removeErr != nil {
83+
return removeErr
84+
}
85+
// Keep track of this vendor directory to clear later
86+
b.tmpVenDirs = append(b.tmpVenDirs, dst)
87+
}
88+
89+
return nil
90+
}
91+
92+
func (c *composer) CleanUp() error {
93+
for _, venDir := range c.tmpVenDirs {
94+
err := os.RemoveAll(venDir)
95+
if err != nil {
96+
return err
97+
}
98+
}
99+
return nil
100+
}

pkg/plugin/generator.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ package plugin
1818

1919
import (
2020
"errors"
21+
2122
"github.com/vulcanize/vulcanizedb/pkg/config"
2223
"github.com/vulcanize/vulcanizedb/pkg/plugin/builder"
24+
"github.com/vulcanize/vulcanizedb/pkg/plugin/composer"
2325
"github.com/vulcanize/vulcanizedb/pkg/plugin/manager"
2426
"github.com/vulcanize/vulcanizedb/pkg/plugin/writer"
2527
)
@@ -40,11 +42,16 @@ func NewGenerator(gc config.Plugin, dbc config.Database) (*generator, error) {
4042
if len(gc.Transformers) < 1 {
4143
return nil, errors.New("plugin generator is not configured with any transformers")
4244
}
43-
return &generator{
45+
gen := &generator{
4446
PluginWriter: writer.NewPluginWriter(gc),
45-
PluginBuilder: builder.NewPluginBuilder(gc),
4647
MigrationManager: manager.NewMigrationManager(gc, dbc),
47-
}, nil
48+
}
49+
if gc.WriteOnly {
50+
gen.PluginBuilder = composer.NewPluginComposer(gc)
51+
} else {
52+
gen.PluginBuilder = builder.NewPluginBuilder(gc)
53+
}
54+
return gen, nil
4855
}
4956

5057
// Generates plugin for the transformer initializers specified in the generator config

pkg/plugin/manager/manager.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ import (
2020
"database/sql"
2121
"errors"
2222
"fmt"
23+
"io/ioutil"
24+
"os"
25+
"path/filepath"
26+
2327
"github.com/lib/pq"
2428
"github.com/pressly/goose"
2529
"github.com/vulcanize/vulcanizedb/pkg/config"
2630
"github.com/vulcanize/vulcanizedb/pkg/plugin/helpers"
27-
"io/ioutil"
28-
"os"
29-
"path/filepath"
3031
)
3132

3233
// Interface for managing the db migrations for plugin transformers

0 commit comments

Comments
 (0)