-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
109 lines (90 loc) · 3.21 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
package main
import (
"flag"
"fmt"
"os"
"github.com/alvaroloes/ocgen/generator"
"github.com/alvaroloes/ocgen/parser"
"strings"
"path/filepath"
)
const version = "0.5.2"
const (
defaultNSCodingProtocolName = "NSCoding"
defaultNSCopyingProtocolName = "NSCopying"
)
var params struct {
printVersion, printHelp bool
extraNSCodingProtocols string
extraNSCopyingProtocols string
backup bool
backupDir string
}
func main() {
configureUsage();
if flag.NArg() == 0 {
fmt.Fprintln(os.Stderr, "At least one directory must be specified")
flag.Usage()
return
}
if params.printVersion {
fmt.Printf("OCGen v%v",version)
return
}
if params.printHelp {
flag.Usage()
return
}
var backupDir string
if params.backup {
backupDir = params.backupDir
}
parser := parser.NewParser()
NSCodingProtocols := []string{defaultNSCodingProtocolName}
if len(params.extraNSCodingProtocols) > 0 {
NSCodingProtocols = append(NSCodingProtocols, strings.Split(params.extraNSCodingProtocols, ",")...)
}
NSCopyingProtocols := []string{defaultNSCopyingProtocolName}
if len(params.extraNSCopyingProtocols) > 0 {
NSCopyingProtocols = append(NSCopyingProtocols, strings.Split(params.extraNSCopyingProtocols, ",")...)
}
for _, dir := range flag.Args() {
processDirectory(parser, dir, NSCodingProtocols, NSCopyingProtocols, backupDir)
}
}
func processDirectory(parser parser.Parser, dir string, NSCodingProtocols, NSCopyingProtocols []string, backupDir string) {
// Get all the header files under the directory
fileNames := parser.GetParseableFiles(dir)
for _, fileName := range fileNames {
classFile, err := parser.Parse(fileName)
if err != nil {
fmt.Fprintln(os.Stderr, err)
continue
}
//Stop here if no classes where found
if len(classFile.Classes) > 0 {
err = generator.GenerateMethods(classFile, NSCodingProtocols, NSCopyingProtocols, backupDir)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
}
}
func configureUsage() {
// Tune a little the "usage" message
flag.Usage = func() {
executableName := filepath.Base(os.Args[0])
fmt.Fprintf(os.Stderr, "Usage: %s [options] directory1 [directory2,...]\n", executableName)
flag.PrintDefaults()
}
extraProtoDescription := "A comma separated list (without spaces) of protocol names that will be considered as if they were %v. " +
"This is useful if your class does not conform %v directly, but through another protocol that conforms it. " +
"Example: extra%vProtocols=\"MyProtocolThatConforms%v,OtherProtocolThatConforms%v\""
flag.StringVar(¶ms.extraNSCodingProtocols, "extraNSCodingProtocols", "", strings.Replace(extraProtoDescription,"%v","NSCoding",-1))
flag.StringVar(¶ms.extraNSCopyingProtocols, "extraNSCopyingProtocols", "", strings.Replace(extraProtoDescription,"%v","NSCopying",-1))
flag.BoolVar(¶ms.backup, "backup", false, "Whether to create a backup of all files before modifying them")
flag.StringVar(¶ms.backupDir, "backupDir", "./.ocgen", "The directory where the backups will be placed if 'backup' is present")
flag.BoolVar(¶ms.printVersion, "v", false, "Prints the current version")
flag.BoolVar(¶ms.printHelp, "h", false, "Prints the usage")
flag.Parse()
}