diff --git a/cli/tasks/downloadtemplate/main.go b/cli/tasks/downloadtemplate/main.go index 9516ff2..9203cfd 100644 --- a/cli/tasks/downloadtemplate/main.go +++ b/cli/tasks/downloadtemplate/main.go @@ -5,9 +5,11 @@ import ( "fmt" "github.com/maddalax/htmgo/cli/tasks/process" "github.com/maddalax/htmgo/cli/tasks/run" + "github.com/maddalax/htmgo/cli/tasks/util" "os" "os/exec" "path/filepath" + "regexp" "strings" ) @@ -50,7 +52,11 @@ func DownloadTemplate(outPath string) { return } - excludeDir := "starter-template" + templateName := "starter-template" + + re := regexp.MustCompile(`[^a-zA-Z]+`) + // Replace all non-alphabetic characters with an empty string + newModuleName := re.ReplaceAllString(outPath, "") install := exec.Command("git", "clone", "https://github.com/maddalax/htmgo", "--depth=1", outPath) install.Stdout = os.Stdout @@ -62,13 +68,13 @@ func DownloadTemplate(outPath string) { return } - deleteAllExceptTemplate(outPath, excludeDir) + deleteAllExceptTemplate(outPath, templateName) newDir := filepath.Join(cwd, outPath) commands := [][]string{ - {"cp", "-vaR", fmt.Sprintf("%s/.", excludeDir), "."}, - {"rm", "-rf", excludeDir}, + {"cp", "-vaR", fmt.Sprintf("%s/.", templateName), "."}, + {"rm", "-rf", templateName}, {"go", "get", "github.com/maddalax/htmgo/framework"}, {"go", "get", "github.com/maddalax/htmgo/framework-ui"}, {"git", "init"}, @@ -77,8 +83,8 @@ func DownloadTemplate(outPath string) { for _, command := range commands { cmd := exec.Command(command[0], command[1:]...) cmd.Dir = newDir - cmd.Stdout = nil - cmd.Stderr = nil + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr err = cmd.Run() if err != nil { println("Error executing command %s\n", err.Error()) @@ -86,6 +92,10 @@ func DownloadTemplate(outPath string) { } } + _ = util.ReplaceTextInFile(filepath.Join(newDir, "go.mod"), + fmt.Sprintf("module %s", templateName), + fmt.Sprintf("module %s", newModuleName)) + fmt.Printf("Setting up the project in %s\n", newDir) process.SetWorkingDir(newDir) run.Setup() diff --git a/cli/tasks/util/file.go b/cli/tasks/util/file.go new file mode 100644 index 0000000..f434d36 --- /dev/null +++ b/cli/tasks/util/file.go @@ -0,0 +1,16 @@ +package util + +import ( + "os" + "strings" +) + +func ReplaceTextInFile(file string, text string, replacement string) error { + bytes, err := os.ReadFile(file) + if err != nil { + return err + } + str := string(bytes) + updated := strings.ReplaceAll(str, text, replacement) + return os.WriteFile(file, []byte(updated), 0644) +} diff --git a/starter-template/pages/index.go b/starter-template/pages/index.go index ddd3e57..1f16b47 100644 --- a/starter-template/pages/index.go +++ b/starter-template/pages/index.go @@ -17,7 +17,10 @@ func IndexPage(c echo.Context) *h.Page { ), h.Body( h.Class("flex flex-col gap-4"), - h.Div(h.Class("flex flex-col gap-2 mt-6"), + h.Div(h.Class("flex gap-2 mt-6"), + Button(), + Button(), + Button(), Button(), ), ),