-
Notifications
You must be signed in to change notification settings - Fork 1
/
tracker.go
102 lines (86 loc) · 1.63 KB
/
tracker.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
package main
import (
"sync/atomic"
)
var counter int64 = 0
type Tracker struct {
conf Config
wait chan error
request chan Artifact
}
func (t Tracker) Request(artifact Artifact) {
up()
t.request <- artifact
}
func (t Tracker) Wait() error {
for err := range t.wait {
return err
}
return nil
}
func up() int64 {
return atomic.AddInt64(&counter, 1)
}
func down() int64 {
return atomic.AddInt64(&counter, -1)
}
func NewTracker(conf Config) Tracker {
wait := make(chan error, 1)
requests := make(chan Artifact, 100)
pomResults := make(chan DownloadResult, 100)
results := make(chan DownloadResult, 100)
t := Tracker{conf, wait, requests}
downloader := NewDownloader(conf)
go func() {
for res := range pomResults {
if res.err != nil {
wait <- res.err
} else {
err, pom := ParsePomFromString(res.content)
if err != nil {
wait <- res.err
}
for _, a := range pom.Dependencies.ToArtifacts("compile") {
t.Request(a)
}
if down() <= 0 {
wait <- nil
}
}
}
}()
go func() {
set := make(map[string]bool)
for artifact := range requests {
if !set[artifact.String()] {
set[artifact.String()] = true
if conf.Recursive && !artifact.IsPom() {
up()
pomArtifact := artifact.Pom()
downloader.Request(DownloadRequest{
pomArtifact,
true,
pomResults,
})
}
downloader.Request(DownloadRequest{
artifact,
false,
results,
})
} else {
down()
}
}
}()
go func() {
for res := range results {
if res.err != nil {
wait <- res.err
} else if down() <= 0 {
wait <- nil
}
}
}()
return t
}