Repository navigation
Expand file tree
/
Copy pathwatch.go
More file actions
67 lines (56 loc) · 1.51 KB
/
Copy pathwatch.go
File metadata and controls
67 lines (56 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package definitionc
import (
"go/parser"
"go/token"
)
// NewFileEvent implements the file-event contract for watchers (e.g. webtyp/app's devwatch).
func (g *Generator) NewFileEvent(fileName, extension, filePath, event string) error {
if fileName != DefinitionFile {
return nil
}
// 1. Parse only that file
infos, err := g.parseDefinitionsInFile(filePath)
if err != nil {
return err
}
fset := token.NewFileSet()
node, err := parser.ParseFile(fset, filePath, nil, parser.ParseComments)
if err != nil {
return err
}
if err := g.resolveStorage(infos, node); err != nil {
return err
}
// 2. Merge into cache
for _, info := range infos {
g.cache[info.Name] = info
}
// 3. Regenerate just <file>_gen.go
if err := g.GenerateForFile(infos, filePath); err != nil {
return err
}
// 4. Sync each DB struct
if g.syncer != nil {
for _, info := range infos {
if info.NoDB {
continue
}
if err := g.syncer.SyncSchema(info.ModelName, info.asFields()); err != nil {
g.log("sync error for", info.ModelName, ":", err)
}
}
}
return nil
}
// SupportedExtensions returns the list of file extensions this generator handles.
func (g *Generator) SupportedExtensions() []string {
return []string{".go"}
}
// UnobservedFiles returns a list of files that should be ignored by the watcher.
func (g *Generator) UnobservedFiles() []string {
return nil
}
// MainInputFileRelativePath returns the relative path to the main input file, if any.
func (g *Generator) MainInputFileRelativePath() string {
return ""
}