rubydex 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +45 -7
  3. data/THIRD_PARTY_LICENSES.html +238 -2
  4. data/exe/rdx +2 -149
  5. data/ext/rubydex/config.c +140 -0
  6. data/ext/rubydex/config.h +16 -0
  7. data/ext/rubydex/diagnostic.c +75 -1
  8. data/ext/rubydex/diagnostic.h +2 -0
  9. data/ext/rubydex/graph.c +13 -45
  10. data/ext/rubydex/graph.h +6 -0
  11. data/ext/rubydex/query.c +398 -16
  12. data/ext/rubydex/rubydex.c +2 -0
  13. data/ext/rubydex/utils.c +11 -4
  14. data/lib/ruby_lsp/rubydex/addon.rb +211 -0
  15. data/lib/rubydex/cli/command/console.rb +55 -0
  16. data/lib/rubydex/cli/command/lint/explain.rb +74 -0
  17. data/lib/rubydex/cli/command/lint.rb +202 -0
  18. data/lib/rubydex/cli/command/mcp.rb +30 -0
  19. data/lib/rubydex/cli/command/query.rb +70 -0
  20. data/lib/rubydex/cli/command/skill.rb +69 -0
  21. data/lib/rubydex/cli/command.rb +168 -0
  22. data/lib/rubydex/cli.rb +93 -0
  23. data/lib/rubydex/config.rb +59 -0
  24. data/lib/rubydex/diagnostic.rb +12 -3
  25. data/lib/rubydex/errors.rb +42 -1
  26. data/lib/rubydex/graph.rb +10 -3
  27. data/lib/rubydex/linter/custom_rule.rb +97 -0
  28. data/lib/rubydex/linter/helpers/path_helpers.rb +78 -0
  29. data/lib/rubydex/linter/helpers/source_access_helpers.rb +31 -0
  30. data/lib/rubydex/linter/rule_loader.rb +36 -0
  31. data/lib/rubydex/linter/rule_test_case.rb +343 -0
  32. data/lib/rubydex/linter/runner.rb +56 -0
  33. data/lib/rubydex/linter.rb +19 -0
  34. data/lib/rubydex/location.rb +3 -0
  35. data/lib/rubydex/mcp_server.rb +1 -2
  36. data/lib/rubydex/related_information.rb +17 -0
  37. data/lib/rubydex/rule.rb +33 -0
  38. data/lib/rubydex/severity.rb +70 -0
  39. data/lib/rubydex/skill.rb +88 -0
  40. data/lib/rubydex/skill_registry.rb +62 -0
  41. data/lib/rubydex/version.rb +1 -1
  42. data/lib/rubydex.rb +6 -0
  43. data/lib/rubydex_linter/rules/rule_structure.rb +125 -0
  44. data/rbi/rubydex.rbi +558 -17
  45. data/rust/Cargo.lock +2 -2
  46. data/rust/rubydex/Cargo.toml +1 -1
  47. data/rust/rubydex/benches/graph_memory.rs +3 -5
  48. data/rust/rubydex/src/config.rs +538 -157
  49. data/rust/rubydex/src/diagnostic.rs +66 -40
  50. data/rust/rubydex/src/errors.rs +0 -1
  51. data/rust/rubydex/src/indexing/local_graph.rs +6 -5
  52. data/rust/rubydex/src/indexing/rbs_indexer.rs +270 -6
  53. data/rust/rubydex/src/indexing/ruby_indexer.rs +10 -12
  54. data/rust/rubydex/src/indexing/ruby_indexer_tests.rs +134 -81
  55. data/rust/rubydex/src/lib.rs +1 -0
  56. data/rust/rubydex/src/listing.rs +26 -1
  57. data/rust/rubydex/src/main.rs +7 -4
  58. data/rust/rubydex/src/model/declaration.rs +302 -219
  59. data/rust/rubydex/src/model/graph.rs +27 -40
  60. data/rust/rubydex/src/model/name.rs +58 -17
  61. data/rust/rubydex/src/operation/ruby_builder.rs +30 -45
  62. data/rust/rubydex/src/path_helpers.rs +77 -0
  63. data/rust/rubydex/src/query/cypher/schema.rs +63 -0
  64. data/rust/rubydex/src/query/cypher/tests.rs +26 -1
  65. data/rust/rubydex/src/query/cypher.rs +8 -11
  66. data/rust/rubydex/src/query.rs +123 -43
  67. data/rust/rubydex/src/resolution.rs +139 -187
  68. data/rust/rubydex/src/resolution_tests.rs +247 -19
  69. data/rust/rubydex/src/test_utils/context.rs +2 -1
  70. data/rust/rubydex/src/test_utils/graph_test.rs +26 -12
  71. data/rust/rubydex/src/test_utils/local_graph_test.rs +19 -0
  72. data/rust/rubydex/tests/cli.rs +4 -4
  73. data/rust/rubydex-sys/src/config_api.rs +205 -0
  74. data/rust/rubydex-sys/src/cypher_api.rs +791 -0
  75. data/rust/rubydex-sys/src/diagnostic_api.rs +77 -8
  76. data/rust/rubydex-sys/src/graph_api.rs +13 -194
  77. data/rust/rubydex-sys/src/lib.rs +2 -0
  78. data/rust/rubydex-sys/src/name_api.rs +2 -6
  79. data/rust/rubydex-sys/src/utils.rs +37 -0
  80. data/skills/send-private-method/SKILL.md +133 -0
  81. metadata +31 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cf823835a70dee735f2dc0e898eb1d47be47e4ab635a6be4de5677ceaf6bbe53
