fix templating download issue
This commit is contained in:
parent
8a42ea0c80
commit
b7d9477263
2 changed files with 11 additions and 66 deletions
|
|
@ -8,7 +8,6 @@ import (
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func HasFileFromRoot(file string) bool {
|
func HasFileFromRoot(file string) bool {
|
||||||
|
|
@ -75,64 +74,6 @@ func CopyFile(src, dst string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteAllExcept(path string, keepDir string) error {
|
func DeleteDir(dir string) error {
|
||||||
// Get the info of the path
|
return os.RemoveAll(dir)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func DownloadTemplate(outPath string) {
|
func DownloadTemplate(outPath string) {
|
||||||
|
|
@ -36,7 +37,9 @@ func DownloadTemplate(outPath string) {
|
||||||
// Replace all non-alphabetic characters with an empty string
|
// Replace all non-alphabetic characters with an empty string
|
||||||
newModuleName := re.ReplaceAllString(outPath, "")
|
newModuleName := re.ReplaceAllString(outPath, "")
|
||||||
|
|
||||||
install := exec.Command("git", "clone", "https://github.com/maddalax/htmgo", "--depth=1", outPath)
|
tempOut := newModuleName + "_temp_" + time.Now().String()
|
||||||
|
|
||||||
|
install := exec.Command("git", "clone", "https://github.com/maddalax/htmgo", "--depth=1", tempOut)
|
||||||
install.Stdout = os.Stdout
|
install.Stdout = os.Stdout
|
||||||
install.Stderr = os.Stderr
|
install.Stderr = os.Stderr
|
||||||
err := install.Run()
|
err := install.Run()
|
||||||
|
|
@ -46,16 +49,17 @@ func DownloadTemplate(outPath string) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
dirutil.DeleteAllExcept(outPath, templatePath)
|
|
||||||
|
|
||||||
newDir := filepath.Join(cwd, outPath)
|
newDir := filepath.Join(cwd, outPath)
|
||||||
|
|
||||||
dirutil.CopyDir(templatePath, newDir, func(path string, exists bool) bool {
|
dirutil.CopyDir(filepath.Join(tempOut, templatePath), newDir, func(path string, exists bool) bool {
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
|
||||||
|
dirutil.DeleteDir(tempOut)
|
||||||
|
|
||||||
|
process.SetWorkingDir(newDir)
|
||||||
|
|
||||||
commands := [][]string{
|
commands := [][]string{
|
||||||
{"rm", "-rf", templatePath},
|
|
||||||
{"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"},
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue