diff --git a/.gitignore b/.gitignore index b66eed5..5f752a3 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ js/node_modules go.work go.work.sum .idea -!framework/assets/dist \ No newline at end of file +!framework/assets/dist +/**/__htmgo \ No newline at end of file diff --git a/cli/htmgo/internal/dirutil/dir.go b/cli/htmgo/internal/dirutil/dir.go index 466f599..30bd77b 100644 --- a/cli/htmgo/internal/dirutil/dir.go +++ b/cli/htmgo/internal/dirutil/dir.go @@ -8,6 +8,7 @@ import ( "log/slog" "os" "path/filepath" + "strings" ) func HasFileFromRoot(file string) bool { @@ -73,3 +74,65 @@ func CopyFile(src, dst string) error { } return nil } + +func DeleteAllExcept(path string, keepDir string) error { + // Get the info of the path + info, err := os.Stat(path) + if err != nil { + return err + } + + if !info.IsDir() { + // We only care about directories + return nil + } + + // If path is the keepDir, do nothing + if path == keepDir { + return nil + } + + // Check if keepDir is under path (path is an ancestor of keepDir) + relToKeepDir, err := filepath.Rel(path, keepDir) + if err != nil { + return err + } + + if !strings.HasPrefix(relToKeepDir, "..") { + // keepDir is under path; recurse into subdirectories + entries, err := os.ReadDir(path) + if err != nil { + return err + } + + for _, entry := range entries { + entryPath := filepath.Join(path, entry.Name()) + err := DeleteAllExcept(entryPath, keepDir) + if err != nil { + return err + } + } + } else { + // Check if path is under keepDir (path is a descendant of keepDir) + relFromKeepDir, err := filepath.Rel(keepDir, path) + if err != nil { + return err + } + + if !strings.HasPrefix(relFromKeepDir, "..") { + // path is under keepDir; do nothing + return nil + } else { + // path is neither keepDir nor related; delete it + slog.Debug("Deleting directory:", slog.String("path", path)) + err = os.RemoveAll(path) + if err != nil { + return err + } + // Skip further traversal since the directory is deleted + return nil + } + } + + return nil +} diff --git a/cli/htmgo/tasks/downloadtemplate/main.go b/cli/htmgo/tasks/downloadtemplate/main.go index 1e4aa7e..d53c4ea 100644 --- a/cli/htmgo/tasks/downloadtemplate/main.go +++ b/cli/htmgo/tasks/downloadtemplate/main.go @@ -3,6 +3,7 @@ package downloadtemplate import ( "flag" "fmt" + "github.com/maddalax/htmgo/cli/htmgo/internal/dirutil" "github.com/maddalax/htmgo/cli/htmgo/tasks/process" "github.com/maddalax/htmgo/cli/htmgo/tasks/run" "github.com/maddalax/htmgo/cli/htmgo/tasks/util" @@ -13,31 +14,6 @@ import ( "strings" ) -func deleteAllExceptTemplate(outPath string, excludeDir string) { - // List all files and directories in the root folder - files, err := os.ReadDir(outPath) - if err != nil { - fmt.Printf("Error reading directory: %v\n", err) - return - } - - // Iterate through each item in the root folder - for _, file := range files { - // Skip the excluded directory - if file.Name() == excludeDir { - continue - } - - // Get full path - fullPath := filepath.Join(outPath, file.Name()) - - err := os.RemoveAll(fullPath) - if err != nil { - fmt.Printf("Error removing %s: %v\n", fullPath, err) - } - } -} - func DownloadTemplate(outPath string) { cwd, _ := os.Getwd() @@ -69,13 +45,16 @@ func DownloadTemplate(outPath string) { return } - deleteAllExceptTemplate(outPath, templatePath) + dirutil.DeleteAllExcept(outPath, templatePath) newDir := filepath.Join(cwd, outPath) + dirutil.CopyDir(templatePath, newDir, func(path string, exists bool) bool { + return true + }) + commands := [][]string{ - {"cp", "-vaR", fmt.Sprintf("%s/.", templateName), "."}, - {"rm", "-rf", templateName}, + {"rm", "-rf", templatePath}, {"go", "get", "github.com/maddalax/htmgo/framework"}, {"go", "get", "github.com/maddalax/htmgo/framework-ui"}, {"git", "init"}, diff --git a/templates/starter/.gitignore b/templates/starter/.gitignore index 864f165..3d6a979 100644 --- a/templates/starter/.gitignore +++ b/templates/starter/.gitignore @@ -2,3 +2,5 @@ tmp node_modules .idea +__htmgo +dist \ No newline at end of file diff --git a/templates/starter/__htmgo/pages-generated.go b/templates/starter/__htmgo/pages-generated.go deleted file mode 100644 index 7a2c8ad..0000000 --- a/templates/starter/__htmgo/pages-generated.go +++ /dev/null @@ -1,13 +0,0 @@ -// Package __htmgo THIS FILE IS GENERATED. DO NOT EDIT. -package __htmgo - -import "github.com/labstack/echo/v4" -import "github.com/maddalax/htmgo/framework/h" -import "starter-template/pages" - -func RegisterPages(f *echo.Echo) { - f.GET("/", func(ctx echo.Context) error { - cc := ctx.(*h.RequestContext) - return h.HtmlView(ctx, pages.IndexPage(cc)) - }) -} diff --git a/templates/starter/__htmgo/partials-generated.go b/templates/starter/__htmgo/partials-generated.go deleted file mode 100644 index 4604eec..0000000 --- a/templates/starter/__htmgo/partials-generated.go +++ /dev/null @@ -1,25 +0,0 @@ -// Package __htmgo THIS FILE IS GENERATED. DO NOT EDIT. -package __htmgo - -import "github.com/maddalax/htmgo/framework/h" -import "github.com/labstack/echo/v4" -import "starter-template/partials" - -func GetPartialFromContext(ctx echo.Context) *h.Partial { - path := ctx.Request().URL.Path - if path == "CounterPartial" || path == "/starter-template/partials.CounterPartial" { - cc := ctx.(*h.RequestContext) - return partials.CounterPartial(cc) - } - return nil -} - -func RegisterPartials(f *echo.Echo) { - f.Any("starter-template/partials*", func(ctx echo.Context) error { - partial := GetPartialFromContext(ctx) - if partial == nil { - return ctx.NoContent(404) - } - return h.PartialView(ctx, partial) - }) -} diff --git a/templates/starter/__htmgo/setup-generated.go b/templates/starter/__htmgo/setup-generated.go deleted file mode 100644 index 1bf4b9c..0000000 --- a/templates/starter/__htmgo/setup-generated.go +++ /dev/null @@ -1,11 +0,0 @@ -// Package __htmgo THIS FILE IS GENERATED. DO NOT EDIT. -package __htmgo - -import ( - "github.com/labstack/echo/v4" -) - -func Register(e *echo.Echo) { - RegisterPartials(e) - RegisterPages(e) -}