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

gen cylces #2

Open
gedw99 opened this issue May 1, 2023 · 4 comments
Open

gen cylces #2

gedw99 opened this issue May 1, 2023 · 4 comments

Comments

@gedw99
Copy link

gedw99 commented May 1, 2023

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.

@halturin
Copy link
Contributor

halturin commented May 2, 2023

yeah, its a great feature. is there any working examples in go? not sure what package should be used for that (diff/patch?)

@gedw99
Copy link
Author

gedw99 commented Jun 27, 2024

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
	})
}

@halturin
Copy link
Contributor

thanks for the example. will take a look. but after the release 3.0

@gedw99
Copy link
Author

gedw99 commented Jun 27, 2024

V3 looks awesome BTW. Well done...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants