yerba 0.1.2 → 0.2.1

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 (49) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +492 -15
  3. data/exe/x86_64-linux/yerba +0 -0
  4. data/ext/yerba/extconf.rb +87 -30
  5. data/ext/yerba/include/yerba.h +168 -0
  6. data/ext/yerba/yerba.c +752 -0
  7. data/lib/yerba/collection.rb +31 -0
  8. data/lib/yerba/document.rb +59 -0
  9. data/lib/yerba/formatting.rb +18 -0
  10. data/lib/yerba/location.rb +5 -0
  11. data/lib/yerba/map.rb +166 -0
  12. data/lib/yerba/scalar.rb +85 -0
  13. data/lib/yerba/sequence.rb +182 -0
  14. data/lib/yerba/version.rb +1 -1
  15. data/lib/yerba.rb +32 -4
  16. data/rust/Cargo.lock +378 -2
  17. data/rust/Cargo.toml +5 -1
  18. data/rust/build.rs +11 -0
  19. data/rust/cbindgen.toml +27 -0
  20. data/rust/src/commands/apply.rs +5 -0
  21. data/rust/src/commands/blank_lines.rs +58 -0
  22. data/rust/src/commands/check.rs +5 -0
  23. data/rust/src/commands/delete.rs +35 -0
  24. data/rust/src/commands/get.rs +194 -0
  25. data/rust/src/commands/init.rs +89 -0
  26. data/rust/src/commands/insert.rs +106 -0
  27. data/rust/src/commands/mate.rs +55 -0
  28. data/rust/src/commands/mod.rs +349 -0
  29. data/rust/src/commands/move_item.rs +54 -0
  30. data/rust/src/commands/move_key.rs +87 -0
  31. data/rust/src/commands/quote_style.rs +62 -0
  32. data/rust/src/commands/remove.rs +35 -0
  33. data/rust/src/commands/rename.rs +37 -0
  34. data/rust/src/commands/set.rs +59 -0
  35. data/rust/src/commands/sort.rs +52 -0
  36. data/rust/src/commands/sort_keys.rs +62 -0
  37. data/rust/src/commands/version.rs +8 -0
  38. data/rust/src/document.rs +764 -333
  39. data/rust/src/error.rs +0 -5
  40. data/rust/src/ffi.rs +991 -0
  41. data/rust/src/json.rs +49 -90
  42. data/rust/src/lib.rs +9 -2
  43. data/rust/src/main.rs +55 -843
  44. data/rust/src/selector.rs +241 -0
  45. data/rust/src/syntax.rs +97 -21
  46. data/rust/src/yaml_writer.rs +89 -0
  47. data/rust/src/yerbafile.rs +11 -126
  48. data/yerba.gemspec +5 -0
  49. metadata +34 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9ab1031fe2ea6b59f49bad4c2b7acf7e6679f16a97cbf7fa93d4e2790cc3c421
4
- data.tar.gz: 79f77237c9ecbc6ed8404d31d50407e9ab4296829c8db983fba2300347ff06bc
3
+ metadata.gz: 6282fdc069d8c91009e7b59e31c5c295233a08d984ecebb3a4cbdbd6edcbaf53
4
+ data.tar.gz: 5831d5eb1499827cbc6f110bd743763c362019c80eef44b30b1e60d7b0b2c5dc
5
5
  SHA512:
6
- metadata.gz: b38dcb297b53bdae8ac76a174922a544271a0564111149a864a2d0b6a4e5572b9219b266e29e22510cf567ac48b351d08ab47a9fb563734ea3834bed3655396b
7
- data.tar.gz: 41907521d78760c21315891b7aa26bc772b7f9f871f72275f215f65e42726313612e61997d8458823e4d67ed25cb6dca5bf7dfcae913306a0e4cd5269f5065a0
6
+ metadata.gz: b287e64dbe34d1e4cd8ce839fd4c9d7ad2a2453423c8c991edaf543b601f1584b1991d2cfbcf64e809605bc9562b0b62b9e380278bc40d0ffe36fa4381536764
7
+ data.tar.gz: 2b4c58ff1dfe703b994547de01a23c4ec7dfeace739d756f47a35680592d18ce4f67c01522e88e79434cf26a00131a32447ccefffd355293b9e7febc2d65ae81
data/README.md CHANGED
@@ -1,8 +1,12 @@
1
- <h2 align="center">🧉 Yerba</h2>
1
+ <p align="center">
2
+ <img src="assets/logo.png" alt="Yerba Logo" width="150px">
3
+ </p>
4
+
5
+ <h2 align="center">Yerba</h2>
2
6
 
