Skip to content

Commit

Permalink
feat: add regexp:bool in rules
Browse files Browse the repository at this point in the history
  • Loading branch information
ShuheiKubota committed Dec 25, 2023
1 parent c6008a4 commit 5013b40
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 15 deletions.
30 changes: 17 additions & 13 deletions detour.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package main
import (
"errors"
"fmt"
"slices"

//"path/filepath"
"os"
"regexp"
"strings"

"encoding/json"
Expand Down Expand Up @@ -41,17 +41,21 @@ func (c globalCmd) Run(args []string) error {

err = json.Unmarshal(data, &rules)
if err != nil {
a := struct {
cover := struct {
Rules []rule `json:"rules"`
}{}
err = json.Unmarshal(data, &a)
err = json.Unmarshal(data, &cover)
if err != nil {
return err
}
rules = a.Rules
rules = cover.Rules
}
}

rules = slices.DeleteFunc(rules, func(r rule) bool {
return strings.TrimSpace(r.Old) == ""
})

if c.DryRun {
fmt.Println("")
fmt.Println("DRY RUN MODE")
Expand Down Expand Up @@ -102,17 +106,16 @@ func (c globalCmd) Run(args []string) error {
}

for _, r := range rules {
re := regexp.MustCompile("(?i)" + r.Old)

after := re.ReplaceAllString(s.TargetPath, r.New)
if s.TargetPath != after {
changed = true
after, tmpchanged := r.Apply(s.TargetPath)
if tmpchanged {
s.TargetPath = after
}
after = re.ReplaceAllString(s.WorkingDirectory, r.New)
if s.WorkingDirectory != after {
changed = true
}

after, tmpchanged = r.Apply(s.WorkingDirectory)
if tmpchanged {
s.WorkingDirectory = after
changed = true
}
}

Expand Down Expand Up @@ -147,6 +150,7 @@ func (c genCmd) Run(args []string) error {
Rules: []rule{
{Name: "C: -> D:", Old: "C:", New: "D:"},
{Name: "detour -> shortcut", Old: "detour", New: "shortcut"},
{Name: "", Old: `\bg.`, New: "go", Regexp: true},
},
}

Expand Down Expand Up @@ -179,7 +183,7 @@ func main() {
app.Name = "detour"
app.Desc = "Windows shortcut replacer tool"
app.Version = Version
app.Usage = `detour --rule-set my_rules.json ./subdir/**/*`
app.Usage = `detour -v --rule-set myrules.json ./subdir/**/*`
app.Copyright = "(C) 2020 Shuhei Kubota"
app.Run(os.Args)

Expand Down
26 changes: 24 additions & 2 deletions rules.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,38 @@
package main

import "github.com/shu-go/nmfmt"
import (
"regexp"

"github.com/shu-go/nmfmt"
)

type rule struct {
Name string `json:"name,omitempty"`
Old string `json:"old,omitempty"`
New string `json:"new,omitempty"`

Regexp bool `json:"regexp,omitempty"`
}

func (r rule) String() string {
if r.Name != "" {
return r.Name
}
return nmfmt.Sprintf("$Old->$New", "Old", r.Old, "New", r.New)
re := ""
if r.Regexp {
re = "(regexp) "
}
return nmfmt.Sprintf("$Regexp$Old -> $New", "Old", r.Old, "New", r.New, "Regexp", re)
}

func (r rule) Apply(s string) (string, bool) {
var re *regexp.Regexp
if r.Regexp {
re = regexp.MustCompile("(?i)" + r.Old)
} else {
re = regexp.MustCompile("(?i)" + regexp.QuoteMeta(r.Old))
}

after := re.ReplaceAllString(s, r.New)
return after, s != after
}

0 comments on commit 5013b40

Please sign in to comment.