mirror of
https://github.com/kevin-DL/revel-cmd.git
synced 2026-01-17 21:14:52 +00:00
Initial commit to go mod
This commit is contained in:
committed by
notzippy@gmail.com
parent
d2014633af
commit
acb8fb631b
@@ -2,6 +2,9 @@ package model
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/revel/cmd"
|
||||
// "github.com/revel/cmd/logger"
|
||||
"github.com/revel/cmd/utils"
|
||||
"go/ast"
|
||||
"go/build"
|
||||
"go/parser"
|
||||
@@ -11,10 +14,7 @@ import (
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/revel/cmd"
|
||||
"github.com/revel/cmd/logger"
|
||||
"github.com/revel/cmd/utils"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
// The constants
|
||||
@@ -45,15 +45,17 @@ type (
|
||||
SrcRoot string // The source root
|
||||
AppPath string // The application path (absolute)
|
||||
AppName string // The application name
|
||||
Vendored bool // True if the application is vendored
|
||||
HistoricBuildMode bool `long:"historic-build-mode" description:"If set the code is scanned using the original parsers, not the go.1.11+"` // True if debug is active
|
||||
Vendored bool // True if the application is vendored
|
||||
PackageResolver func(pkgName string) error // a packge resolver for the config
|
||||
BuildFlags []string `short:"X" long:"build-flags" description:"These flags will be used when building the application. May be specified multiple times, only applicable for Build, Run, Package, Test commands"`
|
||||
// The new command
|
||||
New struct {
|
||||
ImportPath string `short:"a" long:"application-path" description:"Path to application folder" required:"false"`
|
||||
SkeletonPath string `short:"s" long:"skeleton" description:"Path to skeleton folder (Must exist on GO PATH)" required:"false"`
|
||||
Vendored bool `short:"V" long:"vendor" description:"True if project should contain a vendor folder to be initialized. Creates the vendor folder and the 'Gopkg.toml' file in the root"`
|
||||
Run bool `short:"r" long:"run" description:"True if you want to run the application right away"`
|
||||
ImportPath string `short:"a" long:"application-path" description:"Path to application folder" required:"false"`
|
||||
SkeletonPath string `short:"s" long:"skeleton" description:"Path to skeleton folder (Must exist on GO PATH)" required:"false"`
|
||||
Package string `short:"p" long:"package" description:"The package name, this becomes the repfix to the app name, if defined vendored is set to true" required:"false"`
|
||||
Vendored bool `short:"V" long:"vendor" description:"True if project should contain a vendor folder to be initialized. Creates the vendor folder and the 'Gopkg.toml' file in the root"`
|
||||
Run bool `short:"r" long:"run" description:"True if you want to run the application right away"`
|
||||
} `command:"new"`
|
||||
// The build command
|
||||
Build struct {
|
||||
@@ -89,7 +91,7 @@ type (
|
||||
// The version command
|
||||
Version struct {
|
||||
ImportPath string `short:"a" long:"application-path" description:"Path to application folder" required:"false"`
|
||||
Update bool `short:"u" long:"update" description:"Update the framework and modules" required:"false"`
|
||||
Update bool `short:"u" long:"Update the framework and modules" required:"false"`
|
||||
} `command:"version"`
|
||||
}
|
||||
)
|
||||
@@ -103,14 +105,19 @@ func (c *CommandConfig) UpdateImportPath() error {
|
||||
importPath = c.New.ImportPath
|
||||
case RUN:
|
||||
importPath = c.Run.ImportPath
|
||||
c.Vendored = utils.Exists(filepath.Join(importPath,"src","go.mod"))
|
||||
case BUILD:
|
||||
importPath = c.Build.ImportPath
|
||||
c.Vendored = utils.Exists(filepath.Join(importPath,"src","go.mod"))
|
||||
case PACKAGE:
|
||||
importPath = c.Package.ImportPath
|
||||
c.Vendored = utils.Exists(filepath.Join(importPath,"src","go.mod"))
|
||||
case CLEAN:
|
||||
importPath = c.Clean.ImportPath
|
||||
c.Vendored = utils.Exists(filepath.Join(importPath,"src","go.mod"))
|
||||
case TEST:
|
||||
importPath = c.Test.ImportPath
|
||||
c.Vendored = utils.Exists(filepath.Join(importPath,"src","go.mod"))
|
||||
case VERSION:
|
||||
importPath = c.Version.ImportPath
|
||||
required = false
|
||||
@@ -135,8 +142,7 @@ func (c *CommandConfig) UpdateImportPath() error {
|
||||
if strings.HasPrefix(currentPath, path) && len(currentPath) > len(path)+1 {
|
||||
importPath = currentPath[len(path)+1:]
|
||||
// Remove the source from the path if it is there
|
||||
isSRC := strings.ToLower(importPath[0:4])
|
||||
if len(importPath) > 4 && (isSRC == "src/" || isSRC == "src\\") {
|
||||
if len(importPath) > 4 && strings.ToLower(importPath[0:4]) == "src/" {
|
||||
importPath = importPath[4:]
|
||||
} else if importPath == "src" {
|
||||
if c.Index != VERSION {
|
||||
@@ -151,12 +157,14 @@ func (c *CommandConfig) UpdateImportPath() error {
|
||||
}
|
||||
|
||||
c.ImportPath = importPath
|
||||
utils.Logger.Info("Returned import path", "path", importPath, "buildpath", build.Default.GOPATH)
|
||||
// We need the source root determined at this point to check the setversions
|
||||
c.initAppFolder()
|
||||
utils.Logger.Info("Returned import path", "path", importPath)
|
||||
if required && c.Index != NEW {
|
||||
if err := c.SetVersions(); err != nil {
|
||||
utils.Logger.Panic("Failed to fetch revel versions", "error", err)
|
||||
}
|
||||
if err := c.FrameworkVersion.CompatibleFramework(c); err != nil {
|
||||
if err:=c.FrameworkVersion.CompatibleFramework(c);err!=nil {
|
||||
utils.Logger.Fatal("Compatibility Error", "message", err,
|
||||
"Revel framework version", c.FrameworkVersion.String(), "Revel tool version", c.CommandVersion.String())
|
||||
}
|
||||
@@ -165,34 +173,130 @@ func (c *CommandConfig) UpdateImportPath() error {
|
||||
if !required {
|
||||
return nil
|
||||
}
|
||||
if len(importPath) == 0 {
|
||||
if len(importPath) == 0 {
|
||||
return fmt.Errorf("Unable to determine import path from : %s", importPath)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *CommandConfig) initAppFolder() (err error) {
|
||||
utils.Logger.Info("initAppFolder")
|
||||
// check for go executable
|
||||
c.GoCmd, err = exec.LookPath("go")
|
||||
if err != nil {
|
||||
utils.Logger.Fatal("Go executable not found in PATH.")
|
||||
}
|
||||
|
||||
// First try to determine where the application is located - this should be the import value
|
||||
appFolder := c.ImportPath
|
||||
wd,err := os.Getwd()
|
||||
if len(appFolder) == 0 {
|
||||
// We will assume the working directory is the appFolder
|
||||
appFolder = wd
|
||||
} else if strings.LastIndex(wd,appFolder)==len(wd)-len(appFolder) {
|
||||
// Check for existence of an /app folder
|
||||
if utils.Exists(filepath.Join(wd,"app")) {
|
||||
appFolder = wd
|
||||
} else {
|
||||
appFolder = filepath.Join(wd,appFolder)
|
||||
}
|
||||
} else if strings.Contains(appFolder,".") {
|
||||
appFolder = filepath.Join(wd,filepath.Base(c.ImportPath))
|
||||
} else if !filepath.IsAbs(appFolder) {
|
||||
appFolder = filepath.Join(wd,appFolder)
|
||||
}
|
||||
|
||||
utils.Logger.Info("Determined app folder to be", "folder",appFolder, "working",wd)
|
||||
|
||||
// Use app folder to read the go.mod if it exists and extract the package information
|
||||
goModFile := filepath.Join(appFolder,"go.mod")
|
||||
if utils.Exists(goModFile) {
|
||||
c.Vendored = true
|
||||
file,err:=ioutil.ReadFile(goModFile)
|
||||
if err!=nil {
|
||||
return err
|
||||
}
|
||||
for _,line := range strings.Split(string(file),"\n") {
|
||||
if strings.Index(line,"module ")==0 {
|
||||
c.ImportPath = strings.TrimSpace(strings.Split(line,"module")[1])
|
||||
c.AppPath = appFolder
|
||||
c.SrcRoot = appFolder
|
||||
utils.Logger.Info("Set application path and package based on go mod", "path", c.AppPath, "sourceroot", c.SrcRoot)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
utils.Logger.Fatal("Trying to set path based on gopath")
|
||||
// lookup go path
|
||||
c.GoPath = build.Default.GOPATH
|
||||
if c.GoPath == "" {
|
||||
utils.Logger.Fatal("Abort: GOPATH environment variable is not set. " +
|
||||
"Please refer to http://golang.org/doc/code.html to configure your Go environment.")
|
||||
}
|
||||
|
||||
// revel/revel#1004 choose go path relative to current working directory
|
||||
|
||||
// What we want to do is to add the import to the end of the
|
||||
// gopath, and discover which import exists - If none exist this is an error except in the case
|
||||
// where we are dealing with new which is a special case where we will attempt to target the working directory first
|
||||
workingDir, _ := os.Getwd()
|
||||
goPathList := filepath.SplitList(c.GoPath)
|
||||
bestpath := ""
|
||||
for _, path := range goPathList {
|
||||
if c.Index == NEW {
|
||||
// If the GOPATH is part of the working dir this is the most likely target
|
||||
if strings.HasPrefix(workingDir, path) {
|
||||
bestpath = path
|
||||
}
|
||||
} else {
|
||||
if utils.Exists(filepath.Join(path, "src", c.ImportPath)) {
|
||||
c.SrcRoot = path
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
utils.Logger.Info("Source root", "path", c.SrcRoot, "cwd", workingDir, "gopath", c.GoPath, "bestpath",bestpath)
|
||||
if len(c.SrcRoot) == 0 && len(bestpath) > 0 {
|
||||
c.SrcRoot = bestpath
|
||||
}
|
||||
|
||||
// If source root is empty and this isn't a version then skip it
|
||||
if len(c.SrcRoot) == 0 {
|
||||
if c.Index == NEW {
|
||||
c.SrcRoot = c.New.ImportPath
|
||||
} else {
|
||||
if c.Index != VERSION {
|
||||
utils.Logger.Fatal("Abort: could not create a Revel application outside of GOPATH.")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// set go src path
|
||||
c.SrcRoot = filepath.Join(c.SrcRoot, "src")
|
||||
|
||||
c.AppPath = filepath.Join(c.SrcRoot, filepath.FromSlash(c.ImportPath))
|
||||
utils.Logger.Info("Set application path", "path", c.AppPath)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Used to initialize the package resolver
|
||||
func (c *CommandConfig) InitPackageResolver() {
|
||||
c.Vendored = utils.DirExists(filepath.Join(c.AppPath, "vendor"))
|
||||
c.Vendored = utils.DirExists(filepath.Join(c.AppPath, "go.mod"))
|
||||
if c.Index == NEW && c.New.Vendored {
|
||||
c.Vendored = true
|
||||
}
|
||||
|
||||
utils.Logger.Info("InitPackageResolver", "useVendor", c.Vendored, "path", c.AppPath)
|
||||
|
||||
var (
|
||||
depPath string
|
||||
err error
|
||||
)
|
||||
|
||||
if c.Vendored {
|
||||
utils.Logger.Info("Vendor folder detected, scanning for deps in path")
|
||||
depPath, err = exec.LookPath("dep")
|
||||
if err != nil {
|
||||
utils.Logger.Info("Vendor folder detected, for go version")
|
||||
if runtime.Version()!="" {
|
||||
// Do not halt build unless a new package needs to be imported
|
||||
utils.Logger.Fatal("Build: `dep` executable not found in PATH, but vendor folder detected." +
|
||||
"Packages can only be added automatically to the vendor folder using the `dep` tool. " +
|
||||
"You can install the `dep` tool by doing a `go get -u github.com/golang/dep/cmd/dep`")
|
||||
utils.Logger.Fatal(`Go version 1.11 or newer is required to build`)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -200,45 +304,42 @@ func (c *CommandConfig) InitPackageResolver() {
|
||||
c.PackageResolver = func(pkgName string) error {
|
||||
//useVendor := utils.DirExists(filepath.Join(c.AppPath, "vendor"))
|
||||
|
||||
var getCmd *exec.Cmd
|
||||
//var getCmd *exec.Cmd
|
||||
utils.Logger.Info("Request for package ", "package", pkgName, "use vendor", c.Vendored)
|
||||
if c.Vendored {
|
||||
utils.Logger.Info("Using dependency manager to import package", "package", pkgName)
|
||||
|
||||
if depPath == "" {
|
||||
utils.Logger.Error("Build: Vendor folder found, but the `dep` tool was not found, " +
|
||||
"if you use a different vendoring (package management) tool please add the following packages by hand, " +
|
||||
"or install the `dep` tool into your gopath by doing a `go get -u github.com/golang/dep/cmd/dep`. " +
|
||||
"For more information and usage of the tool please see http://github.com/golang/dep")
|
||||
utils.Logger.Error("Missing package", "package", pkgName)
|
||||
return fmt.Errorf("Missing package %s", pkgName)
|
||||
}
|
||||
// Check to see if the package exists locally
|
||||
_, err := build.Import(pkgName, c.AppPath, build.FindOnly)
|
||||
if err != nil {
|
||||
getCmd = exec.Command(depPath, "ensure", "-add", pkgName)
|
||||
} else {
|
||||
getCmd = exec.Command(depPath, "ensure", "-update", pkgName)
|
||||
}
|
||||
|
||||
} else {
|
||||
utils.Logger.Info("No vendor folder detected, not using dependency manager to import package", "package", pkgName)
|
||||
getCmd = exec.Command(c.GoCmd, "get", "-u", pkgName)
|
||||
goModCmd := exec.Command("go", "mod", "tidy")
|
||||
utils.CmdInit(goModCmd, c.AppPath)
|
||||
return nil
|
||||
}
|
||||
|
||||
utils.CmdInit(getCmd, c.AppPath)
|
||||
utils.Logger.Info("Go get command ", "exec", getCmd.Path, "dir", getCmd.Dir, "args", getCmd.Args, "env", getCmd.Env, "package", pkgName)
|
||||
output, err := getCmd.CombinedOutput()
|
||||
if err != nil {
|
||||
fmt.Printf("Error stack %v\n", logger.NewCallStack())
|
||||
utils.Logger.Error("Failed to import package", "error", err, "gopath", build.Default.GOPATH, "GO-ROOT", build.Default.GOROOT, "output", string(output))
|
||||
}
|
||||
return err
|
||||
//utils.Logger.Info("Using dependency manager to import package", "package", pkgName)
|
||||
//
|
||||
//// Check to see if the package exists locally
|
||||
//_, err := build.Import(pkgName, c.AppPath, build.FindOnly)
|
||||
//if err != nil {
|
||||
// getCmd = exec.Command(depPath, "ensure", "-add", pkgName)
|
||||
//} else {
|
||||
// getCmd = exec.Command(depPath, "ensure", "-update", pkgName)
|
||||
//}
|
||||
//
|
||||
//
|
||||
//} else {
|
||||
// utils.Logger.Info("No vendor folder detected, not using dependency manager to import package", "package", pkgName)
|
||||
// getCmd = exec.Command(c.GoCmd, "get", "-u", pkgName)
|
||||
//}
|
||||
//
|
||||
//utils.CmdInit(getCmd, c.AppPath)
|
||||
//utils.Logger.Info("Go get command ", "exec", getCmd.Path, "dir", getCmd.Dir, "args", getCmd.Args, "env", getCmd.Env, "package", pkgName)
|
||||
//output, err := getCmd.CombinedOutput()
|
||||
//if err != nil {
|
||||
// fmt.Printf("Error stack %v\n", logger.NewCallStack())
|
||||
// utils.Logger.Error("Failed to import package", "error", err, "gopath", build.Default.GOPATH, "GO-ROOT", build.Default.GOROOT, "output", string(output))
|
||||
//}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// lookup and set Go related variables
|
||||
func (c *CommandConfig) InitGoPaths() {
|
||||
func (c *CommandConfig) InitGoPathsOld() {
|
||||
utils.Logger.Info("InitGoPaths")
|
||||
// lookup go path
|
||||
c.GoPath = build.Default.GOPATH
|
||||
@@ -276,17 +377,21 @@ func (c *CommandConfig) InitGoPaths() {
|
||||
}
|
||||
}
|
||||
|
||||
utils.Logger.Info("Source root", "path", c.SrcRoot, "cwd", workingDir, "gopath", c.GoPath, "bestpath", bestpath)
|
||||
utils.Logger.Info("Source root", "path", c.SrcRoot, "cwd", workingDir, "gopath", c.GoPath, "bestpath",bestpath)
|
||||
if len(c.SrcRoot) == 0 && len(bestpath) > 0 {
|
||||
c.SrcRoot = bestpath
|
||||
}
|
||||
|
||||
// If source root is empty and this isn't a version then skip it
|
||||
if len(c.SrcRoot) == 0 {
|
||||
if c.Index != VERSION {
|
||||
utils.Logger.Fatal("Abort: could not create a Revel application outside of GOPATH.")
|
||||
if c.Index == NEW {
|
||||
c.SrcRoot = c.New.ImportPath
|
||||
} else {
|
||||
if c.Index != VERSION {
|
||||
utils.Logger.Fatal("Abort: could not create a Revel application outside of GOPATH.")
|
||||
}
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// set go src path
|
||||
@@ -299,14 +404,14 @@ func (c *CommandConfig) InitGoPaths() {
|
||||
// Sets the versions on the command config
|
||||
func (c *CommandConfig) SetVersions() (err error) {
|
||||
c.CommandVersion, _ = ParseVersion(cmd.Version)
|
||||
_, revelPath, err := utils.FindSrcPaths(c.ImportPath, RevelImportPath, c.PackageResolver)
|
||||
pathMap, err := utils.FindSrcPaths(c.AppPath, []string{RevelImportPath}, c.PackageResolver)
|
||||
if err == nil {
|
||||
utils.Logger.Info("Fullpath to revel", "dir", revelPath)
|
||||
utils.Logger.Info("Fullpath to revel", "dir", pathMap[RevelImportPath])
|
||||
fset := token.NewFileSet() // positions are relative to fset
|
||||
|
||||
versionData, err := ioutil.ReadFile(filepath.Join(revelPath, RevelImportPath, "version.go"))
|
||||
versionData, err := ioutil.ReadFile(filepath.Join(pathMap[RevelImportPath], "version.go"))
|
||||
if err != nil {
|
||||
utils.Logger.Error("Failed to find Revel version:", "error", err, "path", revelPath)
|
||||
utils.Logger.Error("Failed to find Revel version:", "error", err, "path", pathMap[RevelImportPath])
|
||||
}
|
||||
|
||||
// Parse src but stop after processing the imports.
|
||||
|
||||
Reference in New Issue
Block a user