Debian Codegraph @

core/replacement.go

raw
1 package core
2
3 import (
4 "bytes"
5 "errors"
6 "fmt"
7 "os"
8 "path/filepath"
9 "regexp"
10 "text/template"
11 )
12
13 var ErrNoReplacementFilesFound = errors.New("glob pattern did not match any files")
14
15 // Template data used for replacing version values.
16 type ReplaceData struct {
17 // Version of the release, will include "v" prefix if used
18 Version string
19 // Version of the release without the "v" prefix if used
20 VersionNoPrefix string
21 // Major value of the version
22 Major int
23 // Minor value of the version
24 Minor int
25 // Patch value of the version
26 Patch int
27 // Prerelease value of the version
28 Prerelease string
29 // Metadata value of the version
30 Metadata string
31 }
32
33 // Replacement handles the finding and replacing values when merging the changelog.
34 // This can be used to keep version strings in-sync when preparing a release without having to
35 // manually update them.
36 // This works similar to the find and replace from IDE tools but also includes the file path of the
37 // file.
38 // example: yaml
39 // # NodeJS package.json
40 // replacements:
41 // - path: package.json
42 // find: ' "version": ".*",'
43 // replace: ' "version": "{{.VersionNoPrefix}}",'
44 type Replacement struct {
45 // Path of the file to find and replace in.
46 // Also supports Go filepath globs.
47 // example: yaml
48 // # Will match any .json file in the current directory
49 // replacements:
50 // - path: *.json
51 // find: ' "version": ".*",'
52 // replace: ' "version": "{{.VersionNoPrefix}}",'
53 Path string `yaml:"path" required:"true"`
54 // Regular expression to search for in the file.
55 // Capture groups are supported and can be used in the replace value.
56 Find string `yaml:"find" required:"true"`
57 // Template string to replace the line with.
58 Replace string `yaml:"replace" required:"true" templateType:"ReplaceData"`
59 // Optional regular expression mode flags.
60 // Defaults to the m flag for multiline such that ^ and $ will match the start and end of each line
61 // and not just the start and end of the string.
62 //
63 // For more details on regular expression flags in Go view the
64 // [regexp/syntax](https://pkg.go.dev/regexp/syntax).
65 Flags string `yaml:"flags,omitempty" default:"m"`
66 }
67
68 func (r Replacement) Execute(data ReplaceData) error {
69 templ, err := template.New("replacement").Parse(r.Replace)
70 if err != nil {
71 return err
72 }
73
74 var buf bytes.Buffer
75
76 err = templ.Execute(&buf, data)
77 if err != nil {
78 return err
79 }
80
81 globs, err := filepath.Glob(r.Path)
82 if err != nil {
83 return err
84 }
85
86 if len(globs) == 0 {
87 return fmt.Errorf("%w: %s", ErrNoReplacementFilesFound, r.Path)
88 }
89
90 flags := r.Flags
91 if flags == "" {
92 flags = "m"
93 }
94
95 regex, err := regexp.Compile(fmt.Sprintf("(?%s)%s", flags, r.Find))
96 if err != nil {
97 return err
98 }
99
100 for _, path := range globs {
101 fileData, err := os.ReadFile(path)
102 if err != nil {
103 return err
104 }
105
106 newData := regex.ReplaceAll(fileData, buf.Bytes())
107
108 // Replacement paths come from the user's own config, writing them is the point.
109 // #nosec G304,G703
110 err = os.WriteFile(path, newData, CreateFileMode)
111 if err != nil {
112 return err
113 }
114 }
115
116 return nil
117 }