-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.go
203 lines (185 loc) · 5.39 KB
/
generate.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package main
import (
"crypto/md5"
"crypto/sha512"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"sync"
)
type AppMetadata struct {
Name string `json:"name"`
UniqueId string `json:"uniqueId"`
ShortDescription string `json:"shortDescription"`
Description string `json:"description"`
Source string `json:"source"`
Banner string `json:"banner"`
SupportedReleases []string `json:"supportedReleases"`
Releases map[string]*Release `json:"releases"`
}
type Release struct {
Image string `json:"image"`
Command string `json:"command"`
SourceTarball string `json:"sourceTarball"`
WebXDCDownload string `json:"WebXDCDownload"`
XdcSha512sum string `json:"xdcsha512sum"`
TarSha512sum string `json:"tarsha512sum"`
}
type RepositoryMetadata struct {
Apps map[string]*AppMetadata `json:"apps"`
}
var repo RepositoryMetadata
func init() {
repo.Apps = map[string]*AppMetadata{}
}
func init() {
// Define a custom log format that includes the short file name
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
}
func sha512String(file string) string {
f, err := os.Open(file)
if err != nil {
log.Fatalln(err)
}
defer f.Close()
h := sha512.New()
if _, err := io.Copy(h, f); err != nil {
log.Fatalln(err)
}
return fmt.Sprintf("%x", h.Sum(nil))
}
func main() {
if _, err := os.Stat("apps"); err != nil {
log.Println("'apps' directory is not available, are you in correct repository?")
return
}
if _, err := os.Stat("build"); err != nil {
log.Println("'build' directory is not available, are you in correct repository?")
return
}
b, err := os.ReadFile("build/meta.json")
if err == nil {
err := json.Unmarshal(b, &repo)
if err != nil {
log.Fatalln(err)
}
}
err = filepath.Walk("apps", walk)
if err != nil {
log.Println(err)
}
}
var saveMutex sync.Mutex
func save() {
saveMutex.Lock()
b, err := json.MarshalIndent(repo, "", " ")
if err != nil {
log.Fatalln(err)
}
err = os.WriteFile("build/meta.json", b, 0750)
if err != nil {
log.Fatalln(err)
}
saveMutex.Unlock()
}
func walk(p string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if !strings.HasSuffix(info.Name(), ".json") {
return nil
}
dirname := path.Dir(p)
basename := path.Base(p)
log.Println(dirname, basename, info.Size())
b, err := os.ReadFile(p)
if err != nil {
return err
}
var app AppMetadata
err = json.Unmarshal(b, &app)
if err != nil {
return err
}
if _, ok := repo.Apps[app.UniqueId]; !ok {
repo.Apps[app.UniqueId] = &AppMetadata{
Name: app.Name,
UniqueId: app.UniqueId,
ShortDescription: app.ShortDescription,
Description: app.Description,
Source: app.Source,
Banner: path.Join(app.UniqueId, app.Banner),
SupportedReleases: app.SupportedReleases,
}
} else {
repo.Apps[app.UniqueId].Name = app.Name
repo.Apps[app.UniqueId].UniqueId = app.UniqueId
repo.Apps[app.UniqueId].ShortDescription = app.ShortDescription
repo.Apps[app.UniqueId].Description = app.Description
repo.Apps[app.UniqueId].Source = app.Source
repo.Apps[app.UniqueId].Banner = path.Join(app.UniqueId, app.Banner)
repo.Apps[app.UniqueId].SupportedReleases = app.SupportedReleases
}
save()
for i := range app.Releases {
if _, ok := repo.Apps[app.UniqueId].Releases[i]; ok {
log.Println("repo contains build of", app.UniqueId, "tag:", i)
continue
}
if repo.Apps[app.UniqueId].Releases == nil {
repo.Apps[app.UniqueId].Releases = make(map[string]*Release)
}
build(&app, i, app.Releases[i])
repo.Apps[app.UniqueId].Releases[i] = &Release{
Image: app.Releases[i].Image,
Command: app.Releases[i].Command,
SourceTarball: path.Join(app.UniqueId, i, app.UniqueId+".tar.gz"),
WebXDCDownload: path.Join(app.UniqueId, i, app.UniqueId+".xdc"),
TarSha512sum: sha512String(path.Join("build", app.UniqueId, i, app.UniqueId+".tar.gz")),
XdcSha512sum: sha512String(path.Join("build", app.UniqueId, i, app.UniqueId+".xdc")),
}
run("cp", path.Join(dirname, app.Banner), path.Join("build", app.UniqueId, app.Banner)) //, path.Join(outDir, app.UniqueId+".tar.gz"))
save()
}
return nil
}
func build(app *AppMetadata, tag string, rel *Release) {
log.Println("build:", app.UniqueId, "tag:", tag)
outDir := path.Join("build", app.UniqueId, tag)
tmpDir, err := os.MkdirTemp(os.TempDir(), "tmpbld")
if err != nil {
log.Fatalln(err)
}
run("docker", "run", "--rm", "-i", "-v", tmpDir+":/out", rel.Image, "/bin/bash", "-c", "git clone '"+app.Source+"' /build && cd /build && git checkout "+tag+" && tar --exclude-vcs -zcvf /out/source.tar.gz . && "+rel.Command)
os.MkdirAll(outDir, 0750)
run("mv", path.Join(tmpDir, "source.tar.gz"), path.Join(outDir, app.UniqueId+".tar.gz"))
run("mv", path.Join(tmpDir, "app.xdc"), path.Join(outDir, app.UniqueId+".xdc"))
if err != nil {
log.Fatalln(err)
}
}
func run(name string, args ...string) {
log.Println("run:", name, strings.Join(args, " "))
cmd := exec.Command(name, args...)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
err := cmd.Run()
if err != nil {
log.Fatalln(err)
}
}
func GetMD5Hash(text string) string {
hasher := md5.New()
hasher.Write([]byte(text))
return hex.EncodeToString(hasher.Sum(nil))
}