Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support replacement capture groups #707

Merged
merged 9 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/unreleased/added-20240907-150508.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: added
body: Rework replacement to support grouping
time: 2024-09-07T15:05:08.518032783-07:00
custom:
Issue: "702"
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
with:
# Required: the version of golangci-lint is required and must be specified
# without patch version: we always use the latest patch version.
version: v1.58
version: v1.60

- name: Gen
run: go run main.go gen
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ coverage.html
site/
venv/
docs/cli/
!docs/cli/.gitkeep
docs/config/

# goreleaser builds
dist/
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ lint: # Run linters
goimports -w -local github.com/miniscruff/changie .
golangci-lint run ./...

.PHONY: format
format: lint # alias for lint

.PHONY: gen
gen: # Generate config and CLI docs
go run main.go gen
Expand Down
6 changes: 3 additions & 3 deletions cmd/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ func (b *Batch) getBatchData() (*core.BatchData, error) {
Version: currentVersion.Original(),
VersionNoPrefix: currentVersion.String(),
PreviousVersion: previousVersion.Original(),
Major: int(currentVersion.Major()),
Minor: int(currentVersion.Minor()),
Patch: int(currentVersion.Patch()),
Major: int(currentVersion.Major()), //nolint:gosec
Minor: int(currentVersion.Minor()), //nolint:gosec
Patch: int(currentVersion.Patch()), //nolint:gosec
Prerelease: currentVersion.Prerelease(),
Metadata: currentVersion.Metadata(),
Changes: allChanges,
Expand Down
14 changes: 13 additions & 1 deletion cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (

"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"

"github.com/miniscruff/changie/core"
)

const fmTemplate = `---
Expand Down Expand Up @@ -75,9 +77,19 @@ func NewGen() *Gen {
}

func (g *Gen) Run(cmd *cobra.Command, args []string) error {
err := os.MkdirAll(filepath.Join("docs", "config"), core.CreateDirMode)
if err != nil {
return fmt.Errorf("creating docs/config directory: %w", err)
}

err = os.MkdirAll(filepath.Join("docs", "cli"), core.CreateDirMode)
if err != nil {
return fmt.Errorf("creating docs/cli directory: %w", err)
}

file, err := os.Create(filepath.Join("docs", "config", "index.md"))
if err != nil {
return fmt.Errorf("unable to create or open config index: %w", err)
return fmt.Errorf("creating or opening config index: %w", err)
}

defer file.Close()
Expand Down
9 changes: 0 additions & 9 deletions cmd/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,6 @@ func TestCanGetFiles(t *testing.T) {
then.FileExists(t, configDocsPath)
}

func TestErrorGenIfContentPathIsMissing(t *testing.T) {
cmd := RootCmd()

then.WithTempDir(t)

cmd.SetArgs([]string{"gen"})
then.NotNil(t, cmd.Execute())
}

func TestErrorGenBadWriteTypesWriter(t *testing.T) {
w := then.NewErrWriter()
parent := TypeProps{
Expand Down
6 changes: 3 additions & 3 deletions cmd/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ func (m *Merge) mergeProject(
replaceData := core.ReplaceData{
Version: version.Original(),
VersionNoPrefix: version.String(),
Major: int(version.Major()),
Minor: int(version.Minor()),
Patch: int(version.Patch()),
Major: int(version.Major()), //nolint:gosec
Minor: int(version.Minor()), //nolint:gosec
Patch: int(version.Patch()), //nolint:gosec
Prerelease: version.Prerelease(),
Metadata: version.Metadata(),
}
Expand Down
13 changes: 7 additions & 6 deletions core/replacement.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import (
"os"
"regexp"
"text/template"

"github.com/icholy/replace"
"golang.org/x/text/transform"
)

// Template data used for replacing version values.
Expand Down Expand Up @@ -44,6 +41,7 @@ type Replacement struct {
// Path of the file to find and replace in.
Path string `yaml:"path" required:"true"`
// Regular expression to search for in the file.
// Capture groups are supported and can be used in the replace value.
Find string `yaml:"find" required:"true"`
// Template string to replace the line with.
Replace string `yaml:"replace" required:"true" templateType:"ReplaceData"`
Expand Down Expand Up @@ -79,9 +77,12 @@ func (r Replacement) Execute(data ReplaceData) error {
flags = "m"
}

regexString := fmt.Sprintf("(?%s)%s", flags, r.Find)
transformer := replace.Regexp(regexp.MustCompile(regexString), buf.Bytes())
newData, _, _ := transform.Bytes(transformer, fileData)
regex, err := regexp.Compile(fmt.Sprintf("(?%s)%s", flags, r.Find))
if err != nil {
return err
}

newData := regex.ReplaceAll(fileData, buf.Bytes())

err = os.WriteFile(r.Path, newData, CreateFileMode)
if err != nil {
Expand Down
25 changes: 25 additions & 0 deletions core/replacement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,31 @@ func TestErrorBadTemplateParse(t *testing.T) {
then.NotNil(t, err)
}

func TestErrorBadRegex(t *testing.T) {
then.WithTempDir(t)

filepath := "insensitive.txt"
startData := `# yaml file
Version: 0.0.1
level1:
level2:
version: 0.0.1
`

err := os.WriteFile(filepath, []byte(startData), os.ModePerm)
then.Nil(t, err)

rep := Replacement{
Path: filepath,
Find: "a(b",
Flags: "im",
}
err = rep.Execute(ReplaceData{
VersionNoPrefix: "1.2.3",
})
then.NotNil(t, err)
}

func TestErrorBadTemplateExec(t *testing.T) {
then.WithTempDir(t)

Expand Down
Empty file removed docs/cli/.gitkeep
Empty file.
Loading
Loading