Skip to content

Commit

Permalink
feat: devtools cli closes #57
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnCoene committed Sep 13, 2024
1 parent 588be94 commit df740c0
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 40 deletions.
55 changes: 30 additions & 25 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ import (
)

type CLI struct {
Indir *string
Outdir *string
LSP *bool
TCP *bool
Port *string
Repl *bool
Help *bool
Version *bool
Check *bool
Run *bool
Types *string
Infile *string
Outfile *string
Indir *string
Outdir *string
LSP *bool
TCP *bool
Port *string
Repl *bool
Help *bool
Version *bool
Check *bool
Run *bool
Types *string
Infile *string
Outfile *string
Devtools *string
}

func Cli() CLI {
Expand Down Expand Up @@ -47,20 +48,24 @@ func Cli() CLI {
// version
version := flag.Bool("version", false, "Retrieve vapour version")

// devtools
devtools := flag.String("devtools", "", "Run {devtools} functions after transpilation, accepts `document`, `check`, `install`, separate by comma (e.g.: `document,check`)")

flag.Parse()

return CLI{
Indir: indir,
Outdir: outdir,
LSP: lsp,
TCP: tcp,
Port: port,
Infile: infile,
Outfile: outfile,
Repl: repl,
Check: check,
Run: run,
Version: version,
Types: types,
Indir: indir,
Outdir: outdir,
LSP: lsp,
TCP: tcp,
Port: port,
Infile: infile,
Outfile: outfile,
Repl: repl,
Check: check,
Run: run,
Version: version,
Types: types,
Devtools: devtools,
}
}
33 changes: 33 additions & 0 deletions devtools/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package devtools

import (
"fmt"
"strings"

"github.com/devOpifex/vapour/cli"
"github.com/devOpifex/vapour/r"
)

func Run(valid bool, args cli.CLI) {
if !valid {
return
}

if *args.Devtools == "" {
return
}

commands := strings.Split(*args.Devtools, ",")

for _, c := range commands {
cmd := "devtools::" + c + "()"
out, err := r.Callr(cmd)

if err != nil {
fmt.Println(err.Error())
continue
}

fmt.Println(string(out[:]))
}
}
7 changes: 5 additions & 2 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/devOpifex/vapour/cli"
"github.com/devOpifex/vapour/config"
"github.com/devOpifex/vapour/devtools"
"github.com/devOpifex/vapour/lsp"
)

Expand All @@ -33,12 +34,14 @@ func (v *vapour) Run(args cli.CLI) {
v.config = config.ReadConfig()

if *args.Indir != "" {
v.transpile(args)
ok := v.transpile(args)
devtools.Run(ok, args)
return
}

if *args.Infile != "" {
v.transpileFile(args)
ok := v.transpileFile(args)
devtools.Run(ok, args)
return
}

Expand Down
30 changes: 17 additions & 13 deletions transpile.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/devOpifex/vapour/walker"
)

func (v *vapour) transpile(conf cli.CLI) {
func (v *vapour) transpile(conf cli.CLI) bool {
v.root = conf.Indir
err := v.readDir()

Expand All @@ -25,7 +25,7 @@ func (v *vapour) transpile(conf cli.CLI) {

if l.HasError() {
l.Errors().Print()
return
return false
}

// parse
Expand All @@ -34,7 +34,7 @@ func (v *vapour) transpile(conf cli.CLI) {

if p.HasError() {
p.Errors().Print()
return
return false
}

// walk tree
Expand All @@ -44,11 +44,11 @@ func (v *vapour) transpile(conf cli.CLI) {
w.Errors().Print()

if w.HasError() {
return
return false
}

if *conf.Check {
return
return false
}

// transpile
Expand All @@ -58,7 +58,7 @@ func (v *vapour) transpile(conf cli.CLI) {

if *conf.Run {
run(code)
return
return false
}

code = addHeader(code)
Expand All @@ -81,7 +81,7 @@ func (v *vapour) transpile(conf cli.CLI) {

// we only generate types if it's an R package
if *conf.Outdir != "R" {
return
return false
}

// write types
Expand All @@ -99,9 +99,11 @@ func (v *vapour) transpile(conf cli.CLI) {
if err != nil {
log.Fatalf("Failed to write to types file: %v", err.Error())
}

return true
}

func (v *vapour) transpileFile(conf cli.CLI) {
func (v *vapour) transpileFile(conf cli.CLI) bool {
content, err := os.ReadFile(*conf.Infile)

if err != nil {
Expand All @@ -114,7 +116,7 @@ func (v *vapour) transpileFile(conf cli.CLI) {

if l.HasError() {
l.Errors().Print()
return
return false
}

// parse
Expand All @@ -123,19 +125,19 @@ func (v *vapour) transpileFile(conf cli.CLI) {

if p.HasError() {
p.Errors().Print()
return
return false
}

// walk tree
w := walker.New()
w.Walk(prog)
w.Errors().Print()
if w.HasError() {
return
return false
}

if *conf.Check {
return
return false
}

// transpile
Expand All @@ -145,7 +147,7 @@ func (v *vapour) transpileFile(conf cli.CLI) {

if *conf.Run {
run(code)
return
return false
}

code = addHeader(code)
Expand All @@ -164,4 +166,6 @@ func (v *vapour) transpileFile(conf cli.CLI) {
if err != nil {
log.Fatal("Failed to write to output file")
}

return true
}

0 comments on commit df740c0

Please sign in to comment.