3
7
  <h4 align="center"><u>Y</u>AML <u>E</u>diting and <u>R</u>efactoring with <u>B</u>etter <u>A</u>ccuracy</h4>
4
8
 
5
- <div align="center">A CLI tool for editing YAML while preserving structure, comments, and format.</div><br/>
9
+ <div align="center">A Rust CLI tool and Ruby library for editing YAML while preserving structure, comments, and format.</div><br/>
6
10
 
7
11
  <p align="center">
8
12
  <a href="https://rubygems.org/gems/yerba"><img alt="Gem Version" src="https://img.shields.io/gem/v/yerba"></a>
@@ -13,32 +17,489 @@
13
17
 
14
18
  <br/>
15
19
 
16
- ### What is Yerba?
20
+ ## What is Yerba?
21
+
22
+ Yerba is a lossless YAML editing tool. It lets you programmatically read, modify, and enforce formatting in YAML files while preserving their original structure, including comments, blank lines, quote styles, and key ordering.
23
+
24
+ Most YAML libraries parse a file into a data structure and serialize it back, discarding all formatting in the process. Yerba operates on the concrete syntax tree (CST), so your edits are surgical: only the targeted values change, and everything else stays exactly as it was.
25
+
26
+ Yerba is available as:
27
+
28
+ - A **standalone CLI binary** with zero runtime dependencies
29
+ - A **Rust crate** for embedding in Rust applications
30
+ - A **Ruby gem** for programmatic YAML editing from Ruby
17
31
 
