1
1
package revel
2
2
3
3
import (
4
+ "go/build"
5
+ "path/filepath"
4
6
"sort"
5
7
"strings"
6
- "path/filepath"
7
- "go/build"
8
8
)
9
9
10
10
// Module specific functions
11
11
type Module struct {
12
12
Name , ImportPath , Path string
13
- ControllerTypeList []* ControllerType
13
+ ControllerTypeList []* ControllerType
14
14
}
15
15
16
- const namespaceSeperator = "|" // ., : are already used
16
+ // The namespace separator constant
17
+ const namespaceSeperator = "|" // (note cannot be . or : as this is already used for routes)
17
18
18
19
var (
19
- anyModule = & Module {}
20
- appModule = & Module {Name :"App" }
20
+ Modules []* Module // The list of modules in use
21
+ anyModule = & Module {} // Wildcard search for controllers for a module (for backward compatible lookups)
22
+ appModule = & Module {Name : "App" } // The app module
21
23
)
22
24
23
25
// Returns the namespace for the module in the format `module_name|`
@@ -27,24 +29,73 @@ func (m *Module) Namespace() (namespace string) {
27
29
}
28
30
29
31
// Returns the named controller and action that is in this module
30
- func (m * Module ) ControllerByName (name ,action string )(ctype * ControllerType ) {
32
+ func (m * Module ) ControllerByName (name , action string ) (ctype * ControllerType ) {
31
33
comparision := name
32
- if strings .Index (name ,namespaceSeperator )< 0 {
33
- comparision = m .Namespace () + name
34
+ if strings .Index (name , namespaceSeperator ) < 0 {
35
+ comparision = m .Namespace () + name
34
36
}
35
- for _ ,c := range m .ControllerTypeList {
36
- if strings .Index (c .Name (),comparision )> - 1 {
37
+ for _ , c := range m .ControllerTypeList {
38
+ if strings .Index (c .Name (), comparision ) > - 1 {
37
39
ctype = c
38
40
break
39
41
}
40
42
}
41
43
return
42
44
}
45
+
46
+ // Adds the controller type to this module
43
47
func (m * Module ) AddController (ct * ControllerType ) {
44
- m .ControllerTypeList = append (m .ControllerTypeList ,ct )
48
+ m .ControllerTypeList = append (m .ControllerTypeList , ct )
45
49
}
46
50
51
+ // Based on the full path given return the relevant module
52
+ // Only to be used on initialization
53
+ func ModuleFromPath (path string , addGopathToPath bool ) (module * Module ) {
54
+ gopathList := filepath .SplitList (build .Default .GOPATH )
55
+
56
+ // See if the path exists in the module based
57
+ for i := range Modules {
58
+ if addGopathToPath {
59
+ for _ , gopath := range gopathList {
60
+ if strings .HasPrefix (gopath + "/src/" + path , Modules [i ].Path ) {
61
+ module = Modules [i ]
62
+ break
63
+ }
64
+ }
65
+ } else {
66
+ if strings .HasPrefix (path , Modules [i ].Path ) {
67
+ module = Modules [i ]
68
+ break
69
+ }
70
+
71
+ }
72
+
73
+ if module != nil {
74
+ break
75
+ }
76
+ }
77
+ return
78
+ }
47
79
80
+ // ModuleByName returns the module of the given name, if loaded, case insensitive.
81
+ func ModuleByName (name string ) (m * Module , found bool ) {
82
+ // If the name ends with the namespace separator remove it
83
+ if name [len (name )- 1 ] == []byte (namespaceSeperator )[0 ] {
84
+ name = name [:len (name )- 1 ]
85
+ }
86
+ name = strings .ToLower (name )
87
+ if name == strings .ToLower (appModule .Name ) {
88
+ return appModule ,true
89
+ }
90
+ for _ , module := range Modules {
91
+ if strings .ToLower (module .Name ) == name {
92
+ return module , true
93
+ }
94
+ }
95
+ return nil , false
96
+ }
97
+
98
+ // Loads the modules specified in the config
48
99
func loadModules () {
49
100
keys := []string {}
50
101
for _ , key := range Config .Options ("module." ) {
@@ -77,32 +128,26 @@ func loadModules() {
77
128
}
78
129
}
79
130
80
- // Based on the full path given return the relevant module
81
- // Only be used on initialization
82
- func ModuleFromPath (path string , addGopathToPath bool ) (module * Module ) {
83
- gopathList := filepath .SplitList (build .Default .GOPATH )
84
-
85
- // See if the path exists in the module based
86
- for i := range Modules {
87
- if addGopathToPath {
88
- for _ , gopath := range gopathList {
89
- if strings .HasPrefix (gopath + "/src/" + path , Modules [i ].Path ) {
90
- module = Modules [i ]
91
- break
92
- }
93
- }
94
- } else {
95
- if strings .HasPrefix (path , Modules [i ].Path ) {
96
- module = Modules [i ]
97
- break
98
- }
99
-
131
+ //
132
+ func addModule (name , importPath , modulePath string ) {
133
+ Modules = append (Modules , & Module {Name : name , ImportPath : importPath , Path : modulePath })
134
+ if codePath := filepath .Join (modulePath , "app" ); DirExists (codePath ) {
135
+ CodePaths = append (CodePaths , codePath )
136
+ if viewsPath := filepath .Join (modulePath , "app" , "views" ); DirExists (viewsPath ) {
137
+ TemplatePaths = append (TemplatePaths , viewsPath )
100
138
}
139
+ }
101
140
102
- if module != nil {
103
- break
104
- }
141
+ INFO .Print ("Loaded module " , filepath .Base (modulePath ))
142
+
143
+ // Hack: There is presently no way for the testrunner module to add the
144
+ // "test" subdirectory to the CodePaths. So this does it instead.
145
+ if importPath == Config .StringDefault ("module.testrunner" , "github.com/revel/modules/testrunner" ) {
146
+ INFO .Print ("Found testrunner module, adding `tests` path " , filepath .Join (BasePath , "tests" ))
147
+ CodePaths = append (CodePaths , filepath .Join (BasePath , "tests" ))
148
+ }
149
+ if testsPath := filepath .Join (modulePath , "tests" ); DirExists (testsPath ) {
150
+ INFO .Print ("Found tests path " , testsPath )
151
+ CodePaths = append (CodePaths , testsPath )
105
152
}
106
- return
107
153
}
108
-
0 commit comments