Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): ags run naive --watch mode #660

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 60 additions & 21 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"

"github.com/spf13/cobra"
)


var (
gtk4 bool
targetDir string
logFile string
args []string
watch bool
)

var runCommand = &cobra.Command{
Expand Down Expand Up @@ -58,6 +61,7 @@ when no positional argument is given
f.StringArrayVarP(&args, "arg", "a", []string{}, "cli args to pass to gjs")
f.StringVar(&logFile, "log-file", "", "file to redirect the stdout of gjs to")
f.BoolVarP(&usePackage, "package", "p", false, "use astal package as defined in package.json")
f.BoolVarP(&watch, "watch", "w", false, "watch for changes and restart the app")
f.MarkHidden("package")
}

Expand Down Expand Up @@ -107,41 +111,76 @@ func logging() (io.Writer, io.Writer, *os.File) {
return io.MultiWriter(os.Stdout, file), io.MultiWriter(os.Stderr, file), file
}

var gjs *exec.Cmd
func executeGjs(infile string, stdout io.Writer, stderr io.Writer) *exec.Cmd {
if(gjs != nil) {
if err := gjs.Process.Kill(); err != nil {
lib.Err(err)
}
gjs.Wait()
}

if gtk4 {
os.Setenv("LD_PRELOAD", gtk4LayerShell)
}

outfile := getOutfile()
args := append([]string{"-m", outfile}, args...)
gjs = lib.Exec("gjs", args...)

gjs.Stdin = os.Stdin
gjs.Dir = filepath.Dir(infile)

gjs.Stdout = stdout
gjs.Stderr = stderr

return gjs
}

func run(infile string, rootdir string) {
gtk := 3
if gtk4 {
gtk = 4
}

outfile := getOutfile()
lib.Bundle(lib.BundleOpts{
bundleOpts := lib.BundleOpts{
Infile: infile,
Outfile: outfile,
Outfile: getOutfile(),
Defines: defines,
UsePackage: usePackage,
GtkVersion: gtk,
WorkingDirectory: rootdir,
})

if gtk4 {
os.Setenv("LD_PRELOAD", gtk4LayerShell)
}

args = append([]string{"-m", outfile}, args...)
stdout, stderr, file := logging()
gjs := lib.Exec("gjs", args...)
gjs.Stdin = os.Stdin
gjs.Dir = filepath.Dir(infile)

gjs.Stdout = stdout
gjs.Stderr = stderr

// TODO: watch and restart
if err := gjs.Run(); err != nil {
lib.Err(err)
if(watch){
lib.Watch(lib.WatchOpts{
BundleOpts: bundleOpts,
ReloadPluginOpts: lib.ReloadPluginOpts{
OnBuild: func(){
gjs = executeGjs(infile, stdout, stderr)
gjs.Start()
},
OnExit: func(){
if file != nil {
file.Close()
}
},
},
})
} else {
lib.Bundle(bundleOpts)

gjs = executeGjs(infile, stdout, stderr)

if err := gjs.Run(); err != nil {
lib.Err(err)
}

if file != nil {
file.Close()
}
}

if file != nil {
file.Close()
}
}
}
63 changes: 58 additions & 5 deletions lib/esbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,28 @@ var blpPlugin api.Plugin = api.Plugin{
},
}


type ReloadPluginOpts struct {
OnBuild func()
OnExit func()
}

func makeReloadPlugin(opts ReloadPluginOpts) api.Plugin {
return api.Plugin {
Name: "reload",
Setup: func(build api.PluginBuild) {
build.OnEnd(func(result *api.BuildResult) (api.OnEndResult, error) {
opts.OnBuild()
return api.OnEndResult{}, nil
})

build.OnDispose(func() {
opts.OnExit()
})
},
}
}

func sliceToKV(keyValuePairs []string) map[string]string {
pairs := make(map[string]string)
for _, pair := range keyValuePairs {
Expand All @@ -143,7 +165,7 @@ type BundleOpts struct {
// svg loader
// other css preproceccors
// http plugin with caching
func Bundle(opts BundleOpts) {
func Prepare(opts BundleOpts) api.BuildOptions {
defines := sliceToKV(opts.Defines)

if _, ok := defines["SRC"]; !ok {
Expand Down Expand Up @@ -203,10 +225,41 @@ func Bundle(opts BundleOpts) {
}
}

result := api.Build(buildOpts)
return buildOpts
}


// TODO: custom error logs
if len(result.Errors) > 0 {
os.Exit(1)
func Bundle(opts BundleOpts) api.BuildResult {
ctx := Prepare(opts)
result := api.Build(ctx)

if result.Errors != nil {
Err(result.Errors)
}

return result
}

type WatchOpts struct {
BundleOpts
ReloadPluginOpts
}

func Watch(opts WatchOpts){
buildOpts := Prepare(opts.BundleOpts)

buildOpts.Plugins = append(buildOpts.Plugins, makeReloadPlugin(opts.ReloadPluginOpts))

ctx, ctxErr := api.Context(buildOpts)
if ctxErr != nil {
Err(ctxErr)
}

watchErr := ctx.Watch(api.WatchOptions{})

if watchErr != nil {
Err(watchErr)
}

<-make(chan struct{})
}