18
- **Yerba** is a lossless YAML editing tool that lets you programmatically modify YAML files while preserving their original structure, comments, and formatting.
32
+ Yerba was born out of the need to manage, validate, and enforce consistent formatting for hundreds of YAML data files in the [RubyEvents.org](https://github.com/rubyevents/rubyevents) project.
19
33
 
20
- Yerba was born out of the need to manage, validate, programmatically modify, and enforce lossless and consistent style/formatting for the YAML data files in the [RubyEvents.org](https://github.com/rubyevents/rubyevents) project.
34
+ ## Installation
21
35
 
22
- ### Command-Line Usage
36
+ ### CLI (standalone)
23
37
 
24
- Install the Yerba gem via RubyGems:
38
+ The `yerba` CLI is a standalone Rust binary with no Ruby dependency. Install it via Cargo:
39
+
40
+ ```bash
41
+ cargo install yerba
42
+ ```
43
+
44
+ ### Rust Crate
45
+
46
+ Use `yerba` as a library in your Rust project:
47
+
48
+ ```toml
49
+ [dependencies]
50
+ yerba = "0.2"
51
+ ```
52
+
53
+ ```rust
54
+ let mut document = yerba::parse_file("config.yml")?;
55
+ document.set("database.host", "0.0.0.0")?;
56
+ document.save()?;
57
+ ```
58
+
59
+ ### Ruby Gem
60
+
61
+ The Ruby gem bundles both the CLI binary and a native extension for programmatic access from Ruby:
25
62
 
26
63
  ```bash
27
64
  gem install yerba
28
65
  ```
29
66
 
30
- ### Examples
67
+ Or add it to your `Gemfile`:
68
+
69
+ ```ruby
70
+ gem "yerba"
71
+ ```
72
+
73
+ The gem ships with precompiled binaries for macOS and Linux.
74
+
75
+ If no precompiled binary is available for your platform, it will compile from source automatically, which requires a [Rust toolchain](https://rustup.rs).
76
+
77
+ ## CLI Usage
78
+
79
+ The `yerba` CLI follows a consistent pattern:
80
+
81
+ ```
82
+ yerba <command> <file> <selector> [value] [options]
83
+ ```
84
+
85
+ Selectors use dot-notation for nested keys, brackets for array access, and support glob patterns for operating on multiple files at once.
86
+
87
+ ### Selectors
88
+
89
+ Selectors let you address any node in a YAML document:
90
+
91
+ | Pattern | Meaning | Example |
92
+ |---------|---------|---------|
93
+ | `key` | A single key | `"database.host"` |
94
+ | `key.nested` | Nested key path | `"database.settings.pool"` |
95
+ | `[]` | All items in array | `"[].title"` |
96
+ | `[N]` | Item at index | `"[0].title"` |
97
+ | `[].key[].nested` | Nested array access | `"[].speakers[].name"` |
98
+
99
+ ### Conditions
100
+
101
+ Conditions filter which items a command operates on:
102
+
103
+ | Syntax | Meaning | Example |
104
+ |--------|---------|---------|
105
+ | `.key == value` | Equality | `".kind == keynote"` |
106
+ | `.key != value` | Inequality | `".status != draft"` |
107
+ | `.key contains val` | Substring or member | `".title contains Ruby"` |
108
+ | `.key not_contains val` | Negated contains | `".title not_contains test"` |
109
+
110
+ ---
111
+
112
+ ### `get`
113
+
114
+ Retrieve values from YAML files. Supports single values, array traversal, glob patterns across multiple files, conditions for filtering, and field selection.
115
+
116
+ ```bash
117
+ yerba get config.yml "database.host"
118
+ yerba get videos.yml "[].title"
119
+ yerba get videos.yml "[0].title"
120
+ ```
121
+
122
+ Use `--select` to pick specific fields from each item, and `--condition` to filter which items are returned:
123
+
124
+ ```bash
125
+ yerba get videos.yml "[]" --select ".title,.speakers"
126
+ yerba get videos.yml "[]" --condition ".kind == keynote"
127
+ yerba get videos.yml "[]" --select ".title" --condition ".kind == keynote"
128
+ ```
129
+
130
+ Glob patterns let you query across many files at once:
131
+
132
+ ```bash
133
+ yerba get "data/**/videos.yml" "[].speakers[].name"
134
+ yerba get "data/**/videos.yml" "[]" --condition ".kind == keynote" --select ".id,.title"
135
+ ```
136
+
137
+ Use `--raw` to output plain values (one per line) instead of JSON:
138
+
139
+ ```bash
140
+ yerba get videos.yml "[]" --condition ".speakers contains Matz" --raw
141
+ ```
142
+
143
+ ### `set`
144
+
145
+ Update an existing value at a path. The original quote style is preserved automatically, if a value was double-quoted before, it stays double-quoted after the edit.
146
+
147
+ ```bash
148
+ yerba set config.yml "database.host" "0.0.0.0"
149
+ yerba set videos.yml "[0].title" "New Title"
150
+ ```
151
+
152
+ Use `--if-exists` to only set the value when the path already exists, or `--if-missing` to only set it when the path does not exist:
153
+
154
+ ```bash
155
+ yerba set config.yml "database.host" "0.0.0.0" --if-exists
156
+ yerba set "data/**/event.yml" "website" "" --if-exists
157
+ ```
158
+
159
+ Use `--condition` to only apply the change when a sibling field matches:
160
+
161
+ ```bash
162
+ yerba set config.yml "database.host" "0.0.0.0" --condition ".port == 5432"
163
+ ```
164
+
165
+ ### `insert`
166
+
167
+ Insert a new key into a map or a new item into a sequence. By default, new items are appended at the end.
168
+
169
+ ```bash
170
+ yerba insert config.yml "database.ssl" true
171
+ yerba insert config.yml "tags" "yaml"
172
+ ```
173
+
174
+ Control placement with `--before`, `--after`, or `--at`:
175
+
176
+ ```bash
177
+ yerba insert config.yml "database.ssl" true --after "host"
178
+ yerba insert config.yml "database.ssl" true --before "port"
179
+ yerba insert config.yml "tags" "yaml" --at 0
180
+ yerba insert config.yml "tags" "yaml" --after "ruby"
181
+ ```
182
+
183
+ For sequences of maps, use conditions to position relative to other items:
184
+
185
+ ```bash
186
+ yerba insert speakers.yml "" "name: Bob" --after ".name == Alice"
187
+ yerba insert videos.yml "[0].speakers" "Diana" --before ".name == Charlie"
188
+ ```
189
+
190
+ Use `--from` to read the value from another file (or stdin with `-`):
191
+
192
+ ```bash
193
+ yerba insert videos.yml "" --from "new_talk.yml" --after ".id == first-talk"
194
+ ```
195
+
196
+ ### `delete`
197
+
198
+ Remove a key and its value from a map:
199
+
200
+ ```bash
201
+ yerba delete config.yml "database.pool"
202
+ yerba delete videos.yml "[0].description"
203
+ ```
204
+
205
+ Use `--dry-run` to preview the result without writing to the file:
206
+
207
+ ```bash
208
+ yerba delete config.yml "database.pool" --dry-run
209
+ ```
210
+
211
+ ### `remove`
212
+
213
+ Remove a specific item from a sequence by its value:
214
+
215
+ ```bash
216
+ yerba remove config.yml "tags" "rust"
217
+ yerba remove videos.yml "[0].speakers" "Alice"
218
+ ```
219
+
220
+ ### `rename`
221
+
222
+ Rename a key in a map while preserving its value and position:
223
+
224
+ ```bash
225
+ yerba rename config.yml "database.host" "database.hostname"
226
+ yerba rename config.yml "database.host" "hostname"
227
+ ```
228
+
229
+ ### `move`
230
+
231
+ Move a sequence item to a new position. You can reference items by value, index, or condition:
232
+
233
+ ```bash
234
+ yerba move config.yml "tags" "rust" --before "ruby"
235
+ yerba move config.yml "tags" "rust" --after "yaml"
236
+ yerba move config.yml "tags" 2 --to 0
237
+ yerba move videos.yml "" ".id == talk-2" --after ".id == talk-1"
238
+ ```
239
+
240
+ ### `move-key`
241
+
242
+ Move a key to a new position within a map:
243
+
244
+ ```bash
245
+ yerba move-key config.yml "database.name" --to 0
246
+ yerba move-key config.yml "database.pool" --before "database.host"
247
+ yerba move-key config.yml "database.pool" --after "database.name"
248
+ ```
249
+
250
+ ### `sort`
251
+
252
+ Sort items in a sequence. For simple scalar sequences, no options are needed. For sequences of maps, use `--by` to specify sort fields. Append `:desc` for descending order:
253
+
254
+ ```bash
255
+ yerba sort config.yml "tags"
256
+ yerba sort videos.yml --by "title"
257
+ yerba sort videos.yml --by "date:desc,title"
258
+ yerba sort videos.yml "[].speakers" --by "name"
259
+ ```
260
+
261
+ ### `sort-keys`
262
+
263
+ Reorder the keys in a map to match a predefined order. If any key in the document is not present in the order list, the command aborts with an error, this ensures you account for every field:
264
+
265
+ ```bash
266
+ yerba sort-keys config.yml "database" "host,port,name,pool"
267
+ yerba sort-keys "data/**/event.yml" "" "id,title,kind,location"
268
+ yerba sort-keys "data/**/videos.yml" "[]" "id,title,speakers"
269
+ ```
270
+
271
+ ### `quote-style`
272
+
273
+ Enforce a consistent quote style across keys and/or values. Available styles are `plain`, `single`, and `double`:
274
+
275
+ ```bash
276
+ yerba quote-style config.yml --values double
277
+ yerba quote-style config.yml --keys plain
278
+ yerba quote-style config.yml --keys plain --values double
279
+ ```
280
+
281
+ Scope the operation to a specific selector:
282
+
283
+ ```bash
284
+ yerba quote-style config.yml "[].speakers" --values plain
285
+ yerba quote-style "data/**/*.yml" --keys plain --values double
286
+ ```
287
+
288
+ ### `blank-lines`
289
+
290
+ Enforce a consistent number of blank lines between sequence entries:
291
+
292
+ ```bash
293
+ yerba blank-lines videos.yml 1
294
+ yerba blank-lines videos.yml "[]" 1
295
+ yerba blank-lines config.yml "tags" 0
296
+ ```
297
+
298
+ ## `Yerbafile`
299
+
300
+ A `Yerbafile` is a YAML configuration file that defines formatting and editing rules as pipelines of operations that are applied to your files across your project.
301
+
302
+ Use `yerba init` to create one, then `yerba apply` to apply all rules, or `yerba check` to verify compliance (exits with code `1` if files would change):
31
303
 
32
304
  ```bash
33
- yerba set config.yml path.to.key value
34
- yerba get config.yml path.to.key
305
+ yerba init
306
+ yerba apply
307
+ yerba check
308
+ ```
309
+
310
+ Each rule specifies a file glob and a list of steps to run in order:
311
+
312
+ ```yaml
313
+ rules:
314
+ - files: "config/**/*.yml"
315
+ pipeline:
316
+ - quote_style:
317
+ key_style: plain
318
+ value_style: double
319
+
320
+ - sort_keys:
321
+ path: ""
322
+ order:
323
+ - id
324
+ - title
325
+ - description
326
+
327
+ - blank_lines:
328
+ count: 1
329
+
330
+ - files: "data/speakers.yml"
331
+ pipeline:
332
+ - quote_style:
333
+ key_style: plain
334
+ value_style: double
335
+
336
+ - sort_keys:
337
+ path: ""
338
+ order:
339
+ - name
340
+ - slug
341
+ - github
342
+ - twitter
343
+ - website
344
+
345
+ - sort:
346
+ path: ""
347
+ by: name
348
+ ```
349
+
350
+ Available pipeline steps:
351
+
352
+ - `quote_style` Enforce quote style on keys and/or values, optionally scoped by path
353
+ - `sort_keys` Reorder keys to match a predefined list
354
+ - `sort` Sort sequence items by field(s)
355
+ - `blank_lines` Enforce blank lines between sequence entries
356
+ - `set` Set a value (supports conditions)
357
+ - `insert` Insert a new key or sequence item
358
+ - `delete` Remove a key (supports conditions)
359
+ - `rename` Rename a key
360
+ - `remove` Remove an item from a sequence
361
+ - `get` Read a value and store it as a variable for subsequent steps
362
+
363
+ This makes it easy to enforce project-wide YAML conventions in CI:
364
+
365
+ ```bash
366
+ yerba check
367
+ ```
368
+
369
+ ## Ruby API
370
+
371
+ Yerba includes a native C extension (backed by the same Rust core) that provides a full Ruby API for YAML editing.
372
+
373
+ ### Parsing
374
+
375
+ Create a document from a file path or from a string:
376
+
377
+ ```ruby
378
+ require "yerba"
379
+
380
+ document = Yerba.parse_file("config.yml")
381
+ document = Yerba.parse("database:\n host: localhost\n port: 5432\n")
382
+ ```
383
+
384
+ ### Reading Values
385
+
386
+ Use `get` to retrieve the raw value at a path. Values are returned with their YAML types, strings, integers, booleans, and nil are all mapped to their Ruby equivalents:
387
+
388
+ ```ruby
389
+ document.get("database.host") # => "localhost"
390
+ document.get("database.port") # => 5432
391
+ document.get("database.ssl") # => false
392
+ ```
393
+
394
+ ### Structured Navigation
395
+
396
+ Use bracket notation to get typed wrapper objects (`Scalar`, `Map`, or `Sequence`) representing nodes in the document. These are live references, mutations flow back to the document.
397
+
398
+ You can use a full dot-path in a single bracket call, or chain brackets to navigate one level at a time:
399
+
400
+ ```ruby
401
+ document["database.host"].value # => "localhost"
402
+ document["database"]["host"].value # => "localhost"
403
+ document["database"]["port"].value # => 5432
404
+ ```
405
+
406
+ The returned object type depends on what's at the path:
407
+
408
+ ```ruby
409
+ document["database"] # => Yerba::Map
410
+ document["database.host"] # => Yerba::Scalar
411
+ document["tags"] # => Yerba::Sequence
412
+ ```
413
+
414
+ Scalars expose their value and quote style:
415
+
416
+ ```ruby
417
+ scalar = document["database.host"]
418
+ scalar.value # => "localhost"
419
+ scalar.quote_style # => :double
420
+ ```
421
+
422
+ ### Mutations
423
+
424
+ Modify values in place. The original formatting is preserved:
425
+
426
+ ```ruby
427
+ document["database"]["host"].value = "0.0.0.0"
428
+ document.set("database.port", 3306)
429
+ ```
430
+
431
+ Insert new keys with positional control:
432
+
433
+ ```ruby
434
+ document["database"].insert("ssl", true, after: "host")
435
+ ```
436
+
437
+ Work with sequences using familiar Ruby patterns:
438
+
439
+ ```ruby
440
+ tags = document["tags"]
441
+ tags << "yaml"
442
+ tags << { name: "Rust", version: "1.80" }
443
+ tags.remove("obsolete")
444
+ tags.sort(by: "name")
445
+ ```
446
+
447
+ ### Quote Style Control
448
+
449
+ Read and set the quote style on individual scalars:
450
+
451
+ ```ruby
452
+ scalar = document["database.host"]
453
+ scalar.quote_style # => :double
454
+ scalar.quote_style = :single
455
+ ```
456
+
457
+ ### Wildcard Access
458
+
459
+ Use `at_path` to access all items matching a wildcard selector:
460
+
461
+ ```ruby
462
+ titles = document.at_path("[].title")
463
+ titles.each { |scalar| puts scalar.value }
464
+ ```
465
+
466
+ ### Collections
467
+
468
+ Operate on multiple files matching a glob pattern:
469
+
470
+ ```ruby
471
+ collection = Yerba.files("data/**/videos.yml")
472
+
473
+ collection.each do |document|
474
+ puts document.get("[0].title")
475
+ end
476
+
477
+ collection.apply! do |document|
478
+ document.set("status", "published")
479
+ end
35
480
  ```
36
481
 
37
- ### Development
482
+ ### Saving
38
483
 
39
- After checking out the repo, run `bundle install` to install dependencies. Then, run `bundle exec rake test` to run the tests.
484
+ Write changes back to the original file:
485
+
486
+ ```ruby
487
+ document.save!
488
+ ```
489
+
490
+ Or render the document as a string without writing to disk:
491
+
492
+ ```ruby
493
+ document.to_s
494
+ ```
40
495
 
41
- ### Rust
496
+ ## Development
497
+
498
+ After checking out the repo, run `bundle install` to install Ruby dependencies, then `bundle exec rake test` to run the test suite.
499
+
500
+ ### Building from source
501
+
502
+ The Rust core is in the `rust/` directory:
42
503
 
43
504
  ```bash
44
505
  cd rust
@@ -46,6 +507,22 @@ cargo build
46
507
  cargo test
47
508
  ```
48
509
 
49
- ### License
510
+ The C extension (for the Ruby API) is compiled via `ext/yerba/extconf.rb` which invokes `cargo build` and links against the resulting static library. Running `bundle exec rake compile` will build both the Rust library and the C extension.
511
+
512
+ ### Running the CLI locally
513
+
514
+ ```bash
515
+ cargo run -- get config.yml "database.host"
516
+ ```
517
+
518
+ Or build a release binary:
519
+
520
+ ```bash
521
+ cd rust
522
+ cargo build --release
523
+ ./target/release/yerba --help
524
+ ```
525
+
526
+ ## License
50
527
 
51
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
528
+ The gem is available as open source under the terms of the [MIT License](https://github.com/marcoroth/yerba/blob/main/LICENSE.txt).
Binary file
data/ext/yerba/extconf.rb CHANGED
@@ -1,54 +1,111 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "mkmf"
3
4
  require "fileutils"
4
5
 
5
6
  rust_dir = File.expand_path("../../rust", __dir__)
6
7
  root_dir = File.expand_path("../..", __dir__)
7
8
 
8
- platform = Gem::Platform.local
9
- platform_key = "#{platform.cpu}-#{platform.os}"
10
- exe_directory = File.join(root_dir, "exe", platform_key)
11
- exe_file = File.join(exe_directory, "yerba")
9
+ unless system("cargo --version > /dev/null 2>&1")
10
+ abort <<~MESSAGE
12
11
 
13
- if File.executable?(exe_file)
14
- puts "yerba: Precompiled binary found at #{exe_file}"
15
- else
16
- puts "yerba: No precompiled binary found. Compiling from Rust source..."
12
+ ERROR: Rust toolchain not found.
17
13
 
18
- unless system("cargo --version > /dev/null 2>&1")
19
- abort <<~MESSAGE
14
+ yerba requires the Rust toolchain to compile from source.
20
15
 
21
- ERROR: Rust toolchain not found.
16
+ Install Rust: https://rustup.rs
22
17
 
23
- yerba requires a precompiled binary or the Rust toolchain to compile from source.
18
+ MESSAGE
19
+ end
24
20
 
25
- Install Rust: https://rustup.rs
21
+ RUST_TARGETS = {
22
+ "aarch64-linux-gnu" => "aarch64-unknown-linux-gnu",
23
+ "aarch64-linux-musl" => "aarch64-unknown-linux-musl",
24
+ "arm-linux-gnu" => "armv7-unknown-linux-gnueabihf",
25
+ "arm-linux-musl" => "armv7-unknown-linux-musleabihf",
26
+ "arm64-darwin" => "aarch64-apple-darwin",
27
+ "x86_64-darwin" => "x86_64-apple-darwin",
28
+ "x86_64-linux-gnu" => "x86_64-unknown-linux-gnu",
29
+ "x86_64-linux-musl" => "x86_64-unknown-linux-musl",
30
+ "x86-linux-gnu" => "i686-unknown-linux-gnu",
31
+ "x86-linux-musl" => "i686-unknown-linux-musl",
32
+ }.freeze
26
33
 
27
- MESSAGE
28
- end
34
+ cross_compiling = ENV.key?("RUBY_CC_VERSION")
35
+ target_platform = ENV.fetch("CARGO_BUILD_TARGET", nil)
36
+
37
+ if cross_compiling && target_platform.nil?
38
+ rcd_platform = ENV.fetch("RCD_PLATFORM", "")
39
+ target_platform = RUST_TARGETS[rcd_platform]
29
40
 
30
- unless system("cd #{rust_dir} && cargo build --release")
31
- abort "ERROR: Failed to compile yerba from Rust source."
41
+ if target_platform.nil?
42
+ ruby_platform = RbConfig::CONFIG["arch"]
43
+ target_platform = RUST_TARGETS.values.find { |t| ruby_platform.include?(t.split("-").first) }
32
44
  end
45
+ end
33
46
 
34
- source_binary = File.join(rust_dir, "target", "release", "yerba")
47
+ if target_platform
48
+ puts "yerba: Cross-compiling Rust for target: #{target_platform}"
49
+ system("rustup target add #{target_platform}") || warn("yerba: Failed to add Rust target #{target_platform}")
35
50
 
36
- unless File.exist?(source_binary)
37
- abort "ERROR: Compilation succeeded but binary not found at #{source_binary}"
38
- end
51
+ cargo_args = "--release --target #{target_platform}"
52
+ lib_dir = File.join(rust_dir, "target", target_platform, "release")
53
+ else
54
+ puts "yerba: Compiling Rust library for native platform..."
55
+ cargo_args = "--release"
56
+ lib_dir = File.join(rust_dir, "target", "release")
57
+ end
58
+
59
+ unless system("cd #{rust_dir} && cargo build #{cargo_args}")
60
+ abort "ERROR: Failed to compile yerba from Rust source."
61
+ end
62
+
63
+ if target_platform
64
+ platform_key = ENV.fetch("RCD_PLATFORM", "")
65
+ else
66
+ platform = Gem::Platform.local
67
+ platform_key = "#{platform.cpu}-#{platform.os}"
68
+ end
69
+
70
+ exe_directory = File.join(root_dir, "exe", platform_key)
71
+ exe_file = File.join(exe_directory, "yerba")
72
+ source_binary = File.join(lib_dir, "yerba")
39
73
 
74
+ if File.exist?(source_binary)
40
75
  FileUtils.mkdir_p(exe_directory)
41
76
  FileUtils.cp(source_binary, exe_file)
42
77
  FileUtils.chmod(0o755, exe_file)
43
78
 
44
- puts "yerba: Successfully compiled binary to #{exe_file}"
79
+ puts "yerba: CLI binary installed to #{exe_file}"
80
+ end
81
+
82
+ lib_name = case RbConfig::CONFIG["host_os"]
83
+ when /darwin/ then "libyerba.dylib"
84
+ when /mingw|mswin/ then "yerba.dll"
85
+ else "libyerba.so"
86
+ end
87
+
88
+ if target_platform
89
+ lib_name = case target_platform
90
+ when /darwin/ then "libyerba.dylib"
91
+ when /windows|mingw/ then "yerba.dll"
92
+ else "libyerba.so"
93
+ end
94
+ end
95
+
96
+ lib_path = File.join(lib_dir, lib_name)
97
+
98
+ unless File.exist?(lib_path)
99
+ abort "ERROR: Shared library not found at #{lib_path}"
100
+ end
101
+
102
+ puts "yerba: Shared library found at #{lib_path}"
103
+
104
+ $LDFLAGS << " -L#{lib_dir} -lyerba"
105
+ $CFLAGS << " -I#{File.join(__dir__, "include")}"
106
+
107
+ if RbConfig::CONFIG["host_os"].match?(/darwin|linux/)
108
+ $LDFLAGS << " -Wl,-rpath,#{lib_dir}"
45
109
  end
46
110
 
47
- File.write("Makefile", <<~MAKEFILE)
48
- all:
49
- \t@echo "yerba: nothing to build"
50
- install:
51
- \t@echo "yerba: nothing to install"
52
- clean:
53
- \t@echo "yerba: nothing to clean"
54
- MAKEFILE
111
+ create_makefile("yerba/yerba")