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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
use anyhow::{Context, Result};
use clap::{Parser, Subcommand};
use std::path::PathBuf;
use std::process;
mod cache;
mod commands;
mod dispatch;
mod pipeline;
mod registry;
mod version_pin;
#[derive(Parser)]
#[command(
name = "alef",
version,
about = "Opinionated polyglot binding generator",
long_about = None,
)]
struct Cli {
/// Path to alef.toml config file.
#[arg(long, default_value = "alef.toml")]
config: PathBuf,
/// Maximum parallel jobs (0 = all cores, 1 = sequential).
#[arg(short, long, default_value = "0", global = true)]
jobs: usize,
/// Increase verbosity (-v info, -vv debug, -vvv trace). Overridden by RUST_LOG.
#[arg(short, long, action = clap::ArgAction::Count, global = true)]
verbose: u8,
/// Suppress all output below `error`. Overridden by RUST_LOG.
#[arg(short, long, global = true, conflicts_with = "verbose")]
quiet: bool,
/// Disable ANSI colour in log output.
#[arg(long, global = true)]
no_color: bool,
/// Restrict the command to one or more crates by name. May be passed multiple times.
/// When omitted, every crate in the workspace is processed.
#[arg(long = "crate", value_name = "NAME", global = true)]
crate_filter: Vec<String>,
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// Extract API surface from Rust source into IR.
Extract {
/// Output IR JSON file.
#[arg(short, long, default_value = ".alef/ir.json")]
output: PathBuf,
},
/// Generate bindings for selected languages.
Generate {
/// Comma-separated list of languages (default: all from config).
#[arg(long, value_delimiter = ',')]
lang: Option<Vec<String>>,
/// Ignore cache, regenerate everything.
#[arg(long)]
clean: bool,
/// Run post-generation formatters on emitted files (off by default).
#[arg(long)]
format: bool,
},
/// Generate type stubs (.pyi, .rbs).
Stubs {
/// Comma-separated list of languages.
#[arg(long, value_delimiter = ',')]
lang: Option<Vec<String>>,
},
/// Generate package scaffolding (pyproject.toml, package.json, etc.).
Scaffold {
/// Comma-separated list of languages.
#[arg(long, value_delimiter = ',')]
lang: Option<Vec<String>>,
},
/// Generate README files from templates.
Readme {
/// Comma-separated list of languages.
#[arg(long, value_delimiter = ',')]
lang: Option<Vec<String>>,
},
/// Generate API reference documentation (Markdown for mkdocs).
Docs {
/// Comma-separated list of languages.
#[arg(long, value_delimiter = ',')]
lang: Option<Vec<String>>,
/// Output directory (default: docs/reference).
#[arg(long, default_value = "docs/reference")]
output: String,
},
/// Sync version from Cargo.toml to all package manifests.
SyncVersions {
/// Bump version before syncing (major, minor, patch).
#[arg(long)]
bump: Option<String>,
/// Set version explicitly (e.g., "0.1.0-rc.1").
#[arg(long)]
set: Option<String>,
},
/// Run format commands on generated output.
Fmt {
/// Comma-separated list of languages.
#[arg(long, value_delimiter = ',')]
lang: Option<Vec<String>>,
},
/// Run configured lint/format commands on generated output.
Lint {
/// Comma-separated list of languages.
#[arg(long, value_delimiter = ',')]
lang: Option<Vec<String>>,
},
/// Run configured test suites for each language.
Test {
/// Comma-separated list of languages.
#[arg(long, value_delimiter = ',')]
lang: Option<Vec<String>>,
/// Also run e2e tests.
#[arg(long)]
e2e: bool,
/// Run with coverage collection.
#[arg(long)]
coverage: bool,
},
/// Install dependencies for each language.
Setup {
/// Comma-separated list of languages.
#[arg(long, value_delimiter = ',')]
lang: Option<Vec<String>>,
/// Override the per-language setup timeout in seconds (default: 600).
#[arg(long)]
timeout: Option<u64>,
},
/// Clean build artifacts for each language.
Clean {
/// Comma-separated list of languages.
#[arg(long, value_delimiter = ',')]
lang: Option<Vec<String>>,
},
/// Update dependencies for each language.
Update {
/// Comma-separated list of languages.
#[arg(long, value_delimiter = ',')]
lang: Option<Vec<String>>,
/// Upgrade to latest versions, including incompatible/major bumps.
#[arg(long)]
latest: bool,
},
/// Verify bindings are up to date and API surface parity.
Verify {
/// Exit with code 1 if any binding is stale (CI mode).
#[arg(long)]
exit_code: bool,
/// Also run compilation check.
#[arg(long)]
compile: bool,
/// Also run lint check.
#[arg(long)]
lint: bool,
/// Comma-separated list of languages.
#[arg(long, value_delimiter = ',')]
lang: Option<Vec<String>>,
},
/// Show diff of what would change without writing.
Diff {
/// Exit with code 1 if changes exist (CI mode).
#[arg(long)]
exit_code: bool,
},
/// Build language bindings using native tools (napi, maturin, wasm-pack, etc.).
Build {
/// Comma-separated list of languages (default: all from config).
#[arg(long, value_delimiter = ',')]
lang: Option<Vec<String>>,
/// Build with release optimizations.
#[arg(long, short)]
release: bool,
},
/// Run all: generate + stubs + scaffold + readme + sync.
All {
/// Ignore cache.
#[arg(long)]
clean: bool,
/// Run post-generation formatters on emitted files (off by default).
#[arg(long)]
format: bool,
},
/// Initialize a new alef.toml config.
Init {
/// Comma-separated list of languages.
#[arg(long, value_delimiter = ',')]
lang: Option<Vec<String>>,
/// Run post-generation formatters on emitted files (off by default).
#[arg(long)]
format: bool,
},
/// Migrate legacy alef.toml schema to new [workspace] / [[crates]] layout.
Migrate {
/// Path to alef.toml (default: alef.toml from --config flag).
path: Option<PathBuf>,
/// Write migrated config back to file (dry-run by default).
#[arg(long)]
write: bool,
},
/// Generate e2e test suites from fixture files.
E2e {
#[command(subcommand)]
action: E2eAction,
},
/// Prepare, build, and package artifacts for publishing.
Publish {
#[command(subcommand)]
action: PublishAction,
},
/// Manage the build cache.
Cache {
#[command(subcommand)]
action: CacheAction,
},
/// Cross-manifest version consistency checker and release utilities.
Validate {
#[command(subcommand)]
action: ValidateAction,
},
/// Emit release metadata JSON consumed by CI workflows.
ReleaseMetadata {
/// Release tag (e.g. v4.1.0 or v4.1.0-rc.1). Required.
#[arg(long, short)]
tag: String,
/// Comma-separated target list (e.g. "python,node") or "all" (default).
#[arg(long, default_value = "all")]
targets: String,
/// Git ref override (branch, tag, or commit SHA).
#[arg(long)]
git_ref: Option<String>,
/// GitHub event name (release/workflow_dispatch/repository_dispatch).
#[arg(long, default_value = "")]
event: String,
/// Dry-run flag — include in metadata without actually publishing.
#[arg(long)]
dry_run: bool,
/// Force-republish flag — republish even if version already exists.
#[arg(long)]
force_republish: bool,
/// Output machine-readable JSON.
#[arg(long)]
json: bool,
},
/// Check whether a package version exists in a registry.
CheckRegistry {
/// Registry to check.
#[arg(long, value_enum)]
registry: commands::check_registry::Registry,
/// Package name (use `groupId:artifactId` for Maven).
#[arg(long)]
package: String,
/// Version to check.
#[arg(long)]
version: String,
/// Homebrew tap repository (`owner/repo`).
#[arg(long)]
tap_repo: Option<String>,
/// GitHub repository (`owner/repo`) for github-release check.
#[arg(long)]
repo: Option<String>,
/// NuGet source URL (defaults to https://api.nuget.org).
#[arg(long)]
source: Option<String>,
/// Asset name prefix (github-release): require at least one asset on
/// the release whose name starts with this prefix.
#[arg(long)]
asset_prefix: Option<String>,
/// Comma-separated list of required asset names (github-release): all
/// must be present on the release.
#[arg(long, value_delimiter = ',')]
required_assets: Vec<String>,
/// Output machine-readable JSON.
#[arg(long)]
json: bool,
},
/// Create and push Go module tags for a release.
GoTag {
/// Version string (e.g. "4.1.0" or "v4.1.0").
#[arg(long, short)]
version: String,
/// Git remote name (default: origin).
#[arg(long, default_value = "origin")]
remote: String,
/// Print tags that would be created without executing.
#[arg(long)]
dry_run: bool,
/// Output machine-readable JSON.
#[arg(long)]
json: bool,
},
}
#[derive(Subcommand)]
enum PublishAction {
/// Prepare for publishing: vendor deps, stage FFI artifacts.
Prepare {
/// Comma-separated list of languages.
#[arg(long, value_delimiter = ',')]
lang: Option<Vec<String>>,
/// Rust target triple for cross-compilation (e.g. x86_64-unknown-linux-gnu).
#[arg(long)]
target: Option<String>,
/// Show what would be done without executing.
#[arg(long)]
dry_run: bool,
},
/// Build release artifacts for a specific platform.
Build {
/// Comma-separated list of languages.
#[arg(long, value_delimiter = ',')]
lang: Option<Vec<String>>,
/// Rust target triple (defaults to host).
#[arg(long)]
target: Option<String>,
/// Use `cross` instead of `cargo` for cross-compilation.
#[arg(long)]
use_cross: bool,
},
/// Package built artifacts into distributable archives.
Package {
/// Comma-separated list of languages.
#[arg(long, value_delimiter = ',')]
lang: Option<Vec<String>>,
/// Rust target triple (auto-maps to language-specific platform names).
#[arg(long)]
target: Option<String>,
/// Output directory for packages.
#[arg(long, short, default_value = "dist")]
output: String,
/// Version string (auto-detected from Cargo.toml if absent).
#[arg(long)]
version: Option<String>,
/// Show what would be packaged without executing.
#[arg(long)]
dry_run: bool,
/// PHP minor version (e.g. "8.5"). Required when --lang php.
#[arg(long)]
php_version: Option<String>,
/// PHP thread-safety mode: "nts" or "ts". Defaults to "nts".
#[arg(long, default_value = "nts")]
php_ts: String,
/// Linux libc override: "glibc" or "musl". Auto-detected from target triple if absent.
#[arg(long)]
php_libc: Option<String>,
/// Windows compiler tag (e.g. "vs16", "vs17"). Required when target OS is windows and --lang php.
#[arg(long)]
windows_compiler: Option<String>,
},
/// Validate that all package manifests are consistent and ready for publishing.
Validate,
}
#[derive(Subcommand)]
enum E2eAction {
/// Generate e2e test projects from fixtures.
Generate {
/// Comma-separated list of languages.
#[arg(long, value_delimiter = ',')]
lang: Option<Vec<String>>,
/// Generate standalone test apps using registry (published) package
/// versions instead of local path dependencies.
#[arg(long)]
registry: bool,
},
/// Initialize fixture directory with schema and example.
Init,
/// Scaffold a new fixture file.
Scaffold {
/// Fixture ID (snake_case).
#[arg(long)]
id: String,
/// Category name.
#[arg(long)]
category: String,
/// Description.
#[arg(long)]
description: String,
},
/// List all fixtures with counts per category.
List,
/// Validate fixture files against the JSON schema.
Validate,
}
#[derive(Subcommand)]
enum CacheAction {
/// Clear the .alef/ cache directory.
Clear,
/// Show cache status.
Status,
}
#[derive(Subcommand)]
enum ValidateAction {
/// Check that all language manifest versions match the Cargo.toml workspace version.
Versions {
/// Output machine-readable JSON.
#[arg(long)]
json: bool,
/// Exit with code 1 if any mismatch is found.
#[arg(long)]
exit_code: bool,
},
}
fn main() -> Result<()> {
let cli = Cli::parse();
init_tracing(cli.verbose, cli.quiet, cli.no_color);
// Configure rayon thread pool based on --jobs flag
if cli.jobs > 0 {
rayon::ThreadPoolBuilder::new()
.num_threads(cli.jobs)
.build_global()
.ok();
}
let config_path = &cli.config;
match cli.command {
Commands::Extract { output } => {
let (_workspace, resolved) = load_config(config_path)?;
let crates_to_process = dispatch::select_crates(&resolved, &cli.crate_filter)?;
let multi = dispatch::is_multi_crate(&crates_to_process);
for resolved_cfg in &crates_to_process {
// For multi-crate: derive per-crate output path so each crate
// gets its own IR file instead of overwriting a shared path.
let effective_output = if multi {
output
.parent()
.unwrap_or(std::path::Path::new("."))
.join(format!("{}.ir.json", resolved_cfg.name))
} else {
output.clone()
};
let api = pipeline::extract(resolved_cfg, config_path, false)?;
if let Some(parent) = effective_output.parent() {
std::fs::create_dir_all(parent)?;
}
std::fs::write(&effective_output, serde_json::to_string_pretty(&api)?)?;
if multi {
eprintln!("[{}] Wrote IR to {}", resolved_cfg.name, effective_output.display());
} else {
println!("Wrote IR to {}", effective_output.display());
}
}
Ok(())
}
Commands::Generate { lang, clean, format } => {
let (workspace, resolved) = load_config(config_path)?;
version_pin::check_alef_toml_version(&workspace)?;
let crates_to_process = dispatch::select_crates(&resolved, &cli.crate_filter)?;
let multi = dispatch::is_multi_crate(&crates_to_process);
let base_dir = std::env::current_dir()?;
let mut grand_total_written: usize = 0;
for resolved_cfg in &crates_to_process {
let languages = resolve_languages(resolved_cfg, lang.as_deref())?;
if multi {
eprintln!(
"[{}] Generating bindings for: {}",
resolved_cfg.name,
format_languages(&languages)
);
} else {
eprintln!("Generating bindings for: {}", format_languages(&languages));
}
let api = pipeline::extract(resolved_cfg, config_path, clean)?;
let files = pipeline::generate(&api, resolved_cfg, &languages, clean)?;
// Pure source-only fingerprint. The embedded `alef:hash:` line in
// every generated file combines this with the file's own (post-format)
// content, so the hash stays stable across alef CLI bumps as long as
// the rust sources and emitted bytes are unchanged.
let sources_hash = cache::sources_hash(&resolved_cfg.sources)?;
// Collect all files generated in this run for cleanup pass
let mut current_gen_paths = std::collections::HashSet::new();
let mut changed_languages: std::collections::HashSet<alef_core::config::Language> =
std::collections::HashSet::new();
let mut total_written: usize = 0;
let mut any_written = false;
for (lang, lang_files) in &files {
let lang_str = lang.to_string();
for file in lang_files {
current_gen_paths.insert(base_dir.join(&file.path));
}
// Per-language up-to-date short-circuit: hash the codegen output
// (pre-format) and compare with the stored hashes from the last
// run. Independent of the embedded `alef:hash:` line, which is
// finalised on-disk after formatters run.
let hashes: Vec<(String, String)> = lang_files
.iter()
.map(|f| {
let normalized = pipeline::normalize_content(&f.path, &f.content);
(
base_dir.join(&f.path).display().to_string(),
cache::hash_content(&normalized),
)
})
.collect();
let cache_key = format!("{}.{lang_str}", resolved_cfg.name);
let stored = cache::read_generation_hashes(&cache_key).unwrap_or_default();
let all_match = !hashes.is_empty() && hashes.iter().all(|(p, h)| stored.get(p) == Some(h));
if all_match && !clean {
eprintln!(" [{lang_str}] up to date (skipping)");
continue;
}
let single = vec![(*lang, lang_files.clone())];
let written = pipeline::write_files(&single, &base_dir)?;
total_written += written;
any_written = true;
changed_languages.insert(*lang);
let _ = cache::write_generation_hashes(&cache_key, &hashes);
}
// Generate public API wrappers — cache by content hash like
// bindings, otherwise we rewrite hundreds of files on every warm
// run for no net change.
if resolved_cfg.generate.public_api {
let public_api_files = pipeline::generate_public_api(&api, resolved_cfg, &languages)?;
if !public_api_files.is_empty() {
let api_hashes: Vec<(String, String)> = public_api_files
.iter()
.flat_map(|(_, fs)| {
fs.iter().map(|f| {
let normalized = pipeline::normalize_content(&f.path, &f.content);
(
base_dir.join(&f.path).display().to_string(),
cache::hash_content(&normalized),
)
})
})
.collect();
let api_cache_key = format!("{}.public_api", resolved_cfg.name);
let stored_api = cache::read_generation_hashes(&api_cache_key).unwrap_or_default();
let api_match =
!api_hashes.is_empty() && api_hashes.iter().all(|(p, h)| stored_api.get(p) == Some(h));
for (_, files) in &public_api_files {
for file in files {
current_gen_paths.insert(base_dir.join(&file.path));
}
}
if !api_match || clean {
let api_count = pipeline::write_files(&public_api_files, &base_dir)?;
eprintln!("Generated {api_count} public API files");
any_written = true;
let _ = cache::write_generation_hashes(&api_cache_key, &api_hashes);
for (lang, _) in &public_api_files {
changed_languages.insert(*lang);
}
} else {
eprintln!(" [public_api] up to date (skipping)");
}
}
}
// Generate type stubs (e.g., .pyi for Python, .d.ts for TypeScript)
let stub_files = pipeline::generate_stubs(&api, resolved_cfg, &languages)?;
if !stub_files.is_empty() {
let stub_hashes: Vec<(String, String)> = stub_files
.iter()
.flat_map(|(_, fs)| {
fs.iter().map(|f| {
(
base_dir.join(&f.path).display().to_string(),
cache::hash_content(&f.content),
)
})
})
.collect();
let stubs_cache_key = format!("{}.stubs", resolved_cfg.name);
let stored_stubs = cache::read_generation_hashes(&stubs_cache_key).unwrap_or_default();
let stubs_match =
!stub_hashes.is_empty() && stub_hashes.iter().all(|(p, h)| stored_stubs.get(p) == Some(h));
// Always register stub paths in `current_gen_paths` so the
// orphan-sweep pass never deletes them when the cache is warm.
for (_, files) in &stub_files {
for file in files {
current_gen_paths.insert(base_dir.join(&file.path));
}
}
if !stubs_match || clean {
let stub_count = pipeline::write_files(&stub_files, &base_dir)?;
eprintln!("Generated {stub_count} type stub files");
any_written = true;
let _ = cache::write_generation_hashes(&stubs_cache_key, &stub_hashes);
for (lang, _) in &stub_files {
// Track stub-changed languages so formatters run even when
// no bindings changed for this language (e.g. ruff on .pyi).
changed_languages.insert(*lang);
}
} else {
eprintln!(" [stubs] up to date (skipping)");
}
}
if let Ok(removed) = pipeline::cleanup_orphaned_files(¤t_gen_paths) {
if removed > 0 {
eprintln!("Removed {removed} stale alef-generated file(s)");
}
}
if any_written && format && !changed_languages.is_empty() {
eprintln!("Formatting generated files...");
// Include stubs in the format pass so that languages where only
// stubs changed (no bindings written) still trigger their
// formatter (e.g. ruff on .pyi). Without this, `format_generated`
// would iterate over `files` (bindings only) and skip the language
// entirely, leaving stub content unformatted before hash finalisation.
let mut files_to_format = files.clone();
files_to_format.extend(stub_files.clone());
pipeline::format_generated(&files_to_format, resolved_cfg, &base_dir, Some(&changed_languages));
let changed_list: Vec<alef_core::config::Language> = changed_languages.iter().copied().collect();
pipeline::fmt_post_generate(resolved_cfg, &changed_list);
}
// Finalise per-file hashes after all formatters have run, so the
// embedded `alef:hash:` line describes the actual on-disk content
// and `alef verify` can recompute it without regenerating.
pipeline::finalize_hashes(¤t_gen_paths, &sources_hash)?;
// Always re-sync versions across user-owned manifests.
if let Err(e) = pipeline::sync_versions(resolved_cfg, config_path, None) {
tracing::warn!("version sync failed: {e}");
}
// Stamp alef.toml with the CLI version that produced this generate.
if let Err(e) = version_pin::write_alef_toml_version(config_path) {
tracing::warn!("could not update alef.toml version pin: {e}");
}
grand_total_written += total_written;
} // end for resolved_cfg in crates_to_process
println!("Generated {grand_total_written} files");
Ok(())
}
Commands::Stubs { lang } => {
let (_workspace, resolved) = load_config(config_path)?;
let crates_to_process = dispatch::select_crates(&resolved, &cli.crate_filter)?;
let multi = dispatch::is_multi_crate(&crates_to_process);
let base_dir = std::env::current_dir()?;
let mut grand_total: usize = 0;
for resolved_cfg in &crates_to_process {
let languages = resolve_languages(resolved_cfg, lang.as_deref())?;
if multi {
eprintln!(
"[{}] Generating type stubs for: {}",
resolved_cfg.name,
format_languages(&languages)
);
} else {
eprintln!("Generating type stubs for: {}", format_languages(&languages));
}
let api = pipeline::extract(resolved_cfg, config_path, false)?;
let files = pipeline::generate_stubs(&api, resolved_cfg, &languages)?;
let sources_hash = cache::sources_hash(&resolved_cfg.sources)?;
// Compute content hashes and compare against stored values; write
// only when something has actually changed.
let hashes: Vec<(String, String)> = files
.iter()
.flat_map(|(_, fs)| {
fs.iter().map(|f| {
(
base_dir.join(&f.path).display().to_string(),
cache::hash_content(&f.content),
)
})
})
.collect();
let cache_key = format!("{}.stubs", resolved_cfg.name);
let stored = cache::read_generation_hashes(&cache_key).unwrap_or_default();
let all_match = !hashes.is_empty() && hashes.iter().all(|(p, h)| stored.get(p) == Some(h));
if all_match {
if multi {
eprintln!("[{}] Stubs up to date (skipping)", resolved_cfg.name);
} else {
println!("Stubs up to date (skipping)");
}
continue;
}
let count = pipeline::write_files(&files, &base_dir)?;
let _ = cache::write_generation_hashes(&cache_key, &hashes);
// Run language-native formatters on the freshly written stubs before
// computing the embedded hash. Without this step, `alef:hash:` is
// computed over the raw codegen output (e.g. with unused `Any` imports
// or brace-heavy PHP style); when host-language tools (ruff, php-cs-fixer,
// mix format, …) reformat those files the hash no longer matches and
// `alef verify` reports them as stale. Formatter failures are warnings —
// they must not abort the stubs command.
let stub_langs: Vec<alef_core::config::Language> = files.iter().map(|(lang, _)| *lang).collect();
pipeline::format_generated(&files, resolved_cfg, &base_dir, None);
pipeline::fmt_post_generate(resolved_cfg, &stub_langs);
// Finalise per-file hashes for the freshly written (and formatted) stubs.
let stub_paths: std::collections::HashSet<PathBuf> = files
.iter()
.flat_map(|(_, fs)| fs.iter().map(|f| base_dir.join(&f.path)))
.collect();
pipeline::finalize_hashes(&stub_paths, &sources_hash)?;
grand_total += count;
} // end for resolved_cfg in crates_to_process
println!("Generated {grand_total} stub files");
Ok(())
}
Commands::Scaffold { lang } => {
let (_workspace, resolved) = load_config(config_path)?;
let crates_to_process = dispatch::select_crates(&resolved, &cli.crate_filter)?;
let multi = dispatch::is_multi_crate(&crates_to_process);
let base_dir = std::env::current_dir()?;
let config_toml = std::fs::read_to_string(config_path)?;
let mut grand_total: usize = 0;
for resolved_cfg in &crates_to_process {
let languages = resolve_languages(resolved_cfg, lang.as_deref())?;
let api = pipeline::extract(resolved_cfg, config_path, false)?;
let ir_json = serde_json::to_string(&api)?;
let stage_hash = cache::compute_stage_hash(&ir_json, "scaffold", &config_toml, &[]);
if cache::is_stage_cached(&resolved_cfg.name, "scaffold", &stage_hash) {
if multi {
eprintln!("[{}] Scaffold up to date (cached)", resolved_cfg.name);
} else {
println!("Scaffold up to date (cached)");
}
continue;
}
if multi {
eprintln!(
"[{}] Generating scaffolding for: {}",
resolved_cfg.name,
format_languages(&languages)
);
} else {
eprintln!("Generating scaffolding for: {}", format_languages(&languages));
}
let files = pipeline::scaffold(&api, resolved_cfg, &languages)?;
let sources_hash = cache::sources_hash(&resolved_cfg.sources)?;
let count = pipeline::write_scaffold_files(&files, &base_dir)?;
let output_paths: Vec<PathBuf> = files.iter().map(|f| base_dir.join(&f.path)).collect();
let scaffold_paths: std::collections::HashSet<PathBuf> = output_paths.iter().cloned().collect();
pipeline::finalize_hashes(&scaffold_paths, &sources_hash)?;
cache::write_stage_hash(&resolved_cfg.name, "scaffold", &stage_hash, &output_paths)?;
grand_total += count;
} // end for resolved_cfg in crates_to_process
println!("Generated {grand_total} scaffold files");
Ok(())
}
Commands::Readme { lang } => {
let (_workspace, resolved) = load_config(config_path)?;
let crates_to_process = dispatch::select_crates(&resolved, &cli.crate_filter)?;
let multi = dispatch::is_multi_crate(&crates_to_process);
let base_dir = std::env::current_dir()?;
let config_toml = std::fs::read_to_string(config_path)?;
let mut grand_total: usize = 0;
for resolved_cfg in &crates_to_process {
let languages = resolve_languages(resolved_cfg, lang.as_deref())?;
let api = pipeline::extract(resolved_cfg, config_path, false)?;
let ir_json = serde_json::to_string(&api)?;
let stage_hash = cache::compute_stage_hash(&ir_json, "readme", &config_toml, &[]);
if cache::is_stage_cached(&resolved_cfg.name, "readme", &stage_hash) {
if multi {
eprintln!("[{}] READMEs up to date (cached)", resolved_cfg.name);
} else {
println!("READMEs up to date (cached)");
}
continue;
}
if multi {
eprintln!(
"[{}] Generating READMEs for: {}",
resolved_cfg.name,
format_languages(&languages)
);
} else {
eprintln!("Generating READMEs for: {}", format_languages(&languages));
}
let files = pipeline::readme(&api, resolved_cfg, &languages)?;
let sources_hash = cache::sources_hash(&resolved_cfg.sources)?;
let count = pipeline::write_scaffold_files_with_overwrite(&files, &base_dir, true)?;
let output_paths: Vec<PathBuf> = files.iter().map(|f| base_dir.join(&f.path)).collect();
let readme_paths: std::collections::HashSet<PathBuf> = output_paths.iter().cloned().collect();
pipeline::finalize_hashes(&readme_paths, &sources_hash)?;
cache::write_stage_hash(&resolved_cfg.name, "readme", &stage_hash, &output_paths)?;
grand_total += count;
} // end for resolved_cfg in crates_to_process
println!("Generated {grand_total} README files");
Ok(())
}
Commands::Docs { lang, output } => {
let (_workspace, resolved) = load_config(config_path)?;
let crates_to_process = dispatch::select_crates(&resolved, &cli.crate_filter)?;
let multi = dispatch::is_multi_crate(&crates_to_process);
let base_dir = std::env::current_dir()?;
let config_toml = std::fs::read_to_string(config_path)?;
let mut grand_total: usize = 0;
for resolved_cfg in &crates_to_process {
let languages = resolve_doc_languages(resolved_cfg, lang.as_deref())?;
// Use filtered IR so docs only cover the public API surface.
let api = pipeline::extract(resolved_cfg, config_path, false)?;
let ir_json = serde_json::to_string(&api)?;
let stage_hash = cache::compute_stage_hash(&ir_json, "docs", &config_toml, &[]);
if cache::is_stage_cached(&resolved_cfg.name, "docs", &stage_hash) {
if multi {
eprintln!("[{}] API docs up to date (cached)", resolved_cfg.name);
} else {
println!("API docs up to date (cached)");
}
continue;
}
if multi {
eprintln!(
"[{}] Generating API docs for: {}",
resolved_cfg.name,
format_languages(&languages)
);
} else {
eprintln!("Generating API docs for: {}", format_languages(&languages));
}
let files = alef_docs::generate_docs(&api, resolved_cfg, &languages, &output)?;
let sources_hash = cache::sources_hash(&resolved_cfg.sources)?;
let count = pipeline::write_scaffold_files_with_overwrite(&files, &base_dir, true)?;
let output_paths: Vec<PathBuf> = files.iter().map(|f| base_dir.join(&f.path)).collect();
let doc_paths: std::collections::HashSet<PathBuf> = output_paths.iter().cloned().collect();
pipeline::finalize_hashes(&doc_paths, &sources_hash)?;
cache::write_stage_hash(&resolved_cfg.name, "docs", &stage_hash, &output_paths)?;
grand_total += count;
} // end for resolved_cfg in crates_to_process
println!("Generated {grand_total} API doc files");
Ok(())
}
Commands::SyncVersions { bump, set } => {
let (_workspace, resolved) = load_config(config_path)?;
let crates_to_process = dispatch::select_crates(&resolved, &cli.crate_filter)?;
let multi = dispatch::is_multi_crate(&crates_to_process);
for resolved_cfg in &crates_to_process {
if let Some(version) = &set {
if multi {
eprintln!("[{}] Setting version to {version}", resolved_cfg.name);
} else {
eprintln!("Setting version to {version}");
}
pipeline::set_version(resolved_cfg, version)?;
}
if multi {
eprintln!("[{}] Syncing versions from Cargo.toml", resolved_cfg.name);
} else {
eprintln!("Syncing versions from Cargo.toml");
}
pipeline::sync_versions(resolved_cfg, config_path, bump.as_deref())?;
}
println!("Version sync complete");
Ok(())
}
Commands::Build { lang, release } => {
let (_workspace, resolved) = load_config(config_path)?;
let crates_to_process = dispatch::select_crates(&resolved, &cli.crate_filter)?;
let multi = dispatch::is_multi_crate(&crates_to_process);
for resolved_cfg in &crates_to_process {
let languages = resolve_languages(resolved_cfg, lang.as_deref())?;
let profile = if release { "release" } else { "dev" };
if multi {
eprintln!(
"[{}] Building bindings ({profile}) for: {}",
resolved_cfg.name,
format_languages(&languages)
);
} else {
eprintln!("Building bindings ({profile}) for: {}", format_languages(&languages));
}
pipeline::build(resolved_cfg, &languages, release)?;
}
println!("Build complete");
Ok(())
}
Commands::Fmt { lang } => {
let (_workspace, resolved) = load_config(config_path)?;
let crates_to_process = dispatch::select_crates(&resolved, &cli.crate_filter)?;
let multi = dispatch::is_multi_crate(&crates_to_process);
for resolved_cfg in &crates_to_process {
let languages = resolve_languages(resolved_cfg, lang.as_deref())?;
if multi {
eprintln!(
"[{}] Formatting generated output for: {}",
resolved_cfg.name,
format_languages(&languages)
);
} else {
eprintln!("Formatting generated output for: {}", format_languages(&languages));
}
pipeline::fmt(resolved_cfg, &languages)?;
}
println!("Format complete");
Ok(())
}
Commands::Lint { lang } => {
let (_workspace, resolved) = load_config(config_path)?;
let crates_to_process = dispatch::select_crates(&resolved, &cli.crate_filter)?;
let multi = dispatch::is_multi_crate(&crates_to_process);
for resolved_cfg in &crates_to_process {
let languages = resolve_languages(resolved_cfg, lang.as_deref())?;
if multi {
eprintln!(
"[{}] Linting generated output for: {}",
resolved_cfg.name,
format_languages(&languages)
);
} else {
eprintln!("Linting generated output for: {}", format_languages(&languages));
}
pipeline::lint(resolved_cfg, &languages)?;
}
println!("Lint complete");
Ok(())
}
Commands::Test { lang, e2e, coverage } => {
let (_workspace, resolved) = load_config(config_path)?;
let crates_to_process = dispatch::select_crates(&resolved, &cli.crate_filter)?;
let multi = dispatch::is_multi_crate(&crates_to_process);
for resolved_cfg in &crates_to_process {
let languages = resolve_test_languages(resolved_cfg, lang.as_deref(), e2e)?;
if multi {
eprintln!(
"[{}] Running tests for: {}",
resolved_cfg.name,
format_languages(&languages)
);
} else {
eprintln!("Running tests for: {}", format_languages(&languages));
}
if e2e {
eprintln!(" (with e2e tests)");
}
if coverage {
eprintln!(" (with coverage)");
}
pipeline::test(resolved_cfg, &languages, e2e, coverage)?;
}
println!("Tests complete");
Ok(())
}
Commands::Setup { lang, timeout } => {
let (_workspace, resolved) = load_config(config_path)?;
let crates_to_process = dispatch::select_crates(&resolved, &cli.crate_filter)?;
let multi = dispatch::is_multi_crate(&crates_to_process);
for resolved_cfg in &crates_to_process {
let languages = resolve_languages(resolved_cfg, lang.as_deref())?;
if multi {
eprintln!(
"[{}] Setting up dependencies for: {}",
resolved_cfg.name,
format_languages(&languages)
);
} else {
eprintln!("Setting up dependencies for: {}", format_languages(&languages));
}
pipeline::setup(resolved_cfg, &languages, timeout)?;
}
println!("Setup complete");
Ok(())
}
Commands::Clean { lang } => {
let (_workspace, resolved) = load_config(config_path)?;
let crates_to_process = dispatch::select_crates(&resolved, &cli.crate_filter)?;
let multi = dispatch::is_multi_crate(&crates_to_process);
for resolved_cfg in &crates_to_process {
let languages = resolve_languages(resolved_cfg, lang.as_deref())?;
if multi {
eprintln!(
"[{}] Cleaning build artifacts for: {}",
resolved_cfg.name,
format_languages(&languages)
);
} else {
eprintln!("Cleaning build artifacts for: {}", format_languages(&languages));
}
pipeline::clean(resolved_cfg, &languages)?;
}
println!("Clean complete");
Ok(())
}
Commands::Update { lang, latest } => {
let (_workspace, resolved) = load_config(config_path)?;
let crates_to_process = dispatch::select_crates(&resolved, &cli.crate_filter)?;
let multi = dispatch::is_multi_crate(&crates_to_process);
for resolved_cfg in &crates_to_process {
let languages = resolve_languages(resolved_cfg, lang.as_deref())?;
let mode = if latest { "latest" } else { "compatible" };
if multi {
eprintln!(
"[{}] Updating dependencies ({mode}) for: {}",
resolved_cfg.name,
format_languages(&languages)
);
} else {
eprintln!("Updating dependencies ({mode}) for: {}", format_languages(&languages));
}
pipeline::update(resolved_cfg, &languages, latest)?;
}
println!("Update complete");
Ok(())
}
Commands::Verify {
exit_code,
compile: _,
lint: _,
lang: _,
} => {
// alef verify is **idempotent across alef versions**: for each
// alef-headered file on disk it recomputes
// `blake3(sources_hash || file_content_without_hash_line)` and
// compares with the embedded `alef:hash:<hex>` line. There is no
// alef-version dimension and no `alef.toml` dimension, so a green
// `alef verify` stays green after upgrading the alef CLI as long
// as the rust sources and on-disk file contents are unchanged.
//
// Verify never regenerates and never writes — pure read+rehash.
// The legacy `--compile` / `--lint` / `--lang` flags are accepted
// but ignored; run `alef build` / `alef lint` / `alef test` for
// those concerns.
let (_workspace, resolved) = load_config(config_path)?;
let crates_to_process = dispatch::select_crates(&resolved, &cli.crate_filter)?;
eprintln!("Verifying alef-generated files (per-file hash mode)");
let base_dir = std::env::current_dir()?;
// Collect sources hashes for all selected crates so that the
// file walk can validate each file against ANY crate's hash.
// A file is valid if it matches at least one crate's sources hash
// (each file was generated by exactly one crate).
let all_sources_hashes: Vec<String> = crates_to_process
.iter()
.filter_map(|c| cache::sources_hash(&c.sources).ok())
.collect();
let stale = verify_walk_multi(&base_dir, &all_sources_hashes)?;
// Version consistency check: run per crate, accumulate mismatches.
let mut all_version_mismatches: Vec<String> = Vec::new();
for resolved_cfg in &crates_to_process {
let mismatches = pipeline::verify_versions(resolved_cfg)?;
all_version_mismatches.extend(mismatches);
}
let has_version_issues = !all_version_mismatches.is_empty();
if has_version_issues {
println!("Version mismatches detected:");
for mismatch in &all_version_mismatches {
println!(" {mismatch}");
}
}
if stale.is_empty() && !has_version_issues {
println!("All bindings and versions are up to date.");
} else {
if !stale.is_empty() {
println!("Stale bindings detected:");
for s in &stale {
println!(" {s}");
}
}
if exit_code {
process::exit(1);
}
}
Ok(())
}
Commands::Diff { exit_code } => {
let (_workspace, resolved) = load_config(config_path)?;
let crates_to_process = dispatch::select_crates(&resolved, &cli.crate_filter)?;
eprintln!("Computing diff of generated bindings...");
let base_dir = std::env::current_dir()?;
let mut all_diffs: Vec<String> = Vec::new();
for resolved_cfg in &crates_to_process {
let languages = resolve_languages(resolved_cfg, None)?;
let api = pipeline::extract(resolved_cfg, config_path, false)?;
let bindings = pipeline::generate(&api, resolved_cfg, &languages, true)?;
let stubs = pipeline::generate_stubs(&api, resolved_cfg, &languages)?;
all_diffs.extend(pipeline::diff_files(&bindings, &base_dir)?);
all_diffs.extend(pipeline::diff_files(&stubs, &base_dir)?);
}
if all_diffs.is_empty() {
println!("No changes detected.");
} else {
println!("Files that would change:");
for diff in &all_diffs {
println!(" {diff}");
}
if exit_code {
process::exit(1);
}
}
Ok(())
}
Commands::All { clean, format } => {
let (workspace, resolved) = load_config(config_path)?;
version_pin::check_alef_toml_version(&workspace)?;
let crates_to_process = dispatch::select_crates(&resolved, &cli.crate_filter)?;
let multi = dispatch::is_multi_crate(&crates_to_process);
let base_dir = std::env::current_dir()?;
let mut grand_binding_count: usize = 0;
let mut grand_stub_count: usize = 0;
let mut grand_api_count: usize = 0;
let mut grand_scaffold_count: usize = 0;
let mut grand_readme_count: usize = 0;
let mut grand_e2e_count: usize = 0;
let mut grand_doc_count: usize = 0;
for resolved_cfg in &crates_to_process {
let languages = resolve_languages(resolved_cfg, None)?;
if multi {
eprintln!(
"[{}] Running all for: {}",
resolved_cfg.name,
format_languages(&languages)
);
} else {
eprintln!("Running all for: {}", format_languages(&languages));
}
let api = pipeline::extract(resolved_cfg, config_path, clean)?;
let sources_hash = cache::sources_hash(&resolved_cfg.sources)?;
// Collect all files generated in this run for cleanup pass
let mut current_gen_paths = std::collections::HashSet::new();
let mut changed_languages: std::collections::HashSet<alef_core::config::Language> =
std::collections::HashSet::new();
eprintln!("Generating bindings...");
let bindings = pipeline::generate(&api, resolved_cfg, &languages, clean)?;
// Per-language: hash content, skip writing if all hashes match.
let mut binding_count: usize = 0;
for (lang, lang_files) in &bindings {
let lang_str = lang.to_string();
for file in lang_files {
current_gen_paths.insert(base_dir.join(&file.path));
}
let hashes: Vec<(String, String)> = lang_files
.iter()
.map(|f| {
(
base_dir.join(&f.path).display().to_string(),
cache::hash_content(&f.content),
)
})
.collect();
let cache_key = format!("{}.{lang_str}", resolved_cfg.name);
let stored = cache::read_generation_hashes(&cache_key).unwrap_or_default();
let all_match = !hashes.is_empty() && hashes.iter().all(|(p, h)| stored.get(p) == Some(h));
if all_match && !clean {
eprintln!(" [{lang_str}] up to date (skipping)");
continue;
}
let single = vec![(*lang, lang_files.clone())];
binding_count += pipeline::write_files(&single, &base_dir)?;
changed_languages.insert(*lang);
let _ = cache::write_generation_hashes(&cache_key, &hashes);
}
eprintln!("Generating type stubs...");
let stubs = pipeline::generate_stubs(&api, resolved_cfg, &languages)?;
let stub_hashes: Vec<(String, String)> = stubs
.iter()
.flat_map(|(_, fs)| {
fs.iter().map(|f| {
(
base_dir.join(&f.path).display().to_string(),
cache::hash_content(&f.content),
)
})
})
.collect();
let stubs_cache_key = format!("{}.stubs", resolved_cfg.name);
let stored_stubs = cache::read_generation_hashes(&stubs_cache_key).unwrap_or_default();
let stubs_match =
!stub_hashes.is_empty() && stub_hashes.iter().all(|(p, h)| stored_stubs.get(p) == Some(h));
let stub_count = if !stubs_match || clean {
let count = pipeline::write_files(&stubs, &base_dir)?;
let _ = cache::write_generation_hashes(&stubs_cache_key, &stub_hashes);
for (lang, _) in &stubs {
// Track stub-changed languages so formatters run even when
// no bindings changed for this language (e.g. ruff on .pyi).
changed_languages.insert(*lang);
}
count
} else {
eprintln!(" [stubs] up to date (skipping)");
0
};
for (_, files) in &stubs {
for file in files {
current_gen_paths.insert(base_dir.join(&file.path));
}
}
let mut api_count = 0;
if resolved_cfg.generate.public_api {
let public_api_files = pipeline::generate_public_api(&api, resolved_cfg, &languages)?;
if !public_api_files.is_empty() {
api_count = pipeline::write_files(&public_api_files, &base_dir)?;
for (_, files) in &public_api_files {
for file in files {
current_gen_paths.insert(base_dir.join(&file.path));
}
}
}
}
eprintln!("Generating scaffolding...");
let scaffold_files = pipeline::scaffold(&api, resolved_cfg, &languages)?;
let scaffold_count = pipeline::write_scaffold_files_with_overwrite(&scaffold_files, &base_dir, clean)?;
for file in &scaffold_files {
current_gen_paths.insert(base_dir.join(&file.path));
}
eprintln!("Generating READMEs...");
let readme_files = pipeline::readme(&api, resolved_cfg, &languages)?;
let readme_count = pipeline::write_scaffold_files_with_overwrite(&readme_files, &base_dir, clean)?;
for file in &readme_files {
current_gen_paths.insert(base_dir.join(&file.path));
}
let mut e2e_count = 0;
if let Some(e2e_config) = &resolved_cfg.e2e {
// Validate that every call config's (module, function) pair is
// actually exported at the declared path in the IR. This catches
// C1 (unexported function) and C2 (wrong definition selected) early
// so codegen never emits an unresolvable use statement.
let all_calls = std::iter::once(("_default", &e2e_config.call))
.chain(e2e_config.calls.iter().map(|(k, v)| (k.as_str(), v)));
for (call_name, call_config) in all_calls {
if call_config.function.is_empty() || call_config.module.is_empty() {
continue;
}
// Derive the Rust module path from the module field:
// replace hyphens with underscores to match rust_path convention.
let module_path = call_config.module.replace('-', "_");
let function_name = &call_config.function;
match alef_extract::validate_call_export(&api, &module_path, function_name) {
alef_extract::ExportValidation::Ok => {}
alef_extract::ExportValidation::NotFound { function } => {
anyhow::bail!(
"e2e call '{call_name}': function '{function}' was not found in the extracted API surface. \
Check that it is declared `pub` and that its source file is listed in \
[[crate.sources]] or [[crate.source_crates]]."
);
}
alef_extract::ExportValidation::WrongPath {
function,
declared_module,
actual_paths,
} => {
let paths = actual_paths.join(", ");
anyhow::bail!(
"e2e call '{call_name}': function '{function}' is not exported at module path \
'{declared_module}' -- the Rust codegen would emit `use {declared_module}::{function};`. \
Actual rust_path(s) found: {paths}. \
Fix: either add `pub use <path>::{function};` at the crate root, \
or update `module` in [e2e.calls.{call_name}] to the correct path."
);
}
}
}
eprintln!("Generating e2e test suites...");
let files = alef_e2e::generate_e2e(resolved_cfg, e2e_config, None)?;
e2e_count = pipeline::write_scaffold_files_with_overwrite(&files, &base_dir, clean)?;
alef_e2e::format::run_formatters(&files, e2e_config);
for file in &files {
current_gen_paths.insert(base_dir.join(&file.path));
}
}
eprintln!("Generating API docs...");
let docs_api = pipeline::extract(resolved_cfg, config_path, false)?;
let doc_languages = resolve_doc_languages(resolved_cfg, None)?;
let doc_files = alef_docs::generate_docs(&docs_api, resolved_cfg, &doc_languages, "docs/reference")?;
let doc_count = pipeline::write_scaffold_files_with_overwrite(&doc_files, &base_dir, clean)?;
for file in &doc_files {
current_gen_paths.insert(base_dir.join(&file.path));
}
if let Ok(removed) = pipeline::cleanup_orphaned_files(¤t_gen_paths) {
if removed > 0 {
eprintln!("Removed {removed} stale alef-generated file(s)");
}
}
// Formatters run by default. They are best-effort: a missing
// formatter or non-zero exit must not abort the pipeline.
// Two passes when enabled:
// 1. `format_generated` runs language-native defaults (cargo fmt,
// ruff format, mix format, oxfmt, etc.) on the freshly
// emitted files.
// 2. `fmt_post_generate` runs any extra repo-configured
// `[lint.<lang>].format` commands (linters, custom passes).
// Both are scoped to languages that actually regenerated this run.
if format && !changed_languages.is_empty() {
eprintln!("Formatting generated files...");
// Include stubs in the format pass so that languages where only
// stubs changed (no bindings written) still trigger their formatter.
let mut files_to_format = bindings.clone();
files_to_format.extend(stubs.clone());
pipeline::format_generated(&files_to_format, resolved_cfg, &base_dir, Some(&changed_languages));
eprintln!("Running formatters...");
let changed_list: Vec<alef_core::config::Language> = changed_languages.iter().copied().collect();
pipeline::fmt_post_generate(resolved_cfg, &changed_list);
}
// Finalise per-file hashes after every formatter has run, so
// `alef verify` can recompute the same hash from on-disk content.
eprintln!("Finalising hashes...");
pipeline::finalize_hashes(¤t_gen_paths, &sources_hash)?;
grand_binding_count += binding_count;
grand_stub_count += stub_count;
grand_api_count += api_count;
grand_scaffold_count += scaffold_count;
grand_readme_count += readme_count;
grand_e2e_count += e2e_count;
grand_doc_count += doc_count;
} // end for resolved_cfg in crates_to_process
// Stamp alef.toml with the CLI version that produced this run.
if let Err(e) = version_pin::write_alef_toml_version(config_path) {
tracing::warn!("could not update alef.toml version pin: {e}");
}
println!(
"Done: {grand_binding_count} binding files, {grand_stub_count} stub files, {grand_api_count} API files, {grand_scaffold_count} scaffold files, {grand_readme_count} readme files, {grand_e2e_count} e2e files, {grand_doc_count} doc files"
);
Ok(())
}
Commands::Init { lang, format } => {
eprintln!("Initializing alef project");
if let Some(langs) = &lang {
eprintln!(" Languages: {}", langs.join(", "));
}
pipeline::init(config_path, lang.clone())?;
eprintln!(" Created alef.toml");
// Load the generated config and bootstrap the project
let (_workspace, resolved) = load_config(config_path)?;
let resolved_cfg = &resolved[0];
let languages = resolve_languages(resolved_cfg, lang.as_deref())?;
let base_dir = std::env::current_dir()?;
// Extract API surface
let api = pipeline::extract(resolved_cfg, config_path, false)?;
let sources_hash = cache::sources_hash(&resolved_cfg.sources)?;
// Generate bindings
eprintln!(" Generating bindings...");
let bindings = pipeline::generate(&api, resolved_cfg, &languages, false)?;
let mut binding_count: usize = 0;
let mut all_paths = std::collections::HashSet::new();
for (lang_key, lang_files) in &bindings {
for file in lang_files {
all_paths.insert(base_dir.join(&file.path));
}
let single = vec![(*lang_key, lang_files.clone())];
binding_count += pipeline::write_files(&single, &base_dir)?;
}
// Scaffold package manifests and lint configs
eprintln!(" Generating scaffolding...");
let scaffold_files = pipeline::scaffold(&api, resolved_cfg, &languages)?;
let scaffold_count = pipeline::write_scaffold_files(&scaffold_files, &base_dir)?;
for file in &scaffold_files {
all_paths.insert(base_dir.join(&file.path));
}
// Format generated code only when --format is requested.
if format {
eprintln!(" Formatting...");
pipeline::fmt_post_generate(resolved_cfg, &languages);
}
// Finalise per-file hashes after formatting.
pipeline::finalize_hashes(&all_paths, &sources_hash)?;
println!("Initialized: {binding_count} binding files, {scaffold_count} scaffold files");
Ok(())
}
Commands::Migrate { path, write } => {
let migrate_path = path.unwrap_or_else(|| cli.config.clone());
let options = commands::migrate::MigrateOptions {
path: migrate_path,
write,
};
commands::migrate::run(options)?;
Ok(())
}
Commands::E2e { action } => {
let (_workspace, resolved) = load_config(config_path)?;
let crates_to_process = dispatch::select_crates(&resolved, &cli.crate_filter)?;
// E2e operates on per-crate e2e config. Use first crate that has an
// e2e section, or error if none has one. For multi-crate workspaces,
// all crates with an e2e section are processed in the loop below.
// The action dispatch still uses the first crate's e2e config for
// non-Generate actions (Init, Scaffold, List, Validate) since those
// are fixture-directory operations.
let resolved_cfg = crates_to_process
.iter()
.find(|c| c.e2e.is_some())
.copied()
.unwrap_or_else(|| crates_to_process[0]);
let e2e_config = resolved_cfg.e2e.as_ref().context("no [e2e] section in alef.toml")?;
match action {
E2eAction::Generate { lang, registry } => {
let config_toml = std::fs::read_to_string(config_path)?;
let base_dir = std::env::current_dir()?;
let mut grand_count: usize = 0;
for e2e_crate in &crates_to_process {
let Some(this_e2e_config) = e2e_crate.e2e.as_ref() else {
continue;
};
let fixtures_dir = std::path::Path::new(&this_e2e_config.fixtures);
let fixture_hash = cache::hash_directory(fixtures_dir).unwrap_or_default();
let api = pipeline::extract(e2e_crate, config_path, false)?;
let ir_json = serde_json::to_string(&api)?;
let cache_key = if registry { "e2e-registry" } else { "e2e" };
let stage_hash = cache::compute_stage_hash(&ir_json, cache_key, &config_toml, &fixture_hash);
if cache::is_stage_cached(&e2e_crate.name, cache_key, &stage_hash) {
println!("E2E tests up to date (cached)");
continue;
}
// When --registry is set, clone the e2e config and switch to
// registry dependency mode so generators emit version-based
// dependencies instead of local paths.
let effective_e2e_config;
let e2e_ref = if registry {
let mut cloned = this_e2e_config.clone();
cloned.dep_mode = alef_core::config::e2e::DependencyMode::Registry;
effective_e2e_config = cloned;
eprintln!("Generating e2e test apps (registry mode)...");
&effective_e2e_config
} else {
eprintln!("Generating e2e test suites...");
this_e2e_config
};
let languages = lang.as_deref();
let files = alef_e2e::generate_e2e(e2e_crate, e2e_ref, languages)?;
let sources_hash = cache::sources_hash(&e2e_crate.sources)?;
let count = pipeline::write_scaffold_files_with_overwrite(&files, &base_dir, true)?;
// Run per-language formatters
alef_e2e::format::run_formatters(&files, e2e_ref);
let output_paths: Vec<PathBuf> = files.iter().map(|f| base_dir.join(&f.path)).collect();
let path_set: std::collections::HashSet<PathBuf> = output_paths.iter().cloned().collect();
pipeline::finalize_hashes(&path_set, &sources_hash)?;
// Sweep orphan alef-generated files. When a --lang filter is
// active, scope the sweep to only the per-language subdirectories
// that were regenerated — sweeping the full e2e root would delete
// other languages' files that were intentionally left on disk.
// Without a filter, sweep the entire e2e output root as before.
let e2e_output_root = if registry {
base_dir.join(&e2e_ref.registry.output)
} else {
base_dir.join(&e2e_ref.output)
};
let sweep_roots: Vec<PathBuf> = if lang.is_some() {
// Derive sweep roots from the top-level subdirectories of the
// e2e output root that appear in the generated file set. Each
// generator writes into `<output>/<lang>/`, so taking the first
// two path components relative to the e2e root gives us the
// per-language directory.
let mut seen = std::collections::HashSet::new();
for path in &output_paths {
if let Ok(rel) = path.strip_prefix(&e2e_output_root) {
if let Some(top) = rel.components().next() {
let lang_dir = e2e_output_root.join(top.as_os_str());
seen.insert(lang_dir);
}
}
}
seen.into_iter().collect()
} else {
vec![e2e_output_root]
};
pipeline::sweep_orphans(&sweep_roots, &path_set)?;
cache::write_stage_hash(&e2e_crate.name, cache_key, &stage_hash, &output_paths)?;
grand_count += count;
}
println!("Generated {grand_count} e2e files");
Ok(())
}
E2eAction::Init => {
eprintln!("Initializing e2e fixtures directory...");
let created = alef_e2e::scaffold::init_fixtures(e2e_config, resolved_cfg)?;
for path in &created {
println!(" created {path}");
}
println!("Initialized {} file(s)", created.len());
Ok(())
}
E2eAction::Scaffold {
id,
category,
description,
} => {
let path =
alef_e2e::scaffold::scaffold_fixture(e2e_config, resolved_cfg, &id, &category, &description)?;
println!("Created {path}");
Ok(())
}
E2eAction::List => {
let fixtures_dir = std::path::Path::new(&e2e_config.fixtures);
let fixtures = alef_e2e::fixture::load_fixtures(fixtures_dir)
.with_context(|| format!("failed to load fixtures from {}", fixtures_dir.display()))?;
let groups = alef_e2e::fixture::group_fixtures(&fixtures);
println!("Fixtures: {} total", fixtures.len());
for group in &groups {
println!(" {}: {} fixture(s)", group.category, group.fixtures.len());
}
Ok(())
}
E2eAction::Validate => {
let fixtures_dir = std::path::Path::new(&e2e_config.fixtures);
eprintln!("Validating fixtures in {}...", fixtures_dir.display());
// Schema validation
let mut all_errors = alef_e2e::validate::validate_fixtures(fixtures_dir)
.with_context(|| format!("failed to validate fixtures from {}", fixtures_dir.display()))?;
// Semantic validation
let fixtures = alef_e2e::fixture::load_fixtures(fixtures_dir)
.with_context(|| format!("failed to load fixtures from {}", fixtures_dir.display()))?;
let semantic_errors =
alef_e2e::validate::validate_fixtures_semantic(&fixtures, e2e_config, &e2e_config.languages);
all_errors.extend(semantic_errors);
if all_errors.is_empty() {
println!("All fixtures are valid.");
Ok(())
} else {
use alef_e2e::validate::Severity;
let error_count = all_errors.iter().filter(|e| e.severity == Severity::Error).count();
let warning_count = all_errors.iter().filter(|e| e.severity == Severity::Warning).count();
println!("Found {} error(s) and {} warning(s):", error_count, warning_count);
for err in &all_errors {
println!(" {err}");
}
if error_count > 0 {
process::exit(1);
}
Ok(())
}
}
}
}
Commands::Publish { action } => {
let (_workspace, resolved) = load_config(config_path)?;
let crates_to_process = dispatch::select_crates(&resolved, &cli.crate_filter)?;
let multi = dispatch::is_multi_crate(&crates_to_process);
match action {
PublishAction::Prepare { lang, target, dry_run } => {
let rust_target = target
.as_deref()
.map(alef_publish::platform::RustTarget::parse)
.transpose()?;
for resolved_cfg in &crates_to_process {
let languages = resolve_languages(resolved_cfg, lang.as_deref())?;
if multi {
eprintln!(
"[{}] Preparing publish for: {}",
resolved_cfg.name,
format_languages(&languages)
);
} else {
eprintln!("Preparing publish for: {}", format_languages(&languages));
}
alef_publish::prepare(resolved_cfg, &languages, rust_target.as_ref(), dry_run)?;
}
println!("Prepare complete");
Ok(())
}
PublishAction::Build {
lang,
target,
use_cross,
} => {
let rust_target = target
.as_deref()
.map(alef_publish::platform::RustTarget::parse)
.transpose()?;
for resolved_cfg in &crates_to_process {
let languages = resolve_languages(resolved_cfg, lang.as_deref())?;
if multi {
eprintln!(
"[{}] Building publish artifacts for: {}",
resolved_cfg.name,
format_languages(&languages)
);
} else {
eprintln!("Building publish artifacts for: {}", format_languages(&languages));
}
alef_publish::build(resolved_cfg, &languages, rust_target.as_ref(), use_cross)?;
}
println!("Build complete");
Ok(())
}
PublishAction::Package {
lang,
target,
output,
version,
dry_run,
php_version,
php_ts,
php_libc,
windows_compiler,
} => {
let rust_target = target
.as_deref()
.map(alef_publish::platform::RustTarget::parse)
.transpose()?;
let output_dir = std::path::Path::new(&output);
for resolved_cfg in &crates_to_process {
let languages = resolve_languages(resolved_cfg, lang.as_deref())?;
let ver = version
.clone()
.or_else(|| resolved_cfg.resolved_version())
.context("could not determine version — set --version or version_from in alef.toml")?;
// Build PHP-specific options when any PHP language is in the list.
let needs_php = languages.contains(&alef_core::config::Language::Php);
let pie_opts: Option<alef_publish::package::php::PiePackageOptions<'_>> = if needs_php {
let php_ver = php_version
.as_deref()
.context("--php-version is required when packaging --lang php")?;
let ts_mode = alef_publish::package::php::TsMode::parse(&php_ts)?;
// Validate: Windows target requires --windows-compiler.
if let Some(ref rt) = rust_target {
if rt.os == alef_publish::platform::Os::Windows && windows_compiler.is_none() {
anyhow::bail!(
"--windows-compiler is required when packaging PHP for a Windows target"
);
}
}
Some(alef_publish::package::php::PiePackageOptions {
php_version: php_ver,
ts_mode,
libc_override: php_libc.as_deref(),
windows_compiler: windows_compiler.as_deref(),
})
} else {
None
};
let pkg_options = alef_publish::PackageOptions { php: pie_opts };
if multi {
eprintln!(
"[{}] Packaging {} (v{ver}) for: {}",
resolved_cfg.name,
output_dir.display(),
format_languages(&languages)
);
} else {
eprintln!(
"Packaging {} (v{ver}) for: {}",
output_dir.display(),
format_languages(&languages)
);
}
alef_publish::package(
resolved_cfg,
&languages,
rust_target.as_ref(),
output_dir,
&ver,
dry_run,
&pkg_options,
)?;
}
println!("Package complete");
Ok(())
}
PublishAction::Validate => {
let mut all_issues: Vec<String> = Vec::new();
for resolved_cfg in &crates_to_process {
let languages = resolve_languages(resolved_cfg, None)?;
let issues = alef_publish::validate(resolved_cfg, &languages)?;
all_issues.extend(issues);
}
if all_issues.is_empty() {
println!("All package manifests are consistent");
} else {
eprintln!("Validation issues:");
for issue in &all_issues {
eprintln!(" - {issue}");
}
anyhow::bail!("{} validation issue(s) found", all_issues.len());
}
Ok(())
}
}
}
Commands::Cache { action } => match action {
CacheAction::Clear => {
cache::clear_cache()?;
println!("Cache cleared.");
Ok(())
}
CacheAction::Status => {
cache::show_status();
Ok(())
}
},
Commands::Validate { action } => match action {
ValidateAction::Versions { json, exit_code } => {
let (_workspace, resolved) = load_config(config_path)?;
let crates_to_process = dispatch::select_crates(&resolved, &cli.crate_filter)?;
let workspace_root = std::env::current_dir()?;
let mut has_mismatches = false;
for resolved_cfg in &crates_to_process {
let checks = commands::validate_versions::run(resolved_cfg, &workspace_root, json)?;
if checks.iter().any(|c| !c.matches) {
has_mismatches = true;
}
}
if has_mismatches && exit_code {
process::exit(1);
}
Ok(())
}
},
Commands::ReleaseMetadata {
tag,
targets,
git_ref,
event,
dry_run,
force_republish,
json: _,
} => {
// Sniff event from env when not provided.
let effective_event = if event.is_empty() {
std::env::var("GITHUB_EVENT_NAME").unwrap_or_default()
} else {
event.clone()
};
let resolved_opt = load_config(config_path).ok().map(|(_ws, r)| r);
// For release metadata, use the first crate matching the filter (or first crate overall).
// This command emits a single JSON object per invocation; multi-crate is an
// unusual case. If the user needs per-crate metadata they can filter with --crate.
let resolved_cfg_opt: Option<&alef_core::config::ResolvedCrateConfig> =
resolved_opt.as_ref().and_then(|r| {
dispatch::select_crates(r, &cli.crate_filter)
.ok()
.and_then(|v| v.into_iter().next())
});
let meta = commands::release_metadata::compute(
&tag,
&targets,
git_ref.as_deref(),
&effective_event,
dry_run,
force_republish,
resolved_cfg_opt,
)?;
println!("{}", meta.to_json()?);
Ok(())
}
Commands::CheckRegistry {
registry,
package,
version,
tap_repo,
repo,
source,
asset_prefix,
required_assets,
json,
} => {
let extra = commands::check_registry::ExtraParams {
nuget_source: source,
tap_repo,
repo,
asset_prefix,
required_assets,
};
commands::check_registry::check(registry, &package, &version, &extra, json)?;
Ok(())
}
Commands::GoTag {
version,
remote,
dry_run,
json,
} => {
let (_workspace, resolved) = load_config(config_path)?;
let crates_to_process = dispatch::select_crates(&resolved, &cli.crate_filter)?;
let workspace_root = std::env::current_dir()?;
for resolved_cfg in &crates_to_process {
let params = commands::go_tag::GoTagParams {
version: &version,
remote: &remote,
dry_run,
output_json: json,
config: resolved_cfg,
workspace_root: &workspace_root,
};
commands::go_tag::run(¶ms)?;
}
Ok(())
}
}
}
fn init_tracing(verbose: u8, quiet: bool, no_color: bool) {
use tracing_subscriber::EnvFilter;
let default_level = if quiet {
"error"
} else {
match verbose {
0 => "info",
1 => "info",
2 => "debug",
_ => "trace",
}
};
let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(default_level));
tracing_subscriber::fmt()
.with_env_filter(filter)
.with_ansi(!no_color)
.with_writer(std::io::stderr)
.without_time()
.with_target(false)
.init();
}
/// Load and resolve an alef.toml, returning the workspace-level config and
/// the per-crate resolved configs. Detects legacy schema and returns an error
/// with a migration hint rather than a confusing parse error.
fn load_config(
path: &std::path::Path,
) -> Result<(
alef_core::config::WorkspaceConfig,
Vec<alef_core::config::ResolvedCrateConfig>,
)> {
let content =
std::fs::read_to_string(path).with_context(|| format!("Failed to read config: {}", path.display()))?;
alef_core::config::detect_legacy_keys(&content).with_context(|| {
format!(
"legacy schema detected in {} — run `alef migrate` to update automatically",
path.display()
)
})?;
let cfg: alef_core::config::NewAlefConfig =
toml::from_str(&content).with_context(|| format!("Failed to parse alef.toml ({})", path.display()))?;
let resolved = cfg
.resolve()
.with_context(|| format!("failed to resolve crates in {}", path.display()))?;
Ok((cfg.workspace, resolved))
}
fn resolve_languages(
config: &alef_core::config::ResolvedCrateConfig,
filter: Option<&[String]>,
) -> Result<Vec<alef_core::config::Language>> {
resolve_languages_inner(config, filter, false)
}
/// Like `resolve_languages` but also allows `rust` regardless of the config languages list.
/// Docs can always be generated for Rust since it's the source language.
fn resolve_doc_languages(
config: &alef_core::config::ResolvedCrateConfig,
filter: Option<&[String]>,
) -> Result<Vec<alef_core::config::Language>> {
resolve_languages_inner(config, filter, true)
}
/// Resolve languages for `alef test`.
///
/// Test suites can exist for targets that do not generate host bindings, such
/// as Rust e2e tests for the source crate. Keep binding language resolution
/// strict for generation/build commands, but allow explicit test targets and
/// include e2e-only entries when `alef test --e2e` runs without a filter.
fn resolve_test_languages(
config: &alef_core::config::ResolvedCrateConfig,
filter: Option<&[String]>,
include_e2e: bool,
) -> Result<Vec<alef_core::config::Language>> {
match filter {
Some(langs) => {
let mut result = vec![];
for lang_str in langs {
let lang = parse_language(lang_str)?;
if config.languages.contains(&lang) || config.test.contains_key(&lang.to_string()) {
result.push(lang);
} else {
anyhow::bail!("Language '{lang_str}' not in config languages list or test configuration");
}
}
Ok(result)
}
None => {
let mut langs = config.languages.clone();
if include_e2e {
let mut extra_test_langs = vec![];
for (lang_str, test_config) in &config.test {
if test_config.e2e.is_none() {
continue;
}
let lang = parse_language(lang_str)
.with_context(|| format!("Invalid test language in alef.toml: {lang_str}"))?;
if !langs.contains(&lang) {
extra_test_langs.push(lang);
}
}
extra_test_langs.sort_by_key(|lang| lang.to_string());
for lang in extra_test_langs {
if !langs.contains(&lang) {
langs.push(lang);
}
}
}
Ok(langs)
}
}
}
fn resolve_languages_inner(
config: &alef_core::config::ResolvedCrateConfig,
filter: Option<&[String]>,
allow_rust: bool,
) -> Result<Vec<alef_core::config::Language>> {
match filter {
Some(langs) => {
let mut result = vec![];
for lang_str in langs {
let lang = parse_language(lang_str)?;
if config.languages.contains(&lang) || (allow_rust && lang == alef_core::config::Language::Rust) {
result.push(lang);
} else {
anyhow::bail!("Language '{lang_str}' not in config languages list");
}
}
Ok(result)
}
None => {
let mut langs = config.languages.clone();
if allow_rust && !langs.contains(&alef_core::config::Language::Rust) {
langs.push(alef_core::config::Language::Rust);
}
Ok(langs)
}
}
}
fn parse_language(lang_str: &str) -> Result<alef_core::config::Language> {
toml::Value::String(lang_str.to_string())
.try_into()
.with_context(|| format!("Unknown language: {lang_str}"))
}
fn format_languages(languages: &[alef_core::config::Language]) -> String {
languages.iter().map(|l| l.to_string()).collect::<Vec<_>>().join(", ")
}
/// Multi-crate variant of [`verify_walk`].
///
/// A file is considered valid if its embedded `alef:hash:` matches the hash
/// computed using ANY of the provided `sources_hashes`. In a multi-crate
/// workspace each file was generated by exactly one crate, so the file passes
/// verification when it matches its generating crate's hash.
fn verify_walk_multi(base_dir: &std::path::Path, sources_hashes: &[String]) -> anyhow::Result<Vec<String>> {
if sources_hashes.is_empty() {
return Ok(Vec::new());
}
if sources_hashes.len() == 1 {
return verify_walk(base_dir, &sources_hashes[0]);
}
const SKIP_DIRS: &[&str] = &[
".git",
".alef",
"target",
"node_modules",
"_build",
"deps",
"parsers",
"dist",
"dist-node",
"vendor",
".venv",
".cache",
".remote-cache",
"__pycache__",
"build",
"tmp",
"out",
".idea",
".vscode",
];
const SCAN_EXTENSIONS: &[&str] = &[
"rs", "py", "pyi", "ts", "tsx", "js", "mjs", "cjs", "rb", "rbs", "php", "phpstub", "go", "java", "cs", "ex",
"exs", "R", "r", "toml", "json", "md", "h", "c", "yaml", "yml",
];
let mut stale: Vec<String> = Vec::new();
let mut stack: Vec<std::path::PathBuf> = vec![base_dir.to_path_buf()];
while let Some(dir) = stack.pop() {
let entries = match std::fs::read_dir(&dir) {
Ok(e) => e,
Err(_) => continue,
};
for entry in entries.flatten() {
let path = entry.path();
let file_type = match entry.file_type() {
Ok(ft) => ft,
Err(_) => continue,
};
if file_type.is_dir() {
let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
if SKIP_DIRS.contains(&name) || name.starts_with('.') {
continue;
}
stack.push(path);
continue;
}
if !file_type.is_file() {
continue;
}
let ext_ok = path
.extension()
.and_then(|e| e.to_str())
.map(|e| SCAN_EXTENSIONS.iter().any(|allowed| allowed.eq_ignore_ascii_case(e)))
.unwrap_or(false);
if !ext_ok {
continue;
}
let content = match std::fs::read_to_string(&path) {
Ok(c) => c,
Err(_) => continue,
};
let Some(disk_hash) = alef_core::hash::extract_hash(&content) else {
continue;
};
// A file is valid if it matches ANY crate's sources hash.
let valid = sources_hashes
.iter()
.any(|sh| alef_core::hash::compute_file_hash(sh, &content) == disk_hash);
if !valid {
stale.push(path.display().to_string());
}
}
}
stale.sort();
Ok(stale)
}
/// Walk the consumer's repo from `base_dir`, find every alef-headered file, and
/// return the list of stale ones — where
/// `compute_file_hash(sources_hash, on_disk_content)` doesn't match the
/// embedded `alef:hash:` line.
///
/// Skips obvious build/cache directories (`target/`, `node_modules/`, `_build/`,
/// `.alef/`, `parsers/`, `dist/`, `vendor/`, `.git/`) so verify stays fast on
/// large repos. Files without the alef header marker are skipped silently —
/// those are user-owned (scaffold-once Cargo.toml templates, composer.json,
/// gemspec, package.json, lockfiles, etc.) and alef has no claim.
fn verify_walk(base_dir: &std::path::Path, sources_hash: &str) -> anyhow::Result<Vec<String>> {
const SKIP_DIRS: &[&str] = &[
".git",
".alef",
"target",
"node_modules",
"_build",
"deps",
"parsers",
"dist",
"dist-node",
"vendor",
".venv",
".cache",
".remote-cache",
"__pycache__",
"build",
"tmp",
"out",
".idea",
".vscode",
];
// Only scan files alef plausibly emits. The check is cheap (extension
// match + read-first-10-lines), but constraining the set keeps the walk
// O(generated files) instead of O(every file in the repo).
const SCAN_EXTENSIONS: &[&str] = &[
"rs", "py", "pyi", "ts", "tsx", "js", "mjs", "cjs", "rb", "rbs", "php", "phpstub", "go", "java", "cs", "ex",
"exs", "R", "r", "toml", "json", "md", "h", "c", "yaml", "yml",
];
let mut stale: Vec<String> = Vec::new();
let mut stack: Vec<std::path::PathBuf> = vec![base_dir.to_path_buf()];
while let Some(dir) = stack.pop() {
let entries = match std::fs::read_dir(&dir) {
Ok(e) => e,
Err(_) => continue,
};
for entry in entries.flatten() {
let path = entry.path();
let file_type = match entry.file_type() {
Ok(ft) => ft,
Err(_) => continue,
};
if file_type.is_dir() {
let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
if SKIP_DIRS.contains(&name) || name.starts_with('.') {
continue;
}
stack.push(path);
continue;
}
if !file_type.is_file() {
continue;
}
let ext_ok = path
.extension()
.and_then(|e| e.to_str())
.map(|e| SCAN_EXTENSIONS.iter().any(|allowed| allowed.eq_ignore_ascii_case(e)))
.unwrap_or(false);
if !ext_ok {
continue;
}
let content = match std::fs::read_to_string(&path) {
Ok(c) => c,
Err(_) => continue,
};
let Some(disk_hash) = alef_core::hash::extract_hash(&content) else {
continue;
};
// Recompute the per-file hash from the on-disk byte content.
// `compute_file_hash` strips the existing `alef:hash:` line so the
// computation is symmetric with the post-format finalisation in
// `pipeline::finalize_hashes`.
let expected = alef_core::hash::compute_file_hash(sources_hash, &content);
if disk_hash != expected {
stale.push(path.display().to_string());
}
}
}
stale.sort();
Ok(stale)
}
#[cfg(test)]
mod tests {
use super::*;
use alef_core::config::Language;
fn resolved_test_config() -> alef_core::config::ResolvedCrateConfig {
let cfg: alef_core::config::NewAlefConfig = toml::from_str(
r#"
[workspace]
languages = ["python"]
[[crates]]
name = "test-lib"
sources = ["src/lib.rs"]
[crates.test.python]
command = "pytest"
[crates.test.rust]
e2e = "cargo test"
"#,
)
.unwrap();
cfg.resolve().unwrap().remove(0)
}
#[test]
fn resolve_test_languages_allows_explicit_test_only_language() {
let config = resolved_test_config();
let langs = resolve_test_languages(&config, Some(&["rust".to_string()]), true).unwrap();
assert_eq!(langs, vec![Language::Rust]);
}
#[test]
fn resolve_test_languages_appends_e2e_only_languages() {
let config = resolved_test_config();
let langs = resolve_test_languages(&config, None, true).unwrap();
assert_eq!(langs, vec![Language::Python, Language::Rust]);
}
#[test]
fn resolve_test_languages_omits_e2e_only_languages_without_e2e() {
let config = resolved_test_config();
let langs = resolve_test_languages(&config, None, false).unwrap();
assert_eq!(langs, vec![Language::Python]);
}
}