-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathconfig.go
136 lines (106 loc) · 2.76 KB
/
config.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"bytes"
"crypto/md5"
"encoding/hex"
"fmt"
"github.com/pelletier/go-toml"
"io/ioutil"
"os"
"path/filepath"
)
type Config struct {
Checksum []byte
// Path to the configuration file.
Path string
// Name of your repository "github.com/d2fn/gopack" for instance.
Repository string
// Dependencies tree
DepsTree *toml.TomlTree
}
func NewConfig(dir string) *Config {
config := &Config{Path: fmt.Sprintf("%s/gopack.config", dir)}
t, err := toml.LoadFile(config.Path)
if err != nil {
fail(err)
}
if deps := t.Get("deps"); deps != nil {
config.DepsTree = deps.(*toml.TomlTree)
}
if repo := t.Get("repo"); repo != nil {
config.Repository = repo.(string)
}
return config
}
func (c *Config) InitRepo(importGraph *Graph) {
if c.Repository != "" {
src := fmt.Sprintf("%s/%s/src", pwd, VendorDir)
os.MkdirAll(src, 0755)
dir := filepath.Dir(c.Repository)
base := fmt.Sprintf("%s/%s", src, dir)
os.MkdirAll(base, 0755)
repo := fmt.Sprintf("%s/%s", src, c.Repository)
err := os.Symlink(pwd, repo)
if err != nil && !os.IsExist(err) {
fail(err)
}
dependency := NewDependency(c.Repository)
importGraph.Insert(dependency)
}
}
func (c *Config) modifiedChecksum() bool {
dat, err := ioutil.ReadFile(c.checksumPath())
return (err != nil && os.IsNotExist(err)) || !bytes.Equal(dat, c.checksum())
}
func (c *Config) WriteChecksum() {
os.MkdirAll(filepath.Join(pwd, GopackDir), 0755)
err := ioutil.WriteFile(c.checksumPath(), c.checksum(), 0644)
if err != nil {
fail(err)
}
}
func (c *Config) checksumPath() string {
return filepath.Join(pwd, GopackChecksum)
}
func (c *Config) checksum() []byte {
if c.Checksum == nil {
dat, err := ioutil.ReadFile(c.Path)
if err != nil {
fail(err)
}
h := md5.New()
h.Write(dat)
c.Checksum = h.Sum(nil)
}
return []byte(hex.EncodeToString(c.Checksum))
}
func (c *Config) LoadDependencyModel(importGraph *Graph) (deps *Dependencies, err error) {
depsTree := c.DepsTree
if depsTree == nil {
return
}
deps = new(Dependencies)
deps.Imports = make([]string, len(depsTree.Keys()))
deps.Keys = make([]string, len(depsTree.Keys()))
deps.DepList = make([]*Dep, len(depsTree.Keys()))
deps.ImportGraph = importGraph
modifiedChecksum := c.modifiedChecksum()
for i, k := range depsTree.Keys() {
depTree := depsTree.Get(k).(*toml.TomlTree)
d := NewDependency(depTree.Get("import").(string))
d.setScm(depTree)
d.setSource(depTree)
d.setCheckout(depTree, "branch", BranchFlag)
d.setCheckout(depTree, "commit", CommitFlag)
d.setCheckout(depTree, "tag", TagFlag)
if err := d.Validate(); err != nil {
return nil, err
}
d.Fetch(modifiedChecksum)
deps.Keys[i] = k
deps.Imports[i] = d.Import
deps.DepList[i] = d
deps.ImportGraph.Insert(d)
}
return deps, nil
}