mirror of
https://github.com/kevin-DL/revel-cmd.git
synced 2026-01-11 18:54:31 +00:00
Updated so revel new works and revel run starts parsing the source.
This commit is contained in:
@@ -81,7 +81,7 @@ func (rl *RevelLogger) SetStackDepth(amount int) MultiLogger {
|
||||
// Create a new logger
|
||||
func New(ctx ...interface{}) MultiLogger {
|
||||
r := &RevelLogger{Logger: log15.New(ctx...)}
|
||||
r.SetStackDepth(1)
|
||||
r.SetStackDepth(0)
|
||||
return r
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,6 @@ import (
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
// The constants
|
||||
@@ -54,7 +53,7 @@ type (
|
||||
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"`
|
||||
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"`
|
||||
} `command:"new"`
|
||||
// The build command
|
||||
@@ -105,19 +104,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"))
|
||||
c.Vendored = utils.Exists(filepath.Join(importPath,"go.mod"))
|
||||
case BUILD:
|
||||
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:
|
||||
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:
|
||||
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:
|
||||
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:
|
||||
importPath = c.Version.ImportPath
|
||||
required = false
|
||||
@@ -180,7 +179,8 @@ func (c *CommandConfig) UpdateImportPath() error {
|
||||
}
|
||||
|
||||
func (c *CommandConfig) initAppFolder() (err error) {
|
||||
utils.Logger.Info("initAppFolder")
|
||||
utils.Logger.Info("initAppFolder","vendored", c.Vendored)
|
||||
|
||||
// check for go executable
|
||||
c.GoCmd, err = exec.LookPath("go")
|
||||
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
|
||||
c.GoPath = build.Default.GOPATH
|
||||
if c.GoPath == "" {
|
||||
@@ -275,31 +275,22 @@ func (c *CommandConfig) initAppFolder() (err error) {
|
||||
}
|
||||
|
||||
// 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)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Used to initialize the package resolver
|
||||
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)
|
||||
|
||||
|
||||
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
|
||||
c.PackageResolver = func(pkgName string) error {
|
||||
//useVendor := utils.DirExists(filepath.Join(c.AppPath, "vendor"))
|
||||
|
||||
116
parser2/read.go
116
parser2/read.go
@@ -1,14 +1,14 @@
|
||||
package parser2
|
||||
|
||||
import (
|
||||
"go/ast"
|
||||
"go/token"
|
||||
//"go/ast"
|
||||
//"go/token"
|
||||
|
||||
"github.com/revel/cmd/model"
|
||||
"golang.org/x/tools/go/packages"
|
||||
"github.com/revel/cmd/utils"
|
||||
"github.com/pkg/errors"
|
||||
"golang.org/x/tools/go/ssa/interp/testdata/src/strings"
|
||||
"errors"
|
||||
|
||||
)
|
||||
func ProcessSource(revelContainer *model.RevelContainer) (sourceInfo *model.SourceInfo, compileError error) {
|
||||
utils.Logger.Info("ProcessSource")
|
||||
@@ -43,13 +43,13 @@ func ProcessSource(revelContainer *model.RevelContainer) (sourceInfo *model.Sour
|
||||
// utils.Logger.Info("File","name",t.Name)
|
||||
//}
|
||||
println("package typoe fouhnd ",p.Types.Name())
|
||||
imports := map[string]string{}
|
||||
//imports := map[string]string{}
|
||||
|
||||
for _,s := range p.Syntax {
|
||||
println("File ",s.Name.Name )
|
||||
for _, decl := range file.Decls {
|
||||
if decl.Tok == token.IMPORT {
|
||||
}
|
||||
//for _, decl := range s.Decls {
|
||||
// if decl.Tok == token.IMPORT {
|
||||
// }
|
||||
}
|
||||
}
|
||||
//p.Fset.Iterate(func(file *token.File) bool{
|
||||
@@ -62,9 +62,9 @@ func ProcessSource(revelContainer *model.RevelContainer) (sourceInfo *model.Sour
|
||||
// counter ++
|
||||
// return true
|
||||
//})
|
||||
}
|
||||
//}
|
||||
|
||||
err = errors.New("Incompleted")
|
||||
compileError = errors.New("Incompleted")
|
||||
println("*******************", counter)
|
||||
utils.Logger.Panic("Not implemented")
|
||||
return
|
||||
@@ -73,51 +73,51 @@ func ProcessSource(revelContainer *model.RevelContainer) (sourceInfo *model.Sour
|
||||
|
||||
|
||||
// Add imports to the map from the source dir
|
||||
func addImports(imports map[string]string, decl ast.Decl, srcDir string) {
|
||||
genDecl, ok := decl.(*ast.GenDecl)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
if genDecl.Tok != token.IMPORT {
|
||||
return
|
||||
}
|
||||
|
||||
for _, spec := range genDecl.Specs {
|
||||
importSpec := spec.(*ast.ImportSpec)
|
||||
var pkgAlias string
|
||||
if importSpec.Name != nil {
|
||||
pkgAlias = importSpec.Name.Name
|
||||
if pkgAlias == "_" {
|
||||
continue
|
||||
}
|
||||
}
|
||||
quotedPath := importSpec.Path.Value // e.g. "\"sample/app/models\""
|
||||
fullPath := quotedPath[1 : len(quotedPath)-1] // Remove the quotes
|
||||
|
||||
// If the package was not aliased (common case), we have to import it
|
||||
// to see what the package name is.
|
||||
// TODO: Can improve performance here a lot:
|
||||
// 1. Do not import everything over and over again. Keep a cache.
|
||||
// 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
|
||||
if pkgAlias == "" {
|
||||
|
||||
utils.Logger.Debug("Reading from build", "path", fullPath, "srcPath", srcDir, "gopath", build.Default.GOPATH)
|
||||
pkg, err := build.Import(fullPath, srcDir, 0)
|
||||
if err != nil {
|
||||
// We expect this to happen for apps using reverse routing (since we
|
||||
// have not yet generated the routes). Don't log that.
|
||||
if !strings.HasSuffix(fullPath, "/app/routes") {
|
||||
utils.Logger.Warn("Could not find import:", "path", fullPath, "srcPath", srcDir, "error", err)
|
||||
}
|
||||
continue
|
||||
} else {
|
||||
utils.Logger.Debug("Found package in dir", "dir", pkg.Dir, "name", pkg.ImportPath)
|
||||
}
|
||||
pkgAlias = pkg.Name
|
||||
}
|
||||
|
||||
imports[pkgAlias] = fullPath
|
||||
}
|
||||
}
|
||||
//func addImports(imports map[string]string, decl ast.Decl, srcDir string) {
|
||||
// genDecl, ok := decl.(*ast.GenDecl)
|
||||
// if !ok {
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// if genDecl.Tok != token.IMPORT {
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// for _, spec := range genDecl.Specs {
|
||||
// importSpec := spec.(*ast.ImportSpec)
|
||||
// var pkgAlias string
|
||||
// if importSpec.Name != nil {
|
||||
// pkgAlias = importSpec.Name.Name
|
||||
// if pkgAlias == "_" {
|
||||
// continue
|
||||
// }
|
||||
// }
|
||||
// quotedPath := importSpec.Path.Value // e.g. "\"sample/app/models\""
|
||||
// fullPath := quotedPath[1 : len(quotedPath)-1] // Remove the quotes
|
||||
//
|
||||
// // If the package was not aliased (common case), we have to import it
|
||||
// // to see what the package name is.
|
||||
// // TODO: Can improve performance here a lot:
|
||||
// // 1. Do not import everything over and over again. Keep a cache.
|
||||
// // 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
|
||||
// if pkgAlias == "" {
|
||||
//
|
||||
// utils.Logger.Debug("Reading from build", "path", fullPath, "srcPath", srcDir, "gopath", build.Default.GOPATH)
|
||||
// pkg, err := build.Import(fullPath, srcDir, 0)
|
||||
// if err != nil {
|
||||
// // We expect this to happen for apps using reverse routing (since we
|
||||
// // have not yet generated the routes). Don't log that.
|
||||
// if !strings.HasSuffix(fullPath, "/app/routes") {
|
||||
// utils.Logger.Warn("Could not find import:", "path", fullPath, "srcPath", srcDir, "error", err)
|
||||
// }
|
||||
// continue
|
||||
// } else {
|
||||
// utils.Logger.Debug("Found package in dir", "dir", pkg.Dir, "name", pkg.ImportPath)
|
||||
// }
|
||||
// pkgAlias = pkg.Name
|
||||
// }
|
||||
//
|
||||
// imports[pkgAlias] = fullPath
|
||||
// }
|
||||
//}
|
||||
24
revel/new.go
24
revel/new.go
@@ -46,14 +46,23 @@ func init() {
|
||||
// Called when unable to parse the command line automatically and assumes an old launch
|
||||
func updateNewConfig(c *model.CommandConfig, args []string) bool {
|
||||
c.Index = model.NEW
|
||||
if len(c.New.Package)>0 {
|
||||
c.New.NotVendored = false
|
||||
}
|
||||
c.Vendored = !c.New.NotVendored
|
||||
|
||||
if len(args) == 0 {
|
||||
fmt.Fprintf(os.Stderr, cmdNew.Long)
|
||||
return false
|
||||
if len(c.New.ImportPath)==0 {
|
||||
fmt.Fprintf(os.Stderr, cmdNew.Long)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
c.New.ImportPath = args[0]
|
||||
if len(args) > 1 {
|
||||
c.New.SkeletonPath = args[1]
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
if len(c.New.Package)>0 {
|
||||
c.New.Vendored = true
|
||||
}
|
||||
|
||||
// checking and setting application
|
||||
if err = setApplicationPath(c); err != nil {
|
||||
return err
|
||||
@@ -93,8 +98,9 @@ func newApp(c *model.CommandConfig) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// Rerun the dep tool if vendored
|
||||
if c.New.Vendored {
|
||||
// Run the vendor tool if needed
|
||||
println("********** here",c.Vendored)
|
||||
if c.Vendored {
|
||||
if err=createModVendor(c); err!=nil {
|
||||
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 !c.New.Vendored {
|
||||
if !c.Vendored {
|
||||
_, err = build.Import(model.RevelImportPath, "", build.FindOnly)
|
||||
if err != nil {
|
||||
//// Go get the revel project
|
||||
|
||||
@@ -142,13 +142,10 @@ func ParseArgs(c *model.CommandConfig, parser *flags.Parser, args []string) (err
|
||||
}
|
||||
}
|
||||
|
||||
if len(extraArgs) > 0 {
|
||||
utils.Logger.Info("Found additional arguements, setting them")
|
||||
if !Commands[c.Index].UpdateConfig(c, extraArgs) {
|
||||
buffer := &bytes.Buffer{}
|
||||
parser.WriteHelp(buffer)
|
||||
err = fmt.Errorf("Invalid command line arguements %v\n%s", extraArgs, buffer.String())
|
||||
}
|
||||
if !Commands[c.Index].UpdateConfig(c, extraArgs) {
|
||||
buffer := &bytes.Buffer{}
|
||||
parser.WriteHelp(buffer)
|
||||
err = fmt.Errorf("Invalid command line arguements %v\n%s", extraArgs, buffer.String())
|
||||
}
|
||||
|
||||
return
|
||||
|
||||
@@ -106,7 +106,9 @@ func updateRunConfig(c *model.CommandConfig, args []string) bool {
|
||||
}
|
||||
case 0:
|
||||
// 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
|
||||
return true
|
||||
|
||||
Reference in New Issue
Block a user