/
planning_file.go
58 lines (49 loc) · 1.05 KB
/
planning_file.go
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
package handwriting
import (
"github.com/pkg/errors"
)
// PlanningFile :
type PlanningFile struct {
Filename string
Root *Planner
Setups []func(*File) error
Actions []func(*File) error
imports []importspec
used map[string]struct{}
}
// Code :
func (f *PlanningFile) Code(fn func(*File) error) {
f.Actions = append(f.Actions, fn)
}
// Import :
func (f *PlanningFile) Import(path string) {
f.ImportWithName(path, "")
}
// ImportWithName :
func (f *PlanningFile) ImportWithName(path string, name string) {
if _, existed := f.used[path]; existed {
return
}
f.used[path] = struct{}{}
f.Root.Import(path)
if f.Root.Pkg.Path() == path {
return
}
f.Setups = append(f.Setups, func(f *File) error {
info := f.Prog.Package(path)
if info == nil {
return errors.Errorf("package not found %q", path)
}
name := name
if name == "" {
name = info.Pkg.Name()
}
f.Resolver.ImportWithName(info.Pkg, name)
return nil
})
f.imports = append(f.imports, importspec{Name: name, Path: path})
}
type importspec struct {
Name string
Path string
}