Skip to content

Commit 28ac65f

Browse files
authored
Merge pull request #185 from notzippy/go-mod
Go mod updates
2 parents 60b88a4 + 5070fb8 commit 28ac65f

File tree

12 files changed

+263
-160
lines changed

12 files changed

+263
-160
lines changed

.travis.yml

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,25 @@ script:
4747
#- revel package my/testapp prod
4848

4949
# Ensure the new-app flow works (plus the other commands).
50-
- revel new --gomod-flags "edit -replace=github.com/revel/revel=github.com/revel/revel@develop" -a my/testapp2 -v
51-
- revel test --gomod-flags "edit -replace=github.com/revel/revel=github.com/revel/revel@develop" -a my/testapp2 -v
52-
- revel clean --gomod-flags "edit -replace=github.com/revel/revel=github.com/revel/revel@develop" -a my/testapp2 -v
53-
- revel build --gomod-flags "edit -replace=github.com/revel/revel=github.com/revel/revel@develop" -a my/testapp2 -v -t build/testapp2
54-
- revel build --gomod-flags "edit -replace=github.com/revel/revel=github.com/revel/revel@develop" -a my/testapp2 -v -t build/testapp2 -m prod
55-
- revel package --gomod-flags "edit -replace=github.com/revel/revel=github.com/revel/revel@develop" -a my/testapp2 -v
56-
- revel package --gomod-flags "edit -replace=github.com/revel/revel=github.com/revel/revel@develop" -a my/testapp2 -v -m prod
50+
- revel new --gomod-flags "edit -replace=github.com/revel/revel=github.com/revel/revel@$REVEL_BRANCH" -a my/testapp2 --package revelframework.com -v
51+
- revel test --gomod-flags "edit -replace=github.com/revel/revel=github.com/revel/revel@$REVEL_BRANCH" -a my/testapp2 -v
52+
- revel clean --gomod-flags "edit -replace=github.com/revel/revel=github.com/revel/revel@$REVEL_BRANCH" -a my/testapp2 -v
53+
- revel build --gomod-flags "edit -replace=github.com/revel/revel=github.com/revel/revel@$REVEL_BRANCH" -a my/testapp2 -v -t build/testapp2
54+
- revel build --gomod-flags "edit -replace=github.com/revel/revel=github.com/revel/revel@$REVEL_BRANCH" -a my/testapp2 -v -t build/testapp2 -m prod
55+
- revel package --gomod-flags "edit -replace=github.com/revel/revel=github.com/revel/revel@$REVEL_BRANCH" -a my/testapp2 -v
56+
- revel package --gomod-flags "edit -replace=github.com/revel/revel=github.com/revel/revel@$REVEL_BRANCH" -a my/testapp2 -v -m prod
5757

58+
- export INITIALWD=$PWD
5859
# Check build works with no-vendor flag
5960
- cd $GOPATH
6061
- export GO111MODULE=auto
61-
- revel new -a my/testapp2 --no-vendor
62-
- revel test -a my/testapp2
62+
- revel new -a my/testapp2 --no-vendor -v
63+
- revel test -a my/testapp2 -v
64+
65+
# Check non verbose build, outside of GO path
66+
- cd $INITIALWD
67+
- revel new --gomod-flags "edit -replace=github.com/revel/revel=github.com/revel/revel@$REVEL_BRANCH" -a my/testapp3 --package revelframework.com
68+
- revel test --gomod-flags "edit -replace=github.com/revel/revel=github.com/revel/revel@$REVEL_BRANCH" -a my/testapp3
6369

6470
matrix:
6571
allow_failures:

