htmgo/cli/htmgo/runner.go

142 lines
3.4 KiB
Go
Raw Normal View History

package main
import (
2024-09-16 23:02:35 +00:00
"bufio"
"flag"
"fmt"
"github.com/maddalax/htmgo/cli/htmgo/internal"
2024-09-26 16:47:16 +00:00
"github.com/maddalax/htmgo/cli/htmgo/internal/dirutil"
2024-09-20 16:57:45 +00:00
"github.com/maddalax/htmgo/cli/htmgo/tasks/astgen"
"github.com/maddalax/htmgo/cli/htmgo/tasks/copyassets"
"github.com/maddalax/htmgo/cli/htmgo/tasks/css"
"github.com/maddalax/htmgo/cli/htmgo/tasks/downloadtemplate"
"github.com/maddalax/htmgo/cli/htmgo/tasks/process"
"github.com/maddalax/htmgo/cli/htmgo/tasks/reloader"
"github.com/maddalax/htmgo/cli/htmgo/tasks/run"
"log/slog"
"os"
"strings"
2024-09-25 15:16:05 +00:00
"sync"
)
func main() {
done := RegisterSignals()
commandMap := make(map[string]*flag.FlagSet)
2024-09-23 15:57:59 +00:00
commands := []string{"template", "run", "watch", "build", "setup", "css", "schema", "generate"}
for _, command := range commands {
commandMap[command] = flag.NewFlagSet(command, flag.ExitOnError)
}
if len(os.Args) < 2 {
fmt.Println(fmt.Sprintf("Usage: htmgo [%s]", strings.Join(commands, " | ")))
os.Exit(1)
}
c := commandMap[os.Args[1]]
if c == nil {
fmt.Println(fmt.Sprintf("Usage: htmgo [%s]", strings.Join(commands, " | ")))
os.Exit(1)
return
}
err := c.Parse(os.Args[2:])
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
return
}
slog.SetLogLoggerLevel(internal.GetLogLevel())
taskName := os.Args[1]
slog.Debug("Running task:", slog.String("task", taskName))
slog.Debug("working dir:", slog.String("dir", process.GetWorkingDir()))
2024-09-26 16:47:16 +00:00
if !dirutil.HasFileFromRoot("__htmgo") {
dirutil.CreateDirFromRoot("__htmgo")
}
2024-09-21 18:20:34 +00:00
if taskName == "watch" {
2024-09-24 16:08:35 +00:00
fmt.Printf("Running in watch mode\n")
os.Setenv("ENV", "development")
os.Setenv("WATCH_MODE", "true")
2024-09-25 15:16:05 +00:00
fmt.Printf("Starting processes...\n")
2024-09-17 17:13:22 +00:00
copyassets.CopyAssets()
2024-09-25 15:16:05 +00:00
fmt.Printf("Generating CSS...\n")
2024-09-24 16:08:35 +00:00
css.GenerateCss(process.ExitOnError)
2024-09-25 15:16:05 +00:00
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
astgen.GenAst(process.ExitOnError)
}()
wg.Add(1)
go func() {
defer wg.Done()
run.EntGenerate()
}()
wg.Wait()
2024-09-24 16:08:35 +00:00
fmt.Printf("Starting server...\n")
process.KillAll()
go func() {
2024-09-24 03:44:20 +00:00
_ = run.Server()
}()
startWatcher(reloader.OnFileChange)
} else {
2024-09-23 15:57:59 +00:00
if taskName == "schema" {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter entity name:")
text, _ := reader.ReadString('\n')
text = strings.TrimSuffix(text, "\n")
run.EntNewSchema(text)
2024-09-21 18:20:34 +00:00
} else if taskName == "generate" {
run.EntGenerate()
astgen.GenAst(process.ExitOnError)
2024-09-21 18:20:34 +00:00
} else if taskName == "setup" {
2024-09-17 01:40:56 +00:00
run.Setup()
2024-09-16 23:02:35 +00:00
} else if taskName == "css" {
_ = css.GenerateCss(process.ExitOnError)
2024-09-16 23:02:35 +00:00
} else if taskName == "ast" {
_ = astgen.GenAst(process.ExitOnError)
2024-09-16 23:02:35 +00:00
} else if taskName == "run" {
_ = astgen.GenAst(process.ExitOnError)
_ = css.GenerateCss(process.ExitOnError)
_ = run.Server(process.ExitOnError)
2024-09-16 23:02:35 +00:00
} else if taskName == "template" {
2024-09-25 19:02:10 +00:00
name := ""
if len(os.Args) > 2 {
name = os.Args[2]
} else {
reader := bufio.NewReader(os.Stdin)
fmt.Print("What would you like to call your new app?: ")
name, _ = reader.ReadString('\n')
}
name = strings.TrimSuffix(name, "\n")
name = strings.ReplaceAll(name, " ", "-")
name = strings.ToLower(name)
downloadtemplate.DownloadTemplate(fmt.Sprintf("./%s", name))
2024-09-16 23:02:35 +00:00
} else if taskName == "build" {
run.Build()
2024-09-20 16:45:23 +00:00
} else if taskName == "generate" {
astgen.GenAst(process.ExitOnError)
2024-09-16 23:02:35 +00:00
} else {
fmt.Println(fmt.Sprintf("Usage: htmgo [%s]", strings.Join(commands, " | ")))
}
2024-09-17 01:40:56 +00:00
os.Exit(0)
}
<-done
}