-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.go
110 lines (90 loc) · 2.11 KB
/
project.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package gosolc
import (
"path/filepath"
"github.com/umbracle/gosolc/svm"
)
type Project struct {
// config is the configuration of the Solidity project
config *Config
// svm handles the lifecycle of the Solidity compiler binaries
svm *svm.SolidityVersionManager
sources []*Source
contracts contractsList
}
type contractsList []*Contract
func (c *contractsList) Filter(cond func(c *Contract) bool) (res contractsList) {
for _, cc := range *c {
if cond(cc) {
res = append(res, cc)
}
}
return
}
func NewProject(opts ...Option) (*Project, error) {
cfg := DefaultConfig()
for _, opt := range opts {
opt(cfg)
}
cfg.ContractsDir = filepath.Clean(cfg.ContractsDir)
// default artifacts directory to the contracts directory if not set
if cfg.ArtifactsDir == "" {
cfg.ArtifactsDir = cfg.ContractsDir
} else {
cfg.ArtifactsDir = filepath.Clean(cfg.ArtifactsDir)
}
p := &Project{
config: cfg,
sources: []*Source{},
contracts: []*Contract{},
}
svm, err := svm.NewSolidityVersionManager()
if err != nil {
return nil, err
}
p.svm = svm
return p, nil
}
func (p *Project) findContractByFullName(name string) *Contract {
res := p.contracts.Filter(func(c *Contract) bool {
return name == c.Source+":"+c.Name
})
if len(res) != 1 {
return nil
}
return res[0]
}
func (p *Project) getSourceByPath(path string) *Source {
for _, s := range p.sources {
sourcePath := filepath.Join(s.Dir, s.Filename)
if path == sourcePath {
return s
}
}
return nil
}
func (p *Project) ListContracts() ([]*Contract, error) {
return p.contracts, nil
}
func (p *Project) ListSources() ([]*Source, error) {
return p.sources, nil
}
func (p *Project) UpsertContract(c *Contract) error {
for indx, cc := range p.contracts {
if cc.Source == c.Source && cc.Name == c.Name {
p.contracts[indx] = c
return nil
}
}
p.contracts = append(p.contracts, c)
return nil
}
func (p *Project) UpsertSource(src *Source) error {
for indx, ss := range p.sources {
if ss.Dir == src.Dir && ss.Filename == src.Filename {
p.sources[indx] = src
return nil
}
}
p.sources = append(p.sources, src)
return nil
}