@@ -0,0 +1,103 @@
1+package main
2+
3+//go:generate ../../scripts/gofiles.sh generated_files.go
4+
5+import (
6+ "fmt"
7+ "io/ioutil"
8+ "os"
9+ "os/exec"
10+ "syscall"
11+ "github.com/replit/prybar/utils"
12+)
13+
14+func findHelper(path string) string {
15+ fileName := path + ".clj"
16+ bytes, err := File(fileName)
17+ if bytes != nil {
18+ f, err := ioutil.TempFile("", fileName)
19+ if err != nil {
20+ panic(err)
21+ }
22+
23+ if _, err = f.Write(bytes); err != nil {
24+ panic(err)
25+ }
26+ if err = f.Close(); err != nil {
27+ panic(err)
28+ }
29+ return f.Name()
30+ }
31+ if err != nil {
32+ panic(err)
33+ }
34+ panic("File not found: " + fileName)
35+}
36+
37+func Execute(config *utils.Config) {
38+ path, err := exec.LookPath("clj")
39+
40+ if err != nil {
41+ panic(err)
42+ }
43+
44+ env := os.Environ()
45+ args := []string{"clj"}
46+
47+ hasOption := config.Code != "" || config.Exp != "" ||
48+ config.Interactive || config.OurInteractive
49+ hasFile := false
50+
51+ if config.Ps1 != "" {
52+ args = append(args, "-J-DPRYBAR_PS1=" + config.Ps1)
53+ }
54+
55+ if config.Ps2 != "" {
56+ args = append(args, "-J-DPRYBAR_PS2=" + config.Ps2)
57+ }
58+
59+ if config.Quiet {
60+ args = append(args, "-J-DPRYBAR_QUIET=true")
61+ }
62+
63+ if hasOption {
64+ if config.Args != nil && len(config.Args) > 0 {
65+ if _, err := os.Stat(config.Args[0]); os.IsNotExist(err) {
66+ fmt.Println("No such file:", config.Args[0])
67+ os.Exit(2)
68+ }
69+
70+ hasFile = true
71+ }
72+
73+ if hasFile {
74+ args = append(args, "--init", config.Args[0])
75+ }
76+
77+ if config.Code != "" {
78+ // "--eval" prints non-nil results only.
79+ effect := fmt.Sprintf("(do %s nil)", config.Code)
80+ args = append(args, "--eval", effect)
81+ }
82+
83+ if config.Exp != "" {
84+ args = append(args, "--eval", config.Exp)
85+ }
86+
87+ if config.Interactive || config.OurInteractive {
88+ args = append(args, findHelper("prybar_repl"))
89+ }
90+
91+ if hasFile {
92+ args = append(args, config.Args[1:]...)
93+ } else {
94+ args = append(args, config.Args...)
95+ }
96+ } else {
97+ args = append(args, "--eval", "")
98+ args = append(args, config.Args...)
99+ }
100+
101+ syscall.Exec(path, args, env)
102+}
103+