Skip to content

Commit

Permalink
migrate to cobra
Browse files Browse the repository at this point in the history
  • Loading branch information
Aylur committed Nov 6, 2024
1 parent 198993b commit 42624a5
Show file tree
Hide file tree
Showing 38 changed files with 832 additions and 674 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1 @@
src/data/**/* linguist-vendored
data/**/* linguist-vendored
37 changes: 37 additions & 0 deletions cmd/bundle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package cmd

import (
"ags/lib"
"os"
"path/filepath"

"github.com/spf13/cobra"
)

var bundleCommand = &cobra.Command{
Use: "bundle [file or directory] [outfile]",
Short: "Bundle an app",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
path, err := filepath.Abs(args[0])
if err != nil {
lib.Err(err)
}

outfile, err := filepath.Abs(args[1])
if err != nil {
lib.Err(err)
}

info, err := os.Stat(path)
if err != nil {
lib.Err(err)
}

if info.IsDir() {
lib.Bundle(path, getAppEntry(path), outfile)
} else {
lib.Bundle(filepath.Dir(path), path, outfile)
}
},
}
83 changes: 83 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package cmd

import (
"ags/lib"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/spf13/cobra"
)

var (
force bool
gtk int
)

var initCommand = &cobra.Command{
Use: "init [directory]",
Short: "Initialize a project directory",
Long: "Setup files needed by TypeScript and generate types",
Args: cobra.MaximumNArgs(1),
Run: initConfig,
}

func init() {
f := initCommand.Flags()

f.IntVarP(&gtk, "gtk", "g", 3, "use this gtk version")
f.BoolVarP(&force, "force", "f", false, "override existing files")
}

func getDataFile(name string) string {
content, err := data.ReadFile("data/" + name)
if err != nil {
lib.Err(err)
}
return string(content)
}

func initConfig(cmd *cobra.Command, args []string) {
if gtk != 3 {
lib.Err("currently only gtk3 is supported")
}

var configDir string
if len(args) > 0 {
var err error
configDir, err = filepath.Abs(args[0])
if err != nil {
lib.Err(err)
}
} else {
configDir = defaultConfigDir()
}

tsconfig := getDataFile("tsconfig.json")
envdts := getDataFile("env.d.ts")
stylescss := getDataFile("style.scss")
bartsx := getDataFile("gtk3/Bar.tsx")
appts := getDataFile("gtk3/app.ts")

if info, err := os.Stat(configDir); err == nil && info.IsDir() && !force {
lib.Err("could not initialize: " + lib.Cyan(configDir) + " already exists")
}

tsconf := strings.ReplaceAll(tsconfig, "@ASTAL_GJS@", astalGjs)
tsconf = strings.ReplaceAll(tsconf, "@GTK_VERSION@", "gtk3") // TODO: gtk4 flag
lib.Mkdir(configDir + "/widget")

lib.WriteFile(configDir+"/.gitignore", "@girs/\nnode_modules/")
lib.WriteFile(configDir+"/tsconfig.json", tsconf)
lib.WriteFile(configDir+"/env.d.ts", envdts)
lib.WriteFile(configDir+"/style.scss", stylescss)
lib.WriteFile(configDir+"/widget/Bar.tsx", bartsx)
lib.WriteFile(configDir+"/app.ts", appts)

if err := genTypes(configDir, "*"); err != nil {
lib.Err(err)
}

fmt.Println(lib.Green("project ready") + " at " + lib.Cyan(configDir))
}
21 changes: 21 additions & 0 deletions cmd/inspect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cmd

import (
"ags/lib"

"github.com/spf13/cobra"
)

var inspectCommand = &cobra.Command{
Use: "inspect",
Short: "Open up Gtk debug tool",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
instance, _ := cmd.Flags().GetString("instance")
lib.Astal("--instance", instance, "--inspector")
},
}

func init() {
lib.AddInstanceFlag(inspectCommand)
}
16 changes: 16 additions & 0 deletions cmd/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package cmd

import (
"ags/lib"

"github.com/spf13/cobra"
)

var listCommand = &cobra.Command{
Use: "list",
Short: "List running instances",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
lib.Astal("--list")
},
}
21 changes: 21 additions & 0 deletions cmd/quit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cmd

import (
"ags/lib"

"github.com/spf13/cobra"
)

var quitCommand = &cobra.Command{
Use: "quit",
Short: "Quit an instances",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
instance, _ := cmd.Flags().GetString("instance")
lib.Astal("--quit", "--instance", instance)
},
}

