Updated so revel new works and revel run starts parsing the source.

This commit is contained in:
notzippy@gmail.com
2020-04-25 09:18:29 -07:00
parent acb8fb631b
commit 9a9511d28f
6 changed files with 97 additions and 101 deletions

View File

@@ -81,7 +81,7 @@ func (rl *RevelLogger) SetStackDepth(amount int) MultiLogger {
// Create a new logger // Create a new logger
func New(ctx ...interface{}) MultiLogger { func New(ctx ...interface{}) MultiLogger {
r := &RevelLogger{Logger: log15.New(ctx...)} r := &RevelLogger{Logger: log15.New(ctx...)}
r.SetStackDepth(1) r.SetStackDepth(0)
return r return r
} }

View File

@@ -14,7 +14,6 @@ import (
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strings" "strings"
"runtime"
) )
// The constants // The constants
@@ -54,7 +53,7 @@ type (
ImportPath string `short:"a" long:"application-path" description:"Path to application folder" required:"false"` 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"` 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"` 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"` NotVendored bool `short:"V" long:"vendor" description:"True if project should not be configured with a go.mod"`
Run bool `short:"r" long:"run" description:"True if you want to run the application right away"` Run bool `short:"r" long:"run" description:"True if you want to run the application right away"`
} `command:"new"` } `command:"new"`
// The build command // The build command
@@ -105,19 +104,19 @@ func (c *CommandConfig) UpdateImportPath() error {
importPath = c.New.ImportPath importPath = c.New.ImportPath
case RUN: case RUN:
importPath = c.Run.ImportPath importPath = c.Run.ImportPath
c.Vendored = utils.Exists(filepath.Join(importPath,"src","go.mod")) c.Vendored = utils.Exists(filepath.Join(importPath,"go.mod"))
case BUILD: case BUILD:
importPath = c.Build.ImportPath importPath = c.Build.ImportPath
c.Vendored = utils.Exists(filepath.Join(importPath,"src","go.mod")) c.Vendored = utils.Exists(filepath.Join(importPath,"go.mod"))
case PACKAGE: case PACKAGE:
importPath = c.Package.ImportPath importPath = c.Package.ImportPath
c.Vendored = utils.Exists(filepath.Join(importPath,"src","go.mod")) c.Vendored = utils.Exists(filepath.Join(importPath,"go.mod"))
case CLEAN: case CLEAN:
importPath = c.Clean.ImportPath importPath = c.Clean.ImportPath
c.Vendored = utils.Exists(filepath.Join(importPath,"src","go.mod")) c.Vendored = utils.Exists(filepath.Join(importPath,"go.mod"))
case TEST: case TEST:
importPath = c.Test.ImportPath importPath = c.Test.ImportPath
c.Vendored = utils.Exists(filepath.Join(importPath,"src","go.mod")) c.Vendored = utils.Exists(filepath.Join(importPath,"go.mod"))
case VERSION: case VERSION:
importPath = c.Version.ImportPath importPath = c.Version.ImportPath
required = false required = false
@@ -180,7 +179,8 @@ func (c *CommandConfig) UpdateImportPath() error {
} }
func (c *CommandConfig) initAppFolder() (err error) { func (c *CommandConfig) initAppFolder() (err error) {
utils.Logger.Info("initAppFolder") utils.Logger.Info("initAppFolder","vendored", c.Vendored)
// check for go executable // check for go executable
c.GoCmd, err = exec.LookPath("go") c.GoCmd, err = exec.LookPath("go")
if err != nil { if err != nil {
@@ -227,7 +227,7 @@ func (c *CommandConfig) initAppFolder() (err error) {
} }
} }
utils.Logger.Fatal("Trying to set path based on gopath") utils.Logger.Debug("Trying to set path based on gopath")
// lookup go path // lookup go path
c.GoPath = build.Default.GOPATH c.GoPath = build.Default.GOPATH
if c.GoPath == "" { if c.GoPath == "" {
@@ -275,31 +275,22 @@ func (c *CommandConfig) initAppFolder() (err error) {
} }
// set go src path // set go src path
c.SrcRoot = filepath.Join(c.SrcRoot, "src") if c.Vendored {
c.AppPath = c.SrcRoot
c.AppPath = filepath.Join(c.SrcRoot, filepath.FromSlash(c.ImportPath)) } else {
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) utils.Logger.Info("Set application path", "path", c.AppPath)
return nil return nil
} }
// Used to initialize the package resolver // Used to initialize the package resolver
func (c *CommandConfig) InitPackageResolver() { func (c *CommandConfig) InitPackageResolver() {
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) utils.Logger.Info("InitPackageResolver", "useVendor", c.Vendored, "path", c.AppPath)
if c.Vendored {
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(`Go version 1.11 or newer is required to build`)
}
}
// This should get called when needed // This should get called when needed
c.PackageResolver = func(pkgName string) error { c.PackageResolver = func(pkgName string) error {
//useVendor := utils.DirExists(filepath.Join(c.AppPath, "vendor")) //useVendor := utils.DirExists(filepath.Join(c.AppPath, "vendor"))

View File

@@ -1,14 +1,14 @@
package parser2 package parser2
import ( import (
"go/ast" //"go/ast"
"go/token" //"go/token"
"github.com/revel/cmd/model" "github.com/revel/cmd/model"
"golang.org/x/tools/go/packages" "golang.org/x/tools/go/packages"
"github.com/revel/cmd/utils" "github.com/revel/cmd/utils"
"github.com/pkg/errors" "errors"
"golang.org/x/tools/go/ssa/interp/testdata/src/strings"
) )
func ProcessSource(revelContainer *model.RevelContainer) (sourceInfo *model.SourceInfo, compileError error) { func ProcessSource(revelContainer *model.RevelContainer) (sourceInfo *model.SourceInfo, compileError error) {
utils.Logger.Info("ProcessSource") utils.Logger.Info("ProcessSource")
@@ -43,13 +43,13 @@ func ProcessSource(revelContainer *model.RevelContainer) (sourceInfo *model.Sour
// utils.Logger.Info("File","name",t.Name) // utils.Logger.Info("File","name",t.Name)
//} //}
println("package typoe fouhnd ",p.Types.Name()) println("package typoe fouhnd ",p.Types.Name())
imports := map[string]string{} //imports := map[string]string{}
for _,s := range p.Syntax { for _,s := range p.Syntax {
println("File ",s.Name.Name ) println("File ",s.Name.Name )
for _, decl := range file.Decls { //for _, decl := range s.Decls {
if decl.Tok == token.IMPORT { // if decl.Tok == token.IMPORT {
} // }
} }
} }
//p.Fset.Iterate(func(file *token.File) bool{ //p.Fset.Iterate(func(file *token.File) bool{
@@ -62,9 +62,9 @@ func ProcessSource(revelContainer *model.RevelContainer) (sourceInfo *model.Sour
// counter ++ // counter ++
// return true // return true
//}) //})
} //}
err = errors.New("Incompleted") compileError = errors.New("Incompleted")
println("*******************", counter) println("*******************", counter)
utils.Logger.Panic("Not implemented") utils.Logger.Panic("Not implemented")
return return
@@ -73,51 +73,51 @@ func ProcessSource(revelContainer *model.RevelContainer) (sourceInfo *model.Sour
// Add imports to the map from the source dir // Add imports to the map from the source dir
func addImports(imports map[string]string, decl ast.Decl, srcDir string) { //func addImports(imports map[string]string, decl ast.Decl, srcDir string) {
genDecl, ok := decl.(*ast.GenDecl) // genDecl, ok := decl.(*ast.GenDecl)
if !ok { // if !ok {
return // return
} // }
//
if genDecl.Tok != token.IMPORT { // if genDecl.Tok != token.IMPORT {
return // return
} // }
//
for _, spec := range genDecl.Specs { // for _, spec := range genDecl.Specs {
importSpec := spec.(*ast.ImportSpec) // importSpec := spec.(*ast.ImportSpec)
var pkgAlias string // var pkgAlias string
if importSpec.Name != nil { // if importSpec.Name != nil {
pkgAlias = importSpec.Name.Name // pkgAlias = importSpec.Name.Name
if pkgAlias == "_" { // if pkgAlias == "_" {
continue // continue
} // }
} // }
quotedPath := importSpec.Path.Value // e.g. "\"sample/app/models\"" // quotedPath := importSpec.Path.Value // e.g. "\"sample/app/models\""
fullPath := quotedPath[1 : len(quotedPath)-1] // Remove the quotes // fullPath := quotedPath[1 : len(quotedPath)-1] // Remove the quotes
//
// If the package was not aliased (common case), we have to import it // // If the package was not aliased (common case), we have to import it
// to see what the package name is. // // to see what the package name is.
// TODO: Can improve performance here a lot: // // TODO: Can improve performance here a lot:
// 1. Do not import everything over and over again. Keep a cache. // // 1. Do not import everything over and over again. Keep a cache.
// 2. Exempt the standard library; their directories always match the package name. // // 2. Exempt the standard library; their directories always match the package name.
// 3. Can use build.FindOnly and then use parser.ParseDir with mode PackageClauseOnly // // 3. Can use build.FindOnly and then use parser.ParseDir with mode PackageClauseOnly
if pkgAlias == "" { // if pkgAlias == "" {
//
utils.Logger.Debug("Reading from build", "path", fullPath, "srcPath", srcDir, "gopath", build.Default.GOPATH) // utils.Logger.Debug("Reading from build", "path", fullPath, "srcPath", srcDir, "gopath", build.Default.GOPATH)
pkg, err := build.Import(fullPath, srcDir, 0) // pkg, err := build.Import(fullPath, srcDir, 0)
if err != nil { // if err != nil {
// We expect this to happen for apps using reverse routing (since we // // We expect this to happen for apps using reverse routing (since we
// have not yet generated the routes). Don't log that. // // have not yet generated the routes). Don't log that.
if !strings.HasSuffix(fullPath, "/app/routes") { // if !strings.HasSuffix(fullPath, "/app/routes") {
utils.Logger.Warn("Could not find import:", "path", fullPath, "srcPath", srcDir, "error", err) // utils.Logger.Warn("Could not find import:", "path", fullPath, "srcPath", srcDir, "error", err)
} // }
continue // continue
} else { // } else {
utils.Logger.Debug("Found package in dir", "dir", pkg.Dir, "name", pkg.ImportPath) // utils.Logger.Debug("Found package in dir", "dir", pkg.Dir, "name", pkg.ImportPath)
} // }
pkgAlias = pkg.Name // pkgAlias = pkg.Name
} // }
//
imports[pkgAlias] = fullPath // imports[pkgAlias] = fullPath
} // }
} //}

View File

@@ -46,14 +46,23 @@ func init() {
// Called when unable to parse the command line automatically and assumes an old launch // Called when unable to parse the command line automatically and assumes an old launch
func updateNewConfig(c *model.CommandConfig, args []string) bool { func updateNewConfig(c *model.CommandConfig, args []string) bool {
c.Index = model.NEW c.Index = model.NEW
if len(c.New.Package)>0 {
c.New.NotVendored = false
}
c.Vendored = !c.New.NotVendored
if len(args) == 0 { if len(args) == 0 {
fmt.Fprintf(os.Stderr, cmdNew.Long) if len(c.New.ImportPath)==0 {
return false fmt.Fprintf(os.Stderr, cmdNew.Long)
return false
}
return true
} }
c.New.ImportPath = args[0] c.New.ImportPath = args[0]
if len(args) > 1 { if len(args) > 1 {
c.New.SkeletonPath = args[1] c.New.SkeletonPath = args[1]
} }
return true return true
} }
@@ -76,10 +85,6 @@ func newApp(c *model.CommandConfig) (err error) {
return utils.NewBuildError("Abort: Unable to create app path.", "path", c.AppPath) return utils.NewBuildError("Abort: Unable to create app path.", "path", c.AppPath)
} }
if len(c.New.Package)>0 {
c.New.Vendored = true
}
// checking and setting application // checking and setting application
if err = setApplicationPath(c); err != nil { if err = setApplicationPath(c); err != nil {
return err return err
@@ -93,8 +98,9 @@ func newApp(c *model.CommandConfig) (err error) {
return return
} }
// Rerun the dep tool if vendored // Run the vendor tool if needed
if c.New.Vendored { println("********** here",c.Vendored)
if c.Vendored {
if err=createModVendor(c); err!=nil { if err=createModVendor(c); err!=nil {
return return
} }
@@ -199,7 +205,7 @@ func setApplicationPath(c *model.CommandConfig) (err error) {
} }
// If we are running a vendored version of Revel we do not need to check for it. // If we are running a vendored version of Revel we do not need to check for it.
if !c.New.Vendored { if !c.Vendored {
_, err = build.Import(model.RevelImportPath, "", build.FindOnly) _, err = build.Import(model.RevelImportPath, "", build.FindOnly)
if err != nil { if err != nil {
//// Go get the revel project //// Go get the revel project

View File

@@ -142,13 +142,10 @@ func ParseArgs(c *model.CommandConfig, parser *flags.Parser, args []string) (err
} }
} }
if len(extraArgs) > 0 { if !Commands[c.Index].UpdateConfig(c, extraArgs) {
utils.Logger.Info("Found additional arguements, setting them") buffer := &bytes.Buffer{}
if !Commands[c.Index].UpdateConfig(c, extraArgs) { parser.WriteHelp(buffer)
buffer := &bytes.Buffer{} err = fmt.Errorf("Invalid command line arguements %v\n%s", extraArgs, buffer.String())
parser.WriteHelp(buffer)
err = fmt.Errorf("Invalid command line arguements %v\n%s", extraArgs, buffer.String())
}
} }
return return

View File

@@ -106,7 +106,9 @@ func updateRunConfig(c *model.CommandConfig, args []string) bool {
} }
case 0: case 0:
// Attempt to set the import path to the current working director. // Attempt to set the import path to the current working director.
c.Run.ImportPath,_ = os.Getwd() if c.Run.ImportPath=="" {
c.Run.ImportPath, _ = os.Getwd()
}
} }
c.Index = model.RUN c.Index = model.RUN
return true return true