2019-05-31 02:26:34 +03:00
|
|
|
// Package golang is a go package manager
|
|
|
|
package golang
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
|
2020-01-19 20:47:27 +03:00
|
|
|
"github.com/micro/go-micro/runtime/local/build"
|
2019-05-31 02:26:34 +03:00
|
|
|
)
|
|
|
|
|
2019-11-19 19:09:43 +03:00
|
|
|
type Builder struct {
|
|
|
|
Options build.Options
|
2019-05-31 02:26:34 +03:00
|
|
|
Cmd string
|
|
|
|
Path string
|
|
|
|
}
|
|
|
|
|
|
|
|
// whichGo locates the go command
|
|
|
|
func whichGo() string {
|
|
|
|
// check GOROOT
|
|
|
|
if gr := os.Getenv("GOROOT"); len(gr) > 0 {
|
|
|
|
return filepath.Join(gr, "bin", "go")
|
|
|
|
}
|
|
|
|
|
|
|
|
// check path
|
|
|
|
for _, p := range filepath.SplitList(os.Getenv("PATH")) {
|
|
|
|
bin := filepath.Join(p, "go")
|
|
|
|
if _, err := os.Stat(bin); err == nil {
|
|
|
|
return bin
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// best effort
|
|
|
|
return "go"
|
|
|
|
}
|
|
|
|
|
2019-11-19 19:09:43 +03:00
|
|
|
func (g *Builder) Build(s *build.Source) (*build.Package, error) {
|
2019-05-31 02:26:34 +03:00
|
|
|
binary := filepath.Join(g.Path, s.Repository.Name)
|
|
|
|
source := filepath.Join(s.Repository.Path, s.Repository.Name)
|
|
|
|
|
|
|
|
cmd := exec.Command(g.Cmd, "build", "-o", binary, source)
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-19 19:09:43 +03:00
|
|
|
return &build.Package{
|
2019-05-31 02:26:34 +03:00
|
|
|
Name: s.Repository.Name,
|
|
|
|
Path: binary,
|
|
|
|
Type: "go",
|
|
|
|
Source: s,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-11-19 19:09:43 +03:00
|
|
|
func (g *Builder) Clean(b *build.Package) error {
|
2019-05-31 02:26:34 +03:00
|
|
|
binary := filepath.Join(b.Path, b.Name)
|
|
|
|
return os.Remove(binary)
|
|
|
|
}
|
|
|
|
|
2019-11-19 19:09:43 +03:00
|
|
|
func NewBuild(opts ...build.Option) build.Builder {
|
|
|
|
options := build.Options{
|
2019-05-31 02:26:34 +03:00
|
|
|
Path: os.TempDir(),
|
|
|
|
}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
2019-11-19 19:09:43 +03:00
|
|
|
return &Builder{
|
2019-05-31 02:26:34 +03:00
|
|
|
Options: options,
|
|
|
|
Cmd: whichGo(),
|
|
|
|
Path: options.Path,
|
|
|
|
}
|
|
|
|
}
|