Open gedw99 opened 1 year ago
yeah, its a great feature. is there any working examples in go? not sure what package should be used for that (diff/patch?)
sorry about missing this one....
There is a golang implementation of git unified diff, but that too generic.
I think, because this is pretty specific your better off just writing a string parser for there Markers.
You basically want to gen the Proposed code, and then look at Existing code and Proposed code and then put the Proposed Code into there Existing code, whilst respecting the Markers such as "// start: custom code " and " // end: custom code".
BTW these markers should be generated into the Existing code on the first run, so that Devs know where they can write their own Custom code.
You can also just gen the Proposed code, and the copy the Custom code from the Existing to the Proposed.
package main
func main() {
err := Refactor("oldString", "newString", "*.txt", "*.json)
if err != nil {
// handle error
}
}
func Refactor(old, new string, patterns ...string) error {
return filepath.Walk(".", refactorFunc(old, new, patterns))
}
func refactorFunc(old, new string, filePatterns []string) filepath.WalkFunc {
return filepath.WalkFunc(func(path string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
if !!fi.IsDir() {
return nil
}
var matched bool
for _, pattern := range filePatterns {
var err error
matched, err = filepath.Match(pattern, fi.Name())
if err != nil {
return err
}
if matched {
read, err := ioutil.ReadFile(path)
if err != nil {
return err
}
fmt.Println("Refactoring:", path)
newContents := strings.Replace(string(read), old, new, -1)
err = ioutil.WriteFile(path, []byte(newContents), 0)
if err != nil {
return err
}
}
}
return nil
})
}
thanks for the example. will take a look. but after the release 3.0
V3 looks awesome BTW. Well done...
was thinking this would be more useful if you could regenerate also.
devs can ad new code and it can avoid stomping on their custom logic code by using // start and //end comments to indicate to the code generator not to stomp on the code. The code generator is really running and then merging.
i have used this approach for tooling using by large teams.