-
Notifications
You must be signed in to change notification settings - Fork 6
/
context.go
131 lines (102 loc) · 2.81 KB
/
context.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
package build
import (
"context"
"fmt"
"github.com/greenboxal/agibootstrap/pkg/platform/project"
"github.com/greenboxal/agibootstrap/pkg/platform/tasks"
)
type Context struct {
builder *Builder
log *Log
project project.Project
cfg Configuration
totalEpochs int
totalSteps int
totalChanges int
errors []error
}
func (bctx *Context) Log() *Log { return bctx.Log() }
func (bctx *Context) Project() project.Project { return bctx.project }
func (bctx *Context) Config() Configuration { return bctx.cfg }
func (bctx *Context) ReportError(err error) {
bctx.errors = append(bctx.errors, err)
bctx.log.Error(err)
}
func (bctx *Context) Build(ctx context.Context) (*Result, error) {
err := bctx.runBuild(ctx)
if err != nil {
bctx.ReportError(err)
}
// Return the total number of totalChanges and no error
return &Result{
Errors: bctx.errors,
ChangeCount: bctx.totalChanges,
TotalEpochs: bctx.totalEpochs,
TotalSteps: bctx.totalSteps,
}, nil
}
func (bctx *Context) runBuild(ctx context.Context) error {
// Execute the build steps until no further totalChanges are made
for ; bctx.cfg.MaxEpochs == 0 || bctx.totalEpochs < bctx.cfg.MaxEpochs; bctx.totalEpochs++ {
stepChanges := 0
// Sync the project to ensure it is up to date
if err := bctx.project.Sync(); err != nil {
return err
}
// Execute each build step
for _, step := range bctx.cfg.BuildSteps {
if bctx.cfg.MaxSteps != 0 && bctx.totalSteps >= bctx.cfg.MaxSteps {
break
}
var result StepResult
// Wrap the build step execution in a recover function
task := bctx.project.TaskManager().SpawnTask(ctx, func(progress tasks.TaskProgress) (err error) {
defer func() {
if r := recover(); r != nil {
if e, ok := r.(error); ok {
err = e
} else {
err = fmt.Errorf("%v", r)
}
}
}()
// Execute the build step and return the result
result, err = step.Process(ctx, bctx)
return
})
if err := task.Error(); err != nil {
return err
}
// Update the total number of totalChanges
stepChanges += result.ChangeCount
bctx.totalSteps++
}
// Stage all the totalChanges in the file system
if err := bctx.project.FS().StageAll(); err != nil {
return err
}
// Commit the totalChanges to the file system
if err := bctx.project.Commit(); err != nil {
return err
}
// If no totalChanges were made, exit the loop
if stepChanges == 0 {
break
}
// Update the total number of totalChanges
bctx.totalChanges += stepChanges
if bctx.cfg.MaxSteps != 0 && bctx.totalSteps >= bctx.cfg.MaxSteps {
break
}
}
// Push the totalChanges to the remote repository
if err := bctx.project.FS().Push(); err != nil {
return err
}
return nil
}
func (bctx *Context) Close() {
if bctx.log != nil {
_ = bctx.log.Close()
}
}