forked from tliron/puccini
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit.go
118 lines (90 loc) · 2.31 KB
/
unit.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
package parser
import (
"sort"
"strings"
"github.com/tliron/kutil/terminal"
"github.com/tliron/kutil/util"
"github.com/tliron/puccini/tosca"
)
//
// NoEntity
//
type NoEntity struct {
Context *tosca.Context
}
func NewNoEntity(toscaContext *tosca.Context) *NoEntity {
return &NoEntity{toscaContext}
}
// tosca.Contextual interface
func (self *NoEntity) GetContext() *tosca.Context {
return self.Context
}
//
// Unit
//
type Unit struct {
EntityPtr tosca.EntityPtr
Container *Unit
Imports Units
NameTransformer tosca.NameTransformer
importsLock util.RWLocker
}
func NewUnitNoEntity(toscaContext *tosca.Context, container *Unit, nameTransformer tosca.NameTransformer) *Unit {
return NewUnit(NewNoEntity(toscaContext), container, nameTransformer)
}
func NewUnit(entityPtr tosca.EntityPtr, container *Unit, nameTransformer tosca.NameTransformer) *Unit {
self := Unit{
EntityPtr: entityPtr,
Container: container,
NameTransformer: nameTransformer,
importsLock: util.NewDebugRWLocker(),
}
if container != nil {
container.AddImport(&self)
}
return &self
}
func (self *Unit) AddImport(import_ *Unit) {
self.importsLock.Lock()
defer self.importsLock.Unlock()
self.Imports = append(self.Imports, import_)
}
func (self *Unit) GetContext() *tosca.Context {
return tosca.GetContext(self.EntityPtr)
}
// Print
func (self *Unit) PrintImports(indent int, treePrefix terminal.TreePrefix) {
self.importsLock.RLock()
length := len(self.Imports)
imports := make(Units, length)
copy(imports, self.Imports)
self.importsLock.RUnlock()
last := length - 1
// Sort
sort.Sort(imports)
for i, unit := range imports {
isLast := i == last
unit.PrintNode(indent, treePrefix, isLast)
unit.PrintImports(indent, append(treePrefix, isLast))
}
}
func (self *Unit) PrintNode(indent int, treePrefix terminal.TreePrefix, last bool) {
treePrefix.Print(indent, last)
terminal.Printf("%s\n", terminal.Stylize.Value(self.GetContext().URL.String()))
}
//
// Units
//
type Units []*Unit
// sort.Interface
func (self Units) Len() int {
return len(self)
}
func (self Units) Swap(i, j int) {
self[i], self[j] = self[j], self[i]
}
func (self Units) Less(i, j int) bool {
iName := self[i].GetContext().URL.String()
jName := self[j].GetContext().URL.String()
return strings.Compare(iName, jName) < 0
}