-
Notifications
You must be signed in to change notification settings - Fork 115
/
dsdx.go
107 lines (98 loc) · 2.81 KB
/
dsdx.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
package model
import (
"encoding/json"
"fmt"
"io"
"text/template"
"time"
)
type DsdxDocument struct {
// 文档名称
Name string `json:"name" xml:"name"`
// 创作者
Creator string `json:"creator" xml:"creator"`
// dsdx版本 DSDX-1.0
DSDXVersion string `json:"dsdx_version" xml:"dsdx_version"`
// 文档创建时间 yyyy-MM-dd HH:mm:ss
CreateTime string `json:"create_time" xml:"create_time"`
// dsdx文档标识 自动生成 DSDX-${Name}-${Version}-${CreateTime}
DSDXID string `json:"dsdx_id" xml:"dsdx_id"`
// 项目名称
ProjectName string `json:"project_naem" xml:"project_naem"`
// 组件列表
Components []DsdxComponent `json:"components" xml:"components"`
// 依赖关系
Dependencies DsdxDependencies `json:"dependencies" xml:"-"`
}
type DsdxComponent struct {
// DSDX-xxx
ID string `json:"id" xml:"id"`
Group string `json:"group,omitempty" xml:"group,omitempty"`
Name string `json:"name" xml:"name"`
Version string `json:"version" xml:"version"`
Language string `json:"language,omitempty" xml:"language,omitempty"`
License []string `json:"license,omitempty" xml:"license,omitempty"`
}
type DsdxDependencies map[string][]string
func NewDsdxDocument(name, creator string) *DsdxDocument {
version := "DSDX-1.0"
create := time.Now().Format("2006-01-02 15:04:05")
id := fmt.Sprintf("DSDX-%s-%s-%s", name, version, create)
return &DsdxDocument{
Name: name,
Creator: creator,
ProjectName: name,
CreateTime: create,
DSDXVersion: version,
DSDXID: id,
}
}
func (doc *DsdxDocument) AppendComponents(id, group, name, version, language string, license []string) {
if id == "" {
id = fmt.Sprintf("DSDX-%s-%s-%s", group, name, version)
}
doc.Components = append(doc.Components, DsdxComponent{
ID: id,
Group: group,
Name: name,
Version: version,
Language: language,
License: license,
})
}
func (doc *DsdxDocument) AppendDependencies(parentId string, childrenIds []string) {
if doc.Dependencies == nil {
doc.Dependencies = DsdxDependencies{}
}
if len(childrenIds) > 0 {
doc.Dependencies[parentId] = childrenIds
}
}
func (doc *DsdxDocument) WriteDsdx(w io.Writer) error {
tmpl, err := template.New("tagValue").Funcs(template.FuncMap{
"tojson": func(o any) string {
data, _ := json.Marshal(o)
return string(data)
},
}).Parse(dsdxtpl)
if err != nil {
return err
}
return tmpl.Execute(w, doc)
}
const dsdxtpl = `Name: {{ .Name }}
DSDXVersion: {{ .DSDXVersion }}
DSDXID: {{ .DSDXID }}
Creator: {{ .Creator }}
CreateTime: {{ .CreateTime }}
ProjectName: {{ .ProjectName }}
{{ range .Components }}
ComponentName: {{ .Name }}
ComponentGroup: {{ .Group }}
ComponentVersion: {{ .Version }}
ComponentID: {{ .ID }}
ComponentLanguage: {{ .Language }}
ComponentLicense: {{ .License|tojson }}
{{ end }}
Dependencies: {{ .Dependencies|tojson }}
`