|
6 | 6 | "context" |
7 | 7 | "log/slog" |
8 | 8 | "os" |
| 9 | + "path/filepath" |
9 | 10 |
|
10 | 11 | "github.com/google/go-github/v74/github" |
11 | 12 |
|
@@ -46,10 +47,90 @@ func (s *Scraper) Run(ctx context.Context) ([]openrpc.Method, error) { |
46 | 47 | return nil, err |
47 | 48 | } |
48 | 49 |
|
| 50 | + localMethodSrc, err := s.LocalSource(ctx, "_schemas.yaml") |
| 51 | + if err != nil { |
| 52 | + return nil, err |
| 53 | + } |
| 54 | + s.log.InfoContext(ctx, "Local method files retrieved", "methods", len(localMethodSrc)) |
| 55 | + methodSrc = append(methodSrc, localMethodSrc...) |
| 56 | + |
49 | 57 | schemaSrc, err := s.SchemaSource(ctx) |
50 | 58 | if err != nil { |
51 | 59 | return nil, err |
52 | 60 | } |
53 | 61 |
|
| 62 | + localSchemaSrc, err := s.LocalSource(ctx, "trace.yaml") |
| 63 | + if err != nil { |
| 64 | + return nil, err |
| 65 | + } |
| 66 | + s.log.InfoContext(ctx, "Local schema files retrieved", "schemas", len(localSchemaSrc)) |
| 67 | + schemaSrc = append(schemaSrc, localSchemaSrc...) |
| 68 | + |
54 | 69 | return s.MergeMethodsAndDefinitions(ctx, methodSrc, schemaSrc) |
55 | 70 | } |
| 71 | + |
| 72 | +// LocalSource returns the OpenRPC source for all methods defined by the |
| 73 | +// local OpenRPC specifications. |
| 74 | +func (s *Scraper) LocalSource(ctx context.Context, filter string) ([]string, error) { |
| 75 | + root, err := s.findProjectRoot() |
| 76 | + if err != nil { |
| 77 | + return nil, err |
| 78 | + } |
| 79 | + |
| 80 | + dir := filepath.Join(root, "gen", "internal", "openrpc", "data") |
| 81 | + files, err := os.ReadDir(dir) |
| 82 | + if err != nil { |
| 83 | + return nil, err |
| 84 | + } |
| 85 | + |
| 86 | + var sources []string |
| 87 | + for _, file := range files { |
| 88 | + if file.IsDir() { |
| 89 | + continue |
| 90 | + } |
| 91 | + |
| 92 | + if filepath.Ext(file.Name()) != ".yaml" { |
| 93 | + continue |
| 94 | + } |
| 95 | + |
| 96 | + if filter == "trace.yaml" && file.Name() == "trace.yaml" { |
| 97 | + continue |
| 98 | + } |
| 99 | + |
| 100 | + if filter == "_schemas.yaml" && file.Name() != "trace.yaml" { |
| 101 | + continue |
| 102 | + } |
| 103 | + |
| 104 | + path := filepath.Join(dir, file.Name()) |
| 105 | + path = filepath.Clean(path) |
| 106 | + s.log.DebugContext(ctx, "Reading local method file", "path", path) |
| 107 | + |
| 108 | + data, err := os.ReadFile(path) |
| 109 | + if err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + s.log.DebugContext(ctx, "Read local method file", "path", path) |
| 113 | + sources = append(sources, string(data)) |
| 114 | + } |
| 115 | + |
| 116 | + return sources, nil |
| 117 | +} |
| 118 | + |
| 119 | +func (s *Scraper) findProjectRoot() (string, error) { |
| 120 | + dir, err := os.Getwd() |
| 121 | + if err != nil { |
| 122 | + return "", err |
| 123 | + } |
| 124 | + |
| 125 | + for { |
| 126 | + if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil { |
| 127 | + return dir, nil |
| 128 | + } |
| 129 | + |
| 130 | + if dir == filepath.Dir(dir) { |
| 131 | + return "", os.ErrNotExist |
| 132 | + } |
| 133 | + |
| 134 | + dir = filepath.Dir(dir) |
| 135 | + } |
| 136 | +} |
0 commit comments