-
Notifications
You must be signed in to change notification settings - Fork 0
/
blogc.go
203 lines (167 loc) · 4.06 KB
/
blogc.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 blogc
import (
"errors"
"fmt"
"os"
"strings"
"github.com/hashicorp/go-version"
)
func RequiredVersion(v string) error {
actualVersion, err := version.NewVersion(Version)
if err != nil {
return err
}
reqVersion, err := version.NewVersion(v)
if err != nil {
return err
}
if reqVersion.Compare(actualVersion) > 0 {
return fmt.Errorf("blogc: version %q or greater required, got %q", v, Version)
}
return nil
}
func OutputIsOutdated(inputFiles []File, outputFile File) bool {
if outputFile == nil {
return false
}
if outputFile.IsTempFile() {
return true
}
st, err := os.Stat(outputFile.Path())
if err != nil {
return true
}
mtime := st.ModTime()
selfPath := ""
if len(os.Args) > 0 {
selfPath = os.Args[0]
}
selfInjected := false
for _, f := range inputFiles {
path := f.Path()
if f.IsTempFile() && selfPath != "" {
if selfInjected {
continue
}
path = selfPath
selfInjected = true
}
st, err := os.Stat(path)
if err != nil {
// file not found. recomend a new build, so blogc can generate
// useful error message
return true
}
if mtime.Before(st.ModTime()) {
return true
}
}
return false
}
type BuildContext struct {
Listing bool
GlobalVariables []string
InputFiles []File
ListingEntryFile File
OutputFile File
TemplateFile File
}
func (e *BuildContext) NeedsBuild() bool {
inputFiles := e.InputFiles
if e.TemplateFile != nil {
inputFiles = append(inputFiles, e.TemplateFile)
}
if e.Listing && e.ListingEntryFile != nil {
inputFiles = append(inputFiles, e.ListingEntryFile)
}
return OutputIsOutdated(inputFiles, e.OutputFile)
}
func (e *BuildContext) generateCommand(printVar string) []string {
rv := []string{}
if e.Listing {
rv = append(rv, "-l", "-i")
if e.ListingEntryFile != nil {
rv = append(rv, "-e", e.ListingEntryFile.Path())
}
} else {
rv = append(rv, e.InputFiles[0].Path())
}
for _, v := range e.GlobalVariables {
rv = append(rv, "-D", v)
}
if printVar != "" {
rv = append(rv, "-p", printVar)
} else if e.OutputFile != nil && e.TemplateFile != nil {
rv = append(rv, "-o", e.OutputFile.Path(), "-t", e.TemplateFile.Path())
}
return rv
}
func (e *BuildContext) generateStdin() string {
rv := ""
if e.Listing {
for _, v := range e.InputFiles {
rv += fmt.Sprintf("%s\n", v.Path())
}
}
return rv
}
func (e *BuildContext) validateInputFiles() error {
if e.Listing {
if len(e.InputFiles) == 0 {
return fmt.Errorf("blogc: at least one input file is required")
}
} else {
if len(e.InputFiles) != 1 {
return fmt.Errorf("blogc: one input file is required")
}
if e.ListingEntryFile != nil {
return fmt.Errorf("blogc: listing entry is only supported by listing mode")
}
}
return nil
}
func (e *BuildContext) Build() error {
if err := e.validateInputFiles(); err != nil {
return err
}
if e.OutputFile == nil {
return fmt.Errorf("blogc: output file is required")
}
if e.TemplateFile == nil {
return fmt.Errorf("blogc: template file is required")
}
cmdArgs := e.generateCommand("")
statusCode, _, stderr, err := blogcRun(e.generateStdin(), cmdArgs...)
if err != nil {
return err
}
if statusCode != 0 {
return errors.New(strings.TrimSpace(stderr))
}
return nil
}
func (e *BuildContext) GetEvaluatedVariable(name string) (string, bool, error) {
if name == "" {
return "", false, fmt.Errorf("blogc: variable name is required")
}
if err := e.validateInputFiles(); err != nil {
return "", false, err
}
cmdArgs := e.generateCommand(name)
statusCode, stdout, stderr, err := blogcRun(e.generateStdin(), cmdArgs...)
if err != nil {
return "", false, err
}
if statusCode != 0 {
if statusCode == 78 { // EX_CONFIG, as of blogc-0.15.2, that is our minimum version
return "", false, nil
}
return "", false, errors.New(strings.TrimSpace(stderr))
}
// remove the last newline, introduced by blogc itself.
// we don't want to remove any whitespace that is part of the variable.
if stdout[len(stdout)-1] == byte('\n') {
return string(stdout[:len(stdout)-1]), true, nil
}
return stdout, true, nil
}