-
Notifications
You must be signed in to change notification settings - Fork 0
/
trinsic_postprocessor.go
89 lines (75 loc) · 2.25 KB
/
trinsic_postprocessor.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
package main
import (
"fmt"
pgs "github.com/lyft/protoc-gen-star"
"os"
"regexp"
"strings"
)
type trinsicPostProcessor struct {
inputFile string
outputFile string
}
func (tpp trinsicPostProcessor) Match(a pgs.Artifact) bool {
switch a.(type) {
case pgs.GeneratorTemplateFile:
return true
default:
return false
}
}
func (tpp trinsicPostProcessor) Process(in []byte) ([]byte, error) {
// Determine which file it is
templateFileString := string(in)
templateLines := strings.Split(templateFileString, "\n")
r, err := regexp.Compile("target: ([\\w\\d\\-\\\\\\/\\.\\:]*)")
if err != nil {
panic(err)
}
matches := r.FindStringSubmatch(templateFileString)
//fmt.Fprintln(os.Stderr, matches)
//fmt.Fprintln(os.Stderr, templateLines)
// Write the generated data to the appropriate file
err = updateTargetFile(matches[1], templateLines)
if err != nil {
return nil, err
}
return in, nil
}
func updateTargetFile(targetPath string, templateLines []string) error {
fmt.Fprintf(os.Stderr, "Target file: %s\n", targetPath)
fileBytes, err := os.ReadFile(targetPath)
if err != nil {
return err
}
firstLine := strings.TrimSpace(templateLines[0])
lastLine := strings.TrimSpace(templateLines[len(templateLines)-1])
//fmt.Fprintln(os.Stderr, firstLine, lastLine)
fileLines := strings.Split(string(fileBytes), "\n")
newLines := make([]string, 0)
for idx, line := range fileLines {
testLine := strings.TrimSpace(line)
//fmt.Fprintln(os.Stderr, testLine+" :: "+firstLine)
if strings.HasPrefix(testLine, firstLine) {
//fmt.Fprintln(os.Stderr, fileLines[0:idx])
newLines = append(newLines, fileLines[0:idx]...)
newLines = append(newLines, templateLines...)
} else if strings.HasPrefix(testLine, lastLine) {
//fmt.Fprintln(os.Stderr, fileLines[idx+1:])
newLines = append(newLines, fileLines[idx+1:]...)
}
}
// If we didn't find anything, don't write anything
if len(newLines) == 0 {
fmt.Fprintln(os.Stderr, "No target comments found for file: "+targetPath)
return nil
}
outputString := strings.Join(newLines, "\n")
//fmt.Fprintln(os.Stderr, outputString)
err = os.WriteFile(targetPath, []byte(outputString), 0777)
if err != nil {
return err
}
return nil
}
func applyTemplateFiles() pgs.PostProcessor { return trinsicPostProcessor{} }