From 45d8e58faa2c166cfee402c3dca1a57005dfdb3e Mon Sep 17 00:00:00 2001 From: maddalax Date: Mon, 16 Sep 2024 20:40:56 -0500 Subject: [PATCH] some fixes --- cli/runner.go | 10 ++-------- cli/tasks/astgen/entry.go | 7 ++++--- cli/tasks/astgen/writer.go | 3 ++- cli/tasks/copyassets/bundle.go | 11 ++++------- cli/tasks/downloadtemplate/main.go | 7 +++++++ cli/tasks/process/process.go | 23 +++++++++++++++++++++++ cli/tasks/run/setup.go | 16 ++++++++++++++++ cli/watcher.go | 8 ++++++++ 8 files changed, 66 insertions(+), 19 deletions(-) create mode 100644 cli/tasks/run/setup.go diff --git a/cli/runner.go b/cli/runner.go index 285f395..351dc6a 100644 --- a/cli/runner.go +++ b/cli/runner.go @@ -5,10 +5,8 @@ import ( "flag" "fmt" "github.com/maddalax/htmgo/cli/tasks/astgen" - "github.com/maddalax/htmgo/cli/tasks/copyassets" "github.com/maddalax/htmgo/cli/tasks/css" "github.com/maddalax/htmgo/cli/tasks/downloadtemplate" - "github.com/maddalax/htmgo/cli/tasks/process" "github.com/maddalax/htmgo/cli/tasks/reloader" "github.com/maddalax/htmgo/cli/tasks/run" "os" @@ -57,11 +55,7 @@ func main() { startWatcher(reloader.OnFileChange) } else { if taskName == "setup" { - process.RunOrExit("go mod download") - process.RunOrExit("go mod tidy") - copyassets.CopyAssets() - _ = astgen.GenAst(true) - _ = css.GenerateCss(true) + run.Setup() } else if taskName == "css" { _ = css.GenerateCss(true) } else if taskName == "ast" { @@ -83,7 +77,7 @@ func main() { } else { fmt.Println(fmt.Sprintf("Usage: htmgo [%s]", strings.Join(commands, " | "))) } - os.Exit(1) + os.Exit(0) } <-done diff --git a/cli/tasks/astgen/entry.go b/cli/tasks/astgen/entry.go index 4f55430..5e4e7c4 100644 --- a/cli/tasks/astgen/entry.go +++ b/cli/tasks/astgen/entry.go @@ -2,6 +2,7 @@ package astgen import ( "fmt" + "github.com/maddalax/htmgo/cli/tasks/process" "go/ast" "go/parser" "go/token" @@ -56,7 +57,7 @@ func sliceCommonPrefix(dir1, dir2 string) string { func findPublicFuncsReturningHPartial(dir string) ([]Partial, error) { var partials []Partial - cwd, _ := os.Getwd() + cwd := process.GetWorkingDir() // Walk through the directory to find all Go files. err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { @@ -235,7 +236,7 @@ func buildGetPartialFromContext(builder *CodeBuilder, partials []Partial) { } func writePartialsFile() { - cwd, _ := os.Getwd() + cwd := process.GetWorkingDir() partialPath := filepath.Join(cwd, "partials") partials, err := findPublicFuncsReturningHPartial(partialPath) if err != nil { @@ -340,7 +341,7 @@ func writePagesFile() { } func GetModuleName() string { - wd, _ := os.Getwd() + wd := process.GetWorkingDir() modPath := filepath.Join(wd, "go.mod") goModBytes, err := os.ReadFile(modPath) if err != nil { diff --git a/cli/tasks/astgen/writer.go b/cli/tasks/astgen/writer.go index a600354..e2a1822 100644 --- a/cli/tasks/astgen/writer.go +++ b/cli/tasks/astgen/writer.go @@ -1,6 +1,7 @@ package astgen import ( + "github.com/maddalax/htmgo/cli/tasks/process" "go/ast" "go/format" "go/parser" @@ -12,7 +13,7 @@ import ( ) func WriteFile(path string, cb func(content *ast.File) string) { - currentDir, err := os.Getwd() + currentDir := process.GetWorkingDir() path = filepath.Join(currentDir, path) diff --git a/cli/tasks/copyassets/bundle.go b/cli/tasks/copyassets/bundle.go index 60fadb5..f1ff820 100644 --- a/cli/tasks/copyassets/bundle.go +++ b/cli/tasks/copyassets/bundle.go @@ -2,6 +2,7 @@ package copyassets import ( "fmt" + "github.com/maddalax/htmgo/cli/tasks/process" "golang.org/x/mod/modfile" "io" "log" @@ -11,13 +12,13 @@ import ( func getModuleVersion(modulePath string) (string, error) { // Read the go.mod file - data, err := os.ReadFile("go.mod") + data, err := os.ReadFile(process.GetPathRelativeToCwd("go.mod")) if err != nil { return "", fmt.Errorf("error reading go.mod: %v", err) } // Parse the go.mod file - modFile, err := modfile.Parse("go.mod", data, nil) + modFile, err := modfile.Parse(process.GetPathRelativeToCwd("go.mod"), data, nil) if err != nil { return "", fmt.Errorf("error parsing go.mod: %v", err) } @@ -98,11 +99,7 @@ func CopyAssets() { assetDistDir := fmt.Sprintf("%s/dist", assetDir) assetCssDir := fmt.Sprintf("%s/css", assetDir) - cwd, err := os.Getwd() - - if err != nil { - log.Fatal("failed to get cwd") - } + cwd := process.GetWorkingDir() destDir := fmt.Sprintf("%s/assets", cwd) destDirDist := fmt.Sprintf("%s/dist", destDir) diff --git a/cli/tasks/downloadtemplate/main.go b/cli/tasks/downloadtemplate/main.go index ae3bb5b..9516ff2 100644 --- a/cli/tasks/downloadtemplate/main.go +++ b/cli/tasks/downloadtemplate/main.go @@ -3,6 +3,8 @@ package downloadtemplate import ( "flag" "fmt" + "github.com/maddalax/htmgo/cli/tasks/process" + "github.com/maddalax/htmgo/cli/tasks/run" "os" "os/exec" "path/filepath" @@ -84,6 +86,11 @@ func DownloadTemplate(outPath string) { } } + fmt.Printf("Setting up the project in %s\n", newDir) + process.SetWorkingDir(newDir) + run.Setup() + process.SetWorkingDir("") + fmt.Println("Template downloaded successfully.") fmt.Println("To start the development server, run the following commands:") fmt.Printf("cd %s && htmgo watch\n", outPath) diff --git a/cli/tasks/process/process.go b/cli/tasks/process/process.go index 6c87ac7..efb4d66 100644 --- a/cli/tasks/process/process.go +++ b/cli/tasks/process/process.go @@ -6,17 +6,35 @@ import ( "log" "os" "os/exec" + "path/filepath" "strings" "syscall" "time" ) +var workingDir string var commands = make([]*exec.Cmd, 0) func AppendRunning(cmd *exec.Cmd) { commands = append(commands, cmd) } +func GetWorkingDir() string { + if workingDir == "" { + wd, _ := os.Getwd() + return wd + } + return workingDir +} + +func SetWorkingDir(dir string) { + workingDir = dir +} + +func GetPathRelativeToCwd(path string) string { + return filepath.Join(GetWorkingDir(), path) +} + func KillAll() { tries := 0 @@ -129,6 +147,11 @@ func Run(command string, exitOnError bool) error { cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr + + if workingDir != "" { + cmd.Dir = workingDir + } + AppendRunning(cmd) err := cmd.Run() if err != nil { diff --git a/cli/tasks/run/setup.go b/cli/tasks/run/setup.go new file mode 100644 index 0000000..a6f1c6d --- /dev/null +++ b/cli/tasks/run/setup.go @@ -0,0 +1,16 @@ +package run + +import ( + "github.com/maddalax/htmgo/cli/tasks/astgen" + "github.com/maddalax/htmgo/cli/tasks/copyassets" + "github.com/maddalax/htmgo/cli/tasks/css" + "github.com/maddalax/htmgo/cli/tasks/process" +) + +func Setup() { + process.RunOrExit("go mod download") + process.RunOrExit("go mod tidy") + copyassets.CopyAssets() + _ = astgen.GenAst(true) + _ = css.GenerateCss(true) +} diff --git a/cli/watcher.go b/cli/watcher.go index d67216e..0290d9a 100644 --- a/cli/watcher.go +++ b/cli/watcher.go @@ -8,6 +8,8 @@ import ( "path/filepath" ) +var ignoredDirs = []string{".git", ".idea", "node_modules", "vendor"} + func startWatcher(cb func(file []*fsnotify.Event)) { //debouncer := NewDebouncer(time.Millisecond * 100) events := make([]*fsnotify.Event, 0) @@ -52,6 +54,12 @@ func startWatcher(cb func(file []*fsnotify.Event)) { if err != nil { return err } + // Ignore directories in the ignoredDirs list + for _, ignoredDir := range ignoredDirs { + if ignoredDir == info.Name() { + return filepath.SkipDir + } + } // Only watch directories if info.IsDir() { err = watcher.Add(path)