go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/web/rpcexplorer/src/data/testdata/gen.go (about) 1 // Copyright 2023 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package main 16 17 // This file generates descriptor.ts. Run itself: 18 //go:generate go run gen.go 19 20 import ( 21 "fmt" 22 "io" 23 "io/ioutil" 24 "os" 25 "os/exec" 26 27 "google.golang.org/protobuf/encoding/protojson" 28 "google.golang.org/protobuf/proto" 29 "google.golang.org/protobuf/types/descriptorpb" 30 ) 31 32 func run() error { 33 descFile, err := ioutil.TempFile("", "desc") 34 if err != nil { 35 return err 36 } 37 38 protoc := exec.Command( 39 "protoc", 40 "--descriptor_set_out="+descFile.Name(), 41 "--include_source_info", 42 "types.proto") 43 protoc.Stdout = os.Stdout 44 protoc.Stderr = os.Stderr 45 if err := protoc.Run(); err != nil { 46 return fmt.Errorf("protoc: %s", err) 47 } 48 49 descBytes, err := io.ReadAll(descFile) 50 if err != nil { 51 return err 52 } 53 if len(descBytes) == 0 { 54 return fmt.Errorf("desc file was read as empty") 55 } 56 57 desc := &descriptorpb.FileDescriptorSet{} 58 if err := proto.Unmarshal(descBytes, desc); err != nil { 59 return fmt.Errorf("could not read descriptor file: %s", err) 60 } 61 62 m := protojson.MarshalOptions{Multiline: true, Indent: " "} 63 blob, err := m.Marshal(desc) 64 if err != nil { 65 return err 66 } 67 68 out, err := os.Create("descriptor.ts") 69 if err != nil { 70 return err 71 } 72 _, err = fmt.Fprintf(out, `// Copyright 2023 The LUCI Authors. 73 // 74 // Licensed under the Apache License, Version 2.0 (the "License"); 75 // you may not use this file except in compliance with the License. 76 // You may obtain a copy of the License at 77 // 78 // http://www.apache.org/licenses/LICENSE-2.0 79 // 80 // Unless required by applicable law or agreed to in writing, software 81 // distributed under the License is distributed on an "AS IS" BASIS, 82 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 83 // See the License for the specific language governing permissions and 84 // limitations under the License. 85 86 // This file is generated by gen.go. 87 // To update run "go generate .". 88 89 export const TestDescriptor = %s; 90 91 export default TestDescriptor; 92 `, blob) 93 return err 94 } 95 96 func main() { 97 if err := run(); err != nil { 98 fmt.Fprintln(os.Stderr, err) 99 os.Exit(1) 100 } 101 }