Skip to content

Commit

Permalink
Refactor to use args package
Browse files Browse the repository at this point in the history
  • Loading branch information
electrikmilk committed Dec 31, 2022
1 parent 799c599 commit ebeb01d
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 135 deletions.
100 changes: 0 additions & 100 deletions args.go

This file was deleted.

10 changes: 6 additions & 4 deletions cherri_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"os"
"strings"
"testing"

"github.com/electrikmilk/args-parser"
)

var currentTest string
Expand All @@ -23,14 +25,14 @@ func TestCherri(t *testing.T) {
currentTest = "examples/" + file.Name()
fmt.Printf("Compiling %s...\n", ansi(currentTest, bold))
os.Args[1] = currentTest
args["unsigned"] = ""
args["output"] = file.Name() + "_unsigned.shortcut"
args.Args["unsigned"] = ""
args.Args["output"] = file.Name() + "_unsigned.shortcut"
resetParser()
main()
fmt.Print(ansi("PASSED", green) + "\n\n")
err := os.Remove(args["output"])
err := os.Remove(args.Args["output"])
if err != nil {
fmt.Println(ansi(fmt.Sprintf("Failed to remove test file %s!\n", args["output"]), red))
fmt.Println(ansi(fmt.Sprintf("Failed to remove test file %s!\n", args.Args["output"]), red))
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ module cherri
go 1.19

require github.com/google/uuid v1.3.0

require github.com/electrikmilk/args-parser v0.0.0-20221231051228-bca6f7ec1d5c // indirect
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
github.com/electrikmilk/args-parser v0.0.0-20221231050324-56307138f9bf h1:yL7mc+Z2PTKTQz/C8OndwqkIGsx40XtkDiz0uZKfauo=
github.com/electrikmilk/args-parser v0.0.0-20221231050324-56307138f9bf/go.mod h1:mb/l2JFNNBSA8GVYL0eQCJF6Jd7UtAJTpenLZs6iIJ0=
github.com/electrikmilk/args-parser v0.0.0-20221231051228-bca6f7ec1d5c h1:QAhY7T+qQCvA0J1S1pTqJ1QlzXZaRoqeEoeA6ar80Ug=
github.com/electrikmilk/args-parser v0.0.0-20221231051228-bca6f7ec1d5c/go.mod h1:mb/l2JFNNBSA8GVYL0eQCJF6Jd7UtAJTpenLZs6iIJ0=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
57 changes: 29 additions & 28 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"regexp"
"strings"

"github.com/electrikmilk/args-parser"
"github.com/google/uuid"
)

Expand All @@ -26,15 +27,15 @@ var included []string
const fileExtension = "cherri"

func main() {
registerArg("share", "s", "Signing mode. [anyone, contacts] [default=contacts]")
registerArg("unsigned", "u", "Don't sign compiled Shortcut. Will NOT run on iOS or macOS.")
registerArg("debug", "d", "Save generated plist. Print debug messages and stack traces.")
registerArg("output", "o", "Optional output file path. (e.g. /path/to/file.shortcut).")
registerArg("import", "i", "Opens compiled Shortcut after compilation. Ignored if unsigned.")
registerArg("no-ansi", "a", "Don't output ANSI escape sequences that format and color the output.")
args.Register("share", "s", "Signing mode. [anyone, contacts] [default=contacts]")
args.Register("unsigned", "u", "Don't sign compiled Shortcut. Will NOT run on iOS or macOS.")
args.Register("debug", "d", "Save generated plist. Print debug messages and stack traces.")
args.Register("output", "o", "Optional output file path. (e.g. /path/to/file.shortcut).")
args.Register("import", "i", "Opens compiled Shortcut after compilation. Ignored if unsigned.")
args.Register("no-ansi", "a", "Don't output ANSI escape sequences that format and color the output.")
args.CustomUsage = "[FILE]"
if len(os.Args) <= 1 {
usage()
os.Exit(1)
args.PrintUsage()
}
filePath = os.Args[1]
checkFile(filePath)
Expand All @@ -48,23 +49,23 @@ func main() {
contents = string(bytes)

outputPath = basename + ".shortcut"
if arg("output") {
outputPath = argValue("output")
if args.Using("output") {
outputPath = args.Value("output")
}

if strings.Contains(contents, "#include") {
parseIncludes()
}

if arg("debug") {
if args.Using("debug") {
fmt.Printf("Parsing %s... ", filename)
}
parse()
if arg("debug") {
if args.Using("debug") {
fmt.Print(ansi("done!", green) + "\n")
}

if arg("debug") {
if args.Using("debug") {
fmt.Println(tokens)
fmt.Print("\n")
fmt.Println(variables)
Expand All @@ -73,40 +74,40 @@ func main() {
fmt.Print("\n")
}

if arg("debug") {
if args.Using("debug") {
fmt.Printf("Generating plist... ")
}
var plist = makePlist()
if arg("debug") {
if args.Using("debug") {
fmt.Print(ansi("done!", green) + "\n")
}

if arg("debug") {
if args.Using("debug") {
fmt.Printf("Creating %s.plist... ", basename)
plistWriteErr := os.WriteFile(basename+".plist", []byte(plist), 0600)
handle(plistWriteErr)
fmt.Print(ansi("done!", green) + "\n")
}

if arg("debug") {
if args.Using("debug") {
fmt.Printf("Creating unsigned %s.shortcut... ", basename)
}

var unsignedPath = basename + "_unsigned.shortcut"
if arg("unsigned") {
if args.Using("unsigned") {
unsignedPath = outputPath
}
shortcutWriteErr := os.WriteFile(unsignedPath, []byte(plist), 0600)
handle(shortcutWriteErr)
if arg("debug") {
if args.Using("debug") {
fmt.Print(ansi("done!", green) + "\n")
}

if !arg("unsigned") {
if !args.Using("unsigned") {
sign()
}

if arg("import") && !arg("unsigned") {
if args.Using("import") && !args.Using("unsigned") {
var _, importErr = exec.Command("open", outputPath).Output()
handle(importErr)
}
Expand Down Expand Up @@ -158,12 +159,12 @@ func checkFile(filePath string) {

func sign() {
var signingMode = "people-who-know-me"
if arg("share") {
if argValue("share") == "anyone" {
if args.Using("share") {
if args.Value("share") == "anyone" {
signingMode = "anyone"
}
}
if arg("debug") {
if args.Using("debug") {
fmt.Printf("Signing %s.shortcut... ", basename)
}
var signBytes, signErr = exec.Command(
Expand All @@ -174,7 +175,7 @@ func sign() {
"-m", signingMode,
).Output()
if signErr != nil {
if arg("debug") {
if args.Using("debug") {
fmt.Print(ansi("failed!", red) + "\n")
}
fmt.Println("\n" + ansi("Error: Failed to sign Shortcut, plist may be invalid!", red))
Expand All @@ -183,7 +184,7 @@ func sign() {
}
os.Exit(1)
}
if arg("debug") {
if args.Using("debug") {
fmt.Print(ansi("done!", green) + "\n")
}
removeErr := os.Remove(basename + "_unsigned.shortcut")
Expand Down Expand Up @@ -227,15 +228,15 @@ const (
const CSI = "\033["

func ansi(message string, typeOf outputType) string {
if arg("no-ansi") {
if args.Using("no-ansi") {
return message
}
return fmt.Sprintf("%s%dm%s", CSI, typeOf, message) + "\033[0m"
}

func exit(message string) {
fmt.Println("\nError: " + ansi(message, red) + "\n")
if arg("debug") {
if args.Using("debug") {
panic("debug")
} else {
os.Exit(1)
Expand Down
6 changes: 4 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"os"
"strconv"
"strings"

"github.com/electrikmilk/args-parser"
)

var idx int
Expand Down Expand Up @@ -763,7 +765,7 @@ func parserError(message string) {
if char == '\n' || prev(1) == '\n' {
lineIdx--
}
if lineIdx != -1 && !arg("no-ansi") {
if lineIdx != -1 && !args.Using("no-ansi") {
fmt.Print("\033[31m")
fmt.Println("\n" + ansi(message, bold))
fmt.Printf("\n\033[2m----- \033[0m%s:%d:%d\n", filePath, lineIdx+1, lineCharIdx+1)
Expand Down Expand Up @@ -792,7 +794,7 @@ func parserError(message string) {
} else {
fmt.Printf("Error: %s (%d:%d)\n", message, lineIdx+1, lineCharIdx+1)
}
if arg("debug") {
if args.Using("debug") {
fmt.Println(tokens)
panic("debug")
} else {
Expand Down
4 changes: 3 additions & 1 deletion plistgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package main
import (
"fmt"
"reflect"

"github.com/electrikmilk/args-parser"
)

const header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"https://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n<plist version=\"1.0\">\n<dict>\n"
Expand Down Expand Up @@ -370,7 +372,7 @@ func makePlist() (plist string) {

plist += footer

if arg("debug") {
if args.Using("debug") {
fmt.Println(uuids)
}

Expand Down

0 comments on commit ebeb01d

Please sign in to comment.