/
files.go
146 lines (125 loc) · 2.92 KB
/
files.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
package builder
import (
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/aleroux85/meta-builder/refmap"
"github.com/aleroux85/utils"
"github.com/pkg/errors"
)
type FSTemplate struct {
Name string `json:"name"`
File string `json:"file"`
Body string `json:"body"`
}
type FSFile struct {
Name string `json:"name"`
Copy bool `json:"copy"`
Update string `json:"update"`
Source string `json:"source"`
Templates map[string]string `json:"templates"`
Parent BackRef `json:"-"`
changeDetector
}
func (file *FSFile) CalculateHash() error {
var err error
fileTemp := *file
err = file.changeDetector.CalculateHash(fileTemp)
if err != nil {
return err
}
return nil
}
func (file *FSFile) Build(c refmap.Config) {
if c.Error() != nil {
return
}
dstFilename := strings.TrimSuffix(file.Name, ".tmpl")
srcFilename := file.Name
if file.Source != "" {
srcFilename = filepath.Base(file.Source)
}
parentDS := file.Parent.(*FSDirectory)
branch := parentDS.Branch
sourcePath := parentDS.SourcePath
destination := parentDS.DestinationPath
dstFileLocation := filepath.Join(c.Destination(), destination, dstFilename)
if _, err := os.Stat(dstFileLocation); err == nil {
if !c.Force() && !c.Watching() {
return
}
} else if os.IsNotExist(err) {
os.MkdirAll(filepath.Join(c.Destination(), destination), os.ModePerm)
} else {
c.Error(errors.Wrap(err, "building file, stating"))
return
}
f, err := os.Create(dstFileLocation)
if err != nil {
c.Error(errors.Wrap(err, "building file, creating"))
return
}
if file.Copy || parentDS.Copy {
r, err := os.Open(filepath.Join(c.Source(), sourcePath, srcFilename))
if err != nil {
c.Error(err)
return
}
defer r.Close()
_, err = io.Copy(f, r)
if err != nil {
c.Error(err)
return
}
err = f.Sync()
if err != nil {
c.Error(err)
return
}
goto compile
}
fmt.Println("rebuilding", dstFileLocation)
if parentDS.Template == nil || c.Watching() {
parentDS.Template = new(utils.Templax)
err := parentDS.Template.Prepare(filepath.Join(c.Source(), sourcePath))
if err != nil {
c.Error(err)
return
}
}
for _, templates := range file.Templates {
err := parentDS.Template.Prepare(filepath.Join(c.Source(), templates))
if err != nil {
c.Error(err)
return
}
}
if file.Source != "" {
if filepath.Dir(file.Source) != "." {
err := parentDS.Template.Prepare(filepath.Join(c.Source(), file.Source))
if err != nil {
c.Error(err)
return
}
}
}
branch.SetFile(file)
err = parentDS.Template.FExecute(f, srcFilename, branch)
if err != nil {
c.Error(err)
return
}
f.Close()
compile:
for walker := file.Parent; walker != nil; walker = walker.Up() {
for name, e := range walker.CmdMatch() {
r, _ := regexp.Compile(e.Pattern)
if r.MatchString(dstFilename) {
c.RegisterCmd(name, e.Cmd, e.Deps, e.Timeout)
}
}
}
}