This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
7 changed files
with
141 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package fix | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/gobuffalo/genny/v2" | ||
"github.com/gobuffalo/genny/v2/gogen" | ||
) | ||
|
||
// ReplaceOldImports walks all the .go files in an application | ||
// It will then attempt to convert any old import paths to any new import paths | ||
// used by this version Buffalo. | ||
func ReplaceAppOnce(opts *Options) genny.RunFn { | ||
return func(r *genny.Runner) error { | ||
fmt.Println("~~~ Apply AppOnce ~~~") | ||
|
||
file, err := r.FindFile("actions/app.go") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !strings.Contains(file.String(), "appOnce.Do") { | ||
file, err = gogen.ReplaceBlock(file, "if app == nil {", "}", "appOnce.Do(func() {", "})") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
file, err = gogen.AddGlobal(file, "appOnce sync.Once") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
file, err = gogen.AddImport(file, "sync") | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return r.File(file) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package fix | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/gobuffalo/genny/v2/gentest" | ||
"github.com/gobuffalo/meta" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_ReplaceAppOnce(t *testing.T) { | ||
r := require.New(t) | ||
|
||
tt := []struct { | ||
Name string | ||
OK bool | ||
}{ | ||
{"buffalo0_11", false}, | ||
{"buffaloPre0_18api", true}, | ||
{"buffaloPre0_18web", true}, | ||
} | ||
|
||
for _, tc := range tt { | ||
t.Run(tc.Name, func(t *testing.T) { | ||
run := gentest.NewRunner() | ||
|
||
err := run.Disk.AddFS(os.DirFS(filepath.Join("_fixtures", tc.Name))) | ||
r.NoError(err) | ||
|
||
opts := &Options{ | ||
App: meta.Named("coke", "."), | ||
} | ||
g := ReplaceAppOnce(opts) | ||
run.WithRun(g) | ||
|
||
if tc.OK { | ||
r.NoError(run.Run()) | ||
f, err := run.FindFile("actions/app.go") | ||
r.NoError(err) | ||
|
||
//fmt.Println("XXX =====", f.String()) | ||
r.Contains(f.String(), "appOnce", "files in vendor directory should not be changed") | ||
} else { | ||
r.Error(run.Run()) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters