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"
"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

View file

@ -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 {

View file

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

View file

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

View file

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

View file

@ -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 {

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"
)
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)