4
- data.tar.gz: 663389c429d729bf8d9191f54b7eafe92b8ecb8e0c0a32e5b3f6adba3365f3fa
3
+ metadata.gz: c097d20f2e7fbb252e1a0a9e802986b51c951caec785d9a8a22e41779928f2ce
4
+ data.tar.gz: 8d987121c97c13ffcf3258830c612d5447a7c5efd4a572f6a9ce37de3669e881
5
5
  SHA512:
6
- metadata.gz: aa17ed0f03e392c4ad1a2bf256d3472057d7a78a2baca596bb72f80c06d1fe36d7fa80878ddfabe740ca0b8d23f961d01afe9ab0211219e2f41b36b4587b819d
7
- data.tar.gz: 86d70527d4bcee2b8e90bbebb33912e1f5a6bf1f7fe9ce6e1435e7fff918829ae2a8de692072ab34fca42a199f5cfce5a12e18f77b6f08d5b8ca274c3e8f83f4
6
+ metadata.gz: b978a10b5fe6fe5f76dc916abda86c019bee6b7d36d4b9aa1af4bdb6da88053dc8bceb60d271c559230c16fb2661361bb73f4cbd8dffb4f4fd317b210683c466
7
+ data.tar.gz: c90b49725da1efb7597eb15855f829e7a897feb3d7be4cfb5c59543b346965e3485c06fb7358620b4c36674a453ccc9c66f7182c959508c21324eba4af14d354
data/README.md CHANGED
@@ -75,8 +75,10 @@ location.path
75
75
  # Diagnostics
76
76
  diagnostic = graph.diagnostics.first
77
77
  diagnostic.rule
78
+ diagnostic.rule.default_severity
78
79
  diagnostic.message
79
80
  diagnostic.location
81
+ diagnostic.related_information
80
82
  ```
81
83
 
82
84
  ## Ractor Safety
@@ -108,7 +110,11 @@ Thread safety comes from an `RwLock` on the Rust side, **not** from Ruby's
108
110
  aliasing the Rust allocation (which would double-free on GC) or ballooning
109
111
  memory with a deep copy.
110
112
 
111
- ## Querying with Cypher
113
+ ## Tools
114
+
115
+ All built-in tools are experimental. These tools can change without deprecation warnings.
116
+
117
+ ### `rdx query`
112
118
 
113
119
  Rubydex exposes the indexed graph through a read-only subset of the
114
120
  [Cypher](https://opencypher.org/) query language. Only read clauses (`MATCH`,
@@ -134,21 +140,45 @@ graph = Rubydex::Graph.new
134
140
  graph.index_workspace
135
141
  graph.resolve
136
142
 
137
- # Parse once, render against a graph as a table or JSON string
143
+ # Parse once, then run against a graph. `run` executes the query and returns the result set.
138
144
  query = Rubydex::Query.parse("MATCH (c:Class) RETURN c.name")
139
- puts query.render(graph, "table")
140
- puts query.render(graph, "json")
145
+ result = query.run(graph)
146
+
147
+ # Read the rows as Ruby objects, or render the same result set as a table or JSON string
148
+ result.rows.each { |row| puts row["c.name"] }
149
+ puts result.render("table")
150
+ puts result.render("json")
141
151
 
142
152
  # Describe the schema
143
153
  puts Rubydex::Query.schema("table")
144
154
  ```
145
155
 
146
- ## MCP Server (Experimental)
156
+ ### `rdx lint`
157
+
158
+ Put rule files under `rubydex_linter/rules`. Each rule must inherit from `Rubydex::Linter::CustomRule`.
159
+
160
+ Run the linter:
161
+
162
+ ```bash
163
+ bundle exec rdx lint
164
+ ```
165
+
166
+ Rubydex uses the bundle root, or the current directory when no bundle is available.
167
+
168
+ Configure a rule in `rubydex.toml`:
169
+
170
+ ```toml
171
+ [linter.rules.<Rule name>]
172
+ enabled = true
173
+ exclude = ["path_to_skip/**"]
174
+ ```
175
+
176
+ ### `rdx mcp`
147
177
 
