some fixes

This commit is contained in:
maddalax 2024-09-16 20:40:56 -05:00
parent 7f056b1d5e
commit 45d8e58faa
8 changed files with 66 additions and 19 deletions

View file

@ -5,10 +5,8 @@ import (
"flag" "flag"
"fmt" "fmt"
"github.com/maddalax/htmgo/cli/tasks/astgen" "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/css"
"github.com/maddalax/htmgo/cli/tasks/downloadtemplate" "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/reloader"
"github.com/maddalax/htmgo/cli/tasks/run" "github.com/maddalax/htmgo/cli/tasks/run"
"os" "os"
@ -57,11 +55,7 @@ func main() {
startWatcher(reloader.OnFileChange) startWatcher(reloader.OnFileChange)
} else { } else {
if taskName == "setup" { if taskName == "setup" {
process.RunOrExit("go mod download") run.Setup()
process.RunOrExit("go mod tidy")
copyassets.CopyAssets()
_ = astgen.GenAst(true)
_ = css.GenerateCss(true)
} else if taskName == "css" { } else if taskName == "css" {
_ = css.GenerateCss(true) _ = css.GenerateCss(true)
} else if taskName == "ast" { } else if taskName == "ast" {
@ -83,7 +77,7 @@ func main() {
} else { } else {
fmt.Println(fmt.Sprintf("Usage: htmgo [%s]", strings.Join(commands, " | "))) fmt.Println(fmt.Sprintf("Usage: htmgo [%s]", strings.Join(commands, " | ")))
} }
os.Exit(1) os.Exit(0)
} }
<-done <-done

View file

@ -2,6 +2,7 @@ package astgen
import ( import (
"fmt" "fmt"
"github.com/maddalax/htmgo/cli/tasks/process"
"go/ast" "go/ast"
"go/parser" "go/parser"
"go/token" "go/token"
@ -56,7 +57,7 @@ func sliceCommonPrefix(dir1, dir2 string) string {
func findPublicFuncsReturningHPartial(dir string) ([]Partial, error) { func findPublicFuncsReturningHPartial(dir string) ([]Partial, error) {
var partials []Partial var partials []Partial
cwd, _ := os.Getwd() cwd := process.GetWorkingDir()
// Walk through the directory to find all Go files. // Walk through the directory to find all Go files.
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { 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() { func writePartialsFile() {
cwd, _ := os.Getwd() cwd := process.GetWorkingDir()
partialPath := filepath.Join(cwd, "partials") partialPath := filepath.Join(cwd, "partials")
partials, err := findPublicFuncsReturningHPartial(partialPath) partials, err := findPublicFuncsReturningHPartial(partialPath)
if err != nil { if err != nil {
@ -340,7 +341,7 @@ func writePagesFile() {
} }
func GetModuleName() string { func GetModuleName() string {
wd, _ := os.Getwd() wd := process.GetWorkingDir()
modPath := filepath.Join(wd, "go.mod") modPath := filepath.Join(wd, "go.mod")
goModBytes, err := os.ReadFile(modPath) goModBytes, err := os.ReadFile(modPath)
if err != nil { if err != nil {

View file

@ -1,6 +1,7 @@
package astgen package astgen
import ( import (
"github.com/maddalax/htmgo/cli/tasks/process"
"go/ast" "go/ast"
"go/format" "go/format"
"go/parser" "go/parser"
@ -12,7 +13,7 @@ import (
) )
func WriteFile(path string, cb func(content *ast.File) string) { func WriteFile(path string, cb func(content *ast.File) string) {
currentDir, err := os.Getwd() currentDir := process.GetWorkingDir()
path = filepath.Join(currentDir, path) path = filepath.Join(currentDir, path)

View file

@ -2,6 +2,7 @@ package copyassets
import ( import (
"fmt" "fmt"
"github.com/maddalax/htmgo/cli/tasks/process"
"golang.org/x/mod/modfile" "golang.org/x/mod/modfile"
"io" "io"
"log" "log"
@ -11,13 +12,13 @@ import (
func getModuleVersion(modulePath string) (string, error) { func getModuleVersion(modulePath string) (string, error) {
// Read the go.mod file // Read the go.mod file
data, err := os.ReadFile("go.mod") data, err := os.ReadFile(process.GetPathRelativeToCwd("go.mod"))
if err != nil { if err != nil {
return "", fmt.Errorf("error reading go.mod: %v", err) return "", fmt.Errorf("error reading go.mod: %v", err)
} }
// Parse the go.mod file // 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 { if err != nil {
return "", fmt.Errorf("error parsing go.mod: %v", err) return "", fmt.Errorf("error parsing go.mod: %v", err)
} }
@ -98,11 +99,7 @@ func CopyAssets() {
assetDistDir := fmt.Sprintf("%s/dist", assetDir) assetDistDir := fmt.Sprintf("%s/dist", assetDir)
assetCssDir := fmt.Sprintf("%s/css", assetDir) assetCssDir := fmt.Sprintf("%s/css", assetDir)
cwd, err := os.Getwd() cwd := process.GetWorkingDir()
if err != nil {
log.Fatal("failed to get cwd")
}
destDir := fmt.Sprintf("%s/assets", cwd) destDir := fmt.Sprintf("%s/assets", cwd)
destDirDist := fmt.Sprintf("%s/dist", destDir) destDirDist := fmt.Sprintf("%s/dist", destDir)

View file

@ -3,6 +3,8 @@ package downloadtemplate
import ( import (
"flag" "flag"
"fmt" "fmt"
"github.com/maddalax/htmgo/cli/tasks/process"
"github.com/maddalax/htmgo/cli/tasks/run"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "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("Template downloaded successfully.")
fmt.Println("To start the development server, run the following commands:") fmt.Println("To start the development server, run the following commands:")
fmt.Printf("cd %s && htmgo watch\n", outPath) fmt.Printf("cd %s && htmgo watch\n", outPath)

View file

@ -6,17 +6,35 @@ import (
"log" "log"
"os" "os"
"os/exec" "os/exec"
"path/filepath"
"strings" "strings"
"syscall" "syscall"
"time" "time"
) )
var workingDir string
var commands = make([]*exec.Cmd, 0) var commands = make([]*exec.Cmd, 0)
func AppendRunning(cmd *exec.Cmd) { func AppendRunning(cmd *exec.Cmd) {
commands = append(commands, 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() { func KillAll() {
tries := 0 tries := 0
@ -129,6 +147,11 @@ func Run(command string, exitOnError bool) error {
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
if workingDir != "" {
cmd.Dir = workingDir
}
AppendRunning(cmd) AppendRunning(cmd)
err := cmd.Run() err := cmd.Run()
if err != nil { if err != nil {

16
cli/tasks/run/setup.go Normal file
View file

@ -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)
}

View file

@ -8,6 +8,8 @@ import (
"path/filepath" "path/filepath"
) )
var ignoredDirs = []string{".git", ".idea", "node_modules", "vendor"}
func startWatcher(cb func(file []*fsnotify.Event)) { func startWatcher(cb func(file []*fsnotify.Event)) {
//debouncer := NewDebouncer(time.Millisecond * 100) //debouncer := NewDebouncer(time.Millisecond * 100)
events := make([]*fsnotify.Event, 0) events := make([]*fsnotify.Event, 0)
@ -52,6 +54,12 @@ func startWatcher(cb func(file []*fsnotify.Event)) {
if err != nil { if err != nil {
return err return err
} }
// Ignore directories in the ignoredDirs list
for _, ignoredDir := range ignoredDirs {
if ignoredDir == info.Name() {
return filepath.SkipDir
}
}
// Only watch directories // Only watch directories
if info.IsDir() { if info.IsDir() {
err = watcher.Add(path) err = watcher.Add(path)