func init() {
lib.AddInstanceFlag(quitCommand)
}
21 changes: 21 additions & 0 deletions cmd/request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cmd

import (
"ags/lib"

"github.com/spf13/cobra"
)

var reqCommand = &cobra.Command{
Use: "request [message]",
Short: "Send a request to an instance",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
instance, _ := cmd.Flags().GetString("instance")
lib.Astal("--instance", instance, "--message", args[0])
},
}

func init() {
lib.AddInstanceFlag(reqCommand)
}
75 changes: 75 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package cmd

import (
"embed"
"os"
"path/filepath"

"github.com/spf13/cobra"
)

var (
version string
data embed.FS
gtk4LayerShell string
astalGjs string
tsForGir string
)

var rootCmd = &cobra.Command{
Use: "ags",
Short: "Scaffolding CLI tool for Astal+TypeScript projects.",
Args: cobra.NoArgs,
}

type Variables struct {
Version string
Data embed.FS
Gtk4LayerShell string
AstalGjs string
TsForGir string
}

func Initialize(vars Variables) {
version = vars.Version
data = vars.Data
gtk4LayerShell = vars.Gtk4LayerShell
astalGjs = vars.AstalGjs
tsForGir = vars.TsForGir

rootCmd.Version = version
}

func init() {
rootCmd.CompletionOptions.HiddenDefaultCmd = true
cobra.EnableCommandSorting = false

rootCmd.AddCommand(runCommand)
rootCmd.AddCommand(reqCommand)
rootCmd.AddCommand(listCommand)
rootCmd.AddCommand(inspectCommand)
rootCmd.AddCommand(toggleCommand)
rootCmd.AddCommand(quitCommand)
rootCmd.AddCommand(typesCommand)
rootCmd.AddCommand(bundleCommand)
rootCmd.AddCommand(initCommand)
}

func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}

help, _ := rootCmd.Flags().GetBool("help")
ver, _ := rootCmd.Flags().GetBool("version")

if help || ver {
os.Exit(0)
}
}

func defaultConfigDir() string {
dotconf, _ := os.UserConfigDir()
return filepath.Join(dotconf, "ags")
}
98 changes: 98 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package cmd

import (
"ags/lib"
"fmt"
"os"
"path/filepath"

"github.com/spf13/cobra"
)

var gtk4 bool

var runCommand = &cobra.Command{
Use: "run [optional file or directory]",
Short: "Run an app",
Long: `Run a given app. Defaults to ` + defaultConfigDir(),
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if len(args) > 0 {
path, err := filepath.Abs(args[0])
if err != nil {
lib.Err(err)
}

info, err := os.Stat(path)
if err != nil {
lib.Err(err)
}

if info.IsDir() {
run(getAppEntry(path), path)
} else {
run(path, filepath.Dir(path))
}

} else {
dir := defaultConfigDir()
run(getAppEntry(dir), dir)
}
},
}

func init() {
runCommand.Flags().BoolVar(&gtk4, "gtk4", false, "preload Gtk4LayerShell")
}

func getOutfile() string {
rundir, found := os.LookupEnv("XDG_RUNTIME_DIR")

if !found {
rundir = "/tmp"
}

return filepath.Join(rundir, "ags.js")
}

func getAppEntry(dir string) string {
infile := filepath.Join(dir, "app")
valid := []string{"js", "ts", "jsx", "tsx"}

app := lib.Some(valid, func(ext string) bool {
_, err := os.Stat(infile + "." + ext)
return !os.IsNotExist(err)
})

if !app {
msg := "no such file or directory: " +
fmt.Sprintf("\"%s\"\n", lib.Cyan(dir+"/app")) +
lib.Magenta("tip: ") + "valid names are: "
for _, v := range valid {
msg = msg + fmt.Sprintf(` "%s"`, lib.Cyan("app."+v))
}
lib.Err(msg)
}

return infile
}

func run(infile, dir string) {
outfile := getOutfile()
lib.Bundle(dir, infile, outfile)

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

gjs := lib.Exec("gjs", "-m", outfile)
gjs.Stdout = os.Stdout
gjs.Stderr = os.Stderr
gjs.Stdin = os.Stdin
gjs.Dir = dir

// TODO: watch and restart
if err := gjs.Run(); err != nil {
lib.Err(err)
}
}
Loading

0 comments on commit 42624a5

Please sign in to comment.