-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.go
194 lines (163 loc) Β· 4.78 KB
/
main.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* Copyright (c) Cherri
*/
package main
import (
"fmt"
"github.com/electrikmilk/args-parser"
"os"
"runtime"
"strings"
"unicode"
)
var filePath string
var filename string
var basename string
var contents string
var relativePath string
var inputPath string
var outputPath string
const unsignedEnd = "_unsigned.shortcut"
const darwin = runtime.GOOS == "darwin"
func main() {
if args.Using("help") {
args.PrintUsage()
os.Exit(0)
}
if args.Using("version") {
printVersion()
os.Exit(0)
}
if args.Using("action") {
if args.Value("action") == "" {
for identifier, definition := range actions {
setCurrentAction(identifier, definition)
fmt.Println(generateActionDefinition(parameterDefinition{}, true, true))
fmt.Print("\n---\n\n")
}
} else {
actionsSearch()
}
os.Exit(0)
}
if args.Using("glyph") {
if args.Value("glyph") == "" {
fmt.Println("Search all usable glyphs at https://glyphs.cherrilang.org.")
} else {
glyphsSearch()
}
os.Exit(0)
}
filePath = fileArg()
if len(os.Args) == 1 || filePath == "" {
printLogo()
printVersion()
if !darwin {
fmt.Println(ansi("\nWarning:", yellow, bold), "Shortcuts compiled on this platform will not run on iOS 15+ or macOS 12+.")
}
fmt.Print("\n")
args.PrintUsage()
os.Exit(1)
}
filename = checkFile(filePath)
handleFile()
initParse()
makePlist()
createShortcut()
}
func handle(err error) {
if err == nil {
return
}
fmt.Print(ansi("\nProgram panic :(\n\n", red, bold))
fmt.Print(ansi("Please report this: https://github.com/electrikmilk/cherri/issues/new\n\n", red))
if args.Using("debug") {
panicDebug(err)
} else {
panic(err)
}
}
func printVersion() {
var color outputType
if strings.Contains(version, "beta") {
color = yellow
} else {
color = green
}
fmt.Printf("Cherri Compiler " + ansi(version, color) + "\n")
}
func printLogo() {
fmt.Print(ansi("\n $$$\n $$$$$\n $$$$$$$$$\n $$$$$$$$ $$$$\n $$$$ $$ $$$$\n $$$ $$$$$$ $ $$\n $ $$$$$$$$$ $ $$ \n $$ $$$$$$$$$ $ $$ \n $$ $$$$$$$$$$ $ $$ \n $$$$$$$$$$$ $$ $$ ", green))
fmt.Print(ansi("$$$$$$ \n", red))
fmt.Print(ansi("$$$$$$$$$ $$ $$ ", green))
fmt.Print(ansi("$$$$$$$$$$\n $$$$$ ", red))
fmt.Print(ansi("$$$ $$ $ ", green))
fmt.Print(ansi("$$$$$$$$$\n $$$$$$$$$ $$$$$$$$$$$$$$$$$$$$$$$$\n $$$$$$$$$$$$ $$$$$ $$$$$$$$$$$$$$\n $$$ $$$$$$$$$$$$$$$$ $$$$$$$$$ $$\n $$ $$$$$$$$$$$$$$$$$ $$$$$$$$$ $$\n $$ $$$$$$$$$$$$$$$$$$ $$$$$$$$$ $$ \n $$$ $$$$$$$$$$$$$$$$$$ $$$$$$$$$$$ \n $$$ $$$$$$$$$$$$$$$$ $$$$$$$$$$$ \n $$$$$$$$$$$$$$$$$$$ $$$$$$$$$ \n $$$$$$$$$$$$$$$$$$$ \n $$$$$$$$$$$$$$$ \n $$$$$$$$$$ \n\n", red))
}
func fileArg() string {
for _, arg := range os.Args {
if !strings.Contains(arg, ".cherri") || startsWith("-", arg) {
continue
}
return arg
}
return ""
}
// handleFile splits the file argument into parts.
func handleFile() {
relativePath = strings.Replace(filePath, filename, "", 1)
var nameParts = strings.Split(filename, ".")
basename = nameParts[0]
workflowName = basename
outputPath = relativePath + workflowName + ".shortcut"
if args.Using("output") {
outputPath = args.Value("output")
}
var fileBytes, readErr = os.ReadFile(filePath)
handle(readErr)
contents = string(fileBytes)
}
// checkFile checks if the file exists and is a .cherri file.
func checkFile(filePath string) (filename string) {
var file, statErr = os.Stat(filePath)
if os.IsNotExist(statErr) {
exit(fmt.Sprintf("File '%s' does not exist!", filePath))
}
var nameParts = strings.Split(file.Name(), ".")
var ext = end(nameParts)
if ext != "cherri" {
exit(fmt.Sprintf("File '%s' is not a .cherri file!", filePath))
}
return file.Name()
}
func end(slice []string) string {
return slice[len(slice)-1]
}
func capitalize(s string) string {
var char = s[0]
var after, _ = strings.CutPrefix(s, string(char))
return fmt.Sprintf("%c%s", unicode.ToUpper(rune(char)), after)
}
// startsWith determines if the beginning characters of `substr` match `s`.
func startsWith(s string, substr string) bool {
var stringChars = []rune(s)
var subStringChars = []rune(substr)
for i, char := range subStringChars {
if char != stringChars[i] {
return false
}
}
return true
}
func panicDebug(err error) {
fmt.Println(ansi("###################\n# DEBUG PANIC #\n###################\n", bold, red))
printParsingDebug()
printPlistGenDebug()
printCustomActionsDebug()
printIncludesDebug()
fmt.Println(ansi("#############################################################\n", bold, red))
if err != nil {
panic(err)
}
panic("debug")
}