5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-02 08:41:41 +08:00

feat(cmd/init): change default module name to project name

This commit is contained in:
twacqwq 2024-03-11 15:11:00 +08:00
parent c5381dce70
commit 504d951759
5 changed files with 83 additions and 0 deletions

View File

@ -11,6 +11,7 @@ type Init struct {
InitGit bool `name:"g" description:"Initialise git repository"`
IDE string `name:"ide" description:"Generate IDE project files"`
List bool `name:"l" description:"List templates"`
InitModule bool `name:"m" description:"Change Default go.mod module name to project Name. default: changeme"`
}
func (i *Init) Default() *Init {

View File

@ -14,6 +14,7 @@ import (
"github.com/pkg/errors"
"github.com/pterm/pterm"
"github.com/wailsapp/wails/v2/cmd/wails/flags"
"github.com/wailsapp/wails/v2/cmd/wails/internal/gomod"
"github.com/wailsapp/wails/v2/internal/colour"
"github.com/wailsapp/wails/v2/pkg/buildassets"
"github.com/wailsapp/wails/v2/pkg/clilogger"
@ -125,6 +126,14 @@ func initProject(f *flags.Init) error {
return err
}
if f.InitModule {
// Change the module name to project name
err = gomod.ChangeModuleName(options.ProjectName)
if err != nil {
return err
}
}
if !f.CIMode {
// Run `go mod tidy` to ensure `go.sum` is up to date
cmd := exec.Command("go", "mod", "tidy")

View File

@ -62,6 +62,28 @@ func SyncGoMod(logger *clilogger.CLILogger, updateWailsVersion bool) error {
return nil
}
func ChangeModuleName(newModuleName string) error {
cwd, err := os.Getwd()
if err != nil {
return err
}
gomodFilename := fs.FindFileInParents(cwd, "go.mod")
if gomodFilename == "" {
return fmt.Errorf("no go.mod file found")
}
gomodData, err := os.ReadFile(gomodFilename)
if err != nil {
return err
}
gomodData, err = gomod.UpdateGoModuleName(gomodData, newModuleName)
if err != nil {
return err
}
return os.WriteFile(gomodFilename, gomodData, 0o755)
}
func LogGreen(message string, args ...interface{}) {
text := fmt.Sprintf(message, args...)
println(colour.Green(text))

View File

@ -112,3 +112,13 @@ func SyncGoVersion(goModText []byte, goVersion string) ([]byte, bool, error) {
return goModText, true, nil
}
func UpdateGoModuleName(goModText []byte, newModuleName string) ([]byte, error) {
file, err := modfile.Parse("", goModText, nil)
if err != nil {
return nil, err
}
file.Module.Syntax.Token[1] = newModuleName
return file.Format()
}

View File

@ -137,3 +137,44 @@ func TestUpdateGoModGoVersion(t *testing.T) {
})
}
}
const beforeGoModName string = `module changeme
go 1.19
require github.com/wailsapp/wails/v2 v2.0.0-beta.7
`
const afterGoModName string = `module myproject
go 1.19
require github.com/wailsapp/wails/v2 v2.0.0-beta.7
`
func TestUpdateGoModName(t *testing.T) {
is2 := is.New(t)
type args struct {
goModText []byte
changeModName string
}
tests := []struct {
name string
args args
want []byte
}{
{"default1", args{[]byte(beforeGoModName), "myproject"}, []byte(afterGoModName)},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := UpdateGoModuleName(tt.args.goModText, tt.args.changeModName)
if err != nil {
t.Errorf("UpdateGoModuleName() error = %v", err)
return
}
is2.Equal(got, tt.want)
})
}
}