-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathmain.go
131 lines (126 loc) · 4.48 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
// Copyright 2017 Walter Schulze
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package main implements the goderive binary.
// This pulls in all the plugins, parses the flags and runs the generators using the derive library.
package main
import (
"flag"
"log"
"strings"
"github.com/awalterschulze/goderive/derive"
"github.com/awalterschulze/goderive/plugin/all"
"github.com/awalterschulze/goderive/plugin/any"
"github.com/awalterschulze/goderive/plugin/apply"
"github.com/awalterschulze/goderive/plugin/clone"
"github.com/awalterschulze/goderive/plugin/compare"
"github.com/awalterschulze/goderive/plugin/compose"
"github.com/awalterschulze/goderive/plugin/contains"
"github.com/awalterschulze/goderive/plugin/curry"
"github.com/awalterschulze/goderive/plugin/deepcopy"
"github.com/awalterschulze/goderive/plugin/do"
"github.com/awalterschulze/goderive/plugin/dup"
"github.com/awalterschulze/goderive/plugin/equal"
"github.com/awalterschulze/goderive/plugin/filter"
"github.com/awalterschulze/goderive/plugin/flip"
"github.com/awalterschulze/goderive/plugin/fmap"
"github.com/awalterschulze/goderive/plugin/gostring"
"github.com/awalterschulze/goderive/plugin/hash"
"github.com/awalterschulze/goderive/plugin/intersect"
"github.com/awalterschulze/goderive/plugin/join"
"github.com/awalterschulze/goderive/plugin/keys"
"github.com/awalterschulze/goderive/plugin/max"
"github.com/awalterschulze/goderive/plugin/mem"
"github.com/awalterschulze/goderive/plugin/min"
"github.com/awalterschulze/goderive/plugin/pipeline"
"github.com/awalterschulze/goderive/plugin/set"
"github.com/awalterschulze/goderive/plugin/sort"
"github.com/awalterschulze/goderive/plugin/takewhile"
"github.com/awalterschulze/goderive/plugin/toerror"
"github.com/awalterschulze/goderive/plugin/traverse"
"github.com/awalterschulze/goderive/plugin/tuple"
"github.com/awalterschulze/goderive/plugin/uncurry"
"github.com/awalterschulze/goderive/plugin/union"
"github.com/awalterschulze/goderive/plugin/unique"
)
var autoname = flag.Bool("autoname", false, "rename functions that are conflicting with other functions")
var dedup = flag.Bool("dedup", false, "rename functions to functions that are duplicates")
var prefix = flag.String("prefix", "derive", "prefix of all functions")
var pluginprefix = flag.String("pluginprefix", "", "used to override function prefixes. The input is a comma separated list of function are prefix pairs. For example equal=deriveEqual,copyto=copyTo,fmap=fmap,")
func main() {
plugins := []derive.Plugin{
equal.NewPlugin(),
compare.NewPlugin(),
fmap.NewPlugin(),
join.NewPlugin(),
keys.NewPlugin(),
sort.NewPlugin(),
deepcopy.NewPlugin(),
set.NewPlugin(),
min.NewPlugin(),
max.NewPlugin(),
contains.NewPlugin(),
intersect.NewPlugin(),
union.NewPlugin(),
filter.NewPlugin(),
takewhile.NewPlugin(),
unique.NewPlugin(),
flip.NewPlugin(),
toerror.NewPlugin(),
curry.NewPlugin(),
uncurry.NewPlugin(),
all.NewPlugin(),
any.NewPlugin(),
tuple.NewPlugin(),
gostring.NewPlugin(),
compose.NewPlugin(),
do.NewPlugin(),
pipeline.NewPlugin(),
dup.NewPlugin(),
clone.NewPlugin(),
hash.NewPlugin(),
mem.NewPlugin(),
traverse.NewPlugin(),
apply.NewPlugin(),
}
log.SetFlags(0)
flag.Parse()
overridePrefixes := make(map[string]string)
if len(*pluginprefix) > 0 {
pairs := strings.Split(*pluginprefix, ",")
for _, pair := range pairs {
ss := strings.Split(pair, "=")
if len(ss) != 2 {
log.Fatalf("invalid syntax for plugin prefix <%s>", pair)
}
overridePrefixes[ss[0]] = ss[1]
}
}
for _, p := range plugins {
pluginprefix := p.GetPrefix()
pluginprefix = strings.Replace(pluginprefix, "derive", *prefix, 1)
newprefix, override := overridePrefixes[p.Name()]
if override {
pluginprefix = newprefix
}
p.SetPrefix(pluginprefix)
}
paths := derive.ImportPaths(flag.Args())
g, err := derive.NewPlugins(plugins, *autoname, *dedup).Load(paths)
if err != nil {
log.Fatal(err)
}
if err := g.Generate(); err != nil {
log.Fatal(err)
}
}