2024-09-16 22:41:46 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2024-09-16 23:02:35 +00:00
|
|
|
"bufio"
|
2024-09-16 22:41:46 +00:00
|
|
|
"flag"
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/maddalax/htmgo/cli/tasks/astgen"
|
2024-09-17 17:13:22 +00:00
|
|
|
"github.com/maddalax/htmgo/cli/tasks/copyassets"
|
2024-09-16 22:41:46 +00:00
|
|
|
"github.com/maddalax/htmgo/cli/tasks/css"
|
|
|
|
|
"github.com/maddalax/htmgo/cli/tasks/downloadtemplate"
|
2024-09-17 15:41:29 +00:00
|
|
|
"github.com/maddalax/htmgo/cli/tasks/process"
|
2024-09-16 22:41:46 +00:00
|
|
|
"github.com/maddalax/htmgo/cli/tasks/reloader"
|
|
|
|
|
"github.com/maddalax/htmgo/cli/tasks/run"
|
2024-09-17 15:41:29 +00:00
|
|
|
"log/slog"
|
2024-09-16 22:41:46 +00:00
|
|
|
"os"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
done := RegisterSignals()
|
|
|
|
|
|
|
|
|
|
commandMap := make(map[string]*flag.FlagSet)
|
2024-09-17 15:41:29 +00:00
|
|
|
commands := []string{"template", "run", "watch", "build", "setup", "css", "schema"}
|
2024-09-16 22:41:46 +00:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-17 15:41:29 +00:00
|
|
|
slog.SetLogLoggerLevel(getLogLevel())
|
|
|
|
|
|
2024-09-16 22:41:46 +00:00
|
|
|
taskName := os.Args[1]
|
|
|
|
|
|
2024-09-17 15:41:29 +00:00
|
|
|
slog.Debug("Running task: %s", taskName)
|
|
|
|
|
slog.Debug("working dir %s", process.GetWorkingDir())
|
|
|
|
|
|
2024-09-16 22:41:46 +00:00
|
|
|
if taskName == "watch" {
|
2024-09-17 15:41:29 +00:00
|
|
|
os.Setenv("ENV", "development")
|
|
|
|
|
os.Setenv("WATCH_MODE", "true")
|
2024-09-17 17:13:22 +00:00
|
|
|
copyassets.CopyAssets()
|
2024-09-16 22:41:46 +00:00
|
|
|
astgen.GenAst(true)
|
|
|
|
|
css.GenerateCss(true)
|
2024-09-17 15:41:29 +00:00
|
|
|
run.EntGenerate()
|
2024-09-16 22:41:46 +00:00
|
|
|
go func() {
|
|
|
|
|
_ = run.Server(true)
|
|
|
|
|
}()
|
|
|
|
|
startWatcher(reloader.OnFileChange)
|
|
|
|
|
} else {
|
2024-09-17 15:41:29 +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)
|
|
|
|
|
}
|
|
|
|
|
if taskName == "generate" {
|
|
|
|
|
run.EntGenerate()
|
|
|
|
|
astgen.GenAst(true)
|
|
|
|
|
}
|
2024-09-16 22:41:46 +00:00
|
|
|
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" {
|
2024-09-16 22:41:46 +00:00
|
|
|
_ = css.GenerateCss(true)
|
2024-09-16 23:02:35 +00:00
|
|
|
} else if taskName == "ast" {
|
2024-09-16 22:41:46 +00:00
|
|
|
_ = astgen.GenAst(true)
|
2024-09-16 23:02:35 +00:00
|
|
|
} else if taskName == "run" {
|
2024-09-16 22:41:46 +00:00
|
|
|
_ = astgen.GenAst(true)
|
|
|
|
|
_ = css.GenerateCss(true)
|
|
|
|
|
_ = run.Server(true)
|
2024-09-16 23:02:35 +00:00
|
|
|
} else if taskName == "template" {
|
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
|
fmt.Print("What would you like to call your new app?: ")
|
|
|
|
|
text, _ := reader.ReadString('\n')
|
|
|
|
|
text = strings.TrimSuffix(text, "\n")
|
|
|
|
|
text = strings.ReplaceAll(text, " ", "-")
|
|
|
|
|
text = strings.ToLower(text)
|
|
|
|
|
downloadtemplate.DownloadTemplate(fmt.Sprintf("./%s", text))
|
|
|
|
|
} else if taskName == "build" {
|
|
|
|
|
run.Build()
|
|
|
|
|
} else {
|
|
|
|
|
fmt.Println(fmt.Sprintf("Usage: htmgo [%s]", strings.Join(commands, " | ")))
|
2024-09-16 22:41:46 +00:00
|
|
|
}
|
2024-09-17 01:40:56 +00:00
|
|
|
os.Exit(0)
|
2024-09-16 22:41:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
<-done
|
|
|
|
|
}
|