|
| 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 | +} |
0 commit comments