148
178
  Rubydex can run as an MCP (Model Context Protocol) server, enabling AI assistants
149
179
  like Claude to semantically query your Ruby codebase.
150
180
 
151
- ### Setup
181
+ #### Setup
152
182
 
153
183
  1. Add Rubydex to the Ruby project you want to index:
154
184
  ```ruby
@@ -176,7 +206,7 @@ like Claude to semantically query your Ruby codebase.
176
206
  the project at startup and provides semantic code intelligence tools through
177
207
  the tools below.
178
208
 
179
- ### Available MCP Tools
209
+ #### Available MCP Tools
180
210
 
181
211
  | Tool | Description |
182
212
  |------|-------------|
@@ -187,6 +217,14 @@ like Claude to semantically query your Ruby codebase.
187
217
  | `get_file_declarations` | List declarations defined in a specific file |
188
218
  | `codebase_stats` | High-level statistics about the indexed codebase |
189
219
 
220
+ ## Skill Library (Experimental)
221
+
222
+ Rubydex ships with a built in skill library. These skills are referenced by id by Rubydex tools (for example: `send-private-method` maps to `skills/send-private-method/SKILL.md`).
223
+
224
+ The intention is for Rubydex to provide deterministic skill loading for agents. If a rule returns a skill id, the agent can then fetch the skill with `rdx skill <id>`.
225
+
226
+ This removes the need for agents to load a large number of possibly unrelated skill descriptions when they may or may not be necessary. The information only surfaces if there is a verified violation to fix, and it only needs to be loaded once, rather than inlining it on every violation in every run of the tool (the common pattern for linters and analyzers).
227
+
190
228
  ## Contributing
191
229
 
192
230
  See [the contributing documentation](CONTRIBUTING.md).
@@ -50,7 +50,7 @@
50
50
 
51
51
  <h2>Overview of licenses:</h2>
52
52
  <ul class="licenses-overview">
53
- <li><a href="#Apache-2.0">Apache License 2.0</a> (116)</li>
53
+ <li><a href="#Apache-2.0">Apache License 2.0</a> (119)</li>
54
54
  <li><a href="#Unicode-3.0">Unicode License v3</a> (19)</li>
55
55
  <li><a href="#MIT">MIT License</a> (17)</li>
56
56
  <li><a href="#BSD-2-Clause">BSD 2-Clause &quot;Simplified&quot; License</a> (2)</li>
@@ -277,6 +277,242 @@
277
277
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
278
278
  See the License for the specific language governing permissions and
279
279
  limitations under the License.
280
+ </pre>
281
+ </li>
282
+ <li class="license">
283
+ <h3 id="Apache-2.0">Apache License 2.0</h3>
284
+ <h4>Used by:</h4>
285
+ <ul class="license-used-by">
286
+ <li><a
287
+ href=" https://github.com/sunfishcode/linux-raw-sys ">linux-raw-sys
288
+ 0.9.4</a></li>
289
+ <li><a
290
+ href=" https://github.com/bytecodealliance/rustix ">rustix
291
+ 1.0.8</a></li>
292
+ <li><a
293
+ href=" https://github.com/bytecodealliance/wasi-rs ">wasi
294
+ 0.14.2+wasi-0.2.4</a></li>
295
+ </ul>
296
+ <pre class="license-text">
297
+ Apache License
298
+ Version 2.0, January 2004
299
+ http://www.apache.org/licenses/
300
+
301
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
302
+
303
+ 1. Definitions.
304
+
305
+ &quot;License&quot; shall mean the terms and conditions for use, reproduction,
306
+ and distribution as defined by Sections 1 through 9 of this document.
307
+
308
+ &quot;Licensor&quot; shall mean the copyright owner or entity authorized by
309
+ the copyright owner that is granting the License.
310
+
311
+ &quot;Legal Entity&quot; shall mean the union of the acting entity and all
312
+ other entities that control, are controlled by, or are under common
313
+ control with that entity. For the purposes of this definition,
314
+ &quot;control&quot; means (i) the power, direct or indirect, to cause the
315
+ direction or management of such entity, whether by contract or
316
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
317
+ outstanding shares, or (iii) beneficial ownership of such entity.
318
+
319
+ &quot;You&quot; (or &quot;Your&quot;) shall mean an individual or Legal Entity
320
+ exercising permissions granted by this License.
321
+
322
+ &quot;Source&quot; form shall mean the preferred form for making modifications,
323
+ including but not limited to software source code, documentation
324
+ source, and configuration files.
325
+
326
+ &quot;Object&quot; form shall mean any form resulting from mechanical
327
+ transformation or translation of a Source form, including but
328
+ not limited to compiled object code, generated documentation,
329
+ and conversions to other media types.
330
+
331
+ &quot;Work&quot; shall mean the work of authorship, whether in Source or
332
+ Object form, made available under the License, as indicated by a
333
+ copyright notice that is included in or attached to the work
334
+ (an example is provided in the Appendix below).
335
+
336
+ &quot;Derivative Works&quot; shall mean any work, whether in Source or Object
337
+ form, that is based on (or derived from) the Work and for which the
338
+ editorial revisions, annotations, elaborations, or other modifications
339
+ represent, as a whole, an original work of authorship. For the purposes
340
+ of this License, Derivative Works shall not include works that remain
341
+ separable from, or merely link (or bind by name) to the interfaces of,
342
+ the Work and Derivative Works thereof.
343
+
344
+ &quot;Contribution&quot; shall mean any work of authorship, including
345
+ the original version of the Work and any modifications or additions
346
+ to that Work or Derivative Works thereof, that is intentionally
347
+ submitted to Licensor for inclusion in the Work by the copyright owner
348
+ or by an individual or Legal Entity authorized to submit on behalf of
349
+ the copyright owner. For the purposes of this definition, &quot;submitted&quot;
350
+ means any form of electronic, verbal, or written communication sent
351
+ to the Licensor or its representatives, including but not limited to
352
+ communication on electronic mailing lists, source code control systems,
353
+ and issue tracking systems that are managed by, or on behalf of, the
354
+ Licensor for the purpose of discussing and improving the Work, but
355
+ excluding communication that is conspicuously marked or otherwise
356
+ designated in writing by the copyright owner as &quot;Not a Contribution.&quot;
357
+
358
+ &quot;Contributor&quot; shall mean Licensor and any individual or Legal Entity
359
+ on behalf of whom a Contribution has been received by Licensor and
360
+ subsequently incorporated within the Work.
361
+
362
+ 2. Grant of Copyright License. Subject to the terms and conditions of
363
+ this License, each Contributor hereby grants to You a perpetual,
364
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
365
+ copyright license to reproduce, prepare Derivative Works of,
366
+ publicly display, publicly perform, sublicense, and distribute the
367
+ Work and such Derivative Works in Source or Object form.
368
+
369
+ 3. Grant of Patent License. Subject to the terms and conditions of
370
+ this License, each Contributor hereby grants to You a perpetual,
371
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
372
+ (except as stated in this section) patent license to make, have made,
373
+ use, offer to sell, sell, import, and otherwise transfer the Work,
374
+ where such license applies only to those patent claims licensable
375
+ by such Contributor that are necessarily infringed by their
376
+ Contribution(s) alone or by combination of their Contribution(s)
377
+ with the Work to which such Contribution(s) was submitted. If You
378
+ institute patent litigation against any entity (including a
379
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
380
+ or a Contribution incorporated within the Work constitutes direct
381
+ or contributory patent infringement, then any patent licenses
382
+ granted to You under this License for that Work shall terminate
383
+ as of the date such litigation is filed.
384
+
385
+ 4. Redistribution. You may reproduce and distribute copies of the
386
+ Work or Derivative Works thereof in any medium, with or without
387
+ modifications, and in Source or Object form, provided that You
388
+ meet the following conditions:
389
+
390
+ (a) You must give any other recipients of the Work or
391
+ Derivative Works a copy of this License; and
392
+
393
+ (b) You must cause any modified files to carry prominent notices
394
+ stating that You changed the files; and
395
+
396
+ (c) You must retain, in the Source form of any Derivative Works
397
+ that You distribute, all copyright, patent, trademark, and
398
+ attribution notices from the Source form of the Work,
399
+ excluding those notices that do not pertain to any part of
400
+ the Derivative Works; and
401
+
402
+ (d) If the Work includes a &quot;NOTICE&quot; text file as part of its
403
+ distribution, then any Derivative Works that You distribute must
404
+ include a readable copy of the attribution notices contained
405
+ within such NOTICE file, excluding those notices that do not
406
+ pertain to any part of the Derivative Works, in at least one
407
+ of the following places: within a NOTICE text file distributed
408
+ as part of the Derivative Works; within the Source form or
409
+ documentation, if provided along with the Derivative Works; or,
410
+ within a display generated by the Derivative Works, if and
411
+ wherever such third-party notices normally appear. The contents
412
+ of the NOTICE file are for informational purposes only and
413
+ do not modify the License. You may add Your own attribution
414
+ notices within Derivative Works that You distribute, alongside
415
+ or as an addendum to the NOTICE text from the Work, provided
416
+ that such additional attribution notices cannot be construed
417
+ as modifying the License.
418
+
419
+ You may add Your own copyright statement to Your modifications and
420
+ may provide additional or different license terms and conditions
421
+ for use, reproduction, or distribution of Your modifications, or
422
+ for any such Derivative Works as a whole, provided Your use,
423
+ reproduction, and distribution of the Work otherwise complies with
424
+ the conditions stated in this License.
425
+
426
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
427
+ any Contribution intentionally submitted for inclusion in the Work
428
+ by You to the Licensor shall be under the terms and conditions of
429
+ this License, without any additional terms or conditions.
430
+ Notwithstanding the above, nothing herein shall supersede or modify
431
+ the terms of any separate license agreement you may have executed
432
+ with Licensor regarding such Contributions.
433
+
434
+ 6. Trademarks. This License does not grant permission to use the trade
435
+ names, trademarks, service marks, or product names of the Licensor,
436
+ except as required for reasonable and customary use in describing the
437
+ origin of the Work and reproducing the content of the NOTICE file.
438
+
439
+ 7. Disclaimer of Warranty. Unless required by applicable law or
440
+ agreed to in writing, Licensor provides the Work (and each
441
+ Contributor provides its Contributions) on an &quot;AS IS&quot; BASIS,
442
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
443
+ implied, including, without limitation, any warranties or conditions
444
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
445
+ PARTICULAR PURPOSE. You are solely responsible for determining the
446
+ appropriateness of using or redistributing the Work and assume any
447
+ risks associated with Your exercise of permissions under this License.
448
+
449
+ 8. Limitation of Liability. In no event and under no legal theory,
450
+ whether in tort (including negligence), contract, or otherwise,
451
+ unless required by applicable law (such as deliberate and grossly
452
+ negligent acts) or agreed to in writing, shall any Contributor be
453
+ liable to You for damages, including any direct, indirect, special,
454
+ incidental, or consequential damages of any character arising as a
455
+ result of this License or out of the use or inability to use the
456
+ Work (including but not limited to damages for loss of goodwill,
457
+ work stoppage, computer failure or malfunction, or any and all
458
+ other commercial damages or losses), even if such Contributor
459
+ has been advised of the possibility of such damages.
460
+
461
+ 9. Accepting Warranty or Additional Liability. While redistributing
462
+ the Work or Derivative Works thereof, You may choose to offer,
463
+ and charge a fee for, acceptance of support, warranty, indemnity,
464
+ or other liability obligations and/or rights consistent with this
465
+ License. However, in accepting such obligations, You may act only
466
+ on Your own behalf and on Your sole responsibility, not on behalf
467
+ of any other Contributor, and only if You agree to indemnify,
468
+ defend, and hold each Contributor harmless for any liability
469
+ incurred by, or claims asserted against, such Contributor by reason
470
+ of your accepting any such warranty or additional liability.
471
+
472
+ END OF TERMS AND CONDITIONS
473
+
474
+ APPENDIX: How to apply the Apache License to your work.
475
+
476
+ To apply the Apache License to your work, attach the following
477
+ boilerplate notice, with the fields enclosed by brackets &quot;[]&quot;
478
+ replaced with your own identifying information. (Don&#x27;t include
479
+ the brackets!) The text should be enclosed in the appropriate
480
+ comment syntax for the file format. We also recommend that a
481
+ file or class name and description of purpose be included on the
482
+ same &quot;printed page&quot; as the copyright notice for easier
483
+ identification within third-party archives.
484
+
485
+ Copyright [yyyy] [name of copyright owner]
486
+
487
+ Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
488
+ you may not use this file except in compliance with the License.
489
+ You may obtain a copy of the License at
490
+
491
+ http://www.apache.org/licenses/LICENSE-2.0
492
+
493
+ Unless required by applicable law or agreed to in writing, software
494
+ distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
495
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
496
+ See the License for the specific language governing permissions and
497
+ limitations under the License.
498
+
499
+
500
+ --- LLVM Exceptions to the Apache 2.0 License ----
501
+
502
+ As an exception, if, as a result of your compiling your source code, portions
503
+ of this Software are embedded into an Object form of such source code, you
504
+ may redistribute such embedded portions in such Object form without complying
505
+ with the conditions of Sections 4(a), 4(b) and 4(d) of the License.
506
+
507
+ In addition, if you combine or link compiled forms of this Software with
508
+ software that is licensed under the GPLv2 (&quot;Combined Software&quot;) and if a
509
+ court of competent jurisdiction determines that the patent provision (Section
510
+ 3), the indemnity provision (Section 9) or other Section of the License
511
+ conflicts with the conditions of the GPLv2, you may retroactively and
512
+ prospectively choose to deem waived or otherwise exclude such Section(s) of
513
+ the License, but only in their entirety and only with respect to the Combined
514
+ Software.
515
+
280
516
  </pre>
281
517
  </li>
282
518
  <li class="license">
@@ -2810,7 +3046,7 @@ SOFTWARE.
2810
3046
  <ul class="license-used-by">
2811
3047
  <li><a
2812
3048
  href=" https://github.com/Shopify/cypher-parser ">cypher-parser
2813
- 0.2.0</a></li>
3049
+ 0.8.1</a></li>
2814
3050
  </ul>
2815
3051
  <pre class="license-text">The MIT License (MIT)
2816
3052
 
data/exe/rdx CHANGED
@@ -3,153 +3,6 @@
3
3
 
4
4
  $LOAD_PATH.unshift(File.expand_path("../lib", __dir__))
5
5
 
6
- require "optparse"
6
+ require "rubydex/cli"
7
7
 
8
- USAGE = <<~TEXT
9
- Usage: rdx <command> [options]
10
-
11
- Commands:
12
- query <CYPHER> Run a Cypher query against the workspace graph and print the result.
13
- Use `query --schema` to describe the queryable schema (labels,
14
- relationships, properties) without indexing the workspace.
15
- console Open an interactive session with a populated graph for the current workspace
16
- mcp [PATH] Run the MCP server for AI assistants (workspace defaults to the current dir)
17
- help Show this help message
18
-
19
- Run `rdx <command> --help` for command-specific options.
20
- TEXT
21
-
22
- def abort_with_usage(message)
23
- warn(message)
24
- warn("")
25
- warn(USAGE)
26
- exit(1)
27
- end
28
-
29
- # Top-level --version / --help / bare invocation, handled before command dispatch. Each branch
30
- # performs its action and reports whether it handled the invocation, so we exit exactly once here
31
- # rather than from inside every branch.
32
- handled =
33
- case ARGV.first
34
- when "--version", "version"
35
- require "rubydex/version"
36
- puts "v#{Rubydex::VERSION}"
37
- true
38
- when nil, "-h", "--help", "help"
39
- puts USAGE
40
- true
41
- else
42
- false
43
- end
44
-
45
- exit if handled
46
-
47
- command = ARGV.shift
48
-
49
- def with_timer(io, message)
50
- io.print(message)
51
- start = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
52
- yield
53
- duration = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond) - start
54
- io.puts(" finished in #{duration.round(2)}ms")
55
- end
56
-
57
- # Builds the workspace graph, sending progress messages to `progress_io`.
58
- def build_graph(progress_io)
59
- graph = Rubydex::Graph.new
60
- graph.load_config
61
- with_timer(progress_io, "Indexing workspace...") { graph.index_workspace }
62
- with_timer(progress_io, "Resolving graph...") { graph.resolve }
63
- graph
64
- end
65
-
66
- require "rubydex"
67
-
68
- # Resolve the command into an operation on a populated graph. Each command parses its own options
69
- # and does any graph-independent work here; a command that needs no graph (like `query --schema`)
70
- # handles itself and exits. Whatever falls through returns a lambda that runs against the graph.
71
- operation =
72
- case command
73
- when "query"
74
- schema = false
75
- format = "table"
76
- OptionParser.new do |parser|
77
- parser.banner = "Usage: rdx query <CYPHER> [options]"
78
- parser.on("--schema", "Describe the queryable schema instead of running a query") { schema = true }
79
- parser.on("--format FORMAT", ["table", "json"], "Output format (table or json)") { |value| format = value }
80
- parser.on("-h", "--help", "Show this help") do
81
- puts parser
82
- exit
83
- end
84
- end.parse!
85
-
86
- query = ARGV.shift
87
-
88
- if schema
89
- warn("warning: ignoring query argument because `--schema` was given") if query
90
- # The schema is static, so describe it without building a graph, then exit.
91
- print(Rubydex::Query.schema(format))
92
- exit
93
- end
94
-
95
- abort_with_usage("`query` requires a Cypher query argument (or pass `--schema`)") if query.nil? || query.empty?
96
-
97
- # Parse the query up front so a malformed query fails fast, before the expensive indexing below.
98
- parsed = begin
99
- Rubydex::Query.parse(query)
100
- rescue ArgumentError => e
101
- abort(e.message)
102
- end
103
-
104
- lambda do |graph|
105
- print(parsed.render(graph, format))
106
- rescue ArgumentError => e
107
- abort(e.message)
108
- end
109
- when "console"
110
- OptionParser.new do |parser|
111
- parser.banner = "Usage: rdx console"
112
- parser.on("-h", "--help", "Show this help") do
113
- puts parser
114
- exit
115
- end
116
- end.parse!
117
-
118
- lambda do |graph|
119
- require "irb"
120
- IRB.setup(nil)
121
- IRB.conf[:IRB_NAME] = "rubydex"
122
- IRB::Irb.new(IRB::WorkSpace.new(binding)).run(IRB.conf)
123
- rescue LoadError
124
- abort("Interactive mode requires `irb` to be in the bundle")
125
- end
126
- when "mcp"
127
- parser = OptionParser.new do |p|
128
- p.banner = "Usage: rdx mcp [PATH]"
129
- p.on("-h", "--help", "Show this help") do
130
- puts p
131
- exit
132
- end
133
- end
134
- begin
135
- parser.parse!
136
- rescue OptionParser::ParseError => e
137
- abort_with_usage(e.message)
138
- end
139
-
140
- path = ARGV.shift || Dir.pwd
141
- abort_with_usage("unexpected argument: #{ARGV.first}") unless ARGV.empty?
142
-
143
- # The MCP server manages its own graph lifecycle for the given workspace, so it runs here
144
- # instead of falling through to the shared graph build below.
145
- require "rubydex/mcp_server"
146
- Rubydex::MCPServer.run(path)
147
- exit
148
- else
149
- abort_with_usage("unknown command: #{command}")
150
- end
151
-
152
- # Everything that reaches here operates on a populated graph. Progress goes to stderr so stdout
153
- # carries only the command's output (e.g. for piping a query's JSON).
154
- graph = build_graph($stderr)
155
- operation.call(graph)
8
+ Rubydex::CLI.start
@@ -0,0 +1,140 @@
1
+ #include "config.h"
2
+ #include "diagnostic.h"
3
+ #include "rustbindings.h"
4
+ #include "utils.h"
5
+
6
+ /*
7
+ * RDoc parser workaround for https://github.com/ruby/rdoc/issues/1744:
8
+ * mRubydex = rb_define_module("Rubydex")
9
+ */
10
+
11
+ static VALUE mRubydex;
12
+ // Defined here so that the configuration can build them, but implemented in Ruby (`lib/rubydex/config.rb`), like the
13
+ // other value objects handed back to Ruby.
14
+ static VALUE cLinterConfig;
15
+ static VALUE cRuleConfig;
16
+ static ID id_linter;
17
+
18
+ // Free function for Rubydex::Config: releases the parsed configuration allocated by Rust.
19
+ static void config_free(void *ptr) {
20
+ if (ptr) {
21
+ rdx_config_free(ptr);
22
+ }
23
+ }
24
+
25
+ // Body function for rb_ensure while building a Rubydex::LinterConfig.
26
+ static VALUE config_linter_build(VALUE opaque_rule_array) {
27
+ CLinterRuleArray *rule_array = (CLinterRuleArray *)(uintptr_t)opaque_rule_array;
28
+ VALUE rules = rb_hash_new_capa((long)rule_array->len);
29
+
30
+ for (size_t i = 0; i < rule_array->len; i++) {
31
+ CLinterRule rule = rule_array->items[i];
32
+ VALUE rule_name = rb_str_freeze(rb_utf8_str_new(rule.name, (long)rule.name_length));
33
+ VALUE exclude_patterns = rb_ary_new_capa((long)rule.exclude_patterns_length);
34
+ for (size_t j = 0; j < rule.exclude_patterns_length; j++) {
35
+ CConfigString pattern = rule.exclude_patterns[j];
36
+ rb_ary_push(exclude_patterns, rb_str_freeze(rb_utf8_str_new(pattern.data, (long)pattern.length)));
37
+ }
38
+ rb_obj_freeze(exclude_patterns);
39
+
40
+ VALUE severity = rule.severity == NULL
41
+ ? Qnil
42
+ : rdxi_build_diagnostic_severity_value(mRubydex, *rule.severity);
43
+ VALUE argv[] = {rule_name, rule.enabled ? Qtrue : Qfalse, exclude_patterns, severity};
44
+
45
+ rb_hash_aset(rules, rule_name, rb_class_new_instance(4, argv, cRuleConfig));
46
+ }
47
+
48
+ return rb_class_new_instance(1, &rules, cLinterConfig);
49
+ }
50
+
51
+ // Ensure function for rb_ensure while building a Rubydex::LinterConfig.
52
+ static VALUE config_linter_ensure(VALUE opaque_rule_array) {
53
+ CLinterRuleArray *rule_array = (CLinterRuleArray *)(uintptr_t)opaque_rule_array;
54
+ rdx_config_linter_rules_free(*rule_array);
55
+
56
+ return Qnil;
57
+ }
58
+
59
+ static VALUE config_linter(VALUE config_obj) {
60
+ CLinterRuleArray rule_array = rdx_config_linter_rules(rdxi_config_from_object(config_obj));
61
+ VALUE opaque_rule_array = (VALUE)(uintptr_t)&rule_array;
62
+
63
+ return rb_ensure(config_linter_build, opaque_rule_array, config_linter_ensure, opaque_rule_array);
64
+ }
65
+
66
+ const rb_data_type_t config_type = {
67
+ .wrap_struct_name = "Rubydex::Config",
68
+ .function = {
69
+ .dmark = NULL,
70
+ .dfree = config_free,
71
+ .dsize = NULL,
72
+ .dcompact = NULL,
73
+ },
74
+ .parent = NULL,
75
+ .data = NULL,
76
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_FROZEN_SHAREABLE,
77
+ };
78
+
79
+ /*
80
+ * call-seq:
81
+ * Rubydex::Config.load(workspace_path) -> Rubydex::Config
82
+ *
83
+ * Loads the configuration of the workspace rooted at +workspace_path+, which is where its `rubydex.toml` is expected to
84
+ * be. A workspace without a configuration file gets an empty configuration. Raises Rubydex::ConfigError if the file
85
+ * exists, but cannot be read or is malformed.
86
+ */
87
+ static VALUE rdxr_config_load(VALUE klass, VALUE workspace_path) {
88
+ Check_Type(workspace_path, T_STRING);
89
+
90
+ struct CConfigResult result = rdx_config_load(StringValueCStr(workspace_path));
91
+ if (result.error != NULL) {
92
+ VALUE message = rb_utf8_str_new_cstr(result.error);
93
+ free_c_string(result.error);
94
+
95
+ VALUE config_error = rb_const_get(mRubydex, rb_intern("ConfigError"));
96
+ rb_exc_raise(rb_exc_new_str(config_error, message));
97
+ }
98
+
99
+ VALUE config = TypedData_Wrap_Struct(klass, &config_type, result.config);
100
+ rb_ivar_set(config, id_linter, config_linter(config));
101
+
102
+ return config;
103
+ }
104
+
105
+ /*
106
+ * call-seq:
107
+ * workspace_path -> String
108
+ *
109
+ * Returns the root directory of the workspace this configuration was loaded for.
110
+ */
111
+ static VALUE rdxr_config_workspace_path(VALUE self) {
112
+ const char *result = rdx_config_workspace_path(rdxi_config_from_object(self));
113
+ // A configuration always has a workspace, so there is no absent case to hand back to Ruby. NULL only means the
114
+ // conversion itself failed, which is why this raises instead of returning nil.
115
+ if (result == NULL) {
116
+ rb_raise(rb_eRuntimeError, "Converting workspace path to Ruby string failed");
117
+ }
118
+
119
+ return rdxi_owned_c_string_to_ruby(result);
120
+ }
121
+
122
+ void rdxi_initialize_config(VALUE moduleRubydex) {
123
+ mRubydex = moduleRubydex;
124
+
125
+ cLinterConfig = rb_define_class_under(mRubydex, "LinterConfig", rb_cObject);
126
+ cRuleConfig = rb_define_class_under(mRubydex, "RuleConfig", rb_cObject);
127
+ id_linter = rb_intern("@linter");
128
+
129
+ VALUE cConfig = rb_define_class_under(mRubydex, "Config", rb_cObject);
130
+ rb_undef_alloc_func(cConfig);
131
+
132
+ // A configuration can only be obtained through `load`; `new` would create an object with no Rust data behind it.
133
+ rb_undef_method(rb_singleton_class(cConfig), "new");
134
+
135
+ rb_define_singleton_method(cConfig, "load", rdxr_config_load, 1);
136
+ rb_define_method(cConfig, "workspace_path", rdxr_config_workspace_path, 0);
137
+
138
+ /* Returns the linter settings read from the `[linter]` section. Its rules are empty when the section is absent. */
139
+ rb_define_attr(cConfig, "linter", true, false);
140
+ }
@@ -0,0 +1,16 @@
1
+ #ifndef RUBYDEX_CONFIG_H
2
+ #define RUBYDEX_CONFIG_H
3
+
4
+ #include "ruby.h"
5
+
6
+ extern const rb_data_type_t config_type;
7
+
8
+ static inline void *rdxi_config_from_object(VALUE config_obj) {
9
+ void *config;
10
+ TypedData_Get_Struct(config_obj, void *, &config_type, config);
11
+ return config;
12
+ }
13
+
14
+ void rdxi_initialize_config(VALUE mRubydex);
15
+
16
+ #endif // RUBYDEX_CONFIG_H