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
use std::path::{Path, PathBuf};
use std::fs;
// Removed unused import
use std::process::Command;
use colored::Colorize;
use anyhow::{Result, anyhow};
use dialoguer::{Select, Input};
use crate::template_manager;
use serde_json::{self, json, Value};
use handlebars::Handlebars;
use ferrisup_common::{fs::*, to_pascal_case};
// Using the ferrisup_common module's copy_directory function for directory operations
// Note: For frameworks and libraries that have official CLIs (like Dioxus and Tauri),
// we use those CLIs directly instead of maintaining our own templates.
// This ensures we're always using the most up-to-date project creation methods
// and reduces maintenance burden.
// Main execute function to handle project creation
pub fn execute(
name: Option<&str>,
component_type: Option<&str>,
framework: Option<&str>,
provider: Option<&str>,
application_type: Option<&str>,
git: bool,
build: bool,
no_interactive: bool,
_project_type: Option<&str>,
) -> Result<()> {
// Get project name
let name = match name {
Some(name) => name.to_string(),
None => {
if no_interactive {
return Err(anyhow!("Project name is required in non-interactive mode"));
}
Input::<String>::new()
.with_prompt("Component name")
.interact()?
}
};
// Create project directory
let app_path = Path::new(&name);
create_directory(app_path)?;
// Get component type
let mut template = match component_type {
Some(template) => template.to_string(),
None => {
if no_interactive {
return Err(anyhow!("Component type is required in non-interactive mode"));
}
let templates_with_desc = template_manager::list_templates()?;
let mut templates: Vec<&str> = templates_with_desc.iter().map(|(name, _)| name.as_str()).collect();
// Add edge template if not present
if !templates.contains(&"edge") {
templates.push("edge");
}
// Create a vector of template descriptions
let descriptions: Vec<String> = templates.iter().map(|&name| {
if name == "edge" {
return "Edge computing applications (Cloudflare, Vercel, Fastly, AWS, etc.)".to_string();
}
// Find the description for this template
for (template_name, desc) in &templates_with_desc {
if template_name == name {
return desc.clone();
}
}
// Default description if not found
"".to_string()
}).collect();
// Format the items for display
let template_items: Vec<String> = templates.iter()
.zip(descriptions.iter())
.map(|(&name, desc)| format!("{} - {}", name, desc))
.collect();
let selection = Select::new()
.with_prompt("Select a component type")
.items(&template_items)
.default(0)
.interact()?;
templates[selection].to_string()
}
};
// Declare additional_vars here
let mut additional_vars = None;
// Get template configuration to check for options
let template_config = template_manager::get_template_config(&template)?;
// Handle special templates
if template == "server" {
// Server template handling
let framework_options = ["axum", "actix", "poem"];
// Use the framework parameter if provided, otherwise prompt for selection
let framework_selected = if let Some(fw) = framework {
if framework_options.contains(&fw) {
fw.to_string()
} else {
println!("Warning: Provided framework '{}' is not valid for server components", fw);
println!("Valid options are: axum, actix, poem");
let selection = Select::new()
.with_prompt("Which web framework would you like to use?")
.items(&framework_options)
.default(0)
.interact()?;
framework_options[selection].to_string()
}
} else {
let selection = Select::new()
.with_prompt("Which web framework would you like to use?")
.items(&framework_options)
.default(0)
.interact()?;
framework_options[selection].to_string()
};
println!("Using {} as the server_framework", framework_selected);
// We completely bypass the normal template processing for server templates
// to ensure only the selected framework is included
// Create the framework-specific paths
let template_root = format!("{}/templates", env!("CARGO_MANIFEST_DIR"));
let framework_dir = PathBuf::from(format!("{}/server/{}", template_root, framework_selected));
if !framework_dir.exists() {
return Err(anyhow::anyhow!("Could not find template directory for {} framework", framework_selected));
}
// Process the framework-specific Cargo.toml
let cargo_toml_path = framework_dir.join("Cargo.toml.template");
if cargo_toml_path.exists() {
let content = fs::read_to_string(&cargo_toml_path)?;
// Create handlebars instance for templating
let mut handlebars = Handlebars::new();
handlebars.register_escape_fn(handlebars::no_escape);
// Create template vars
let template_vars = json!({
"project_name": name,
"project_name_pascal_case": to_pascal_case(&name)
});
// Apply templating
let rendered = handlebars.render_template(&content, &template_vars)
.map_err(|e| anyhow::anyhow!("Failed to render template: {}", e))?;
// Write to target file
fs::write(app_path.join("Cargo.toml"), rendered)?;
} else {
return Err(anyhow::anyhow!("Could not find Cargo.toml.template for {} framework", framework_selected));
}
// Process src directory
let src_dir = framework_dir.join("src");
if src_dir.exists() {
// Create the target src directory
fs::create_dir_all(app_path.join("src"))?;
// Copy all files from the source src directory
for entry in fs::read_dir(&src_dir)? {
let entry = entry?;
let file_name = entry.file_name();
let source_path = entry.path();
let target_path = app_path.join("src").join(&file_name);
if source_path.is_dir() {
copy_directory(&source_path, &target_path)?;
} else {
// Read file content
let content = fs::read_to_string(&source_path)?;
// Create handlebars instance for templating
let mut handlebars = Handlebars::new();
handlebars.register_escape_fn(handlebars::no_escape);
// Create template vars
let template_vars = json!({
"project_name": name,
"project_name_pascal_case": to_pascal_case(&name)
});
// Apply templating
let rendered = match handlebars.render_template(&content, &template_vars) {
Ok(result) => result,
Err(e) => {
println!("Warning: Template parsing error in {}: {}", file_name.to_string_lossy(), e);
content
}
};
// Write to target file
fs::write(target_path, rendered)?;
}
}
} else {
return Err(anyhow::anyhow!("Could not find src directory for {} framework", framework_selected));
}
// Process README.md
let readme_path = framework_dir.join("README.md");
if readme_path.exists() {
let content = fs::read_to_string(&readme_path)?;
// Create handlebars instance for templating
let mut handlebars = Handlebars::new();
handlebars.register_escape_fn(handlebars::no_escape);
// Create template vars
let template_vars = json!({
"project_name": name,
"project_name_pascal_case": to_pascal_case(&name),
"server_framework": framework_selected
});
// Apply templating
let rendered = match handlebars.render_template(&content, &template_vars) {
Ok(result) => result,
Err(e) => {
println!("Warning: Template parsing error in README.md: {}", e);
content
}
};
// Write to target file
fs::write(app_path.join("README.md"), rendered)?;
} else {
// Try to use the common README.md
let common_readme_path = PathBuf::from(format!("{}/server/README.md", template_root));
if common_readme_path.exists() {
let content = fs::read_to_string(&common_readme_path)?;
// Create handlebars instance for templating
let mut handlebars = Handlebars::new();
handlebars.register_escape_fn(handlebars::no_escape);
// Register helpers
handlebars.register_helper("eq", Box::new(|h: &handlebars::Helper<'_, '_>,
_: &handlebars::Handlebars<'_>,
_: &handlebars::Context,
_: &mut handlebars::RenderContext<'_, '_>,
out: &mut dyn handlebars::Output|
-> handlebars::HelperResult {
let param1 = h.param(0).unwrap().value();
let param2 = h.param(1).unwrap().value();
out.write(&(param1 == param2).to_string())?;
Ok(())
}));
// Create template vars
let template_vars = json!({
"project_name": name,
"project_name_pascal_case": to_pascal_case(&name),
"server_framework": framework_selected
});
// Apply templating
let rendered = match handlebars.render_template(&content, &template_vars) {
Ok(result) => result,
Err(e) => {
println!("Warning: Template parsing error in README.md: {}", e);
content
}
};
// Write to target file
fs::write(app_path.join("README.md"), rendered)?;
}
}
// Copy any other framework-specific files (excluding the directories we already processed)
for entry in fs::read_dir(&framework_dir)? {
let entry = entry?;
let file_name = entry.file_name();
let file_name_str = file_name.to_string_lossy();
// Skip directories and files we've already processed
if file_name_str == "src" || file_name_str == "Cargo.toml.template" ||
file_name_str == "README.md" || file_name_str == "template.json" {
continue;
}
let source_path = entry.path();
let target_path = app_path.join(&file_name);
if source_path.is_dir() {
copy_directory(&source_path, &target_path)?;
} else {
// Read file content
let content = fs::read_to_string(&source_path)?;
// Create handlebars instance for templating
let mut handlebars = Handlebars::new();
handlebars.register_escape_fn(handlebars::no_escape);
// Create template vars
let template_vars = json!({
"project_name": name,
"project_name_pascal_case": to_pascal_case(&name),
"server_framework": framework_selected
});
// Apply templating
let rendered = match handlebars.render_template(&content, &template_vars) {
Ok(result) => result,
Err(e) => {
println!("Warning: Template parsing error in {}: {}", file_name_str, e);
content
}
};
// Write to target file
fs::write(target_path, rendered)?;
}
}
// Display success message and next steps
println!("\n✅ {} project created successfully!", name);
// Get next steps for the selected framework
let template_json_path = PathBuf::from(format!("{}/templates/server/template.json", env!("CARGO_MANIFEST_DIR")));
if template_json_path.exists() {
let template_json = fs::read_to_string(&template_json_path)?;
let template_config: Value = serde_json::from_str(&template_json)?;
let next_steps_key = format!("next_steps_{}", framework_selected);
println!("\n🚀 Next steps:");
if let Some(next_steps) = template_config.get(&next_steps_key).and_then(|s| s.as_array()) {
for step in next_steps {
if let Some(step_str) = step.as_str() {
let processed_step = step_str.replace("{{project_name}}", &name);
println!(" {}", processed_step);
}
}
} else {
// Generic next steps if framework-specific ones aren't found
println!(" 1. cd {}", name);
println!(" 2. cargo run");
println!(" 3. Visit http://localhost:3000 in your browser");
}
} else {
// Generic next steps if template.json isn't found
println!("\n🚀 Next steps:");
println!(" 1. cd {}", name);
println!(" 2. cargo run");
println!(" 3. Visit http://localhost:3000 in your browser");
}
// Skip the rest of the template handling code
return Ok(());
} else if template == "serverless" {
// Get cloud provider for serverless function
let providers = ["aws", "gcp", "azure", "vercel", "netlify"];
// Use the provider parameter if provided, otherwise prompt for selection
let selected_provider = if let Some(prov) = provider {
if providers.contains(&prov) {
println!("Using {} as the cloud provider for your serverless function", prov);
prov.to_string()
} else {
println!("Warning: Provided provider '{}' is not valid for serverless components", prov);
println!("Valid options are: aws, gcp, azure, vercel, netlify");
let selection = Select::new()
.with_prompt("Which cloud provider would you like to target for your serverless function?")
.items(&providers)
.default(0)
.interact()?;
providers[selection].to_string()
}
} else {
let selection = Select::new()
.with_prompt("Which cloud provider would you like to target for your serverless function?")
.items(&providers)
.default(0)
.interact()?;
providers[selection].to_string()
};
// Create additional variables with the selected provider
let mut vars = serde_json::Map::new();
vars.insert("cloud_provider".to_string(), json!(selected_provider));
// Use the template manager for serverless template with the selected provider
template_manager::apply_template(&template, app_path, &name, Some(serde_json::Value::Object(vars)))?;
// Clean up provider-specific directories that weren't selected
let providers = ["aws", "gcp", "azure", "vercel", "netlify"];
let template_json_path = PathBuf::from(format!("{}/templates/serverless/template.json", env!("CARGO_MANIFEST_DIR")));
let template_content = fs::read_to_string(&template_json_path)?;
let template_json: serde_json::Value = serde_json::from_str(&template_content)?;
// Get the selected cloud provider from the template variables
let _selected_provider = if let Some(vars) = template_json.get("variables").and_then(|v| v.as_object()) {
if let Some(provider) = vars.get("cloud_provider").and_then(|p| p.as_str()) {
provider
} else {
// Default to aws if not found
"aws"
}
} else {
// Default to aws if variables not found
"aws"
};
// Remove all provider directories
for provider in providers {
let provider_dir = app_path.join(provider);
if provider_dir.exists() && provider_dir.is_dir() {
fs::remove_dir_all(provider_dir)?;
}
}
// Remove template.json file
let template_json_file = app_path.join("template.json");
if template_json_file.exists() {
fs::remove_file(template_json_file)?;
}
// Remove main.rs file in root directory if it exists (should be in src/main.rs)
let root_main_rs = app_path.join("main.rs");
if root_main_rs.exists() {
fs::remove_file(root_main_rs)?;
}
return Ok(());
} else if template == "edge" {
// Handle edge template specifically to support the hierarchical structure
// Get the edge template configuration
let edge_template_path = format!("{}/templates/edge", env!("CARGO_MANIFEST_DIR"));
let edge_template_json_path = Path::new(&edge_template_path).join("template.json");
if edge_template_json_path.exists() {
// Read the edge template configuration
let edge_config = fs::read_to_string(&edge_template_json_path)
.map_err(|_| anyhow!("Failed to read edge template configuration"))?;
let edge_template_json: Value = serde_json::from_str(&edge_config)
.map_err(|_| anyhow!("Failed to parse edge template configuration"))?;
// First level: Get application type options
if let Some(options) = edge_template_json.get("options") {
// Get the edge_type options
if let Some(edge_type) = options.get("edge_type") {
if let Some(values) = edge_type.get("values").and_then(|v| v.as_array()) {
let app_type_options: Vec<&str> = values.iter()
.filter_map(|v| v.as_str())
.collect();
if app_type_options.is_empty() {
return Err(anyhow!("No edge application types found"));
}
// Get the descriptions/help text if available
let empty_map = serde_json::Map::new();
let app_type_help = edge_type.get("help")
.and_then(|h| h.as_object())
.unwrap_or(&empty_map);
// Create formatted options with descriptions
let app_type_display: Vec<String> = app_type_options.iter()
.map(|&opt| {
if let Some(help) = app_type_help.get(opt).and_then(|h| h.as_str()) {
format!("{} - {}", opt, help)
} else {
opt.to_string()
}
})
.collect();
println!("Edge computing applications allow you to run Rust code close to your users.");
// Use the application_type parameter if provided, otherwise prompt for selection
let selected_app_type = if let Some(app_type) = application_type {
if app_type_options.contains(&app_type) {
println!("Using {} as the edge application type", app_type);
app_type.to_string()
} else {
println!("Warning: Provided application type '{}' is not valid for edge components", app_type);
println!("Valid options are: {}", app_type_options.join(", "));
// Let user select application type
let app_type_selection = Select::new()
.with_prompt("Select edge application type")
.items(&app_type_display)
.default(0)
.interact()?;
app_type_options[app_type_selection].to_string()
}
} else {
// Let user select application type
let app_type_selection = Select::new()
.with_prompt("Select edge application type")
.items(&app_type_display)
.default(0)
.interact()?;
app_type_options[app_type_selection].to_string()
};
println!("Selected application type: {}", selected_app_type);
// Second level: Get provider options for the selected type
let provider_field = match selected_app_type.as_str() {
"static-site" => "static_site_provider",
"api-function" => "api_function_provider",
"web-component" => "web_component_type",
_ => return Err(anyhow!("Unsupported application type")),
};
if let Some(provider_option) = options.get(provider_field) {
if let Some(provider_values) = provider_option.get("values").and_then(|v| v.as_array()) {
let provider_options: Vec<&str> = provider_values.iter()
.filter_map(|v| v.as_str())
.collect();
if provider_options.is_empty() {
return Err(anyhow!("No providers found for the selected application type"));
}
// Get the descriptions/help text if available
let empty_map = serde_json::Map::new();
let provider_help = provider_option.get("help")
.and_then(|h| h.as_object())
.unwrap_or(&empty_map);
// Create formatted options with descriptions
let provider_display: Vec<String> = provider_options.iter()
.map(|&opt| {
if let Some(help) = provider_help.get(opt).and_then(|h| h.as_str()) {
format!("{} - {}", opt, help)
} else {
opt.to_string()
}
})
.collect();
// Let user select provider
let provider_selection = Select::new()
.with_prompt("Select provider")
.items(&provider_display)
.default(0)
.interact()?;
let selected_provider = provider_options[provider_selection];
println!("Selected provider: {}", selected_provider);
// Create variables for template
let mut vars_map = serde_json::Map::new();
vars_map.insert("edge_type".to_string(), json!(selected_app_type));
vars_map.insert(provider_field.to_string(), json!(selected_provider));
// Clone the selected_app_type for later use
let app_type_clone = selected_app_type.clone();
// No need to clone a &str reference
let provider_clone = selected_provider;
// Check if there's a redirect in the template config
if let Some(redirect) = edge_template_json.get("redirect") {
if let Some(app_redirect) = redirect.get(&selected_app_type) {
if let Some(_provider_redirect) = app_redirect.get(&selected_provider) {
// We found a redirect configuration, use it to determine the template
template = format!("edge/{}/{}", app_type_clone, provider_clone);
additional_vars = Some(json!(vars_map));
// Check if the template directory exists
let full_template_path = format!("{}/templates/{}", env!("CARGO_MANIFEST_DIR"), template);
println!("Using template: {}", template);
// Check if the directory exists
if !Path::new(&full_template_path).exists() {
return Err(anyhow!("Template directory not found: {}", full_template_path));
}
// Handle the edge template explicitly
handle_edge_template(&template, app_path, &name, additional_vars.clone())?;
return Ok(());
} else {
return Err(anyhow!("No template configuration found for provider: {}", selected_provider));
}
} else {
return Err(anyhow!("No template configuration found for application type: {}", selected_app_type));
}
} else {
return Err(anyhow!("No redirect configuration found in the template"));
}
} else {
return Err(anyhow!("No provider values found"));
}
} else {
return Err(anyhow!("No provider options found for the selected application type"));
}
} else {
return Err(anyhow!("No edge_type values found"));
}
} else {
return Err(anyhow!("No edge_type configuration found"));
}
} else {
return Err(anyhow!("No options found in edge template configuration"));
}
} else {
return Err(anyhow!("Edge template configuration not found"));
}
} else {
// Skip options handling for templates we're handling manually or data science templates
if template != "server" && template != "serverless" && !template.starts_with("data-science/") {
// Check if the template has prompts that need user input
if let Some(prompts) = template_config.get("prompts").and_then(|p| p.as_array()) {
println!("\n📊 Data Science Project Configuration\n");
let mut vars = serde_json::Map::new();
for prompt in prompts {
if let (Some(name), Some(question), Some(options)) = (
prompt.get("name").and_then(|n| n.as_str()),
prompt.get("question").and_then(|q| q.as_str()),
prompt.get("options").and_then(|o| o.as_array())
) {
let option_values: Vec<&str> = options
.iter()
.filter_map(|v| v.as_str())
.collect();
if !option_values.is_empty() {
let default_idx = prompt.get("default")
.and_then(|d| d.as_str())
.and_then(|d| option_values.iter().position(|&v| v == d))
.unwrap_or(0);
let selection = Select::new()
.with_prompt(question)
.items(&option_values)
.default(default_idx)
.interact()?;
let selected_value = option_values[selection];
vars.insert(name.to_string(), json!(selected_value));
// Print the selection
println!("\n📊 {} {}: {}",
match name {
"data_source" => "What type of data will you be working with?",
"analysis_type" => "What type of analysis do you plan to perform?",
"visualization" => "Do you need data visualization capabilities?",
_ => question
},
selected_value,
if name == "visualization" && selected_value == "yes" {
"\n📈 Visualization support will be added to your project."
} else {
""
}
);
}
}
}
// Set additional_vars if we have any
if !vars.is_empty() {
additional_vars = Some(json!(vars));
}
}
}
}
// Handle special cases for client frameworks
let mut _framework = String::new();
// For client template, prompt for framework selection
if template == "client" {
println!("Template description: Custom template: client");
println!("Using template: client");
// Get client framework
let frameworks = vec!["dioxus", "tauri", "leptos"];
// Use the framework parameter if provided, otherwise prompt for selection
let framework_selected = if let Some(fw) = framework {
if frameworks.contains(&fw) {
fw.to_string()
} else {
println!("Warning: Provided framework '{}' is not valid for client components", fw);
println!("Valid options are: dioxus, tauri, leptos");
let selection = Select::new()
.with_prompt("Select Rust client framework")
.items(&frameworks)
.default(0)
.interact()?;
frameworks[selection].to_string()
}
} else {
let selection = Select::new()
.with_prompt("Select Rust client framework")
.items(&frameworks)
.default(0)
.interact()?;
frameworks[selection].to_string()
};
// For Leptos, prompt for specific template type
if framework_selected == "leptos" {
println!("📦 Using Leptos templates to bootstrap the project");
println!("🔧 Checking for required dependencies...");
// Check for wasm32-unknown-unknown target
println!("🔍 Checking for wasm32-unknown-unknown target...");
let wasm_check = Command::new("rustup")
.args(["target", "list", "--installed"])
.output()?;
let wasm_output = String::from_utf8_lossy(&wasm_check.stdout);
if !wasm_output.contains("wasm32-unknown-unknown") {
println!("⚠️ wasm32-unknown-unknown target not found. Installing...");
let status = Command::new("rustup")
.args(["target", "add", "wasm32-unknown-unknown"])
.status()?;
if !status.success() {
println!("❌ Failed to install wasm32-unknown-unknown target.");
println!("Please install it manually with: rustup target add wasm32-unknown-unknown");
} else {
println!("✅ wasm32-unknown-unknown target installed successfully");
}
} else {
println!("✅ wasm32-unknown-unknown target is already installed");
}
// Check for trunk (needed for counter, router, todo templates)
println!("🔍 Checking for Trunk...");
let trunk_check = Command::new("trunk")
.arg("--version")
.output();
match trunk_check {
Ok(_) => println!("✅ Trunk is already installed"),
Err(_) => {
println!("⚠️ Trunk not found. Installing...");
let status = Command::new("cargo")
.args(["install", "trunk", "--locked"])
.status()?;
if !status.success() {
println!("❌ Failed to install Trunk.");
println!("Please install it manually with: cargo install trunk --locked");
} else {
println!("✅ Trunk installed successfully");
}
}
}
let leptos_templates = vec![
"Counter - Simple counter with reactive state",
"Router - Multi-page application with routing",
"Todo - Todo application with filtering",
];
let leptos_selection = Select::new()
.with_prompt("✨ Which Leptos template would you like to use?")
.items(&leptos_templates)
.default(0)
.interact()?;
// Map selection to template name
template = match leptos_selection {
0 => "counter".to_string(),
1 => "router".to_string(),
2 => "todo".to_string(),
_ => "counter".to_string(), // Default to counter if somehow none selected
};
println!("🔧 Creating new Leptos project with {} template...", template);
// For Leptos templates, prepend "client/leptos/"
let template_path = format!("client/leptos/{}", template);
if let Err(e) = template_manager::apply_template(
&template_path,
app_path,
&name,
additional_vars,
) {
return Err(e);
}
// DO NOT print next steps here; let the template manager handle it
return Ok(());
} else if framework_selected == "dioxus" {
println!("📦 Creating Dioxus project with dioxus-cli");
// Check if dioxus-cli is installed
println!("🔍 Checking for dioxus-cli...");
let dx_check = Command::new("dx")
.arg("--version")
.output();
let dx_installed = match dx_check {
Ok(output) => output.status.success(),
Err(_) => false
};
if !dx_installed {
println!("⚠️ dioxus-cli not found. Installing...");
let install_status = Command::new("cargo")
.args(["install", "dioxus-cli"])
.status()?;
if !install_status.success() {
return Err(anyhow!("Failed to install dioxus-cli"));
}
println!("✅ dioxus-cli installed successfully");
} else {
println!("✅ dioxus-cli is already installed");
}
// Check for wasm32-unknown-unknown target (required for web)
println!("🔍 Checking for wasm32-unknown-unknown target...");
let wasm_check = Command::new("rustup")
.args(["target", "list", "--installed"])
.output()?;
let wasm_output = String::from_utf8_lossy(&wasm_check.stdout);
if !wasm_output.contains("wasm32-unknown-unknown") {
println!("⚠️ wasm32-unknown-unknown target not found. Installing...");
let status = Command::new("rustup")
.args(["target", "add", "wasm32-unknown-unknown"])
.status()?;
if !status.success() {
println!("❌ Failed to install wasm32-unknown-unknown target.");
println!("Please install it manually with: rustup target add wasm32-unknown-unknown");
} else {
println!("✅ wasm32-unknown-unknown target installed successfully");
}
} else {
println!("✅ wasm32-unknown-unknown target is already installed");
}
// Check for aarch64-apple-ios-sim target (required for iOS simulator)
println!("🔍 Checking for aarch64-apple-ios-sim target...");
let ios_check = Command::new("rustup")
.args(["target", "list", "--installed"])
.output()?;
let ios_output = String::from_utf8_lossy(&ios_check.stdout);
if !ios_output.contains("aarch64-apple-ios-sim") {
println!("⚠️ aarch64-apple-ios-sim target not found.");
println!("This is something that Rustup can do for you. Rustup is Rust's toolchain");
println!("installer and manager, and it can add the iOS simulator target to your Rust installation.");
// Ask if user wants to install iOS simulator target
let install_ios = dialoguer::Confirm::new()
.with_prompt("Would you like to install the iOS simulator target?")
.default(true)
.interact()?;
if install_ios {
println!("Installing aarch64-apple-ios-sim target...");
let status = Command::new("rustup")
.args(["target", "add", "aarch64-apple-ios-sim"])
.status()?;
if !status.success() {
println!("❌ Failed to install aarch64-apple-ios-sim target.");
println!("Please install it manually with: rustup target add aarch64-apple-ios-sim");
} else {
println!("✅ aarch64-apple-ios-sim target installed successfully");
}
} else {
println!("Skipping iOS simulator target installation.");
println!("You can install it later with: rustup target add aarch64-apple-ios-sim");
}
} else {
println!("✅ aarch64-apple-ios-sim target is already installed");
}
// Check for Android targets if on macOS or Linux
#[cfg(any(target_os = "macos", target_os = "linux"))]
{
println!("🔍 Checking for Android targets...");
let android_check = Command::new("rustup")
.args(["target", "list", "--installed"])
.output()?;
let android_output = String::from_utf8_lossy(&android_check.stdout);
if !android_output.contains("aarch64-linux-android") {
println!("⚠️ aarch64-linux-android target not found.");
println!("For most modern Android devices, you'll want the aarch64-linux-android target.");
// Ask if user wants to install Android target
let install_android = dialoguer::Confirm::new()
.with_prompt("Would you like to install the Android target?")
.default(true)
.interact()?;
if install_android {
println!("Installing aarch64-linux-android target...");
let status = Command::new("rustup")
.args(["target", "add", "aarch64-linux-android"])
.status()?;
if !status.success() {
println!("❌ Failed to install aarch64-linux-android target.");
println!("Please install it manually with: rustup target add aarch64-linux-android");
} else {
println!("✅ aarch64-linux-android target installed successfully");
}
} else {
println!("Skipping Android target installation.");
println!("You can install it later with: rustup target add aarch64-linux-android");
}
} else {
println!("✅ aarch64-linux-android target is already installed");
}
}
// Create project directory
create_directory(app_path)?;
// Let dioxus-cli handle the project creation with its own prompts
println!("🔧 Creating Dioxus project...");
let create_status = Command::new("dx")
.args(["init"])
.arg(".")
.current_dir(app_path)
.status()?;
if !create_status.success() {
return Err(anyhow!("Failed to create Dioxus project with dioxus-cli"));
}
// Print success message with instructions
println!("\n🎉 Project {} created successfully!", name);
println!("\nNext steps:");
println!(" cd {}", name);
// Detect if this is a Dioxus workspace (web, desktop, mobile all exist)
let web_exists = std::path::Path::new(&format!("{}/web", name)).exists();
let desktop_exists = std::path::Path::new(&format!("{}/desktop", name)).exists();
let mobile_exists = std::path::Path::new(&format!("{}/mobile", name)).exists();
if web_exists || desktop_exists || mobile_exists {
println!(" dx serve --package web # For web application");
println!(" dx serve --package desktop # For desktop application");
println!(" dx serve --package mobile # For mobile application");
} else {
println!(" dx serve");
}
return Ok(());
} else if framework_selected == "tauri" {
println!("📦 Creating Tauri project with create-tauri-app");
// Get the parent directory
let parent_dir = app_path.parent().unwrap_or(Path::new("."));
// Make sure we're not creating the project in the current directory
// If app_path is just a filename without a directory, use the current directory
let working_dir = if parent_dir == Path::new("") {
Path::new(".")
} else {
parent_dir
};
// Create a temporary script to handle the create-tauri-app command
// This will allow us to run the command interactively while still using the project name
let script_content = format!(r#"#!/bin/sh
cd "{}"
npx create-tauri-app {}
"#, working_dir.display(), name);
let temp_dir = std::env::temp_dir();
let script_path = temp_dir.join("ferrisup_tauri_script.sh");
// Write the script to a temporary file
fs::write(&script_path, script_content)?;
// Make the script executable
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let mut perms = fs::metadata(&script_path)?.permissions();
perms.set_mode(0o755);
fs::set_permissions(&script_path, perms)?;
}
// Run the script
let create_status = Command::new(&script_path)
.status()?;
// Clean up the temporary script
let _ = fs::remove_file(&script_path);
if !create_status.success() {
return Err(anyhow!("Failed to create Tauri project with create-tauri-app"));
}
// Print success message
println!("\n🎉 Project {} created successfully!", name);
return Ok(());
} else {
// If not Leptos, Dioxus, or Tauri, use the selected framework as the template
template = framework_selected.to_string();
}
} else if template == "data-science" {
// For data science templates, check if framework is provided via command line
if let Some(fw) = framework {
match fw.to_lowercase().as_str() {
"polars" => {
template = "data-science/polars-cli".to_string();
println!("📈 Selected: Data Analysis with Polars");
},
"linfa" => {
template = "data-science/linfa-examples".to_string();
println!("🔍 Selected: Machine Learning with Linfa");
},
_ => {
println!("Warning: Provided framework '{}' is not valid for data-science components", fw);
println!("Valid options are: polars, linfa");
// If invalid framework is provided, fall back to interactive selection
let framework_options = vec![
"Data Analysis with Polars",
"Machine Learning with Linfa"
];
let selection = Select::new()
.with_prompt("📊 Select a Data Science approach")
.default(0)
.items(&framework_options)
.interact()?;
match selection {
0 => {
template = "data-science/polars-cli".to_string();
println!("📈 Selected: Data Analysis with Polars");
},
1 => {
template = "data-science/linfa-examples".to_string();
println!("🔍 Selected: Machine Learning with Linfa");
},
_ => {
template = "data-science/polars-cli".to_string();
println!("📈 Selected: Data Analysis with Polars (default)");
}
}
}
}
} else {
// No framework provided, use interactive selection
let framework_options = vec![
"Data Analysis with Polars",
"Machine Learning with Linfa"
];
// Create a selection without additional prompt text to avoid duplication
let selection = Select::new()
.with_prompt("📊 Select a Data Science approach")
.default(0)
.items(&framework_options)
.interact()?;
// Based on framework selection, show appropriate templates
match selection {
0 => {
// Polars selected
template = "data-science/polars-cli".to_string();
println!("📈 Selected: Data Analysis with Polars");
},
1 => {
// Linfa selected
template = "data-science/linfa-examples".to_string();
println!("🔍 Selected: Machine Learning with Linfa");
},
_ => {
// Fallback
template = "data-science/polars-cli".to_string();
println!("📈 Selected: Data Analysis with Polars (default)");
}
}
}
println!("🔍 Checking for wasm32-unknown-unknown target...");
check_dependencies(&template)?;
println!("\n📊 Setting up {} data science project...", template.replace("data-science/", ""));
}
// Check for required dependencies based on template
check_dependencies(&template)?;
// Handle special cases for
//
//
//
//
//
//
// embedded templates
if template == "embedded" {
println!("📦 Creating embedded project for microcontrollers");
// Create a variable to store the framework selection for template variables
let framework_selection;
// Check if the user provided a framework parameter
let use_embassy = if let Some(fw) = framework {
match fw.to_lowercase().as_str() {
"embassy" => {
println!("Using Embassy framework for embedded development");
framework_selection = "Yes, use Embassy framework".to_string();
true
},
"none" | "standard" => {
println!("Using standard embedded template without a framework");
framework_selection = "No, use standard embedded template".to_string();
false
},
_ => {
println!("Warning: Provided framework '{}' is not valid for embedded components", fw);
println!("Valid options are: embassy, none");
// Prompt for framework selection
let frameworks = vec!["No, use standard embedded template", "Yes, use Embassy framework"];
let selection = Select::new()
.with_prompt("Do you want to use an embedded framework?")
.items(&frameworks)
.default(0)
.interact()?;
framework_selection = frameworks[selection].to_string();
framework_selection == "Yes, use Embassy framework"
}
}
} else if let Some(vars) = &additional_vars {
// Fall back to additional_vars if no framework parameter
if let Some(framework_option) = vars.get("framework").and_then(|f| f.as_str()) {
framework_selection = framework_option.to_string();
framework_option == "Yes, use Embassy framework"
} else {
// Prompt for framework selection
let frameworks = vec!["No, use standard embedded template", "Yes, use Embassy framework"];
let selection = Select::new()
.with_prompt("Do you want to use an embedded framework?")
.items(&frameworks)
.default(0)
.interact()?;
framework_selection = frameworks[selection].to_string();
framework_selection == "Yes, use Embassy framework"
}
} else {
// Prompt for framework selection
let frameworks = vec!["No, use standard embedded template", "Yes, use Embassy framework"];
let selection = Select::new()
.with_prompt("Do you want to use an embedded framework?")
.items(&frameworks)
.default(0)
.interact()?;
framework_selection = frameworks[selection].to_string();
framework_selection == "Yes, use Embassy framework"
};
// Create or update additional variables with the framework selection
let mut vars_map = if let Some(ref existing_vars) = additional_vars {
if let Some(existing_map) = existing_vars.as_object() {
// Clone the existing map
existing_map.clone()
} else {
serde_json::Map::new()
}
} else {
serde_json::Map::new()
};
// Add or update the framework selection
vars_map.insert("framework".to_string(), json!(framework_selection));
// Update additional_vars with the merged variables
additional_vars = Some(json!(vars_map));
if use_embassy {
// User selected Embassy framework
println!("📦 Creating Embassy project using cargo-embassy");
// Check if cargo-embassy is installed
println!("🔍 Checking for cargo-embassy...");
let embassy_check = Command::new("cargo")
.args(["embassy", "--version"])
.output();
let embassy_installed = match embassy_check {
Ok(output) => output.status.success(),
Err(_) => false
};
if !embassy_installed {
println!("⚠️ cargo-embassy not found. Installing...");
let status = Command::new("cargo")
.args(["install", "cargo-embassy"])
.status()?;
if !status.success() {
println!("❌ Failed to install cargo-embassy.");
println!("Please install it manually with: cargo install cargo-embassy");
return Err(anyhow!("Failed to install cargo-embassy"));
} else {
println!("✅ cargo-embassy installed successfully");
}
} else {
println!("✅ cargo-embassy is already installed");
}
// Get microcontroller chip for Embassy
let mcu_targets = vec!["rp2040", "stm32f4", "nrf52840", "esp32c3"];
let selection = Select::new()
.with_prompt("Select microcontroller chip")
.items(&mcu_targets)
.default(0)
.interact()?;
let mcu_chip = mcu_targets[selection];
println!("Using {} as the microcontroller chip", mcu_chip);
// Create the project using cargo-embassy
println!("🔄 Creating new Embassy project...");
// Create a parent directory for the Embassy project
let parent_dir = Path::new(".").join("embassy_temp");
fs::create_dir_all(&parent_dir)?;
// Run cargo-embassy init from the parent directory
let status = Command::new("cargo")
.args(["embassy", "init", "--chip", mcu_chip, &name])
.current_dir(&parent_dir)
.status()?;
if !status.success() {
println!("❌ Failed to create Embassy project.");
return Err(anyhow!("Failed to create Embassy project"));
}
// Check if ESP toolchain is needed (for ESP32 chips)
let is_esp_chip = mcu_chip.starts_with("esp");
if is_esp_chip {
println!("🔍 Checking for ESP Rust toolchain...");
// Check if ESP toolchain is installed
let esp_check = Command::new("rustup")
.args(["toolchain", "list"])
.output()?;
let toolchains = String::from_utf8_lossy(&esp_check.stdout);
let esp_installed = toolchains.contains("esp");
if !esp_installed {
println!("⚠️ ESP Rust toolchain not found. Installing...");
// Install ESP toolchain
let install_status = Command::new("rustup")
.args(["toolchain", "install", "esp"])
.status()?;
if !install_status.success() {
println!("❌ Failed to install ESP toolchain.");
println!("Please install it manually with: rustup toolchain install esp");
println!("See https://esp-rs.github.io/book/installation/index.html for more information.");
} else {
println!("✅ ESP toolchain installed successfully");
}
} else {
println!("✅ ESP toolchain is already installed");
}
}
// Move the generated project to the target directory
let project_dir = parent_dir.join(&name);
if project_dir.exists() {
// Copy all files from the generated project to the target directory
let target_dir = Path::new(&name);
if !target_dir.exists() {
fs::create_dir_all(target_dir)?;
}
copy_directory(&project_dir, target_dir)?;
// Clean up the temporary directory
fs::remove_dir_all(parent_dir)?;
println!("🎉 Embassy project {} created successfully!", name);
println!("\nNext steps:");
println!(" cd {}", name);
// Add ESP-specific instructions if needed
if mcu_chip.starts_with("esp") {
println!("\nℹ️ This project uses the ESP Rust toolchain");
println!(" Make sure it's installed with: rustup toolchain install esp");
println!(" For more information: https://esp-rs.github.io/book/installation/index.html");
println!("\n # Build the project");
println!(" cargo build --release");
println!(" # Flash to device");
println!(" cargo run --release");
println!("\nℹ️ If you see an error about custom toolchain, run:");
println!(" rustup toolchain install esp");
} else {
println!(" # Build the project");
println!(" cargo build --release");
println!(" # Run the project");
println!(" cargo run --release");
}
return Ok(());
} else {
println!("❌ Failed to create Embassy project.");
return Err(anyhow!("Failed to create Embassy project: Project directory not found"));
}
} else {
// Standard embedded template
// Get microcontroller target from options
let mcu_target = if let Some(vars) = &additional_vars {
if vars.get("mcu_target").is_some() {
// If mcu_target is explicitly provided in additional_vars, use it
vars.get("mcu_target")
.and_then(|t| t.as_str())
.unwrap_or("rp2040")
.to_string()
} else {
// Otherwise prompt for selection
let mcu_targets = vec!["rp2040", "stm32", "esp32", "arduino"];
let selection = Select::new()
.with_prompt("Select microcontroller target")
.items(&mcu_targets)
.default(0)
.interact()?;
let selected_target = mcu_targets[selection].to_string();
// Update additional_vars with the selected target
if let Some(ref mut vars_obj) = additional_vars {
if let Some(vars_map) = vars_obj.as_object_mut() {
vars_map.insert("mcu_target".to_string(), json!(selected_target.clone()));
}
}
selected_target
}
} else {
// No additional_vars, prompt for selection
let mcu_targets = vec!["rp2040", "stm32", "esp32", "arduino"];
let selection = Select::new()
.with_prompt("Select microcontroller target")
.items(&mcu_targets)
.default(0)
.interact()?;
// Create additional_vars with the selected target
let selected_target = mcu_targets[selection].to_string();
let mut vars_map = serde_json::Map::new();
vars_map.insert("mcu_target".to_string(), json!(selected_target.clone()));
additional_vars = Some(json!(vars_map));
selected_target
};
println!("Using {} as the microcontroller target", mcu_target);
// Create target-specific dependencies string
let mcu_target_deps = match mcu_target.as_str() {
"rp2040" => "rp2040-hal = \"0.9\"\nrp2040-boot2 = \"0.3\"\nusb-device = \"0.2\"\nusbd-serial = \"0.1\"",
"stm32" => "stm32f4xx-hal = { version = \"0.17\", features = [\"stm32f411\"] }",
"esp32" => "esp32-hal = \"0.16\"\nesp-backtrace = \"0.9\"\nesp-println = \"0.6\"",
"arduino" => "arduino-hal = { git = \"https://github.com/rahix/avr-hal\", rev = \"7dfa6d322b9df98b2d98afe0e14a97afe0187ac1\" }\navr-device = \"0.5\"\nufmt = \"0.2\"",
_ => "",
};
// Create variables for template substitution
let mut vars = serde_json::Map::new();
vars.insert("mcu_target".to_string(), json!(mcu_target));
vars.insert("mcu_target_deps".to_string(), json!(mcu_target_deps));
// Merge with existing additional_vars if any
if let Some(ref existing_vars) = additional_vars {
if let Some(existing_obj) = existing_vars.as_object() {
for (k, v) in existing_obj {
if k != "mcu_target" && k != "mcu_target_deps" {
vars.insert(k.clone(), v.clone());
}
}
}
}
// Apply the template using the template manager
template_manager::apply_template(&template, app_path, &name, Some(serde_json::Value::Object(vars)))?;
// Suggest installing the appropriate Rust target
let rust_target = match mcu_target.as_str() {
"rp2040" => "thumbv6m-none-eabi",
"stm32" => "thumbv7em-none-eabihf",
"esp32" => "xtensa-esp32-none-elf",
"arduino" => "avr-unknown-gnu-atmega328",
_ => "thumbv6m-none-eabi",
};
println!("\nℹ️ You'll need to install the appropriate Rust target:");
println!(" rustup target add {}", rust_target);
match mcu_target.as_str() {
"rp2040" => {
println!(" cargo install probe-run");
println!(" cargo run --target {}", rust_target);
},
"esp32" => {
println!(" cargo install espflash");
println!(" cargo build --target {}", rust_target);
println!(" espflash flash --monitor target/{}/debug/{}", rust_target, name);
},
"arduino" => {
// Check if the target is installed
let output = Command::new("rustup")
.args(["target", "list", "--installed"])
.output()
.unwrap_or_else(|_| panic!("Failed to execute rustup command"));
let installed_targets = String::from_utf8_lossy(&output.stdout);
let target_installed = installed_targets.contains(rust_target);
if !target_installed {
println!("🔍 Checking for {} target...", rust_target);
println!("❌ {} target is not installed", rust_target);
} else {
println!("🔍 Checking for {} target...", rust_target);
println!("✅ {} target is already installed", rust_target);
}
println!("\nℹ️ Setup instructions for Arduino development:");
println!(" 1. Install the AVR target: rustup target add {}", rust_target);
println!(" 2. Install ravedude: cargo install ravedude");
println!(" 3. Build the project: cargo build --target {}", rust_target);
println!(" 4. Flash to Arduino: cargo run --target {}", rust_target);
println!("\nNote: arduino-hal is sourced from GitHub, not crates.io");
},
_ => {
println!(" cargo build --target {}", rust_target);
}
}
}
} else if template == "counter" || template == "router" || template == "todo" {
// For Leptos templates, prepend "client/leptos/"
let template_path = format!("client/leptos/{}", template);
template_manager::apply_template(&template_path, app_path, &name, additional_vars.clone())?;
} else {
// For data science templates, handle the prompts directly
if template.starts_with("data-science/") {
// Get the template configuration to access prompts
let template_config = template_manager::get_template_config(&template)?;
println!("\n📊 Data Science Project Configuration\n");
// Create a map to store the user's selections
let mut template_vars = serde_json::Map::new();
// Process prompts if they exist
if let Some(prompts) = template_config.get("prompts").and_then(|p| p.as_array()) {
for prompt in prompts {
if let (Some(name), Some(question), Some(options)) = (
prompt.get("name").and_then(|n| n.as_str()),
prompt.get("question").and_then(|q| q.as_str()),
prompt.get("options").and_then(|o| o.as_array())
) {
let option_values: Vec<&str> = options
.iter()
.filter_map(|v| v.as_str())
.collect();
if !option_values.is_empty() {
let default_idx = prompt.get("default")
.and_then(|d| d.as_str())
.and_then(|d| option_values.iter().position(|&v| v == d))
.unwrap_or(0);
// Create the selection prompt
let selection = Select::new()
.with_prompt(question)
.items(&option_values)
.default(default_idx)
.interact()?;
let selected_value = option_values[selection];
template_vars.insert(name.to_string(), json!(selected_value));
}
}
}
}
// Add any additional variables that might have been set earlier
if let Some(ref additional) = additional_vars {
if let Some(obj) = additional.as_object() {
for (k, v) in obj {
if !template_vars.contains_key(k) {
template_vars.insert(k.clone(), v.clone());
}
}
}
}
// Debug output only when in verbose mode
if std::env::var("FERRISUP_VERBOSE").is_ok() {
println!("Template variables: {}", json!(template_vars));
}
// Apply the template with the user's selections
template_manager::apply_template(&template, app_path, &name, Some(json!(template_vars)))?;
} else {
// For non-data-science templates, use the original approach
template_manager::apply_template(&template, app_path, &name, additional_vars)?;
}
}
// Initialize git repository if requested
if git {
println!("🔄 Initializing git repository...");
let status = Command::new("git")
.args(["init"])
.current_dir(app_path)
.status()?;
if !status.success() {
return Err(anyhow!("Failed to initialize git repository"));
}
// Create .gitignore file
let gitignore = r#"/target
/dist
/Cargo.lock
**/*.rs.bk
"#;
std::fs::write(app_path.join(".gitignore"), gitignore)?;
println!("✅ Git repository initialized");
}
// Build project if requested
if build {
println!("🔄 Building project...");
let status = Command::new("cargo")
.args(["build"])
.current_dir(app_path)
.status()?;
if !status.success() {
return Err(anyhow!("Failed to build project"));
}
println!("✅ Project built successfully");
}
// Print success message with instructions
println!("\n🎉 Project {} created successfully!", name);
// We don't need to print next steps here as they're already printed in apply_template
// The next steps include the static server command if applicable
Ok(())
}
// Helper function to check and install required dependencies
fn check_dependencies(template: &str) -> Result<()> {
// Check for wasm32-unknown-unknown target
println!("🔍 Checking for wasm32-unknown-unknown target...");
let wasm_check = Command::new("rustup")
.args(["target", "list", "--installed"])
.output()?;
let wasm_output = String::from_utf8_lossy(&wasm_check.stdout);
if !wasm_output.contains("wasm32-unknown-unknown") {
println!("⚠️ wasm32-unknown-unknown target not found. Installing...");
let status = Command::new("rustup")
.args(["target", "add", "wasm32-unknown-unknown"])
.status()?;
if !status.success() {
println!("❌ Failed to install wasm32-unknown-unknown target.");
println!("Please install it manually with: rustup target add wasm32-unknown-unknown");
} else {
println!("✅ wasm32-unknown-unknown target installed successfully");
}
} else {
println!("✅ wasm32-unknown-unknown target is already installed");
}
// Check if we're using an edge template that needs wasm-pack
if template.starts_with("edge/") {
println!("🔍 Checking for wasm-pack...");
let wasm_pack_check = Command::new("wasm-pack")
.arg("--version")
.output();
match wasm_pack_check {
Ok(_) => println!("✅ wasm-pack is already installed"),
Err(_) => {
println!("⚠️ wasm-pack not found. Installing...");
let status = Command::new("cargo")
.args(["install", "wasm-pack"])
.status()?;
if !status.success() {
println!("❌ Failed to install wasm-pack.");
println!("Please install it manually with: cargo install wasm-pack");
} else {
println!("✅ wasm-pack installed successfully");
}
}
}
// For Cloudflare Workers templates, check for Wrangler CLI
if template.contains("cloudflare-workers") || template.contains("cloudflare-pages") {
println!("🔍 Checking for Cloudflare Wrangler CLI...");
let wrangler_check = Command::new("wrangler")
.arg("--version")
.output();
match wrangler_check {
Ok(_) => println!("✅ Wrangler CLI is already installed"),
Err(_) => {
println!("ℹ️ Wrangler CLI not found.");
println!("You may need to install it with: npm install -g wrangler");
println!("Learn more at: https://developers.cloudflare.com/workers/wrangler/install-and-update/");
}
}
}
// For Vercel templates, check for Vercel CLI
if template.contains("vercel") {
println!("🔍 Checking for Vercel CLI...");
let vercel_check = Command::new("vercel")
.arg("--version")
.output();
match vercel_check {
Ok(_) => println!("✅ Vercel CLI is already installed"),
Err(_) => {
println!("ℹ️ Vercel CLI not found.");
println!("You may need to install it with: npm install -g vercel");
println!("Learn more at: https://vercel.com/docs/cli");
}
}
}
// For Netlify templates, check for Netlify CLI
if template.contains("netlify") {
println!("🔍 Checking for Netlify CLI...");
let netlify_check = Command::new("netlify")
.arg("--version")
.output();
match netlify_check {
Ok(_) => println!("✅ Netlify CLI is already installed"),
Err(_) => {
println!("ℹ️ Netlify CLI not found.");
println!("You may need to install it with: npm install -g netlify-cli");
println!("Learn more at: https://docs.netlify.com/cli/get-started/");
}
}
}
// For Fastly templates, check for Fastly CLI
if template.contains("fastly") {
println!("🔍 Checking for Fastly CLI...");
let fastly_check = Command::new("fastly")
.arg("--version")
.output();
match fastly_check {
Ok(_) => println!("✅ Fastly CLI is already installed"),
Err(_) => {
println!("ℹ️ Fastly CLI not found.");
println!("You may need to install it from: https://developer.fastly.com/learning/tools/cli/");
}
}
}
}
// Check for trunk (needed for counter, router, todo templates)
if template == "counter" || template == "router" || template == "todo" {
println!("🔍 Checking for Trunk...");
let trunk_check = Command::new("trunk")
.arg("--version")
.output();
match trunk_check {
Ok(_) => println!("✅ Trunk is already installed"),
Err(_) => {
println!("⚠️ Trunk not found. Installing...");
let status = Command::new("cargo")
.args(["install", "trunk", "--locked"])
.status()?;
if !status.success() {
println!("❌ Failed to install Trunk.");
println!("Please install it manually with: cargo install trunk --locked");
} else {
println!("✅ Trunk installed successfully");
}
}
}
}
Ok(())
}
// Helper function to handle edge templates
fn handle_edge_template(template: &str, app_path: &Path, name: &str, additional_vars: Option<serde_json::Value>) -> Result<()> {
// Handle edge template creation manually
let template_dir_path = PathBuf::from(format!("{}/templates/{}", env!("CARGO_MANIFEST_DIR"), template));
if !template_dir_path.exists() {
return Err(anyhow::anyhow!("Could not find template directory for {} template", template));
}
// Create src directory if needed
fs::create_dir_all(app_path.join("src"))?;
// Check if the template has lib.rs in the root, which is common for edge templates
let lib_rs = template_dir_path.join("lib.rs");
if lib_rs.exists() {
let content = fs::read_to_string(&lib_rs)?;
// Create handlebars instance for templating
let mut handlebars = Handlebars::new();
// Important: Disable escaping to preserve raw string literals and SVG content
handlebars.register_escape_fn(handlebars::no_escape);
// Disable strict mode to handle more complex templates
handlebars.set_strict_mode(false);
// Create template vars
let mut template_vars = json!({
"project_name": name,
"crate_name": name.replace("-", "_"),
"project_name_pascal_case": to_pascal_case(&name),
"authors": "Your Name <your.email@example.com>"
});
// Merge additional variables if provided
if let Some(add_vars) = additional_vars.as_ref() {
if let Some(obj) = add_vars.as_object() {
if let Some(obj_mut) = template_vars.as_object_mut() {
for (key, value) in obj {
obj_mut.insert(key.clone(), value.clone());
}
}
}
}
// Apply templating with better error handling
let rendered = match handlebars.render_template(&content, &template_vars) {
Ok(result) => result,
Err(e) => {
println!("Warning: Template parsing error in lib.rs: {}", e);
println!("Proceeding with unmodified template content");
content
}
};
// Write to src/lib.rs in the target directory
fs::write(app_path.join("src").join("lib.rs"), rendered)?;
}
// Copy Cargo.toml.template and process it
let cargo_toml_template = template_dir_path.join("Cargo.toml.template");
let cargo_toml = template_dir_path.join("Cargo.toml");
// Check for either Cargo.toml.template or Cargo.toml
let cargo_toml_path = if cargo_toml_template.exists() {
cargo_toml_template
} else if cargo_toml.exists() {
cargo_toml
} else {
return Err(anyhow::anyhow!("Could not find Cargo.toml or Cargo.toml.template for {} template", template));
};
let content = fs::read_to_string(&cargo_toml_path)?;
// Create handlebars instance for templating
let mut handlebars = Handlebars::new();
// Important: Disable escaping to preserve raw string literals and SVG content
handlebars.register_escape_fn(handlebars::no_escape);
// Disable strict mode to handle more complex templates
handlebars.set_strict_mode(false);
// Create template vars
let mut template_vars = json!({
"project_name": name,
"crate_name": name.replace("-", "_"),
"project_name_pascal_case": to_pascal_case(&name),
"authors": "Your Name <your.email@example.com>"
});
// Merge additional variables if provided
if let Some(add_vars) = additional_vars.as_ref() {
if let Some(obj) = add_vars.as_object() {
if let Some(obj_mut) = template_vars.as_object_mut() {
for (key, value) in obj {
obj_mut.insert(key.clone(), value.clone());
}
}
}
}
// Apply templating with better error handling
let rendered = match handlebars.render_template(&content, &template_vars) {
Ok(result) => result,
Err(e) => {
println!("Warning: Template parsing error in Cargo.toml: {}", e);
println!("Proceeding with unmodified template content");
content
}
};
// Write to target file
fs::write(app_path.join("Cargo.toml"), rendered)?;
// Copy README.md from the template
let readme_src = template_dir_path.join("README.md");
if readme_src.exists() {
let content = fs::read_to_string(&readme_src)?;
// Create handlebars instance for templating
let mut handlebars = Handlebars::new();
// Important: Disable escaping to preserve raw string literals and SVG content
handlebars.register_escape_fn(handlebars::no_escape);
// Disable strict mode to handle more complex templates
handlebars.set_strict_mode(false);
// Create template vars
let mut template_vars = json!({
"project_name": name,
"crate_name": name.replace("-", "_"),
"project_name_pascal_case": to_pascal_case(&name),
"authors": "Your Name <your.email@example.com>"
});
// Merge additional variables if provided
if let Some(add_vars) = additional_vars.as_ref() {
if let Some(obj) = add_vars.as_object() {
if let Some(obj_mut) = template_vars.as_object_mut() {
for (key, value) in obj {
obj_mut.insert(key.clone(), value.clone());
}
}
}
}
// Apply templating with better error handling
let rendered = match handlebars.render_template(&content, &template_vars) {
Ok(result) => result,
Err(e) => {
println!("Warning: Template parsing error in README.md: {}", e);
println!("Proceeding with unmodified template content");
content
}
};
// Write to target file
fs::write(app_path.join("README.md"), rendered)?;
}
// Copy any template-specific files directly from the template directory
for entry in fs::read_dir(&template_dir_path)? {
let entry = entry?;
let file_name = entry.file_name();
let file_name_str = file_name.to_string_lossy();
// Skip already processed files
if file_name_str == "lib.rs" || file_name_str == "template.json" ||
file_name_str == "Cargo.toml.template" || file_name_str == "Cargo.toml" || file_name_str == "README.md" {
continue;
}
let source_path = entry.path();
let target_path = app_path.join(&file_name);
if source_path.is_dir() {
copy_directory(&source_path, &target_path)?;
} else {
// Read the source file
let content = fs::read_to_string(&source_path)?;
// Create handlebars instance for templating
let mut handlebars = Handlebars::new();
// Important: Disable escaping to preserve raw string literals and SVG content
handlebars.register_escape_fn(handlebars::no_escape);
// Disable strict mode to handle more complex templates
handlebars.set_strict_mode(false);
// Create template vars
let mut template_vars = json!({
"project_name": name,
"crate_name": name.replace("-", "_"),
"project_name_pascal_case": to_pascal_case(&name),
"authors": "Your Name <your.email@example.com>"
});
// Merge additional variables if provided
if let Some(add_vars) = additional_vars.as_ref() {
if let Some(obj) = add_vars.as_object() {
if let Some(obj_mut) = template_vars.as_object_mut() {
for (key, value) in obj {
obj_mut.insert(key.clone(), value.clone());
}
}
}
}
// Apply templating with better error handling
let rendered = match handlebars.render_template(&content, &template_vars) {
Ok(result) => result,
Err(e) => {
println!("Warning: Template parsing error in {}: {}", file_name_str, e);
println!("Proceeding with unmodified template content");
content
}
};
// Write to target file
fs::write(target_path, rendered)?;
}
}
// Read template.json to get next steps
let template_json_path = template_dir_path.join("template.json");
if template_json_path.exists() {
let template_json_content = fs::read_to_string(&template_json_path)?;
if let Ok(template_config) = serde_json::from_str::<serde_json::Value>(&template_json_content) {
if let Some(next_steps) = template_config.get("next_steps").and_then(|s| s.as_array()) {
println!("\n✅ {} project created successfully!", name.green());
println!("\n{}", "Next steps:".bold().green());
// Create a handlebars registry for processing templates
let mut handlebars = Handlebars::new();
handlebars.register_escape_fn(handlebars::no_escape);
// Create template vars
let mut data = serde_json::Map::new();
data.insert("project_name".to_string(), json!(name));
// Add template variables to the data
if let Some(ref vars) = additional_vars {
if let Some(obj) = vars.as_object() {
for (k, v) in obj {
data.insert(k.clone(), v.clone());
}
}
}
// Process each next step
for step in next_steps {
if let Some(step_str) = step.as_str() {
// Replace {{project_name}} with the actual project name
let step_text = match handlebars.render_template(step_str, &json!(data)) {
Ok(rendered) => rendered,
Err(_) => step_str.replace("{{project_name}}", name),
};
println!("- {}", step_text);
}
}
return Ok(());
}
}
}
println!("\n🎉 Project {} created successfully!", name);
Ok(())
}