-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.go
142 lines (115 loc) · 2.96 KB
/
build.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
package builder
import (
"context"
"errors"
"fmt"
"github.com/KaiserWerk/Tiny-Build-Server/internal/common"
"github.com/KaiserWerk/Tiny-Build-Server/internal/entity"
"os"
"path/filepath"
"strings"
"time"
)
var (
ErrCanceled = errors.New("build: canceled by context")
)
type (
Build struct {
initiatedBy uint
definition *entity.BuildDefinition
reportWriter strings.Builder
status entity.BuildStatus
executionTime time.Time
projectPath string
artifact string
}
)
func NewBuild(definition *entity.BuildDefinition, basePath string) *Build {
b := Build{
definition: definition,
status: entity.StatusCreated, // can be set later
executionTime: time.Now(),
projectPath: ".",
}
// work with absolute path to avoid discrepancies
absPath, err := filepath.Abs(basePath)
if err == nil {
b.projectPath = absPath
}
b.projectPath = filepath.Join(
b.projectPath,
fmt.Sprintf("%d", b.definition.ID),
fmt.Sprintf("%d", b.executionTime.UnixNano()),
)
return &b
}
func (b *Build) GetStatus() entity.BuildStatus {
return b.status
}
func (b *Build) SetStatus(s entity.BuildStatus) {
b.status = s
}
func (b *Build) AddReportEntry(e string) {
_, _ = b.reportWriter.WriteString(e + "\n")
}
func (b *Build) AddReportEntryf(f string, a ...interface{}) {
_, _ = b.reportWriter.WriteString(fmt.Sprintf(f+"\n", a))
}
func (b *Build) GetReport() string {
return b.reportWriter.String()
}
func (b *Build) GetProjectDir() string {
return b.projectPath
}
func (b *Build) GetCloneDir() string {
return filepath.Join(b.projectPath, "clone")
}
func (b *Build) GetBuildDir() string {
return filepath.Join(b.projectPath, "build")
}
func (b *Build) GetArtifactDir() string {
return filepath.Join(b.projectPath, "artifact")
}
func (b *Build) SetArtifact(a string) {
b.artifact = a
}
func (b *Build) GetArtifact() string {
return b.artifact
}
// Pack packs the Build (the content from the build folder) into a zip file and puts the path to
// the resulting zip file into the artifact field.
func (b *Build) Pack(ctx context.Context) error {
if ctx.Err() != nil {
return ErrCanceled
}
files, err := os.ReadDir(b.GetBuildDir())
if err != nil {
return err
}
fh, err := os.CreateTemp(b.GetArtifactDir(), "artifact-*.zip")
if err != nil {
return err
}
defer fh.Close()
b.SetArtifact(filepath.Join(b.GetArtifactDir(), fh.Name()))
fileList := make([]string, len(files))
for i, f := range files {
fileList[i] = filepath.Join(b.GetBuildDir(), f.Name())
}
return common.ZipFiles(fh, false, fileList)
}
func (b *Build) Setup(ctx context.Context) error {
if ctx.Err() != nil {
return ErrCanceled
}
// create directories
for _, d := range []string{b.GetProjectDir(), b.GetCloneDir(), b.GetBuildDir(), b.GetArtifactDir()} {
if err := os.MkdirAll(d, 0755); err != nil {
return err
}
}
return nil
}
//func (b *Build) UpdateBuildExecution(ds *dbservice.DBService, be *BuildExecution) error {
// return ds.UpdateBuildExecution(be)
//}