-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresourceful.go
116 lines (110 loc) · 2.64 KB
/
resourceful.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
package main
import (
"bufio"
"github.com/codegangsta/cli"
"io/ioutil"
"log"
"os"
"path"
"strings"
"text/template"
)
type EnumCase struct {
Name string
RawValue string
}
type EnumType struct {
Name string
Values []EnumCase
Nested []EnumType
}
func parseFolder(filepath string) (EnumType, error) {
files, err := ioutil.ReadDir(filepath)
if err != nil {
return EnumType{}, err
}
enumCases := []EnumCase{}
enumTypes := []EnumType{}
for _, f := range files {
if strings.HasSuffix(f.Name(), "imageset") {
rawValue := strings.Split(f.Name(), ".")[0]
name := strings.Title(rawValue)
name = strings.Replace(name, "_", "__", -1)
name = strings.Replace(name, "-", "_", -1)
enum := EnumCase{name, rawValue}
enumCases = append(enumCases, enum)
} else if !strings.Contains(f.Name(), ".") {
folderName := path.Join(filepath, f.Name())
enumType, err := parseFolder(folderName)
if err != nil {
return EnumType{}, err
}
enumTypes = append(enumTypes, enumType)
}
}
_, file := path.Split(filepath)
name := strings.Title(file)
return EnumType{name, enumCases, enumTypes}, nil
}
func main() {
app := cli.NewApp()
l := log.New(os.Stderr, "", 0)
app.Name = "resourceful"
app.Usage = "Add strong typing to imageNamed: in Swift apps"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "input, i",
Value: "Images.xcassets",
Usage: "The xcassets directory that contains your app's images",
EnvVar: "SCRIPT_INPUT_FILE_0",
},
cli.StringFlag{
Name: "output, o",
Value: "Resourceful.swift",
Usage: "The destination file for generated Swift code",
EnvVar: "SCRIPT_OUTPUT_FILE_0",
},
}
app.Commands = []cli.Command{
{
Name: "warn",
Usage: "Generate xcode warnings for usage of imageNamed",
Flags: []cli.Flag{
cli.StringFlag{
Name: "warndirectory, w",
Usage: "The directory to search for legacy usage of imageNamed",
EnvVar: "SRCROOT",
},
},
Action: func(c *cli.Context) {
err := warn(c.String("warndirectory"))
if err != nil {
l.Println("Error detecting warnings.")
os.Exit(1)
}
},
},
}
app.Action = func(c *cli.Context) {
enumType, err := parseFolder(c.String("input"))
if err != nil {
l.Println("Error parsing input file.")
os.Exit(1)
}
enumType.Name = "Image"
f, err := os.Create(c.String("output"))
if err != nil {
l.Println("Error writing output file")
os.Exit(1)
}
defer f.Close()
writer := bufio.NewWriter(f)
templ := template.Must(template.New("swiftTemplate").Parse(swiftTemplate))
err = templ.Execute(writer, enumType)
if err != nil {
panic(err)
}
writer.Flush()
}
app.Run(os.Args)
}