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
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
//! Install command implementation
//!
//! This command handles installing bundles from various sources:
//! - Local directory paths
//! - Git repositories (HTTPS/SSH)
//! - GitHub short-form (github:author/repo)
//!
//! The installation process:
//! 1. Initialize or open workspace
//! 2. Acquire workspace lock
//! 3. Parse source and resolve dependencies
//! 4. Detect target platforms
//! 5. Install files with platform transformations
//! 6. Update configuration files
//! 7. Commit transaction (or rollback on error)
use std::collections::HashSet;
use std::path::Path;
use crate::cache;
use crate::cli::InstallArgs;
use crate::commands::menu::{select_bundles_interactively, select_platforms_interactively};
use crate::config::{BundleConfig, BundleDependency, LockedBundle, LockedSource};
use crate::error::{AugentError, Result};
use crate::hash;
use crate::installer::Installer;
use crate::platform::{self, Platform, detection};
use crate::progress::ProgressDisplay;
use crate::resolver::Resolver;
use crate::source::BundleSource;
use crate::transaction::Transaction;
use crate::workspace::Workspace;
use crate::workspace::modified;
use indicatif::{ProgressBar, ProgressStyle};
/// Check if a string looks like a path (contains path separators or relative path indicators)
fn is_path_like(s: &str) -> bool {
s.contains('/') || s.contains('\\') || s.starts_with("./") || s.starts_with("../")
}
/// Run the install command
pub fn run(workspace: Option<std::path::PathBuf>, mut args: InstallArgs) -> Result<()> {
// Get the actual current directory (where the command is being run)
let actual_current_dir = std::env::current_dir().map_err(|e| AugentError::IoError {
message: format!("Failed to get current directory: {}", e),
})?;
// Check if workspace is explicitly provided (CLI flag, not AUGENT_WORKSPACE env var)
let workspace_is_explicit = workspace.is_some();
// Use workspace parameter if provided, otherwise use actual current directory
let current_dir = workspace.unwrap_or(actual_current_dir.clone());
// Check if we're in a subdirectory with no resources
// This happens when:
// 1. User runs from a subdirectory of workspace (actual_current_dir != current_dir)
// 2. The subdirectory has no resources (not a bundle)
// Only apply when we're running from a different directory than current_dir
if actual_current_dir != current_dir {
// Don't treat the .augent directory itself as a bundle directory
let is_augent_dir = actual_current_dir.ends_with(".augent");
if !is_augent_dir {
use crate::installer::Installer;
let has_resources_in_actual_dir = Installer::discover_resources(&actual_current_dir)
.map(|resources| !resources.is_empty())
.unwrap_or(false);
if !has_resources_in_actual_dir {
println!("Nothing to install.");
return Ok(());
}
}
}
// Handle three cases:
// 1. User provides a source (path/URL) - existing behavior
// 2. User provides a bundle name to install by name from workspace
// 3. No source provided - check if we're in a sub-bundle directory
// Track if we're installing by bundle name (for better messaging)
let mut installing_by_bundle_name: Option<String> = None;
// First, check if we're in a sub-bundle directory when no source is provided
if args.source.is_none() {
// Check if we're running from a subdirectory of the workspace
let in_subdirectory = actual_current_dir != current_dir;
if in_subdirectory {
// Don't treat the .augent directory itself as a bundle directory
let is_augent_dir = actual_current_dir.ends_with(".augent");
if !is_augent_dir {
// Check if actual current directory has bundle resources (augent.yaml, commands/, rules/, etc.)
use crate::installer::Installer;
let has_bundle_resources = Installer::discover_resources(&actual_current_dir)
.map(|resources| !resources.is_empty())
.unwrap_or(false);
if has_bundle_resources {
// We're in a bundle directory - install just this bundle and its dependencies
// Use absolute path to the bundle directory
args.source = Some(actual_current_dir.to_string_lossy().to_string());
// Set installing_by_bundle_name to skip workspace bundle during installation
installing_by_bundle_name = Some(
actual_current_dir
.file_name()
.and_then(|n| n.to_str())
.unwrap_or("bundle")
.to_string(),
);
}
}
} else {
// Check if current directory has bundle resources (augent.yaml, commands/, rules/, etc.)
use crate::installer::Installer;
let has_bundle_resources = Installer::discover_resources(¤t_dir)
.map(|resources| !resources.is_empty())
.unwrap_or(false);
if has_bundle_resources {
// Don't treat the workspace root itself as a bundle when there's an existing workspace
// The workspace root might have resources (commands/, skills/, etc.) but if there's an
// existing .augent/ directory with a lockfile, we should use that, not treat root as a bundle.
// Only skip if workspace exists AND we're not providing an explicit source path
let workspace_exists = Workspace::find_from(¤t_dir).is_some();
if !workspace_exists {
// We're in a bundle directory (not a workspace root) - install just this bundle and its dependencies
// Use "." to indicate current directory
args.source = Some(".".to_string());
}
}
}
}
// Now check if we have a source argument and resolve bundle names to paths
if let Some(source_str) = &args.source {
let source_str_ref = source_str.as_str();
// Check if this looks like a path or URL, or a bundle name
if !is_path_like(source_str_ref) {
// Looks like a bundle name - try to find it in workspace
if let Some(workspace_root) = Workspace::find_from(¤t_dir) {
if let Ok(workspace) = Workspace::open(&workspace_root) {
// Look for a bundle with this name in the workspace config
if let Some(bundle_path_str) = workspace
.bundle_config
.bundles
.iter()
.find(|b| b.name == source_str_ref)
.and_then(|b| b.path.clone())
{
// Bundle found - resolve the path relative to config_dir, then make it relative to workspace root
let resolved_path = workspace.config_dir.join(&bundle_path_str);
// Store the bundle name for better messaging
installing_by_bundle_name = Some(source_str_ref.to_string());
// Convert to path relative to workspace root for the resolver
if let Ok(relative_path) = resolved_path.strip_prefix(&workspace_root) {
args.source = Some(relative_path.to_string_lossy().to_string());
} else {
// If it's absolute or can't be made relative, use as-is
args.source = Some(resolved_path.to_string_lossy().to_string());
}
} else {
// Bundle name not found in workspace
// Only error if there are other bundles (meaning the user likely meant to use a bundle name)
let available_bundles: Vec<&str> = workspace
.bundle_config
.bundles
.iter()
.map(|b| b.name.as_str())
.collect();
if !available_bundles.is_empty() {
// There are bundles in the workspace, so this looks like a bundle name that doesn't exist
return Err(AugentError::BundleNotFound {
name: format!(
"Bundle '{}' not found in workspace. Available bundles: {}",
source_str_ref,
available_bundles.join(", ")
),
});
}
// Otherwise, fall through and let normal source parsing handle it (will error as invalid source)
}
}
}
}
}
// Now use the (possibly resolved) source for installation
let source_to_use = args.source.clone();
// If source provided (either directly or inferred), discover and install
if let Some(source_str) = source_to_use {
let source_str = source_str.as_str();
// Parse source and discover bundles BEFORE creating workspace or directory
let source = BundleSource::parse(source_str)?;
// If source is a path and it's not the workspace root itself, skip workspace bundle
// This handles cases like "augent install ./my-bundle" where you only want that bundle, not workspace bundle
if is_path_like(source_str) {
let is_current_dir = source_str == "." || source_str == "./";
if !is_current_dir {
installing_by_bundle_name = Some("".to_string());
}
}
// Print a nice message depending on whether we're installing by name or source
if let Some(ref bundle_name) = installing_by_bundle_name {
println!("Installing {} ({})", bundle_name, source_str);
} else {
println!("Installing from: {}", source.display_url());
}
let resolver = Resolver::new(¤t_dir);
let discovered = resolver.discover_bundles(source_str)?;
// Check if workspace exists to get installed bundle names for menu display
use std::collections::HashSet;
let installed_bundle_names: Option<HashSet<String>> =
if let Some(workspace_root) = Workspace::find_from(¤t_dir) {
if let Ok(workspace) = Workspace::open(&workspace_root) {
// Build a set of discovered bundle names that are already installed
// Match by comparing names: installed bundle names are like "@author/repo/bundle-name"
// while discovered bundle names are just "bundle-name" (from augent.yaml)
let mut installed_names = HashSet::new();
// Get all installed bundle names from lockfile as a HashSet for efficient lookup
let lockfile_bundle_names: HashSet<String> = workspace
.lockfile
.bundles
.iter()
.map(|b| b.name.clone())
.collect();
// For each discovered bundle, check if it matches any installed bundle by name
for discovered in &discovered {
// Check direct name match first
if lockfile_bundle_names.contains(&discovered.name) {
installed_names.insert(discovered.name.clone());
continue;
}
// Check if any installed bundle name ends with the discovered bundle name
// This handles cases like:
// - Installed: "@wshobson/agents/agent-orchestration"
// - Discovered: "agent-orchestration"
if lockfile_bundle_names.iter().any(|installed_name| {
installed_name.ends_with(&format!("/{}", discovered.name))
|| installed_name == &discovered.name
}) {
installed_names.insert(discovered.name.clone());
}
}
Some(installed_names)
} else {
None
}
} else {
None
};
// When installing by bundle name, filter out the workspace bundle itself
// The workspace bundle has a name like "@username/workspace-name"
let discovered = if installing_by_bundle_name.is_some() {
if let Some(workspace_root) = Workspace::find_from(¤t_dir) {
if let Ok(workspace) = Workspace::open(&workspace_root) {
let workspace_name = workspace.get_workspace_name();
// Filter out the workspace bundle from discovered bundles
discovered
.into_iter()
.filter(|b| b.name != workspace_name)
.collect()
} else {
discovered
}
} else {
discovered
}
} else {
discovered
};
// Show interactive menu if multiple bundles, auto-select if one
let discovered_count = discovered.len();
let (selected_bundles, deselected_bundle_names) =
if discovered_count > 1 && !args.all_bundles {
let selection =
select_bundles_interactively(&discovered, installed_bundle_names.as_ref())?;
(selection.selected, selection.deselected)
} else if discovered_count >= 1 {
(discovered, vec![])
} else {
(vec![], vec![]) // No bundles discovered - will be handled in do_install
};
// If user selected nothing from menu (and there were multiple) AND there are
// no deselected installed bundles, exit without creating/updating workspace.
if selected_bundles.is_empty() && discovered_count > 1 && deselected_bundle_names.is_empty()
{
return Ok(());
}
// Something was selected (to install or uninstall) — prompt for platforms if not yet set
// Use workspace root for detection when inside a workspace so we see existing platform dirs
if args.platforms.is_empty() {
let detect_root = Workspace::find_from(¤t_dir).unwrap_or(current_dir.clone());
let detected = if detect_root.exists() {
detection::detect_platforms(&detect_root)?
} else {
vec![]
};
if detected.is_empty() {
let loader = platform::loader::PlatformLoader::new(&detect_root);
let available_platforms = loader.load()?;
if available_platforms.is_empty() {
return Err(AugentError::NoPlatformsDetected);
}
println!("No platforms detected in workspace.");
match select_platforms_interactively(&available_platforms) {
Ok(selected_platforms) => {
if selected_platforms.is_empty() {
println!("No platforms selected. Exiting.");
return Ok(());
}
args.platforms = selected_platforms.iter().map(|p| p.id.clone()).collect();
}
Err(_) => {
return Err(AugentError::NoPlatformsDetected);
}
}
}
}
// Only now create workspace directory (user completed bundle and platform selection)
std::fs::create_dir_all(¤t_dir).map_err(|e| AugentError::IoError {
message: format!("Failed to create workspace directory: {}", e),
})?;
// Initialize or open workspace (after bundle and platform selection)
let mut workspace = Workspace::init_or_open(¤t_dir)?;
// Check if we're installing from a subdirectory that is itself a bundle
// When a source is provided and we're in a subdirectory with bundle resources,
// we should create augent.yaml in that subdirectory, not in workspace's .augent/
if args.source.is_some() && actual_current_dir != current_dir {
// Don't treat the .augent directory itself as a bundle directory
let is_augent_dir = actual_current_dir.ends_with(".augent");
if !is_augent_dir {
use crate::installer::Installer;
let has_bundle_resources = Installer::discover_resources(&actual_current_dir)
.map(|resources| !resources.is_empty())
.unwrap_or(false);
if has_bundle_resources {
// We're installing from a subdirectory that is a bundle
// Set bundle_config_dir to write augent.yaml to this subdirectory
workspace.bundle_config_dir = Some(actual_current_dir.clone());
}
}
}
// If some bundles were deselected that are already installed, handle uninstall FIRST.
// Only if the uninstall succeeds (or is confirmed) do we proceed to install.
if !deselected_bundle_names.is_empty() {
use crate::commands::uninstall;
// Find installed bundle names for deselected bundles
let mut bundles_to_uninstall: Vec<String> = Vec::new();
for bundle_name in &deselected_bundle_names {
// Find the installed bundle name (might be full path like @author/repo/bundle-name)
if let Some(installed_name) = workspace
.lockfile
.bundles
.iter()
.find(|b| {
b.name == *bundle_name || b.name.ends_with(&format!("/{}", bundle_name))
})
.map(|b| b.name.clone())
{
bundles_to_uninstall.push(installed_name);
}
}
if !bundles_to_uninstall.is_empty() {
// Show confirmation prompt unless --dry-run or -y/--yes is given.
// If the user cancels, abort the entire operation before making ANY changes.
if !args.dry_run
&& !args.yes
&& !uninstall::confirm_uninstall(&workspace, &bundles_to_uninstall)?
{
println!("Uninstall cancelled. No changes were made.");
return Ok(());
}
// If there are no bundles selected to install and we're only uninstalling,
// it's clearer to perform uninstall and return without running install logic.
if selected_bundles.is_empty() {
// Create a transaction for uninstall operations
let mut uninstall_transaction = Transaction::new(&workspace);
uninstall_transaction.backup_configs()?;
// Perform uninstallation
let mut failed = false;
for name in &bundles_to_uninstall {
if let Some(locked_bundle) = workspace.lockfile.find_bundle(name) {
// Clone the locked bundle to avoid borrow checker issues
let locked_bundle_clone = locked_bundle.clone();
if let Err(e) = uninstall::do_uninstall(
name,
&mut workspace,
&mut uninstall_transaction,
&locked_bundle_clone,
args.dry_run,
) {
eprintln!("Failed to uninstall {}: {}", name, e);
failed = true;
}
}
}
if failed {
let _ = uninstall_transaction.rollback();
eprintln!("Some bundles failed to uninstall. Changes rolled back.");
return Ok(()); // Don't fail the entire operation, just report the issue
}
// Save workspace after uninstall
if !args.dry_run {
workspace.save()?;
}
// Commit uninstall transaction
uninstall_transaction.commit();
if args.dry_run {
println!(
"[DRY RUN] Would uninstall {} bundle(s)",
bundles_to_uninstall.len()
);
} else {
println!("Uninstalled {} bundle(s)", bundles_to_uninstall.len());
}
return Ok(());
} else {
// We have both deselected (to uninstall) and selected (to install) bundles.
// Perform uninstall first, then continue to installation.
let mut uninstall_transaction = Transaction::new(&workspace);
uninstall_transaction.backup_configs()?;
let mut failed = false;
for name in &bundles_to_uninstall {
if let Some(locked_bundle) = workspace.lockfile.find_bundle(name) {
let locked_bundle_clone = locked_bundle.clone();
if let Err(e) = uninstall::do_uninstall(
name,
&mut workspace,
&mut uninstall_transaction,
&locked_bundle_clone,
args.dry_run,
) {
eprintln!("Failed to uninstall {}: {}", name, e);
failed = true;
}
}
}
if failed {
let _ = uninstall_transaction.rollback();
eprintln!("Some bundles failed to uninstall. Changes rolled back.");
return Ok(()); // Don't proceed to install if uninstall failed
}
if !args.dry_run {
workspace.save()?;
}
uninstall_transaction.commit();
if args.dry_run {
println!(
"[DRY RUN] Would uninstall {} bundle(s) before installing new selection",
bundles_to_uninstall.len()
);
} else {
println!(
"Uninstalled {} bundle(s) before installing new selection",
bundles_to_uninstall.len()
);
}
}
}
}
// Create transaction for atomic operations
let mut transaction = Transaction::new(&workspace);
transaction.backup_configs()?;
// Perform installation
match do_install(
&mut args,
&selected_bundles,
&mut workspace,
&mut transaction,
installing_by_bundle_name.is_some(), // Skip workspace bundle when installing by name
) {
Ok(()) => {
// Commit installation
transaction.commit();
Ok(())
}
Err(e) => Err(e),
}
} else {
// No source provided - check if we're in a sub-bundle directory first
// IMPORTANT: Check if we're in a subdirectory (of workspace root or where workspace would be created) with no resources
// This prevents installing from parent workspace when running from a subdirectory
// Only skip this check when running with --workspace flag OR AUGENT_WORKSPACE env var
if !workspace_is_explicit && std::env::var("AUGENT_WORKSPACE").is_err() {
// Find where the workspace is (or would be)
let workspace_root_opt = Workspace::find_from(¤t_dir);
let workspace_root = workspace_root_opt.as_ref().unwrap_or(¤t_dir);
// Check if we're in a subdirectory with no resources
let in_subdirectory = actual_current_dir != *workspace_root;
if in_subdirectory {
// Don't treat the .augent directory itself as a bundle directory
let is_augent_dir = actual_current_dir.ends_with(".augent");
if !is_augent_dir {
use crate::installer::Installer;
let has_resources_in_actual_dir =
Installer::discover_resources(&actual_current_dir)
.map(|resources| !resources.is_empty())
.unwrap_or(false);
if !has_resources_in_actual_dir {
println!("Nothing to install.");
return Ok(());
}
}
}
}
// No source provided - check if we're in a sub-bundle directory first
let (workspace_root, was_initialized) = match Workspace::find_from(¤t_dir) {
Some(root) => (root, false),
None => {
// No workspace — only create .augent/ if current dir has bundle resources to install
use crate::installer::Installer;
let has_resources_in_current_dir = Installer::discover_resources(¤t_dir)
.map(|resources| !resources.is_empty())
.unwrap_or(false);
if !has_resources_in_current_dir {
println!("Nothing to install.");
return Ok(());
}
let workspace = Workspace::init_or_open(¤t_dir)?;
println!("Initialized .augent/ directory.");
(workspace.root.clone(), true)
}
};
let mut workspace = Workspace::open(&workspace_root)?;
// Check if there are any resources to install BEFORE printing messages or resolving
// Check both augent.yaml bundles and workspace bundle resources
let has_bundles_in_config =
!workspace.bundle_config.bundles.is_empty() || !workspace.lockfile.bundles.is_empty();
let has_workspace_resources = {
use crate::installer::Installer;
let workspace_bundle_path = workspace.get_bundle_source_path();
Installer::discover_resources(&workspace_bundle_path)
.map(|resources| !resources.is_empty())
.unwrap_or(false)
};
// If workspace was just initialized, also check workspace root for local resources
let has_local_resources = if was_initialized {
use crate::installer::Installer;
Installer::discover_resources(&workspace_root)
.map(|resources| !resources.is_empty())
.unwrap_or(false)
} else {
false
};
// If there's nothing to install, show a message and exit (without creating .augent/)
if !has_bundles_in_config && !has_workspace_resources && !has_local_resources {
println!("Nothing to install.");
return Ok(());
}
// Determine which augent.yaml file we're using
let augent_yaml_path = if workspace_root.join("augent.yaml").exists() {
workspace_root.join("augent.yaml")
} else {
workspace_root.join(".augent/augent.yaml")
};
// Calculate relative path for display
let display_path = augent_yaml_path
.strip_prefix(¤t_dir)
.map(|p| p.to_string_lossy().to_string())
.unwrap_or_else(|_| augent_yaml_path.to_string_lossy().to_string());
println!("Augent: Installing bundles from {}", display_path);
// Create transaction for atomic operations
let mut transaction = Transaction::new(&workspace);
transaction.backup_configs()?;
// Install all bundles from augent.yaml
match do_install_from_yaml(
&mut args,
&mut workspace,
&mut transaction,
was_initialized,
has_local_resources,
has_workspace_resources,
) {
Ok(()) => {
transaction.commit();
Ok(())
}
Err(e) => Err(e),
}
}
}
/// Install bundles from augent.yaml
fn do_install_from_yaml(
args: &mut InstallArgs,
workspace: &mut Workspace,
transaction: &mut Transaction,
was_initialized: bool,
has_local_resources: bool,
has_workspace_resources: bool,
) -> Result<()> {
// Detect and preserve any modified files before reinstalling bundles
let cache_dir = cache::bundles_cache_dir()?;
let modified_files = modified::detect_modified_files(workspace, &cache_dir)?;
let mut has_modified_files = false;
if !modified_files.is_empty() {
has_modified_files = true;
println!(
"Detected {} modified file(s). Preserving changes...",
modified_files.len()
);
modified::preserve_modified_files(workspace, &modified_files)?;
}
let augent_yaml_missing =
workspace.bundle_config.bundles.is_empty() && !workspace.lockfile.bundles.is_empty();
if augent_yaml_missing {
println!(
"augent.yaml is missing but augent.lock contains {} bundle(s).",
workspace.lockfile.bundles.len()
);
println!("Reconstructing augent.yaml from augent.lock...");
// Reconstruct augent.yaml from lockfile
reconstruct_augent_yaml_from_lockfile(workspace)?;
}
// Backup the original lockfile - we'll restore it if --update was not given
let original_lockfile = workspace.lockfile.clone();
// If --update is given, resolve new SHAs and update lockfile
// Otherwise, use lockfile (fast, reproducible, respects exact SHAs)
// but automatically fetch missing bundles from cache
let (resolved_bundles, should_update_lockfile) = if args.update {
println!("Checking for updates...");
let mut resolver = Resolver::new(&workspace.root);
// Resolve workspace bundle which will automatically resolve its declared dependencies
// from augent.yaml. All bundles are treated uniformly by the resolver.
// Use root augent.yaml if it exists, otherwise fall back to .augent
let bundle_sources = vec![workspace.get_config_source_path()];
println!("Resolving workspace bundle and its dependencies...");
// Show progress while resolving dependencies
let pb = if !args.dry_run {
let pb = ProgressBar::new_spinner();
pb.set_style(
ProgressStyle::default_spinner()
.template("{spinner} Resolving dependencies...")
.unwrap()
.tick_strings(&["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]),
);
pb.enable_steady_tick(std::time::Duration::from_millis(80));
Some(pb)
} else {
None
};
// Resolve all bundles uniformly through the resolver
let resolved = resolver.resolve_multiple(&bundle_sources)?;
if let Some(pb) = pb {
pb.finish_and_clear();
}
if resolved.is_empty() {
return Err(AugentError::BundleNotFound {
name: "No bundles found in augent.yaml".to_string(),
});
}
println!("Resolved {} bundle(s)", resolved.len());
(resolved, true) // Mark that we should update lockfile
} else {
// Use lockfile - respects exact SHAs, but fetches missing bundles from cache
// If lockfile is empty/doesn't exist, automatically create it
// Also automatically detect if augent.yaml has changed
let lockfile_is_empty = workspace.lockfile.bundles.is_empty();
// If we just reconstructed augent.yaml from lockfile, don't treat it as a change
// (it's not really a change, just a recovery of the previous state)
let augent_yaml_changed = if augent_yaml_missing {
false // We just reconstructed from lockfile, so it's not a "change"
} else {
!lockfile_is_empty && has_augent_yaml_changed(workspace)?
};
let resolved = if lockfile_is_empty || augent_yaml_changed {
// Lockfile doesn't exist, is empty, or augent.yaml has changed - resolve dependencies
if lockfile_is_empty {
println!("Lockfile not found or empty. Resolving dependencies...");
} else {
println!("augent.yaml has changed. Re-resolving dependencies...");
}
let mut resolver = Resolver::new(&workspace.root);
// Resolve workspace bundle which will automatically resolve its declared dependencies
// from augent.yaml. All bundles are treated uniformly by the resolver.
// Use root augent.yaml if it exists, otherwise fall back to .augent
// If workspace was just initialized with local resources, resolve from root to discover them
let bundle_sources = if was_initialized && has_local_resources {
vec![".".to_string()]
} else {
vec![workspace.get_config_source_path()]
};
println!("Resolving workspace bundle and its dependencies...");
// Show progress while resolving dependencies
let pb = if !args.dry_run {
let pb = ProgressBar::new_spinner();
pb.set_style(
ProgressStyle::default_spinner()
.template("{spinner} Resolving dependencies...")
.unwrap()
.tick_strings(&["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]),
);
pb.enable_steady_tick(std::time::Duration::from_millis(80));
Some(pb)
} else {
None
};
// Resolve all bundles uniformly through the resolver
let resolved = resolver.resolve_multiple(&bundle_sources)?;
if let Some(pb) = pb {
pb.finish_and_clear();
}
if resolved.is_empty() {
return Err(AugentError::BundleNotFound {
name: "No bundles found in augent.yaml".to_string(),
});
}
println!("Resolved {} bundle(s)", resolved.len());
resolved
} else {
// Lockfile exists and matches augent.yaml - use it, but fetch missing bundles from cache
println!("Using locked versions from augent.lock.");
let resolved =
locked_bundles_to_resolved(&workspace.lockfile.bundles, &workspace.root)?;
if resolved.is_empty() {
return Err(AugentError::BundleNotFound {
name: "No bundles found in augent.lock".to_string(),
});
}
println!("Prepared {} bundle(s)", resolved.len());
resolved
};
// Fix workspace bundle name: ensure it uses the workspace bundle name, not the directory name
// This handles the case where the workspace bundle is in .augent/ and was named after the dir
// OR when it's resolved from "." (workspace root) and named after the directory
let mut resolved_bundles = resolved;
let workspace_bundle_name = workspace.get_workspace_name();
for bundle in &mut resolved_bundles {
// Check if this is the workspace bundle by checking if its source path matches
let bundle_source_path = workspace.get_bundle_source_path();
let is_workspace_bundle = bundle.source_path == bundle_source_path // .augent dir
|| bundle.source_path == workspace.root; // workspace root (when resolving from ".")
if is_workspace_bundle && bundle.name != workspace_bundle_name {
// This is the workspace bundle but it has the wrong name (probably derived from directory)
// Rename it to use the workspace bundle name
bundle.name = workspace_bundle_name.clone();
}
}
// Update lockfile if --update was given OR if lockfile was empty/changed
(
resolved_bundles,
args.update || lockfile_is_empty || augent_yaml_changed,
)
};
// If we detected modified files OR if workspace has resources, ensure workspace bundle is in the resolved list
// (append LAST so it overrides other bundles)
let mut final_resolved_bundles = resolved_bundles;
let workspace_bundle_name = workspace.get_workspace_name();
if (has_modified_files || has_workspace_resources)
&& !final_resolved_bundles
.iter()
.any(|b| b.name == workspace_bundle_name)
{
let workspace_bundle = crate::resolver::ResolvedBundle {
name: workspace_bundle_name,
dependency: None,
source_path: workspace.get_bundle_source_path(),
resolved_sha: None,
resolved_ref: None,
git_source: None,
config: None,
};
final_resolved_bundles.push(workspace_bundle);
}
let resolved_bundles = final_resolved_bundles;
// Check if any resolved bundles have resources to install
// Only proceed with installation if there are actual resources to install
let has_resources_to_install = resolved_bundles.iter().any(|bundle| {
use crate::installer::Installer;
Installer::discover_resources(&bundle.source_path)
.map(|resources| !resources.is_empty())
.unwrap_or(false)
});
// If there are no resources to install, exit early (don't install for any platforms)
// This applies whether workspace was just initialized or not
if !has_resources_to_install {
// Don't print anything - user's requirement: "it should not say or do anything about the platforms"
return Ok(());
}
// Detect target platforms
// If no platforms detected and no --to flag provided, show platform selection menu
// Skip platform prompt if workspace was just initialized (use all platforms)
if args.platforms.is_empty() && !was_initialized {
let detected = detection::detect_platforms(&workspace.root)?;
if detected.is_empty() {
// No platforms detected - show menu to select platforms
let loader = platform::loader::PlatformLoader::new(&workspace.root);
let available_platforms = loader.load()?;
if available_platforms.is_empty() {
return Err(AugentError::NoPlatformsDetected);
}
println!("No platforms detected in workspace.");
match select_platforms_interactively(&available_platforms) {
Ok(selected_platforms) => {
if selected_platforms.is_empty() {
println!("No platforms selected. Exiting.");
return Ok(());
}
// Convert selected platforms to IDs
args.platforms = selected_platforms.iter().map(|p| p.id.clone()).collect();
}
Err(_) => {
// Non-interactive environment - require --to flag instead of silently using all platforms
return Err(AugentError::NoPlatformsDetected);
}
}
}
}
let platforms = match detect_target_platforms(&workspace.root, &args.platforms) {
Ok(p) => p,
Err(AugentError::NoPlatformsDetected) if args.platforms.is_empty() => {
// e.g. workspace just initialized (was_initialized) or no platform dirs yet
let loader = platform::loader::PlatformLoader::new(&workspace.root);
let available_platforms = loader.load()?;
if available_platforms.is_empty() {
return Err(AugentError::NoPlatformsDetected);
}
println!("No platforms detected in workspace.");
match select_platforms_interactively(&available_platforms) {
Ok(selected_platforms) => {
if selected_platforms.is_empty() {
println!("No platforms selected. Exiting.");
return Ok(());
}
args.platforms = selected_platforms.iter().map(|p| p.id.clone()).collect();
detect_target_platforms(&workspace.root, &args.platforms)?
}
Err(_) => return Err(AugentError::NoPlatformsDetected),
}
}
Err(e) => return Err(e),
};
if platforms.is_empty() {
return Err(AugentError::NoPlatformsDetected);
}
if args.dry_run {
println!(
"[DRY RUN] Would install for {} platform(s): {}",
platforms.len(),
platforms
.iter()
.map(|p| p.id.as_str())
.collect::<Vec<_>>()
.join(", ")
);
} else {
println!(
"Installing for {} platform(s): {}",
platforms.len(),
platforms
.iter()
.map(|p| p.id.as_str())
.collect::<Vec<_>>()
.join(", ")
);
}
// Check --frozen flag
if args.frozen {
// Verify that lockfile wouldn't change
let new_lockfile = generate_lockfile(workspace, &resolved_bundles)?;
if !workspace.lockfile.equals(&new_lockfile) {
return Err(AugentError::LockfileOutdated);
}
}
// Install files
if args.dry_run {
println!("[DRY RUN] Would install files...");
}
let workspace_root = workspace.root.clone();
// Create progress display if not in dry-run mode
let mut progress_display = if !args.dry_run && !resolved_bundles.is_empty() {
Some(ProgressDisplay::new(resolved_bundles.len() as u64))
} else {
None
};
let (workspace_bundles_result, installed_files_map) = {
let mut installer = if let Some(ref mut progress) = progress_display {
Installer::new_with_progress(
&workspace_root,
platforms.clone(),
args.dry_run,
Some(progress),
)
} else {
Installer::new_with_dry_run(&workspace_root, platforms.clone(), args.dry_run)
};
let result = installer.install_bundles(&resolved_bundles);
let installed_files = installer.installed_files().clone();
(result, installed_files)
};
// Handle progress display completion (after installer is dropped)
if let Some(ref mut progress) = progress_display {
match &workspace_bundles_result {
Ok(_) => {
progress.finish_files();
}
Err(_) => {
progress.abandon();
}
}
}
let workspace_bundles = workspace_bundles_result?;
// Track created files in transaction
for installed in installed_files_map.values() {
for target in &installed.target_paths {
let full_path = workspace_root.join(target);
transaction.track_file_created(full_path);
}
}
// Update configuration files
if args.dry_run {
println!("[DRY RUN] Would update configuration files...");
} else {
println!("Updating configuration files...");
}
// Filter out workspace bundles that have no files (nothing actually installed for them)
let workspace_bundles_with_files: Vec<_> = workspace_bundles
.into_iter()
.filter(|wb| !wb.enabled.is_empty())
.collect();
// Only update configurations if changes were made:
// - If --update flag was given (lockfile needs updating), OR
// - If files were actually installed (workspace_bundles has entries with files), OR
// - If modified files were detected and preserved, OR
// - If workspace has resources that need to be installed
let configs_updated = should_update_lockfile
|| !workspace_bundles_with_files.is_empty()
|| has_modified_files
|| has_workspace_resources;
// No longer need to check lockfile name (it's no longer stored in lockfile)
if configs_updated && !args.dry_run {
// Set flag to create augent.yaml during workspace bundle install
workspace.should_create_augent_yaml = true;
update_configs_from_yaml(
workspace,
&resolved_bundles,
workspace_bundles_with_files,
should_update_lockfile,
)?;
}
// If --update was not given, restore the original lockfile (don't modify it)
// UNLESS modified files were detected OR workspace has resources, in which case keep the workspace bundle entry
if !should_update_lockfile {
if has_modified_files || has_workspace_resources {
// Keep the workspace bundle entry, but restore everything else
let workspace_bundle_name = workspace.get_workspace_name();
if let Some(workspace_bundle_entry) = workspace
.lockfile
.find_bundle(&workspace_bundle_name)
.cloned()
{
workspace.lockfile = original_lockfile;
workspace.lockfile.add_bundle(workspace_bundle_entry);
} else {
workspace.lockfile = original_lockfile;
}
} else {
workspace.lockfile = original_lockfile;
}
}
// Check if workspace config is missing or empty - if so, rebuild it by scanning filesystem
let needs_rebuild =
workspace.workspace_config.bundles.is_empty() && !workspace.lockfile.bundles.is_empty();
// Save workspace if configurations were updated
let needs_save = configs_updated;
if needs_save && !args.dry_run {
println!("Saving workspace...");
workspace.save()?;
} else if needs_save && args.dry_run {
println!("[DRY RUN] Would save workspace...");
}
// After saving, if workspace config was empty, rebuild it by scanning the filesystem
if needs_rebuild {
println!("Rebuilding workspace configuration from installed files...");
workspace.rebuild_workspace_config()?;
}
// Print summary
let total_files: usize = installed_files_map
.values()
.map(|f| f.target_paths.len())
.sum();
println!(
"Installed {} bundle(s), {} file(s)",
resolved_bundles.len(),
total_files
);
for bundle in &resolved_bundles {
println!(" - {}", bundle.name);
// Show files installed for this bundle
for (bundle_path, installed) in &installed_files_map {
// Group by resource type for cleaner display
if bundle_path.starts_with(&bundle.name)
|| bundle_path.contains(&bundle.name.replace('@', ""))
{
println!(
" {} ({})",
installed.bundle_path, installed.resource_type
);
}
}
}
Ok(())
}
/// Perform the actual installation
fn do_install(
args: &mut InstallArgs,
selected_bundles: &[crate::resolver::DiscoveredBundle],
workspace: &mut Workspace,
transaction: &mut Transaction,
skip_workspace_bundle: bool,
) -> Result<()> {
// Detect and preserve any modified files before reinstalling bundles
let cache_dir = cache::bundles_cache_dir()?;
let modified_files = modified::detect_modified_files(workspace, &cache_dir)?;
let mut has_modified_files = false;
if !modified_files.is_empty() {
has_modified_files = true;
println!(
"Detected {} modified file(s). Preserving changes...",
modified_files.len()
);
modified::preserve_modified_files(workspace, &modified_files)?;
}
let mut resolver = Resolver::new(&workspace.root);
// Show progress while resolving bundles and their dependencies
let pb = if !args.dry_run {
let pb = ProgressBar::new_spinner();
pb.set_style(
ProgressStyle::default_spinner()
.template("{spinner} Resolving bundles and dependencies...")
.unwrap()
.tick_strings(&["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]),
);
pb.enable_steady_tick(std::time::Duration::from_millis(80));
Some(pb)
} else {
None
};
let mut resolved_bundles = (|| -> Result<Vec<crate::resolver::ResolvedBundle>> {
if selected_bundles.is_empty() {
// No bundles discovered - resolve source directly (might be a bundle itself)
let source_str = args.source.as_ref().unwrap().as_str();
resolver.resolve(source_str, false)
} else if selected_bundles.len() == 1 {
// Single bundle found
// Check if discovered bundle has git source info
if let Some(ref git_source) = selected_bundles[0].git_source {
// Use GitSource directly (already has resolved_sha from discovery)
// This avoids re-cloning the repository
Ok(vec![resolver.resolve_git(git_source, None, false)?])
} else {
// Local directory, use discovered path
let bundle_path = selected_bundles[0].path.to_string_lossy().to_string();
resolver.resolve_multiple(&[bundle_path])
}
} else {
// Multiple bundles selected - check if any have git source
let has_git_source = selected_bundles.iter().any(|b| b.git_source.is_some());
if has_git_source {
// For git sources, resolve each bundle with its specific subdirectory
let mut all_bundles = Vec::new();
for discovered in selected_bundles {
if let Some(ref git_source) = discovered.git_source {
// Use GitSource directly (already has resolved_sha from discovery)
// This avoids re-cloning the repository
let bundle = resolver.resolve_git(git_source, None, false)?;
all_bundles.push(bundle);
} else {
// Local directory
let bundle_path = discovered.path.to_string_lossy().to_string();
let bundles = resolver.resolve_multiple(&[bundle_path])?;
all_bundles.extend(bundles);
}
}
Ok(all_bundles)
} else {
// All local directories
let selected_paths: Vec<String> = selected_bundles
.iter()
.map(|b| b.path.to_string_lossy().to_string())
.collect();
resolver.resolve_multiple(&selected_paths)
}
}
})()?;
if let Some(pb) = pb {
pb.finish_and_clear();
}
// Fix workspace bundle name: ensure it uses the workspace bundle name, not the directory name
// This handles the case where the workspace bundle is in .augent/ and was named after the dir
// OR when it's resolved from "." (workspace root) and named after the directory
let workspace_bundle_name = workspace.get_workspace_name();
for bundle in &mut resolved_bundles {
// Check if this is the workspace bundle by checking if its source path matches
let bundle_source_path = workspace.get_bundle_source_path();
let is_workspace_bundle = bundle.source_path == bundle_source_path // .augent dir
|| bundle.source_path == workspace.root; // workspace root (when resolving from ".")
if is_workspace_bundle && bundle.name != workspace_bundle_name {
// This is the workspace bundle but it has the wrong name (probably derived from directory)
// Rename it to use the workspace bundle name
bundle.name = workspace_bundle_name.clone();
}
}
// If we detected modified files, ensure workspace bundle is in the resolved list
// UNLESS we're installing a specific bundle by name (in which case skip the workspace bundle)
if has_modified_files
&& !skip_workspace_bundle
&& !resolved_bundles
.iter()
.any(|b| b.name == workspace_bundle_name)
{
let workspace_bundle = crate::resolver::ResolvedBundle {
name: workspace_bundle_name.clone(),
dependency: None,
source_path: workspace.get_bundle_source_path(),
resolved_sha: None,
resolved_ref: None,
git_source: None,
config: None,
};
resolved_bundles.push(workspace_bundle);
}
// Also filter out the workspace bundle from resolved_bundles if we're installing by bundle name
if skip_workspace_bundle {
resolved_bundles.retain(|b| b.name != workspace_bundle_name);
}
if resolved_bundles.is_empty() {
let source_display = args.source.as_deref().unwrap_or("unknown");
return Err(AugentError::BundleNotFound {
name: format!("No bundles found at source '{}'", source_display),
});
}
// Detect target platforms
let platforms = match detect_target_platforms(&workspace.root, &args.platforms) {
Ok(p) => p,
Err(AugentError::NoPlatformsDetected) if args.platforms.is_empty() => {
let loader = platform::loader::PlatformLoader::new(&workspace.root);
let available_platforms = loader.load()?;
if available_platforms.is_empty() {
return Err(AugentError::NoPlatformsDetected);
}
println!("No platforms detected in workspace.");
match select_platforms_interactively(&available_platforms) {
Ok(selected_platforms) => {
if selected_platforms.is_empty() {
println!("No platforms selected. Exiting.");
return Err(AugentError::NoPlatformsDetected);
}
args.platforms = selected_platforms.iter().map(|p| p.id.clone()).collect();
detect_target_platforms(&workspace.root, &args.platforms)?
}
Err(_) => return Err(AugentError::NoPlatformsDetected),
}
}
Err(e) => return Err(e),
};
if platforms.is_empty() {
return Err(AugentError::NoPlatformsDetected);
}
if args.dry_run {
println!(
"[DRY RUN] Would install for {} platform(s): {}",
platforms.len(),
platforms
.iter()
.map(|p| p.id.as_str())
.collect::<Vec<_>>()
.join(", ")
);
} else {
println!(
"Installing for {} platform(s): {}",
platforms.len(),
platforms
.iter()
.map(|p| p.id.as_str())
.collect::<Vec<_>>()
.join(", ")
);
}
// Check --frozen flag
if args.frozen {
// Verify that lockfile wouldn't change
let new_lockfile = generate_lockfile(workspace, &resolved_bundles)?;
if !workspace.lockfile.equals(&new_lockfile) {
return Err(AugentError::LockfileOutdated);
}
}
// Install files
if args.dry_run {
println!("[DRY RUN] Would install files...");
}
let workspace_root = workspace.root.clone();
// Create progress display if not in dry-run mode
let mut progress_display = if !args.dry_run && !resolved_bundles.is_empty() {
Some(ProgressDisplay::new(resolved_bundles.len() as u64))
} else {
None
};
let (workspace_bundles_result, installed_files_map) = {
let mut installer = if let Some(ref mut progress) = progress_display {
Installer::new_with_progress(
&workspace_root,
platforms.clone(),
args.dry_run,
Some(progress),
)
} else {
Installer::new_with_dry_run(&workspace_root, platforms.clone(), args.dry_run)
};
let result = installer.install_bundles(&resolved_bundles);
let installed_files = installer.installed_files().clone();
(result, installed_files)
};
// Handle progress display completion (after installer is dropped)
if let Some(ref mut progress) = progress_display {
match &workspace_bundles_result {
Ok(_) => {
progress.finish_files();
}
Err(_) => {
progress.abandon();
}
}
}
let workspace_bundles = workspace_bundles_result?;
// Track created files in transaction
for installed in installed_files_map.values() {
for target in &installed.target_paths {
let full_path = workspace_root.join(target);
transaction.track_file_created(full_path);
}
}
let has_git_bundles = resolved_bundles.iter().any(|b| b.git_source.is_some());
let should_update_augent_yaml = has_git_bundles || !skip_workspace_bundle;
let source_str = args.source.as_deref().unwrap_or("");
if args.dry_run {
println!("[DRY RUN] Would update configuration files...");
} else {
// Set flag to create/update augent.yaml during bundle install
workspace.should_create_augent_yaml = should_update_augent_yaml;
update_configs(
workspace,
source_str,
&resolved_bundles,
workspace_bundles,
should_update_augent_yaml,
)?;
}
if args.dry_run {
println!("[DRY RUN] Would save workspace...");
} else {
workspace.save()?;
}
// Print summary
let total_files: usize = installed_files_map
.values()
.map(|f| f.target_paths.len())
.sum();
if args.dry_run {
println!(
"[DRY RUN] Would install {} bundle(s), {} file(s)",
resolved_bundles.len(),
total_files
);
} else {
println!(
"Installed {} bundle(s), {} file(s)",
resolved_bundles.len(),
total_files
);
}
for bundle in &resolved_bundles {
println!(" - {}", bundle.name);
// Show files installed for this bundle
for (bundle_path, installed) in &installed_files_map {
// Group by resource type for cleaner display
// Note: installed_files contains all bundles, so we check if this bundle_path
// belongs to the current bundle's source_path
if bundle_path.starts_with(&bundle.name)
|| bundle_path.contains(&bundle.name.replace('@', ""))
{
println!(
" {} ({})",
installed.bundle_path, installed.resource_type
);
}
}
}
Ok(())
}
/// Detect target platforms based on workspace and --to flag.
/// When no platforms are specified and none are detected, returns NoPlatformsDetected
/// so the caller can prompt the user (e.g. interactive menu) instead of installing to all platforms.
fn detect_target_platforms(workspace_root: &Path, platforms: &[String]) -> Result<Vec<Platform>> {
if platforms.is_empty() {
let detected = detection::detect_platforms(workspace_root)?;
if detected.is_empty() {
return Err(AugentError::NoPlatformsDetected);
}
Ok(detected)
} else {
detection::get_platforms(platforms, Some(workspace_root))
}
}
/// Generate a new lockfile from resolved bundles
fn generate_lockfile(
workspace: &Workspace,
resolved_bundles: &[crate::resolver::ResolvedBundle],
) -> Result<crate::config::Lockfile> {
let mut lockfile = crate::config::Lockfile::new();
for bundle in resolved_bundles {
let locked_bundle = create_locked_bundle(bundle, Some(&workspace.root))?;
lockfile.add_bundle(locked_bundle);
}
Ok(lockfile)
}
/// Create a LockedBundle from a ResolvedBundle
fn create_locked_bundle(
bundle: &crate::resolver::ResolvedBundle,
workspace_root: Option<&Path>,
) -> Result<LockedBundle> {
// Discover files in the bundle
let resources = Installer::discover_resources(&bundle.source_path)?;
// Normalize paths to always use forward slashes (Unix-style) for cross-platform consistency
let files: Vec<String> = resources
.iter()
.map(|r| r.bundle_path.to_string_lossy().replace('\\', "/"))
.collect();
// Calculate hash
let bundle_hash = hash::hash_directory(&bundle.source_path)?;
let source = if let Some(git_source) = &bundle.git_source {
// ref = user-specified (branch/tag/SHA) or discovered default branch; sha = resolved commit for reproducibility
let git_ref = bundle
.resolved_ref
.clone()
.or_else(|| Some("main".to_string()));
LockedSource::Git {
url: git_source.url.clone(),
git_ref,
sha: bundle.resolved_sha.clone().unwrap_or_default(),
path: git_source.path.clone(), // Use path from git_source
hash: bundle_hash,
}
} else {
// Local directory - convert to relative path from workspace root if possible
let relative_path = if let Some(root) = workspace_root {
match bundle.source_path.strip_prefix(root) {
Ok(rel_path) => {
let mut path_str = rel_path.to_string_lossy().replace('\\', "/");
// Normalize the path - remove all redundant ./ segments
loop {
if let Some(pos) = path_str.find("/./") {
// Replace /./ with /
path_str = format!("{}{}", &path_str[..pos], &path_str[pos + 2..]);
} else if path_str.starts_with("./") {
// Remove leading ./
path_str = path_str[2..].to_string();
} else {
break;
}
}
// If path is empty (bundle is at root), use "."
if path_str.is_empty() {
".".to_string()
} else {
path_str
}
}
Err(_) => bundle.source_path.to_string_lossy().to_string(),
}
} else {
bundle.source_path.to_string_lossy().to_string()
};
LockedSource::Dir {
path: relative_path,
hash: bundle_hash,
}
};
// Extract metadata from bundle config if available
let (description, version, author, license, homepage) = if let Some(ref config) = bundle.config
{
(
config.description.clone(),
config.version.clone(),
config.author.clone(),
config.license.clone(),
config.homepage.clone(),
)
} else {
(None, None, None, None, None)
};
Ok(LockedBundle {
name: bundle.name.clone(),
description,
version,
author,
license,
homepage,
source,
files,
})
}
/// Update workspace configuration files
fn update_configs(
workspace: &mut Workspace,
_source: &str,
resolved_bundles: &[crate::resolver::ResolvedBundle],
workspace_bundles: Vec<crate::config::WorkspaceBundle>,
update_augent_yaml: bool,
) -> Result<()> {
// Add only direct/root bundles to workspace config (not transitive dependencies)
// Per spec: Git bundles are ALWAYS added to augent.yaml (even when installing directly)
// Dir bundles are only added when update_augent_yaml is true (workspace bundle install)
for bundle in resolved_bundles.iter() {
if bundle.dependency.is_none() {
// Skip the workspace bundle - it's not a normal dependency
let workspace_name = workspace.get_workspace_name();
if bundle.name == workspace_name {
continue;
}
// Check if this bundle should be added to augent.yaml
// Git bundles: always add
// Dir bundles: only add when update_augent_yaml is true
let is_git_bundle = bundle.git_source.is_some();
if !is_git_bundle && !update_augent_yaml {
// Skip dir bundle when not doing workspace bundle install
continue;
}
// Root bundle (what user specified): add with original source specification
if !workspace.bundle_config.has_dependency(&bundle.name) {
// Use bundle.git_source directly to preserve subdirectory information
// from interactive selection (instead of re-parsing the original source string)
let dependency = if let Some(ref git_source) = bundle.git_source {
// Git bundle - only write ref in augent.yaml when it's not the default branch
let ref_for_yaml = git_source
.git_ref
.clone()
.or_else(|| bundle.resolved_ref.clone())
.filter(|r| r != "main" && r != "master");
let mut dep =
BundleDependency::git(&bundle.name, &git_source.url, ref_for_yaml);
// Preserve path from git_source
dep.path = git_source.path.clone();
dep
} else {
// Local directory - use the resolved bundle's source_path (which is absolute)
let bundle_path = &bundle.source_path;
// Per spec: dir bundle name is always the directory name
let dir_name = bundle_path
.file_name()
.and_then(|n| n.to_str())
.unwrap_or(&bundle.name)
.to_string();
// Convert path to relative from config_dir (where augent.yaml is)
let relative_path = if let Ok(rel_from_config) =
bundle_path.strip_prefix(&workspace.config_dir)
{
// Bundle is under config_dir
let path_str = rel_from_config.to_string_lossy().replace('\\', "/");
if path_str.is_empty() {
".".to_string()
} else {
path_str
}
} else if let Ok(rel_from_root) = bundle_path.strip_prefix(&workspace.root) {
// Bundle is under workspace root but not under config_dir
// Need to construct path with .. segments
let rel_from_root_str = rel_from_root.to_string_lossy().replace('\\', "/");
// Find how deep config_dir is relative to workspace root
if let Ok(config_rel) = workspace.config_dir.strip_prefix(&workspace.root) {
let config_depth = config_rel.components().count();
let mut parts = vec!["..".to_string(); config_depth];
if !rel_from_root_str.is_empty() {
parts.push(rel_from_root_str);
}
parts.join("/")
} else {
// config_dir is not under root (shouldn't happen), use absolute path
bundle_path.to_string_lossy().to_string()
}
} else {
// Bundle is outside workspace - use absolute path
bundle_path.to_string_lossy().to_string()
};
BundleDependency::local(&dir_name, relative_path)
};
workspace.bundle_config.add_dependency(dependency);
}
}
// NOTE: Transitive dependencies (bundle.dependency.is_some()) are NOT added to
// workspace.bundle_config. They are managed automatically through the dependency
// declarations in the parent bundles. Only direct installs should appear in the
// workspace's own augent.yaml.
}
// Update lockfile - merge new bundles with existing ones
// Process bundles in order: already-installed bundles first (to move to end),
// then new bundles (to add at end), preserving installation order
// Get list of already-installed bundle names
let installed_names: HashSet<String> = workspace
.lockfile
.bundles
.iter()
.map(|b| b.name.clone())
.collect();
// Separate bundles into already-installed and new
let mut already_installed = Vec::new();
let mut new_bundles = Vec::new();
for bundle in resolved_bundles {
let locked_bundle = create_locked_bundle(bundle, Some(&workspace.root))?;
if installed_names.contains(&locked_bundle.name) {
already_installed.push(locked_bundle);
} else {
new_bundles.push(locked_bundle);
}
}
if !new_bundles.is_empty() {
// There are new bundles - process already-installed bundles first (remove and re-add to move to end)
for locked_bundle in already_installed {
workspace.lockfile.remove_bundle(&locked_bundle.name);
workspace.lockfile.add_bundle(locked_bundle);
}
// Then process new bundles (add at end)
for locked_bundle in new_bundles {
workspace.lockfile.add_bundle(locked_bundle);
}
} else {
// No new bundles - update existing ones in place to preserve order
for locked_bundle in already_installed {
// Find the position of the existing bundle
if let Some(pos) = workspace
.lockfile
.bundles
.iter()
.position(|b| b.name == locked_bundle.name)
{
// Remove and re-insert at the same position to update without changing order
workspace.lockfile.bundles.remove(pos);
workspace.lockfile.bundles.insert(pos, locked_bundle);
} else {
// Bundle not found (shouldn't happen), add it normally
workspace.lockfile.add_bundle(locked_bundle);
}
}
}
// Reorganize lockfile to ensure correct ordering
// (git bundles in install order -> dir bundles -> workspace bundle last)
let workspace_name = workspace.get_workspace_name();
workspace.lockfile.reorganize(Some(&workspace_name));
// Reorder augent.yaml dependencies to match lockfile order (excluding workspace bundle)
let workspace_name = workspace.get_workspace_name();
let lockfile_bundle_names: Vec<String> = workspace
.lockfile
.bundles
.iter()
.filter(|b| b.name != workspace_name)
.map(|b| b.name.clone())
.collect();
workspace
.bundle_config
.reorder_dependencies(&lockfile_bundle_names);
// Backfill ref in augent.yaml from lockfile only when ref is not the default branch
for dep in workspace.bundle_config.bundles.iter_mut() {
if dep.git.is_some() && dep.git_ref.is_none() {
if let Some(locked) = workspace.lockfile.find_bundle(&dep.name) {
if let LockedSource::Git {
git_ref: Some(r), ..
} = &locked.source
{
if r != "main" && r != "master" {
dep.git_ref = Some(r.clone());
}
}
}
}
}
// Update workspace config
for bundle in workspace_bundles {
// Remove existing entry for this bundle if present
workspace.workspace_config.remove_bundle(&bundle.name);
// Add new entry
workspace.workspace_config.add_bundle(bundle);
}
// Reorganize workspace config to match lockfile order
workspace.workspace_config.reorganize(&workspace.lockfile);
Ok(())
}
/// Update workspace configuration files when installing from augent.yaml
fn update_configs_from_yaml(
workspace: &mut Workspace,
resolved_bundles: &[crate::resolver::ResolvedBundle],
workspace_bundles: Vec<crate::config::WorkspaceBundle>,
should_update_lockfile: bool,
) -> Result<()> {
// Update lockfile if we resolved new versions (--update was given)
// OR if there's a workspace bundle (which should always be added/updated)
let workspace_name = workspace.get_workspace_name();
let has_workspace_bundle = workspace_bundles.iter().any(|b| b.name == workspace_name);
if should_update_lockfile || has_workspace_bundle {
for bundle in resolved_bundles {
// Always update workspace bundle in lockfile
// Only update other bundles if should_update_lockfile is true
if should_update_lockfile || bundle.name == workspace_name {
let locked_bundle = create_locked_bundle(bundle, Some(&workspace.root))?;
// Remove existing entry if present (to update it)
workspace.lockfile.remove_bundle(&locked_bundle.name);
workspace.lockfile.add_bundle(locked_bundle);
}
}
}
// Reorganize lockfile to ensure correct ordering
// (git bundles in install order -> dir bundles -> workspace bundle last)
let workspace_name = workspace.get_workspace_name();
workspace.lockfile.reorganize(Some(&workspace_name));
// Always update workspace config (which files are installed where)
for bundle in workspace_bundles {
// Remove existing entry for this bundle if present
workspace.workspace_config.remove_bundle(&bundle.name);
// Add new entry
workspace.workspace_config.add_bundle(bundle);
}
// Reorganize workspace config to match lockfile order
workspace.workspace_config.reorganize(&workspace.lockfile);
// Clean up files from earlier bundles that are overridden by later bundles
cleanup_overridden_files(workspace)?;
Ok(())
}
/// Remove file entries from earlier bundles when they're overridden by later bundles
fn cleanup_overridden_files(workspace: &mut Workspace) -> Result<()> {
// Build a map of which files are provided by which bundle (in order)
// Skip workspace bundle when building file-bundle map
let mut file_bundle_map: std::collections::HashMap<String, String> =
std::collections::HashMap::new();
let workspace_name = workspace.get_workspace_name();
for bundle in &workspace.workspace_config.bundles {
if bundle.name == workspace_name {
continue;
}
for file_path in bundle.enabled.keys() {
file_bundle_map.insert(file_path.clone(), bundle.name.clone());
}
}
// Remove files from earlier bundles if they're also in later bundles
for i in 0..workspace.workspace_config.bundles.len() {
// Skip workspace bundle when removing overridden files
if workspace.workspace_config.bundles[i].name == workspace_name {
continue;
}
for file_path in workspace.workspace_config.bundles[i]
.enabled
.keys()
.cloned()
.collect::<Vec<_>>()
{
// Check if a later bundle also provides this file
if let Some(latest_bundle) = file_bundle_map.get(&file_path) {
if latest_bundle != &workspace.workspace_config.bundles[i].name {
// This file is overridden by a later bundle, remove from this bundle
workspace.workspace_config.bundles[i]
.enabled
.remove(&file_path);
}
}
}
}
Ok(())
}
/// Convert locked bundles from lockfile to resolved bundles for installation
///
/// This function is used when installing without the --update flag. It respects
/// exact SHAs from the lockfile for reproducibility, but automatically fetches
/// any bundles that are not in the cache.
///
/// Key behavior:
/// - Checks if each bundle is cached (including marketplace synthetic bundles)
/// - If a bundle is not cached, it is fetched from git and cached
/// - Never shows "File not found" errors for missing cache entries
/// - Ensures installation succeeds even with empty cache
///
/// Reconstruct augent.yaml from lockfile when augent.yaml is missing but lockfile exists.
fn reconstruct_augent_yaml_from_lockfile(workspace: &mut Workspace) -> Result<()> {
// First pass: Collect all transitive dependencies
// A transitive dependency is any bundle that appears in another bundle's augent.yaml
let mut transitive_dependencies = HashSet::new();
for locked in &workspace.lockfile.bundles {
// Only dir bundles can have dependencies (git bundles are always top-level)
if let LockedSource::Dir { path, .. } = &locked.source {
let bundle_path = workspace.root.join(path);
let bundle_augent_yaml = bundle_path.join("augent.yaml");
if bundle_augent_yaml.exists() {
if let Ok(yaml_content) = std::fs::read_to_string(&bundle_augent_yaml) {
if let Ok(bundle_config) = BundleConfig::from_yaml(&yaml_content) {
for dep in &bundle_config.bundles {
transitive_dependencies.insert(dep.name.clone());
}
}
}
}
}
}
let workspace_bundle_name = workspace.get_workspace_name();
let mut bundles = Vec::new();
for locked in &workspace.lockfile.bundles {
// Skip workspace bundle entries with the workspace's own name
if locked.name == workspace_bundle_name {
continue;
}
// Skip bundles from .augent directory that match workspace structure
// (e.g., @asyrjasalo/.augent) - these are workspace config bundles
if let LockedSource::Dir { path, .. } = &locked.source {
// Only skip if path is exactly ".augent" (not subdirectories like ".augent/my-local-bundle")
if path == ".augent" {
continue;
}
}
// Skip transitive dependencies (bundles that are dependencies of other bundles)
if transitive_dependencies.contains(&locked.name) {
continue;
}
let dependency = match &locked.source {
LockedSource::Dir { path, .. } => {
// Validate that the path is not absolute (to prevent non-portable lockfiles)
let path_obj = std::path::Path::new(path);
if path_obj.is_absolute() {
return Err(AugentError::BundleValidationFailed {
message: format!(
"Cannot reconstruct augent.yaml: locked bundle '{}' has absolute path '{}'. \
Absolute paths in augent.lock break portability. Please fix the lockfile by using relative paths.",
locked.name, path
),
});
}
// Convert path from workspace-root-relative to config-dir-relative
// Path in lockfile is relative to workspace root (e.g., "bundles/my-bundle")
// Need to convert to be relative to where augent.yaml lives (config_dir)
let normalized_path = {
let bundle_path = workspace.root.join(path);
if let Ok(rel_from_config) = bundle_path.strip_prefix(&workspace.config_dir) {
// Bundle is under config_dir (relative path is straightforward)
let path_str = rel_from_config.to_string_lossy().replace('\\', "/");
if path_str.is_empty() {
".".to_string()
} else {
path_str
}
} else if let Ok(rel_from_root) = bundle_path.strip_prefix(&workspace.root) {
// Bundle is under workspace root but not under config_dir
// Need to construct path with .. segments
let rel_from_root_str = rel_from_root.to_string_lossy().replace('\\', "/");
// Find how deep config_dir is relative to workspace root
if let Ok(config_rel) = workspace.config_dir.strip_prefix(&workspace.root) {
let config_depth = config_rel.components().count();
let mut parts = vec!["..".to_string(); config_depth];
if !rel_from_root_str.is_empty() {
parts.push(rel_from_root_str);
}
parts.join("/")
} else {
// config_dir is not under root (shouldn't happen), use original path
path.clone()
}
} else {
// Bundle is outside workspace - use original path
path.clone()
}
};
// For directory sources, use the normalized path
BundleDependency {
name: locked.name.clone(),
path: Some(normalized_path),
git: None,
git_ref: None,
}
}
LockedSource::Git {
url, git_ref, path, ..
} => {
// For git sources, reconstruct the git URL and ref
BundleDependency {
name: locked.name.clone(),
git: Some(url.clone()),
git_ref: git_ref.clone(),
path: path.clone(),
}
}
};
bundles.push(dependency);
}
// Update the bundle config with reconstructed bundles
workspace.bundle_config.bundles = bundles;
// Save the reconstructed augent.yaml
let workspace_name = workspace.get_workspace_name();
Workspace::save_bundle_config(
&workspace.config_dir,
&workspace.bundle_config,
&workspace_name,
)?;
println!("Successfully reconstructed augent.yaml from augent.lock.");
Ok(())
}
fn locked_bundles_to_resolved(
locked_bundles: &[LockedBundle],
workspace_root: &std::path::Path,
) -> Result<Vec<crate::resolver::ResolvedBundle>> {
use crate::resolver::Resolver;
use crate::source::BundleSource;
use crate::source::GitSource;
let mut resolved = Vec::new();
let mut resolver = Resolver::new(workspace_root);
for locked in locked_bundles {
let (source_path, git_source, resolved_sha, resolved_ref) = match &locked.source {
LockedSource::Dir { path, .. } => {
let full_path = workspace_root.join(path);
(full_path, None, None, None)
}
LockedSource::Git {
url,
sha,
git_ref,
path,
..
} => {
let git_src = GitSource {
url: url.clone(),
path: path.clone(),
git_ref: git_ref.clone(),
resolved_sha: Some(sha.clone()),
};
// Check cache by (url, sha, path); cache returns resources path
let (final_cache_path, final_source) = if let Some((resources_path, _, _)) =
cache::get_cached(&git_src)?
{
(resources_path, Some(git_src))
} else {
// Reconstruct the source string and resolve (will fetch and cache)
let mut source_string = url.clone();
if let Some(git_ref) = git_ref {
source_string.push('#');
source_string.push_str(git_ref);
}
if let Some(subdir) = path {
source_string.push(':');
source_string.push_str(subdir);
}
// Parse the source string and resolve (this will fetch from git and cache it)
let bundle_source = BundleSource::parse(&source_string)?;
let resolved_bundle = resolver.resolve_source(&bundle_source, None, false)?;
(resolved_bundle.source_path, resolved_bundle.git_source)
};
(
final_cache_path,
final_source,
Some(sha.clone()),
git_ref.clone(),
)
}
};
let resolved_bundle = crate::resolver::ResolvedBundle {
name: locked.name.clone(),
dependency: None,
source_path,
resolved_sha,
resolved_ref,
git_source,
config: None,
};
resolved.push(resolved_bundle);
}
Ok(resolved)
}
/// Check if augent.yaml has changed compared to augent.lock
///
/// Returns true if the set of bundles in augent.yaml differs from
/// what's locked in augent.lock (by name and source).
fn has_augent_yaml_changed(workspace: &Workspace) -> Result<bool> {
// Get the current bundle dependencies from augent.yaml
let current_bundles: std::collections::HashSet<String> = workspace
.bundle_config
.bundles
.iter()
.map(|b| b.name.clone())
.collect();
// Get the locked bundle names
let locked_bundles: std::collections::HashSet<String> = workspace
.lockfile
.bundles
.iter()
.map(|b| b.name.clone())
.collect();
// If the sets differ, augent.yaml has changed
Ok(current_bundles != locked_bundles)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::source::GitSource;
use tempfile::TempDir;
#[test]
fn test_detect_target_platforms_auto() {
let temp = TempDir::new_in(crate::temp::temp_dir_base()).unwrap();
// Create .cursor directory
std::fs::create_dir(temp.path().join(".cursor")).unwrap();
let platforms = detect_target_platforms(temp.path(), &[]).unwrap();
assert!(!platforms.is_empty());
// Should include cursor
assert!(platforms.iter().any(|p| p.id == "cursor"));
}
#[test]
fn test_detect_target_platforms_specified() {
let temp = TempDir::new_in(crate::temp::temp_dir_base()).unwrap();
let platforms =
detect_target_platforms(temp.path(), &["cursor".to_string(), "opencode".to_string()])
.unwrap();
assert_eq!(platforms.len(), 2);
assert!(platforms.iter().any(|p| p.id == "cursor"));
assert!(platforms.iter().any(|p| p.id == "opencode"));
}
#[test]
fn test_detect_target_platforms_none_detected() {
let temp = TempDir::new_in(crate::temp::temp_dir_base()).unwrap();
// No platform dirs (e.g. only .augent exists) — should not fall back to all platforms
let result = detect_target_platforms(temp.path(), &[]);
assert!(matches!(result, Err(AugentError::NoPlatformsDetected)));
}
#[test]
fn test_detect_target_platforms_invalid() {
let temp = TempDir::new_in(crate::temp::temp_dir_base()).unwrap();
let result = detect_target_platforms(temp.path(), &["invalid-platform".to_string()]);
assert!(result.is_err());
}
#[test]
fn test_create_locked_bundle_local() {
let temp = TempDir::new_in(crate::temp::temp_dir_base()).unwrap();
// Create a simple bundle
std::fs::create_dir(temp.path().join("commands")).unwrap();
std::fs::write(temp.path().join("commands/test.md"), "# Test").unwrap();
let bundle = crate::resolver::ResolvedBundle {
name: "@test/bundle".to_string(),
dependency: None,
source_path: temp.path().to_path_buf(),
resolved_sha: None,
resolved_ref: None,
git_source: None,
config: None,
};
let locked = create_locked_bundle(&bundle, None).unwrap();
assert_eq!(locked.name, "@test/bundle");
assert!(locked.files.contains(&"commands/test.md".to_string()));
assert!(matches!(locked.source, LockedSource::Dir { .. }));
}
#[test]
fn test_create_locked_bundle_git() {
let temp = TempDir::new_in(crate::temp::temp_dir_base()).unwrap();
// Create a simple bundle
std::fs::create_dir(temp.path().join("commands")).unwrap();
std::fs::write(temp.path().join("commands/test.md"), "# Test").unwrap();
let git_source = GitSource {
url: "https://github.com/test/repo.git".to_string(),
path: None,
git_ref: Some("main".to_string()),
resolved_sha: Some("abc123".to_string()),
};
let bundle = crate::resolver::ResolvedBundle {
name: "@test/bundle".to_string(),
dependency: None,
source_path: temp.path().to_path_buf(),
resolved_sha: Some("abc123".to_string()),
resolved_ref: Some("main".to_string()),
git_source: Some(git_source),
config: None,
};
let locked = create_locked_bundle(&bundle, None).unwrap();
assert_eq!(locked.name, "@test/bundle");
assert!(locked.files.contains(&"commands/test.md".to_string()));
assert!(matches!(locked.source, LockedSource::Git { .. }));
if let LockedSource::Git { sha, git_ref, .. } = &locked.source {
assert_eq!(sha, "abc123");
assert_eq!(git_ref, &Some("main".to_string()));
}
}
#[test]
fn test_create_locked_bundle_git_with_subdirectory() {
let temp = TempDir::new_in(crate::temp::temp_dir_base()).unwrap();
// Create a simple bundle
std::fs::create_dir(temp.path().join("commands")).unwrap();
std::fs::write(temp.path().join("commands/test.md"), "# Test").unwrap();
let git_source = GitSource {
url: "https://github.com/test/repo.git".to_string(),
path: Some("plugins/accessibility-compliance".to_string()),
git_ref: None, // User didn't specify a ref
resolved_sha: Some("abc123".to_string()),
};
let bundle = crate::resolver::ResolvedBundle {
name: "@test/repo".to_string(),
dependency: None,
source_path: temp.path().to_path_buf(),
resolved_sha: Some("abc123".to_string()),
resolved_ref: Some("main".to_string()), // Actual resolved ref from HEAD
git_source: Some(git_source),
config: None,
};
let locked = create_locked_bundle(&bundle, None).unwrap();
// Verify bundle name doesn't include subdirectory
assert_eq!(locked.name, "@test/repo");
// Verify lockfile has both ref and path fields
if let LockedSource::Git {
url,
git_ref,
sha,
path,
..
} = &locked.source
{
assert_eq!(url, "https://github.com/test/repo.git");
assert_eq!(git_ref, &Some("main".to_string())); // Actual resolved ref
assert_eq!(sha, "abc123");
assert_eq!(path, &Some("plugins/accessibility-compliance".to_string()));
// Subdirectory
} else {
panic!("Expected Git source");
}
}
#[test]
fn test_generate_lockfile_empty() {
let temp = TempDir::new_in(crate::temp::temp_dir_base()).unwrap();
let workspace = crate::workspace::Workspace {
root: temp.path().to_path_buf(),
augent_dir: temp.path().join(".augent"),
config_dir: temp.path().join(".augent"),
bundle_config: crate::config::BundleConfig::new(),
workspace_config: crate::config::WorkspaceConfig::new(),
lockfile: crate::config::Lockfile::new(),
should_create_augent_yaml: false,
bundle_config_dir: None,
};
let lockfile = generate_lockfile(&workspace, &[]).unwrap();
assert!(lockfile.bundles.is_empty());
}
#[test]
fn test_generate_lockfile_with_bundle() {
let temp = TempDir::new_in(crate::temp::temp_dir_base()).unwrap();
std::fs::create_dir(temp.path().join("commands")).unwrap();
std::fs::write(temp.path().join("commands/test.md"), "# Test").unwrap();
let workspace = crate::workspace::Workspace {
root: temp.path().to_path_buf(),
augent_dir: temp.path().join(".augent"),
config_dir: temp.path().join(".augent"),
bundle_config: crate::config::BundleConfig::new(),
workspace_config: crate::config::WorkspaceConfig::new(),
lockfile: crate::config::Lockfile::new(),
should_create_augent_yaml: false,
bundle_config_dir: None,
};
let bundle = crate::resolver::ResolvedBundle {
name: "@test/bundle".to_string(),
dependency: None,
source_path: temp.path().to_path_buf(),
resolved_sha: None,
resolved_ref: None,
git_source: None,
config: None,
};
let lockfile = generate_lockfile(&workspace, &[bundle]).unwrap();
assert_eq!(lockfile.bundles.len(), 1);
assert_eq!(lockfile.bundles[0].name, "@test/bundle");
}
#[test]
fn test_update_configs_adds_new_bundle() {
let temp = TempDir::new_in(crate::temp::temp_dir_base()).unwrap();
let mut workspace = crate::workspace::Workspace {
root: temp.path().to_path_buf(),
augent_dir: temp.path().join(".augent"),
config_dir: temp.path().join(".augent"),
bundle_config: crate::config::BundleConfig::new(),
workspace_config: crate::config::WorkspaceConfig::new(),
lockfile: crate::config::Lockfile::new(),
should_create_augent_yaml: false,
bundle_config_dir: None,
};
std::fs::create_dir(temp.path().join("commands")).unwrap();
std::fs::write(temp.path().join("commands/test.md"), "# Test").unwrap();
let bundle = crate::resolver::ResolvedBundle {
name: "@external/bundle".to_string(),
dependency: None,
source_path: temp.path().to_path_buf(),
resolved_sha: None,
resolved_ref: None,
git_source: None,
config: None,
};
let mut workspace_bundle = crate::config::WorkspaceBundle::new("@external/bundle");
workspace_bundle.add_file(
"commands/test.md",
vec![".cursor/commands/test.md".to_string()],
);
// Pass update_augent_yaml=true to test the case where bundles ARE added
// (workspace bundle install). In production, dir bundles would use update_augent_yaml=false
update_configs(
&mut workspace,
"./",
&[bundle],
vec![workspace_bundle],
true,
)
.unwrap();
// Per spec: dir bundle name is the directory name. Since source is "./", the directory
// name is extracted from the absolute path and would be the temp dir name.
// For this test, we just verify that the dependency was added.
assert!(!workspace.bundle_config.bundles.is_empty());
assert!(
workspace
.workspace_config
.find_bundle("@external/bundle")
.is_some()
);
}
#[test]
fn test_update_configs_handles_existing_bundle() {
let temp = TempDir::new_in(crate::temp::temp_dir_base()).unwrap();
let mut workspace = crate::workspace::Workspace {
root: temp.path().to_path_buf(),
augent_dir: temp.path().join(".augent"),
config_dir: temp.path().join(".augent"),
bundle_config: crate::config::BundleConfig::new(),
workspace_config: crate::config::WorkspaceConfig::new(),
lockfile: crate::config::Lockfile::new(),
should_create_augent_yaml: false,
bundle_config_dir: None,
};
std::fs::create_dir(temp.path().join("commands")).unwrap();
std::fs::write(temp.path().join("commands/test.md"), "# Test").unwrap();
let bundle = crate::resolver::ResolvedBundle {
name: "@existing/bundle".to_string(),
dependency: None,
source_path: temp.path().to_path_buf(),
resolved_sha: None,
resolved_ref: None,
git_source: None,
config: None,
};
let mut workspace_bundle = crate::config::WorkspaceBundle::new("@existing/bundle");
workspace_bundle.add_file(
"commands/test.md",
vec![".cursor/commands/test.md".to_string()],
);
update_configs(
&mut workspace,
temp.path().to_string_lossy().to_string().as_str(),
&[bundle],
vec![workspace_bundle],
true, // update_augent_yaml=true for test
)
.unwrap();
assert!(
workspace
.workspace_config
.find_bundle("@existing/bundle")
.is_some()
);
}
}