Skip to content

Commit

Permalink
Merge branch 'ft-generate-command-cli'
Browse files Browse the repository at this point in the history
  • Loading branch information
YairLevi committed Jan 28, 2024
2 parents 54b356f + 2d2503f commit cdc0d88
Show file tree
Hide file tree
Showing 13 changed files with 353 additions and 199 deletions.
55 changes: 40 additions & 15 deletions cli/coffee/build.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"errors"
"github.com/YairLevi/Coffee/cli/coffee/util"
"github.com/charmbracelet/log"
"os"
Expand All @@ -12,41 +11,67 @@ var sourceDirMapping = map[string]string{
"angular-ts": "frontend/dist/angular-ts/browser",
}

func Build() error {
func Build() {
if len(os.Args) < 3 {
return errors.New("specify which ui template to build for (will change later)")
log.Error("specify which ui template to build for (will change later)")
return
}
err := os.Chdir("frontend")
if err != nil {
return err
log.Errorf("error: %v", err)
return
}

log.Info("Making frontend dist folder")
buildFront := util.CommandWithLog("npm", "run", "build")
err = buildFront.Run()
_, err = RunCommand(CmdProps{
Cmd: BuildFrontend,
Sync: true,
LogBefore: "Making frontend dist folder",
Opts: Opts(WithStdout, WithStderr),
})
if err != nil {
return err
log.Errorf("Failed to build frontend: %v", err)
return
}

err = os.Chdir("..")
if err != nil {
return err
log.Errorf("error moving directory: %v", err)
return
}

log.Info("Adding frontend to resources")
sourceDir := sourceDirMapping[os.Args[2]]
err = util.MoveDirectory(sourceDir, "src/main/resources/dist")
if err != nil {
return err
log.Errorf("Failed to move dist folder to the resources folder. %v", err)
return
}

log.Info("Bundling to JAR")
buildApp := util.CommandWithLog("mvn", "clean", "compile", "assembly:single")
err = buildApp.Run()
log.Info("Preparing for bundle")
file, err := os.Create("src/main/resources/__jar__")
if err != nil {
return err
log.Errorf("Unexpected error: production flag was not able to set. %v", err)
return
}
file.Close()

_, err = RunCommand(CmdProps{
Cmd: BundleApp,
LogBefore: "Bundling to JAR",
Sync: true,
Opts: Opts(WithStdout, WithStderr),
})
if err != nil {
log.Errorf("Failed to build app into JAR. %v", err)
return
}

log.Info("Post bundle cleanup")
err = os.Remove("src/main/resources/__jar__")
if err != nil {
log.Errorf("Unexpected error: was not able to delete temporary production flag. %v", err)
return
}

log.Info("Done. your JAR is located at `./target")
return nil
}
61 changes: 61 additions & 0 deletions cli/coffee/commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package main

import (
"github.com/charmbracelet/log"
"os"
"os/exec"
"strings"
)

const (
InstallFrontendDependencies = "npm install"
LaunchDevServer = "npm run dev"
BuildFrontend = "npm run build"
CompileBackend = "mvn compile"
BundleApp = "mvn compile assembly:single"
LaunchApp = "mvn exec:java"
GenerateAppBinds = "mvn exec:java -Dexec.args=\"generate\""
)

type Opt = func(cmd *exec.Cmd)

func WithStdout(cmd *exec.Cmd) {
cmd.Stdout = os.Stdout
}

func WithStderr(cmd *exec.Cmd) {
cmd.Stderr = os.Stderr
}

type CmdProps struct {
Cmd string
LogBefore string
Sync bool
Opts []Opt
}

func Opts(opts ...Opt) []Opt {
var ropts []Opt
for _, o := range opts {
ropts = append(ropts, o)
}
return ropts
}

func RunCommand(props CmdProps) (*exec.Cmd, error) {
words := strings.Split(props.Cmd, " ")
name := words[0]
args := words[1:]
command := exec.Command(name, args...)
for _, opt := range props.Opts {
opt(command)
}
var err error = nil
log.Info(props.LogBefore)
if props.Sync {
err = command.Run()
} else {
err = command.Start()
}
return command, err
}
107 changes: 69 additions & 38 deletions cli/coffee/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,94 @@ import (
"fmt"
"github.com/YairLevi/Coffee/cli/coffee/util"
"github.com/charmbracelet/log"
"net"
"os"
"os/exec"
"time"
)

func Dev() error {
err := os.Chdir("frontend")
func Dev() {
_, err := RunCommand(CmdProps{
Cmd: CompileBackend,
Sync: true,
Opts: Opts(WithStderr, WithStdout),
LogBefore: "Compiling Application",
})
if err != nil {
fmt.Println(err.Error())
return err
log.Error("Failed to compile backend code.", "err", err)
return
}
log.Info("Installing dependencies")
err = exec.Command("npm", "install").Run()

_, err = RunCommand(CmdProps{
Cmd: GenerateAppBinds,
LogBefore: "Generating type-safe frontend bindings...",
Sync: true,
Opts: Opts(WithStderr),
})
if err != nil {
fmt.Println(err.Error())
return err
log.Errorf("Failed to create bindings. %v", err)
return
}

log.Info("Starting development server")
devServer := exec.Command("npm", "run", "dev")
devServer.Stderr = os.Stderr
err = devServer.Start()
err = os.Chdir("frontend")
if err != nil {
log.Error(err.Error())
return err
log.Error("Can't go to frontend directory.", "err", err)
return
}

err = os.Chdir("..")
_, err = RunCommand(CmdProps{
Cmd: InstallFrontendDependencies,
LogBefore: "Installing dependencies",
Sync: true,
})
if err != nil {
fmt.Println(err.Error())
return err
log.Error("Failed to download NPM dependencies.", "err", err)
return
}

log.Info("Compiling Application")
compile := exec.Command("mvn", "clean", "compile")
compile.Stderr = os.Stderr
err = compile.Run()
devServerCmd, err := RunCommand(CmdProps{
Cmd: LaunchDevServer,
LogBefore: "Starting development server",
Opts: Opts(WithStderr),
Sync: false,
})
if err != nil {
log.Error(err.Error())
return err
log.Error("Failed to start dev server.", "err", err)
return
}
defer func() {
log.Info("Shutting down frontend dev server")
err = util.StopProcessTree(devServerCmd.Process.Pid)
if err != nil {
log.Error("Failed to close the entire process tree of the dev server.", "err", err)
}
}()
log.Info("Waiting for dev server to fully start")
for {
host, port := "localhost", "5173"
timeout := time.Millisecond * 500
conn, err := net.DialTimeout("tcp", net.JoinHostPort(host, port), timeout)
if err == nil {
break
}
if conn != nil {
conn.Close()
}
}

log.Info("Running. application logging below")
log.Info("\n==================================\n")
cmd := exec.Command("mvn", "exec:java")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
err = os.Chdir("..")
if err != nil {
fmt.Println(err.Error())
return err
log.Error("Can't go back to project directory.", "err", err)
return
}
log.Info("\n==================================\n")
log.Info("Shutting down frontend dev server")
err = util.StopProcessTree(devServer.Process.Pid)

_, err = RunCommand(CmdProps{
Cmd: LaunchApp,
Opts: Opts(WithStderr, WithStdout),
Sync: true,
LogBefore: "Running. application logging below",
})
if err != nil {
log.Error("Failed to close the entire process tree of the dev server.")
return err
fmt.Println("Failed to launch application.", "err", err)
return
}
return nil
}
27 changes: 27 additions & 0 deletions cli/coffee/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import "github.com/charmbracelet/log"

func Generate() {
_, err := RunCommand(CmdProps{
Cmd: CompileBackend,
Sync: true,
LogBefore: "Compiling backend code",
Opts: Opts(WithStdout, WithStderr),
})
if err != nil {
log.Errorf("Error compiling backend code for generate. %v", err)
return
}

_, err = RunCommand(CmdProps{
Cmd: GenerateAppBinds,
Opts: Opts(WithStdout, WithStderr),
LogBefore: "Generating bindings.",
Sync: true,
})
if err != nil {
log.Errorf("Couldn't generate bindings for some reason. %v", err)
return
}
}
Loading

0 comments on commit cdc0d88

Please sign in to comment.