forked from TheThingsNetwork/lorawan-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproto.go
More file actions
235 lines (210 loc) · 7.44 KB
/
proto.go
File metadata and controls
235 lines (210 loc) · 7.44 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// 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 ttnmage
import (
"context"
"fmt"
"os"
"os/user"
"path/filepath"
"strings"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"github.com/magefile/mage/target"
"golang.org/x/xerrors"
)
const (
protocName = "thethingsindustries/protoc"
protocVersion = "3.1.20-ttn"
protocOut = "/out"
)
// Proto namespace.
type Proto mg.Namespace
// Image pulls the proto image.
func (Proto) Image(context.Context) error {
out, err := sh.Output("docker", "images", "-q", fmt.Sprintf("%s:%s", protocName, protocVersion))
if err != nil {
return xerrors.Errorf("failed to query docker images: %s", err)
}
if len(out) > 0 {
return nil
}
return sh.Run("docker", "pull", fmt.Sprintf("%s:%s", protocName, protocVersion))
}
type protocContext struct {
WorkingDirectory string
UID, GID string
}
func makeProtoc() (func(...string) error, *protocContext, error) {
mg.Deps(Proto.Image)
wd, err := os.Getwd()
if err != nil {
return nil, nil, xerrors.Errorf("failed to get working directory: %w", err)
}
usr, err := user.Current()
if err != nil {
return nil, nil, xerrors.Errorf("failed to get user: %w", err)
}
mountWD := filepath.ToSlash(filepath.Join(filepath.Dir(wd), "lorawan-stack"))
return sh.RunCmd("docker", "run",
"--rm",
"--user", fmt.Sprintf("%s:%s", usr.Uid, usr.Gid),
"--mount", fmt.Sprintf("type=bind,src=%s,dst=%s/api", filepath.Join(wd, "api"), mountWD),
"--mount", fmt.Sprintf("type=bind,src=%s,dst=%s/go.thethings.network/lorawan-stack/pkg/ttnpb", filepath.Join(wd, "pkg", "ttnpb"), protocOut),
"--mount", fmt.Sprintf("type=bind,src=%s,dst=%s/sdk/js", filepath.Join(wd, "sdk", "js"), mountWD),
"-w", mountWD,
fmt.Sprintf("%s:%s", protocName, protocVersion),
fmt.Sprintf("-I%s", filepath.Dir(wd)),
), &protocContext{
WorkingDirectory: mountWD,
UID: usr.Uid,
GID: usr.Gid,
}, nil
}
func withProtoc(f func(pCtx *protocContext, protoc func(...string) error) error) error {
protoc, pCtx, err := makeProtoc()
if err != nil {
return xerrors.New("failed to construct protoc command")
}
return f(pCtx, protoc)
}
// Go generates Go protos.
func (p Proto) Go(context.Context) error {
if err := withProtoc(func(pCtx *protocContext, protoc func(...string) error) error {
var convs []string
for _, t := range []string{"any", "duration", "empty", "field_mask", "struct", "timestamp", "wrappers"} {
convs = append(convs, fmt.Sprintf("Mgoogle/protobuf/%s.proto=github.com/gogo/protobuf/types", t))
}
convStr := strings.Join(convs, ",")
if err := protoc(
fmt.Sprintf("--fieldmask_out=lang=gogo,%s:%s", convStr, protocOut),
fmt.Sprintf("--gogottn_out=plugins=grpc,%s:%s", convStr, protocOut),
fmt.Sprintf("--grpc-gateway_out=%s:%s", convStr, protocOut),
fmt.Sprintf("%s/api/*.proto", pCtx.WorkingDirectory),
); err != nil {
return xerrors.Errorf("failed to generate protos: %w", err)
}
return nil
}); err != nil {
return err
}
if err := sh.RunV(filepath.Join(".mage", "scripts", "fix-grpc-gateway-names.sh"), "api"); err != nil {
return xerrors.Errorf("failed to fix gRPC-gateway names: %w", err)
}
ttnpb, err := filepath.Abs(filepath.Join("pkg", "ttnpb"))
if err != nil {
return xerrors.Errorf("failed to construct absolute path to pkg/ttnpb: %w", err)
}
if err := execGo("run", "golang.org/x/tools/cmd/goimports", "-w", ttnpb); err != nil {
return xerrors.Errorf("failed to run goimports on generated code: %w", err)
}
if err := execGo("run", "github.com/mdempsky/unconvert", "-apply", ttnpb); err != nil {
return xerrors.Errorf("failed to run unconvert on generated code: %w", err)
}
return sh.RunV("gofmt", "-w", "-s", ttnpb)
}
// GoClean removes generated Go protos.
func (p Proto) GoClean(context.Context) error {
return filepath.Walk(filepath.Join("pkg", "ttnpb"), func(path string, _ os.FileInfo, err error) error {
if err != nil {
return err
}
for _, ext := range []string{".pb.go", ".pb.gw.go", ".pb.fm.go", ".pb.util.go"} {
if strings.HasSuffix(path, ext) {
if err := sh.Rm(path); err != nil {
return err
}
return nil
}
}
return nil
})
}
// Swagger generates Swagger protos.
func (p Proto) Swagger(context.Context) error {
changed, err := target.Glob(filepath.Join("api", "api.swagger.json"), filepath.Join("api", "*.proto"))
if err != nil {
return xerrors.Errorf("failed checking modtime: %w", err)
}
if !changed {
return nil
}
return withProtoc(func(pCtx *protocContext, protoc func(...string) error) error {
if err := protoc(
fmt.Sprintf("--swagger_out=allow_merge,merge_file_name=api:%s/api", pCtx.WorkingDirectory),
fmt.Sprintf("%s/api/*.proto", pCtx.WorkingDirectory),
); err != nil {
return xerrors.Errorf("failed to generate protos: %w", err)
}
return nil
})
}
// SwaggerClean removes generated Swagger protos.
func (p Proto) SwaggerClean(context.Context) error {
return sh.Rm(filepath.Join("api", "api.swagger.json"))
}
// Markdown generates Markdown protos.
func (p Proto) Markdown(context.Context) error {
changed, err := target.Glob(filepath.Join("api", "api.md"), filepath.Join("api", "*.proto"))
if err != nil {
return xerrors.Errorf("failed checking modtime: %w", err)
}
if !changed {
return nil
}
return withProtoc(func(pCtx *protocContext, protoc func(...string) error) error {
if err := protoc(
fmt.Sprintf("--doc_opt=%s/api/api.md.tmpl,api.md --doc_out=%s/api", pCtx.WorkingDirectory, pCtx.WorkingDirectory),
fmt.Sprintf("%s/api/*.proto", pCtx.WorkingDirectory),
); err != nil {
return xerrors.Errorf("failed to generate protos: %w", err)
}
return nil
})
}
// MarkdownClean removes generated Markdown protos.
func (p Proto) MarkdownClean(context.Context) error {
return sh.Rm(filepath.Join("api", "api.md"))
}
// JsSDK generates javascript SDK protos.
func (p Proto) JsSDK(context.Context) error {
changed, err := target.Glob(filepath.Join("sdk", "js", "generated", "api.json"), filepath.Join("api", "*.proto"))
if err != nil {
return xerrors.Errorf("failed checking modtime: %w", err)
}
if !changed {
return nil
}
return withProtoc(func(pCtx *protocContext, protoc func(...string) error) error {
if err := protoc(
fmt.Sprintf("--doc_opt=json,api.json --doc_out=%s/sdk/js/generated", pCtx.WorkingDirectory),
fmt.Sprintf("%s/api/*.proto", pCtx.WorkingDirectory),
); err != nil {
return xerrors.Errorf("failed to generate protos: %w", err)
}
return nil
})
}
// JsSDKClean removes generated javascript SDK protos.
func (p Proto) JsSDKClean(context.Context) error {
return sh.Rm(filepath.Join("sdk", "js", "generated", "api.json"))
}
// All generates protos.
func (p Proto) All(ctx context.Context) {
mg.CtxDeps(ctx, Proto.Go, Proto.Swagger, Proto.Markdown, Proto.JsSDK)
}
// Clean removes generated protos.
func (p Proto) Clean(ctx context.Context) {
mg.CtxDeps(ctx, Proto.GoClean, Proto.SwaggerClean, Proto.MarkdownClean, Proto.JsSDKClean)
}