make sure downloading a template replaces the module name

This commit is contained in:
maddalax 2024-09-18 10:04:17 -05:00
parent 662866df5e
commit f0f979e3a2
3 changed files with 36 additions and 7 deletions

View file

@ -5,9 +5,11 @@ import (
"fmt" "fmt"
"github.com/maddalax/htmgo/cli/tasks/process" "github.com/maddalax/htmgo/cli/tasks/process"
"github.com/maddalax/htmgo/cli/tasks/run" "github.com/maddalax/htmgo/cli/tasks/run"
"github.com/maddalax/htmgo/cli/tasks/util"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"regexp"
"strings" "strings"
) )
@ -50,7 +52,11 @@ func DownloadTemplate(outPath string) {
return 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 := exec.Command("git", "clone", "https://github.com/maddalax/htmgo", "--depth=1", outPath)
install.Stdout = os.Stdout install.Stdout = os.Stdout
@ -62,13 +68,13 @@ func DownloadTemplate(outPath string) {
return return
} }
deleteAllExceptTemplate(outPath, excludeDir) deleteAllExceptTemplate(outPath, templateName)
newDir := filepath.Join(cwd, outPath) newDir := filepath.Join(cwd, outPath)
commands := [][]string{ commands := [][]string{
{"cp", "-vaR", fmt.Sprintf("%s/.", excludeDir), "."}, {"cp", "-vaR", fmt.Sprintf("%s/.", templateName), "."},
{"rm", "-rf", excludeDir}, {"rm", "-rf", templateName},
{"go", "get", "github.com/maddalax/htmgo/framework"}, {"go", "get", "github.com/maddalax/htmgo/framework"},
{"go", "get", "github.com/maddalax/htmgo/framework-ui"}, {"go", "get", "github.com/maddalax/htmgo/framework-ui"},
{"git", "init"}, {"git", "init"},
@ -77,8 +83,8 @@ func DownloadTemplate(outPath string) {
for _, command := range commands { for _, command := range commands {
cmd := exec.Command(command[0], command[1:]...) cmd := exec.Command(command[0], command[1:]...)
cmd.Dir = newDir cmd.Dir = newDir
cmd.Stdout = nil cmd.Stdout = os.Stdout
cmd.Stderr = nil cmd.Stderr = os.Stderr
err = cmd.Run() err = cmd.Run()
if err != nil { if err != nil {
println("Error executing command %s\n", err.Error()) 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) fmt.Printf("Setting up the project in %s\n", newDir)
process.SetWorkingDir(newDir) process.SetWorkingDir(newDir)
run.Setup() run.Setup()

16
cli/tasks/util/file.go Normal file
View file

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

View file

@ -17,7 +17,10 @@ func IndexPage(c echo.Context) *h.Page {
), ),
h.Body( h.Body(
h.Class("flex flex-col gap-4"), 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(), Button(),
), ),
), ),