diff --git a/cli/htmgo/runner.go b/cli/htmgo/runner.go index 2933c45..b71ea41 100644 --- a/cli/htmgo/runner.go +++ b/cli/htmgo/runner.go @@ -65,7 +65,7 @@ func main() { css.GenerateCssWatch(process.ExitOnError) }() go func() { - _ = run.Server(process.ExitOnError) + _ = run.Server() }() startWatcher(reloader.OnFileChange) } else { diff --git a/cli/htmgo/tasks/astgen/entry.go b/cli/htmgo/tasks/astgen/entry.go index e229c3b..3609456 100644 --- a/cli/htmgo/tasks/astgen/entry.go +++ b/cli/htmgo/tasks/astgen/entry.go @@ -102,7 +102,7 @@ func findPublicFuncsReturningHPartial(dir string, predicate func(partial Partial if selectorExpr.Sel.Name == "Partial" { p := Partial{ Package: node.Name.Name, - Import: sliceCommonPrefix(cwd, filepath.Dir(path)), + Import: sliceCommonPrefix(cwd, strings.ReplaceAll(filepath.Dir(path), `\`, `/`)), FuncName: funcDecl.Name.Name, } if predicate(p) { @@ -168,7 +168,7 @@ func findPublicFuncsReturningHPage(dir string) ([]Page, error) { if selectorExpr.Sel.Name == "Page" { pages = append(pages, Page{ Package: node.Name.Name, - Import: filepath.Dir(path), + Import: strings.ReplaceAll(filepath.Dir(path), `\`, `/`), Path: path, FuncName: funcDecl.Name.Name, }) @@ -285,9 +285,11 @@ func formatRoute(path string) string { path = strings.TrimSuffix(path, "index.go") path = strings.TrimSuffix(path, ".go") path = strings.TrimPrefix(path, "pages/") + path = strings.TrimPrefix(path, "pages\\") path = strings.ReplaceAll(path, "$", ":") path = strings.ReplaceAll(path, "_", "/") path = strings.ReplaceAll(path, ".", "/") + path = strings.ReplaceAll(path, "\\", "/") if path == "" { return "/" } diff --git a/cli/htmgo/tasks/process/pid_windows.go b/cli/htmgo/tasks/process/pid_windows.go index 3a79bbe..99acc88 100644 --- a/cli/htmgo/tasks/process/pid_windows.go +++ b/cli/htmgo/tasks/process/pid_windows.go @@ -1,8 +1,11 @@ package process import ( + "fmt" "os" "os/exec" + "strconv" + "time" ) import "golang.org/x/sys/windows" @@ -10,7 +13,9 @@ func KillProcess(process *os.Process) error { if process == nil { return nil } - return process.Kill() + Run(fmt.Sprintf("taskkill /F /T /PID %s", strconv.Itoa(process.Pid))) + time.Sleep(time.Millisecond * 50) + return nil } func PrepareCommand(command *exec.Cmd) { diff --git a/cli/htmgo/tasks/process/process.go b/cli/htmgo/tasks/process/process.go index ddd332b..2e6a340 100644 --- a/cli/htmgo/tasks/process/process.go +++ b/cli/htmgo/tasks/process/process.go @@ -54,7 +54,7 @@ func shouldSkipKilling(flags []RunFlag, skipFlag []RunFlag) bool { func KillAll(skipFlag ...RunFlag) { tries := 0 - removeIndexes := make([]int, 0) + updatedCommands := make([]CmdWithFlags, len(commands)) for { tries++ allFinished := true @@ -66,12 +66,13 @@ func KillAll(skipFlag ...RunFlag) { args := strings.Join(cmd.cmd.Args, " ") slog.Debug("process is not running after 50 tries, breaking.", slog.String("command", args)) allFinished = true - removeIndexes = append(removeIndexes, i) break } else { time.Sleep(time.Millisecond * 50) continue } + } else { + updatedCommands[i] = cmd } } if allFinished { @@ -79,8 +80,11 @@ func KillAll(skipFlag ...RunFlag) { } } - for i := len(removeIndexes) - 1; i >= 0; i-- { - commands = append(commands[:removeIndexes[i]], commands[removeIndexes[i]+1:]...) + commands = make([]CmdWithFlags, 0) + for _, command := range updatedCommands { + if command.cmd != nil && command.cmd.Process != nil { + commands = append(commands, command) + } } for _, command := range commands { diff --git a/framework/h/app.go b/framework/h/app.go index 29f4f33..ab43b73 100644 --- a/framework/h/app.go +++ b/framework/h/app.go @@ -7,6 +7,8 @@ import ( "github.com/maddalax/htmgo/framework/internal/process" "github.com/maddalax/htmgo/framework/service" "log/slog" + "os/exec" + "runtime" "time" ) @@ -94,7 +96,13 @@ func (a App) start() { // and try again if IsDevelopment() && IsWatchMode() { slog.Info("Port already in use, trying to kill the process and start again") - process.RunOrExit(fmt.Sprintf("kill -9 $(lsof -t -i%s)", port)) + if runtime.GOOS == "windows" { + cmd := exec.Command("cmd", "/C", fmt.Sprintf(`for /F "tokens=5" %%i in ('netstat -aon ^| findstr :%s') do taskkill /F /PID %%i`, port)) + cmd.Run() + } else { + process.RunOrExit(fmt.Sprintf("kill -9 $(lsof -t -i%s)", port)) + } + time.Sleep(time.Millisecond * 50) err = a.Echo.Start(port) if err != nil { diff --git a/htmgo-site/main.go b/htmgo-site/main.go index 7f1eb24..06ed2f9 100644 --- a/htmgo-site/main.go +++ b/htmgo-site/main.go @@ -42,8 +42,7 @@ func main() { } }) - __htmgo.RegisterPartials(e) - __htmgo.RegisterPages(e) + __htmgo.Register(e) pages.RegisterMarkdown(e, "md", MarkdownAssets, func(ctx echo.Context, path string) error { return pages.MarkdownHandler(ctx.(*h.RequestContext), path, "")