Nothing Special   »   [go: up one dir, main page]

Skip to content

Commit

Permalink
add vgo support
Browse files Browse the repository at this point in the history
automatic checking project root has go.mod to switch vgo launch, go run , test, vet, fmt ..., etc
  • Loading branch information
hysios committed Aug 6, 2018
1 parent 05f079e commit 359e1ca
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 14 deletions.
8 changes: 5 additions & 3 deletions realize.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package main

import (
"github.com/oxequa/interact"
"github.com/oxequa/realize/realize"
"gopkg.in/urfave/cli.v2"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"time"

"github.com/hysios/realize/realize"
"github.com/oxequa/interact"
"gopkg.in/urfave/cli.v2"
)

var r realize.Realize
Expand Down Expand Up @@ -1125,6 +1126,7 @@ func start(c *cli.Context) (err error) {
if c.Bool("server") {
r.Server.Set(c.Bool("server"), c.Bool("open"), realize.Port, realize.Host)
}

// check no-config and read
if !c.Bool("no-config") {
// read a config if exist
Expand Down
7 changes: 4 additions & 3 deletions realize/cli.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package realize

import (
"fmt"
"github.com/fsnotify/fsnotify"
"errors"
"fmt"
"go/build"
"log"
"os"
Expand All @@ -12,13 +11,15 @@ import (
"strings"
"sync"
"time"

"github.com/fsnotify/fsnotify"
)

var (
// RPrefix tool name
RPrefix = "realize"
// RVersion current version
RVersion = "2.0.2"
RVersion = "2.0.3"
// RExt file extension
RExt = ".yaml"
// RFile config file name
Expand Down
5 changes: 5 additions & 0 deletions realize/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ func (p *Project) Before() {
p.parent.Before(Context{Project: p})
return
}

if hasGoMod(Wdir()) {
p.Tools.vgo = true
}

// setup go tools
p.Tools.Setup()
// set env const
Expand Down
10 changes: 9 additions & 1 deletion realize/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package realize

import (
"errors"
"gopkg.in/urfave/cli.v2"
"path/filepath"
"reflect"

"gopkg.in/urfave/cli.v2"
)

// Schema projects list
Expand Down Expand Up @@ -35,10 +36,16 @@ func (s *Schema) Remove(name string) error {

// New create a project using cli fields
func (s *Schema) New(c *cli.Context) Project {
var vgo bool
name := filepath.Base(c.String("path"))
if len(name) == 0 || name == "." {
name = filepath.Base(Wdir())
}

if hasGoMod(Wdir()) {
vgo = true
}

project := Project{
Name: name,
Path: c.String("path"),
Expand All @@ -64,6 +71,7 @@ func (s *Schema) New(c *cli.Context) Project {
Run: Tool{
Status: c.Bool("run"),
},
vgo: vgo,
},
Args: params(c),
Watcher: Watch{
Expand Down
20 changes: 14 additions & 6 deletions realize/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,31 @@ type Tools struct {
Install Tool `yaml:"install,omitempty" json:"install,omitempty"`
Build Tool `yaml:"build,omitempty" json:"build,omitempty"`
Run Tool `yaml:"run,omitempty" json:"run,omitempty"`
vgo bool
}

// Setup go tools
func (t *Tools) Setup() {
var gocmd string
if t.vgo {
gocmd = "vgo"
} else {
gocmd = "go"
}

// go clean
if t.Clean.Status {
t.Clean.name = "Clean"
t.Clean.isTool = true
t.Clean.cmd = replace([]string{"go", "clean"}, t.Clean.Method)
t.Clean.cmd = replace([]string{gocmd, "clean"}, t.Clean.Method)
t.Clean.Args = split([]string{}, t.Clean.Args)
}
// go generate
if t.Generate.Status {
t.Generate.dir = true
t.Generate.isTool = true
t.Generate.name = "Generate"
t.Generate.cmd = replace([]string{"go", "generate"}, t.Generate.Method)
t.Generate.cmd = replace([]string{gocmd, "generate"}, t.Generate.Method)
t.Generate.Args = split([]string{}, t.Generate.Args)
}
// go fmt
Expand All @@ -70,25 +78,25 @@ func (t *Tools) Setup() {
t.Vet.dir = true
t.Vet.name = "Vet"
t.Vet.isTool = true
t.Vet.cmd = replace([]string{"go", "vet"}, t.Vet.Method)
t.Vet.cmd = replace([]string{gocmd, "vet"}, t.Vet.Method)
t.Vet.Args = split([]string{}, t.Vet.Args)
}
// go test
if t.Test.Status {
t.Test.dir = true
t.Test.isTool = true
t.Test.name = "Test"
t.Test.cmd = replace([]string{"go", "test"}, t.Test.Method)
t.Test.cmd = replace([]string{gocmd, "test"}, t.Test.Method)
t.Test.Args = split([]string{}, t.Test.Args)
}
// go install
t.Install.name = "Install"
t.Install.cmd = replace([]string{"go", "install"}, t.Install.Method)
t.Install.cmd = replace([]string{gocmd, "install"}, t.Install.Method)
t.Install.Args = split([]string{}, t.Install.Args)
// go build
if t.Build.Status {
t.Build.name = "Build"
t.Build.cmd = replace([]string{"go", "build"}, t.Build.Method)
t.Build.cmd = replace([]string{gocmd, "build"}, t.Build.Method)
t.Build.Args = split([]string{}, t.Build.Args)
}
}
Expand Down
13 changes: 12 additions & 1 deletion realize/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package realize

import (
"errors"
"gopkg.in/urfave/cli.v2"
"log"
"os"
"path"
"strings"

"gopkg.in/urfave/cli.v2"
)

// Params parse one by one the given argumentes
Expand Down Expand Up @@ -73,3 +75,12 @@ func Wdir() string {
}
return dir
}

func hasGoMod(dir string) bool {
filename := path.Join(dir, "go.mod")
if _, err := os.Stat(filename); os.IsNotExist(err) {
return false
}

return true
}

0 comments on commit 359e1ca

Please sign in to comment.