This repository was archived by the owner on Mar 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
Copy pathhandler.go
201 lines (183 loc) · 5.31 KB
/
handler.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
195
196
197
198
199
200
201
// Copyright (c) 2019 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package create
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/uber/prototool/internal/protostrs"
"github.com/uber/prototool/internal/settings"
"go.uber.org/zap"
)
var tmpl = template.Must(template.New("tmpl").Parse(`syntax = "proto3";
package {{.Pkg}};
option go_package = "{{.GoPkg}}";
option java_multiple_files = true;
option java_outer_classname = "{{.JavaOuterClassname}}";
option java_package = "{{.JavaPkg}}";`))
type tmplData struct {
Pkg string
GoPkg string
JavaOuterClassname string
JavaPkg string
}
type handler struct {
logger *zap.Logger
configProvider settings.ConfigProvider
pkg string
}
func newHandler(options ...HandlerOption) *handler {
handler := &handler{
logger: zap.NewNop(),
}
for _, option := range options {
option(handler)
}
handler.configProvider = settings.NewConfigProvider(
settings.ConfigProviderWithLogger(handler.logger),
)
return handler
}
func (h *handler) Create(filePaths ...string) error {
for _, filePath := range filePaths {
if err := h.checkFilePath(filePath); err != nil {
return err
}
}
for _, filePath := range filePaths {
if err := h.create(filePath); err != nil {
return err
}
}
return nil
}
func (h *handler) checkFilePath(filePath string) error {
if filePath == "" {
return errors.New("filePath empty")
}
dirPath := filepath.Dir(filePath)
fileInfo, err := os.Stat(dirPath)
if err != nil {
return err
}
if !fileInfo.IsDir() {
return fmt.Errorf("%q is not a directory somehow", dirPath)
}
if _, err := os.Stat(filePath); err == nil {
return fmt.Errorf("%q already exists", filePath)
}
return nil
}
func (h *handler) create(filePath string) error {
pkg, err := h.getPkg(filePath)
if err != nil {
return err
}
data, err := getData(
&tmplData{
Pkg: pkg,
GoPkg: protostrs.GoPackage(pkg),
JavaOuterClassname: protostrs.JavaOuterClassname(filePath),
JavaPkg: protostrs.JavaPackage(pkg),
},
)
if err != nil {
return err
}
return ioutil.WriteFile(filePath, data, 0644)
}
func (h *handler) getPkg(filePath string) (string, error) {
if h.pkg != "" {
return h.pkg, nil
}
absFilePath, err := filepath.Abs(filePath)
if err != nil {
return "", err
}
absDirPath := filepath.Dir(absFilePath)
config, err := h.configProvider.GetForDir(absDirPath)
if err != nil {
return "", err
}
// no config file found, can't compute package
if config.DirPath == "" {
return DefaultPackage, nil
}
// we need to get all the matching directories and then choose the longest
// ie if you have a, a/b, we choose a/b
var longestCreateDirPath string
var longestBasePkg string
// note that createDirPath will always be absolute per the spec in
// the settings package
for createDirPath, basePkg := range config.Create.DirPathToBasePackage {
// TODO: cannot do rel right away because it will do ../.. if necessary
// strings.HasPrefix is not OS independent however
if !strings.HasPrefix(absDirPath, createDirPath) {
continue
}
if len(createDirPath) > len(longestCreateDirPath) {
longestCreateDirPath = createDirPath
longestBasePkg = basePkg
}
}
if longestCreateDirPath != "" {
rel, err := filepath.Rel(longestCreateDirPath, absDirPath)
if err != nil {
return "", err
}
return getPkgFromRel(rel, longestBasePkg), nil
}
// no package mapping found, do default logic
// TODO: cannot do rel right away because it will do ../.. if necessary
// strings.HasPrefix is not OS independent however
if !strings.HasPrefix(absDirPath, config.DirPath) {
return DefaultPackage, nil
}
rel, err := filepath.Rel(config.DirPath, absDirPath)
if err != nil {
return "", err
}
return getPkgFromRel(rel, ""), nil
}
func getPkgFromRel(rel string, basePkg string) string {
if rel == "." {
if basePkg == "" {
return DefaultPackage
}
return basePkg
}
relPkg := strings.Join(strings.Split(rel, string(os.PathSeparator)), ".")
if basePkg == "" {
return relPkg
}
return basePkg + "." + relPkg
}
func getData(tmplData *tmplData) ([]byte, error) {
buffer := bytes.NewBuffer(nil)
if err := tmpl.Execute(buffer, tmplData); err != nil {
return nil, err
}
return buffer.Bytes(), nil
}