github.com/JarrahG/buffalocli@v0.0.0-20230801092127-b85bfd5d395a/internal/genny/fix/action.go (about)

     1  package fix
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/gobuffalo/genny/v2"
     8  	"github.com/gobuffalo/genny/v2/gogen"
     9  )
    10  
    11  // ReplaceAppOnce fixes `actions/app.go` to fix the double execution issue.
    12  // it covers https://github.com/gobuffalo/buffalo/issues/1653 and
    13  // https://github.com/JarrahG/buffalocli/issues/228
    14  func ReplaceAppOnce(opts *Options) genny.RunFn {
    15  	return func(r *genny.Runner) error {
    16  		fmt.Println("~~~ Apply AppOnce ~~~")
    17  
    18  		file, err := r.FindFile("actions/app.go")
    19  		if err != nil {
    20  			return err
    21  		}
    22  
    23  		if !strings.Contains(file.String(), "appOnce.Do") {
    24  			file, err = gogen.ReplaceBlock(file, "if app == nil {", "}", "appOnce.Do(func() {", "})")
    25  			if err != nil {
    26  				return err
    27  			}
    28  
    29  			file, err = gogen.AddGlobal(file, "appOnce sync.Once")
    30  			if err != nil {
    31  				return err
    32  			}
    33  
    34  			file, err = gogen.AddImport(file, "sync")
    35  			if err != nil {
    36  				return err
    37  			}
    38  		}
    39  
    40  		return r.File(file)
    41  	}
    42  }