make sure downloading a template replaces the module name
This commit is contained in:
parent
662866df5e
commit
f0f979e3a2
3 changed files with 36 additions and 7 deletions
|
|
@ -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()
|
||||
|
|
|
|||
16
cli/tasks/util/file.go
Normal file
16
cli/tasks/util/file.go
Normal 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)
|
||||
}
|
||||
|
|
@ -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(),
|
||||
),
|
||||
),
|
||||
|
|
|
|||
Loading…
Reference in a new issue