-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
133 lines (113 loc) · 3.33 KB
/
run.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"unicode"
"github.com/fatih/color"
"github.com/jatindotdev/cli/lib"
)
const (
JavaScript = "js"
C = "c"
Cpp = "cpp"
Dart = "dart"
Swift = "swift"
Python = "py"
Java = "java"
Go = "go"
)
func main() {
args := os.Args[1:]
if len(args) == 0 || lib.Contains(args[0], "help", "-h", "--help") {
printHelp()
os.Exit(0)
}
fileName := args[0]
if !lib.FileExists(fileName) {
fileNameWithoutDir := filepath.Base(fileName)
fmt.Fprintf(os.Stderr, color.RedString("Error: `%s` does not exist!\n"), fileNameWithoutDir)
os.Exit(1)
}
extension := filepath.Ext(fileName)
fileNameWithoutExt := strings.TrimSuffix(fileName, extension)
remainingArgs := []string{}
for _, arg := range args[1:] {
remainingArgs = append(remainingArgs, fmt.Sprintf("'%s'", arg))
}
remainingArgsAsString := strings.Join(remainingArgs, " ")
commandsPerExt := map[string][]string{
JavaScript: {fmt.Sprintf("bun run %s %s", fileName, remainingArgsAsString)},
C: {
fmt.Sprintf("gcc -std=c2x -o %s %s", fileNameWithoutExt, fileName),
fmt.Sprintf("./%s %s", fileNameWithoutExt, remainingArgsAsString),
fmt.Sprintf("rm -rf %s", fileNameWithoutExt),
},
Cpp: {
fmt.Sprintf("g++ -std=c++17 -o %s %s", fileNameWithoutExt, fileName),
fmt.Sprintf("./%s %s", fileNameWithoutExt, remainingArgsAsString),
fmt.Sprintf("rm -rf %s", fileNameWithoutExt),
},
Dart: {fmt.Sprintf("dart run %s %s", fileName, remainingArgsAsString)},
Swift: {fmt.Sprintf("swift %s %s", fileName, remainingArgsAsString)},
Python: {fmt.Sprintf("python3 -u %s %s ", fileName, remainingArgsAsString)},
Java: {
fmt.Sprintf("javac %s", fileName),
fmt.Sprintf("java %s %s", fileNameWithoutExt, remainingArgsAsString),
fmt.Sprintf("rm -rf %s.class", fileNameWithoutExt),
},
Go: {
fmt.Sprintf("go run %s %s", fileName, remainingArgsAsString),
},
}
commands := commandsPerExt[extension[1:]]
if commands == nil {
fmt.Fprintf(os.Stderr, color.RedString("Error: we don't support `%s` extensions yet.\n"), extension)
fmt.Fprintln(os.Stderr, "Please raise an issue here https://github.com/jatindotdev/cli/issues")
os.Exit(0)
}
for _, command := range commands {
cmdIndex := strings.Index(command, " ")
cmdBin := command[:cmdIndex]
// Split command and arguments
inQuotes := false
cmdArgs := strings.FieldsFunc(command[cmdIndex:], func(r rune) bool {
if r == '\'' {
inQuotes = !inQuotes
}
return unicode.IsSpace(r) && !inQuotes
})
// Remove single quotes from arguments
for i, arg := range cmdArgs {
cmdArgs[i] = strings.Trim(arg, "'")
}
cmd := exec.Command(cmdBin, cmdArgs...)
// Set the standard streams
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
err := cmd.Run()
if err != nil {
continue
}
}
}
func printHelp() {
color.Set(color.Bold, color.FgMagenta)
fmt.Print("Run")
color.Unset()
fmt.Print(" does what its name says: it runs a file.\n\n")
color.Unset()
color.Set(color.Bold)
fmt.Print("Usage: run <command>")
color.Set(color.FgGreen)
fmt.Println(" [...args]")
color.Unset()
fmt.Print("\n [...args] Arguments that will be passed to the command\n\n")
color.Set(color.Bold)
fmt.Println("Commands:")
color.Unset()
fmt.Println(" help Show this help message")
}