harness/app.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func (cmd AppCmd) Kill() {
106106
if cmd.Cmd != nil && (cmd.ProcessState == nil || !cmd.ProcessState.Exited()) {
107107
// Windows appears to send the kill to all threads, shutting down the
108108
// server before this can, this check will ensure the process is still running
109-
if _, err := os.FindProcess(int(cmd.Process.Pid));err!=nil {
109+
if _, err := os.FindProcess(int(cmd.Process.Pid)); err != nil {
110110
// Server has already exited
111111
utils.Logger.Info("Server not running revel server pid", "pid", cmd.Process.Pid)
112112
return
@@ -143,9 +143,9 @@ func (cmd AppCmd) Kill() {
143143
}
144144

145145
if err != nil {
146-
utils.Logger.Error(
147-
"Revel app failed to kill process.",
148-
"processid", cmd.Process.Pid,"error",err,
146+
utils.Logger.Info(
147+
"Revel app already exited.",
148+
"processid", cmd.Process.Pid, "error", err,
149149
"killerror", cmd.Process.Kill())
150150
return
151151
}

harness/harness.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package harness
1616
import (
1717
"crypto/tls"
1818
"fmt"
19+
"time"
1920
"go/build"
2021
"io"
2122
"net"
@@ -56,6 +57,8 @@ type Harness struct {
5657
paths *model.RevelContainer // The Revel container
5758
config *model.CommandConfig // The configuration
5859
runMode string // The runmode the harness is running in
60+
isError bool // True if harness is in error state
61+
ranOnce bool // True app compiled once
5962
}
6063

6164
func (h *Harness) renderError(iw http.ResponseWriter, ir *http.Request, err error) {
@@ -202,6 +205,21 @@ func NewHarness(c *model.CommandConfig, paths *model.RevelContainer, runMode str
202205
// Refresh method rebuilds the Revel application and run it on the given port.
203206
// called by the watcher
204207
func (h *Harness) Refresh() (err *utils.SourceError) {
208+
t := time.Now();
209+
fmt.Println("Changed detected, recompiling")
210+
err = h.refresh()
211+
if err!=nil && !h.ranOnce && h.useProxy {
212+
addr := fmt.Sprintf("%s:%d", h.paths.HTTPAddr, h.paths.HTTPPort)
213+
214+
fmt.Printf("\nError compiling code, to view error details see proxy running on http://%s\n\n",addr)
215+
}
216+
217+
h.ranOnce = true
218+
fmt.Printf("\nTime to recompile %s\n",time.Now().Sub(t).String())
219+
return
220+
}
221+
222+
func (h *Harness) refresh() (err *utils.SourceError) {
205223
// Allow only one thread to rebuild the process
206224
// If multiple requests to rebuild are queued only the last one is executed on
207225
// So before a build is started we wait for a second to determine if
@@ -281,7 +299,8 @@ func (h *Harness) Run() {
281299
paths = append(paths, h.paths.CodePaths...)
282300
h.watcher = watcher.NewWatcher(h.paths, false)
283301
h.watcher.Listen(h, paths...)
284-
h.watcher.Notify()
302+
go h.Refresh()
303+
// h.watcher.Notify()
285304

286305
if h.useProxy {
287306
go func() {
@@ -292,6 +311,7 @@ func (h *Harness) Run() {
292311
addr := fmt.Sprintf("%s:%d", h.paths.HTTPAddr, h.paths.HTTPPort)
293312
utils.Logger.Infof("Proxy server is listening on %s", addr)
294313

314+
295315
var err error
296316
if h.paths.HTTPSsl {
297317
err = http.ListenAndServeTLS(
@@ -308,13 +328,15 @@ func (h *Harness) Run() {
308328
}()
309329

310330
}
311-
// Kill the app on signal.
331+
332+
// Make a new channel to listen for the interrupt event
312333
ch := make(chan os.Signal)
313334
signal.Notify(ch, os.Interrupt, os.Kill)
314-
<-ch
335+
// Kill the app and exit
315336
if h.app != nil {
316337
h.app.Kill()
317338
}
339+
<-ch
318340
os.Exit(1)
319341
}
320342

model/command_config.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,10 @@ func (c *CommandConfig) initAppFolder() (err error) {
174174

175175
// Use app folder to read the go.mod if it exists and extract the package information
176176
goModFile := filepath.Join(appFolder, "go.mod")
177+
utils.Logger.Info("Checking gomod, extracting from file", "path", goModFile,"exists", utils.Exists(goModFile))
177178
if utils.Exists(goModFile) {
178179
c.Vendored = true
180+
utils.Logger.Info("Found go mod, extracting from file", "path", goModFile)
179181
file, err := ioutil.ReadFile(goModFile)
180182
if err != nil {
181183
return err
@@ -220,7 +222,7 @@ func (c *CommandConfig) initAppFolder() (err error) {
220222
c.AppPath = appFolder
221223
}
222224

223-
utils.Logger.Info("Set application path", "path", c.AppPath)
225+
utils.Logger.Info("Set application path", "path", c.AppPath, "vendored",c.Vendored, "importpath",c.ImportPath)
224226
return nil
225227
}
226228

@@ -233,6 +235,7 @@ func (c *CommandConfig) InitPackageResolver() {
233235
c.PackageResolver = func(pkgName string) error {
234236
utils.Logger.Info("Request for package ", "package", pkgName, "use vendor", c.Vendored)
235237
var getCmd *exec.Cmd
238+
print("Downloading related packages ...")
236239
if c.Vendored {
237240
getCmd = exec.Command(c.GoCmd, "mod", "tidy")
238241
} else {
@@ -246,6 +249,7 @@ func (c *CommandConfig) InitPackageResolver() {
246249
if err != nil {
247250
utils.Logger.Error("Failed to import package", "error", err, "gopath", build.Default.GOPATH, "GO-ROOT", build.Default.GOROOT, "output", string(output))
248251
}
252+
println(" completed.")
249253

250254
return nil
251255
}

model/source_info.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ func (s *SourceInfo) TypesThatEmbed(targetType, packageFilter string) (filtered
5757

5858
// Look through the embedded types to see if the current type is among them.
5959
for _, embeddedType := range spec.EmbeddedTypes {
60-
6160
// If so, add this type's simple name to the nodeQueue, and its spec to
6261
// the filtered list.
6362
if typeSimpleName == embeddedType.String() {
@@ -111,6 +110,7 @@ func (s *SourceInfo) TypesThatEmbed(targetType, packageFilter string) (filtered
111110
// ControllerSpecs returns the all the controllers that embeds
112111
// `revel.Controller`
113112
func (s *SourceInfo) ControllerSpecs() []*TypeInfo {
113+
utils.Logger.Info("Scanning controller specifications for types ","typePath",RevelImportPath + ".Controller", "speclen",len(s.controllerSpecs))
114114
if s.controllerSpecs == nil {
115115
s.controllerSpecs = s.TypesThatEmbed(RevelImportPath + ".Controller", "controllers")
116116
}

parser2/source_info_processor.go

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"go/token"
99
"strings"
1010
"path/filepath"
11+
"github.com/revel/cmd/logger"
1112
)
1213

1314
type (
@@ -31,14 +32,21 @@ func (s *SourceInfoProcessor) processPackage(p *packages.Package) (sourceInfo *m
3132
strings.Contains(p.PkgPath, "/tests/")
3233
methodMap = map[string][]*model.MethodSpec{}
3334
)
35+
localImportMap := map[string]string{}
36+
log := s.sourceProcessor.log.New("package", p.PkgPath)
37+
log.Info("Processing package")
3438
for _, tree := range p.Syntax {
3539
for _, decl := range tree.Decls {
40+
3641
s.sourceProcessor.packageMap[p.PkgPath] = filepath.Dir(p.Fset.Position(decl.Pos()).Filename)
37-
//println("*** checking", p.Fset.Position(decl.Pos()).Filename)
42+
if !s.addImport(decl, p, localImportMap, log) {
43+
continue
44+
}
3845
spec, found := s.getStructTypeDecl(decl, p.Fset)
46+
//log.Info("Checking file","filename", p.Fset.Position(decl.Pos()).Filename,"found",found)
3947
if found {
4048
if isController || isTest {
41-
controllerSpec := s.getControllerSpec(spec, p)
49+
controllerSpec := s.getControllerSpec(spec, p, localImportMap)
4250
sourceInfo.StructSpecs = append(sourceInfo.StructSpecs, controllerSpec)
4351
}
4452
} else {
@@ -56,7 +64,7 @@ func (s *SourceInfoProcessor) processPackage(p *packages.Package) (sourceInfo *m
5664
// return one result
5765
if m, receiver := s.getControllerFunc(funcDecl, p); m != nil {
5866
methodMap[receiver] = append(methodMap[receiver], m)
59-
s.sourceProcessor.log.Info("Added method map to ", "receiver", receiver, "method", m.Name)
67+
log.Info("Added method map to ", "receiver", receiver, "method", m.Name)
6068
}
6169
}
6270
// Check for validation
@@ -271,7 +279,7 @@ func (s *SourceInfoProcessor) getControllerFunc(funcDecl *ast.FuncDecl, p *packa
271279
}
272280
return
273281
}
274-
func (s *SourceInfoProcessor) getControllerSpec(spec *ast.TypeSpec, p *packages.Package) (controllerSpec *model.TypeInfo) {
282+
func (s *SourceInfoProcessor) getControllerSpec(spec *ast.TypeSpec, p *packages.Package, localImportMap map[string]string) (controllerSpec *model.TypeInfo) {
275283
structType := spec.Type.(*ast.StructType)
276284

277285
// At this point we know it's a type declaration for a struct.
@@ -282,6 +290,7 @@ func (s *SourceInfoProcessor) getControllerSpec(spec *ast.TypeSpec, p *packages.
282290
ImportPath: p.PkgPath,
283291
PackageName: p.Name,
284292
}
293+
log := s.sourceProcessor.log.New("file", p.Fset.Position(spec.Pos()).Filename, "position", p.Fset.Position(spec.Pos()).Line)
285294
for _, field := range structType.Fields.List {
286295
// If field.Names is set, it's not an embedded type.
287296
if field.Names != nil {
@@ -329,9 +338,12 @@ func (s *SourceInfoProcessor) getControllerSpec(spec *ast.TypeSpec, p *packages.
329338
importPath = p.PkgPath
330339
} else {
331340
var ok bool
332-
if importPath, ok = s.sourceProcessor.importMap[pkgName]; !ok {
333-
s.sourceProcessor.log.Error("Error: Failed to find import path for ", "package", pkgName, "type", typeName, "map", s.sourceProcessor.importMap)
334-
continue
341+
if importPath, ok = localImportMap[pkgName]; !ok {
342+
log.Debug("Debug: Unusual, failed to find package locally ", "package", pkgName, "type", typeName, "map", s.sourceProcessor.importMap, "usedin", )
343+
if importPath, ok = s.sourceProcessor.importMap[pkgName]; !ok {
344+
log.Error("Error: Failed to find import path for ", "package", pkgName, "type", typeName, "map", s.sourceProcessor.importMap, "usedin", )
345+
continue
346+
}
335347
}
336348
}
337349

@@ -376,4 +388,37 @@ func (s *SourceInfoProcessor) getFuncName(funcDecl *ast.FuncDecl) string {
376388
prefix += "."
377389
}
378390
return prefix + funcDecl.Name.Name
391+
}
392+
func (s *SourceInfoProcessor) addImport(decl ast.Decl, p *packages.Package, localImportMap map[string]string, log logger.MultiLogger) (shouldContinue bool) {
393+
shouldContinue = true
394+
genDecl, ok := decl.(*ast.GenDecl)
395+
if !ok {
396+
return
397+
}
398+
399+
if genDecl.Tok == token.IMPORT {
400+
shouldContinue = false
401+
for _, spec := range genDecl.Specs {
402+
importSpec := spec.(*ast.ImportSpec)
403+
//fmt.Printf("*** import specification %#v\n", importSpec)
404+
var pkgAlias string
405+
if importSpec.Name != nil {
406+
pkgAlias = importSpec.Name.Name
407+
if pkgAlias == "_" {
408+
continue
409+
}
410+
}
411+
quotedPath := importSpec.Path.Value // e.g. "\"sample/app/models\""
412+
fullPath := quotedPath[1 : len(quotedPath) - 1] // Remove the quotes
413+
if pkgAlias == "" {
414+
pkgAlias = fullPath
415+
if index := strings.LastIndex(pkgAlias, "/"); index > 0 {
416+
pkgAlias = pkgAlias[index + 1:]
417+
}
418+
}
419+
localImportMap[pkgAlias] = fullPath
420+
}
421+
422+
}
423+
return
379424
}

0 commit comments

Comments
 (0)