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
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
//! # types
//!
//! Defines the various types and aliases used by cargo-make.
//!

#[cfg(test)]
#[path = "types_test.rs"]
mod types_test;

use crate::legacy;
use crate::plugin::types::Plugins;
use ci_info::types::CiInfo;
use git_info::types::GitInfo;
use indexmap::{IndexMap, IndexSet};
use regex::Regex;
use rust_info::types::RustInfo;
use std::collections::HashMap;

/// Returns the platform name
pub fn get_platform_name() -> String {
    if cfg!(windows) {
        "windows".to_string()
    } else if cfg!(target_os = "macos") || cfg!(target_os = "ios") {
        "mac".to_string()
    } else {
        "linux".to_string()
    }
}

fn get_namespaced_task_name(namespace: &str, task: &str) -> String {
    let mut namespaced_task = String::new();

    if namespace.len() > 0 {
        namespaced_task.push_str(namespace);
        namespaced_task.push_str("::");
    }
    namespaced_task.push_str(task);

    namespaced_task
}

fn extend_script_value(
    current_script_value: Option<ScriptValue>,
    new_script_value: Option<ScriptValue>,
) -> Option<ScriptValue> {
    match current_script_value {
        Some(ref current_value) => match new_script_value {
            Some(ref new_value) => match current_value {
                ScriptValue::Sections(current_sections) => match new_value {
                    ScriptValue::Sections(new_sections) => {
                        let pre = if new_sections.pre.is_some() {
                            new_sections.pre.clone()
                        } else {
                            current_sections.pre.clone()
                        };
                        let main = if new_sections.main.is_some() {
                            new_sections.main.clone()
                        } else {
                            current_sections.main.clone()
                        };
                        let post = if new_sections.post.is_some() {
                            new_sections.post.clone()
                        } else {
                            current_sections.post.clone()
                        };

                        Some(ScriptValue::Sections(ScriptSections { pre, main, post }))
                    }
                    _ => current_script_value,
                },
                _ => new_script_value,
            },
            None => current_script_value,
        },
        None => new_script_value,
    }
}

#[derive(Debug, Clone)]
/// Holds CLI args
pub struct CliArgs {
    /// The command name
    pub command: String,
    /// The external Makefile.toml path
    pub build_file: Option<String>,
    /// The task to invoke
    pub task: String,
    /// The profile name
    pub profile: Option<String>,
    /// Log level name
    pub log_level: String,
    /// Disables colorful output
    pub disable_color: bool,
    /// Current working directory
    pub cwd: Option<String>,
    /// Environment variables
    pub env: Option<Vec<String>>,
    /// Environment variables file
    pub env_file: Option<String>,
    /// Prevent workspace support
    pub disable_workspace: bool,
    /// Prevent on error flow even if defined in config section
    pub disable_on_error: bool,
    /// Allow invocation of private tasks
    pub allow_private: bool,
    /// If true, the init and end tasks are skipped
    pub skip_init_end_tasks: bool,
    /// Skip tasks that match the provided pattern
    pub skip_tasks_pattern: Option<String>,
    /// Only print the execution plan
    pub print_only: bool,
    /// List all known steps
    pub list_all_steps: bool,
    /// List steps for a given category
    pub list_category_steps: Option<String>,
    /// Diff flows
    pub diff_execution_plan: bool,
    /// Disables the update check during startup
    pub disable_check_for_updates: bool,
    /// Allows access unsupported experimental predefined tasks
    pub experimental: bool,
    /// additional command line arguments
    pub arguments: Option<Vec<String>>,
    /// Output format
    pub output_format: String,
    /// Output file name
    pub output_file: Option<String>,
    /// Print time summary at end of the flow
    pub print_time_summary: bool,
    /// Hide any minor tasks such as pre/post hooks
    pub hide_uninteresting: bool,
}

impl CliArgs {
    /// Creates and returns a new instance.
    pub fn new() -> CliArgs {
        CliArgs {
            command: "".to_string(),
            build_file: None,
            task: "default".to_string(),
            profile: None,
            log_level: "info".to_string(),
            disable_color: false,
            cwd: None,
            env: None,
            env_file: None,
            disable_workspace: false,
            disable_on_error: false,
            allow_private: false,
            skip_init_end_tasks: false,
            skip_tasks_pattern: None,
            print_only: false,
            list_all_steps: false,
            list_category_steps: None,
            diff_execution_plan: false,
            disable_check_for_updates: false,
            experimental: false,
            arguments: None,
            output_format: "default".to_string(),
            output_file: None,
            print_time_summary: false,
            hide_uninteresting: false,
        }
    }
}

#[derive(Debug)]
pub(crate) struct RunTaskOptions {
    pub(crate) plugins_enabled: bool,
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
/// Holds persisted data used by cargo-make
pub struct Cache {
    /// File from which the cache file was loaded from
    #[serde(skip)]
    pub file_name: Option<String>,
    /// Holds last update check with returned no updates result
    pub last_update_check: Option<u64>,
}

impl Cache {
    /// Returns new instance
    pub fn new() -> Cache {
        Default::default()
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
/// Holds configuration info for cargo-make
pub struct GlobalConfig {
    /// File from which the global config was loaded from
    #[serde(skip)]
    pub file_name: Option<String>,
    /// Default log level
    pub log_level: Option<String>,
    /// Default output coloring
    pub disable_color: Option<bool>,
    /// Default task name
    pub default_task_name: Option<String>,
    /// Update check minimum time from the previous check (always, daily, weekly, monthly)
    pub update_check_minimum_interval: Option<String>,
    /// True to search for project root in parent directories if current cwd is not a project root
    pub search_project_root: Option<bool>,
}

impl GlobalConfig {
    /// Returns new instance
    pub fn new() -> GlobalConfig {
        GlobalConfig {
            search_project_root: Some(false),
            ..Default::default()
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
/// Holds crate workspace info, see <http://doc.crates.io/manifest.html#the-workspace-section>
pub struct Workspace {
    /// members paths
    pub members: Option<Vec<String>>,
    /// exclude paths
    pub exclude: Option<Vec<String>>,
    /// workspace level dependencies
    pub dependencies: Option<IndexMap<String, CrateDependency>>,
    /// root package
    pub package: Option<PackageInfo>,
}

impl Workspace {
    /// Creates and returns a new instance.
    pub fn new() -> Workspace {
        Default::default()
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
/// Holds crate package information loaded from the Cargo.toml file package section.
pub struct PackageInfo {
    /// name
    pub name: Option<String>,
    /// version
    pub version: Option<String>,
    /// description
    pub description: Option<String>,
    /// license
    pub license: Option<String>,
    /// documentation link
    pub documentation: Option<String>,
    /// homepage link
    pub homepage: Option<String>,
    /// repository link
    pub repository: Option<String>,
}

impl PackageInfo {
    /// Creates and returns a new instance.
    pub fn new() -> PackageInfo {
        Default::default()
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
/// Holds crate dependency info.
pub struct CrateDependencyInfo {
    /// Holds the dependency path
    pub path: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
/// Holds crate dependency info.
pub enum CrateDependency {
    /// Holds the dependency version
    Version(String),
    /// Hold dependency info
    Info(CrateDependencyInfo),
}

#[derive(Deserialize, Debug, Clone, Default)]
/// Holds crate information loaded from the Cargo.toml file.
pub struct CrateInfo {
    /// package info
    pub package: Option<PackageInfo>,
    /// workspace info
    pub workspace: Option<Workspace>,
    /// crate dependencies
    pub dependencies: Option<IndexMap<String, CrateDependency>>,
}

impl CrateInfo {
    /// Creates and returns a new instance.
    pub fn new() -> CrateInfo {
        Default::default()
    }
}

#[derive(Debug, Clone)]
/// Holds env information
pub struct EnvInfo {
    /// Rust info
    pub rust_info: RustInfo,
    /// Crate info
    pub crate_info: CrateInfo,
    /// Git info
    pub git_info: GitInfo,
    /// CI info
    pub ci_info: CiInfo,
}

#[derive(Debug, Clone)]
/// Holds flow information
pub struct FlowInfo {
    /// The flow config object
    pub config: Config,
    /// The main task of the flow
    pub task: String,
    /// The env info
    pub env_info: EnvInfo,
    /// Prevent workspace support
    pub disable_workspace: bool,
    /// Prevent on error flow even if defined in config section
    pub disable_on_error: bool,
    /// Allow invocation of private tasks
    pub allow_private: bool,
    /// If true, the init and end tasks are skipped
    pub skip_init_end_tasks: bool,
    /// Skip tasks that match the provided pattern
    pub skip_tasks_pattern: Option<Regex>,
    /// additional command line arguments
    pub cli_arguments: Option<Vec<String>>,
}

#[derive(Debug, Clone, Default)]
/// Holds mutable flow state
pub struct FlowState {
    /// timing info for summary
    pub time_summary: Vec<(String, u128)>,
    /// forced plugin name
    pub forced_plugin: Option<String>,
}

impl FlowState {
    /// Creates and returns a new instance.
    pub fn new() -> FlowState {
        Default::default()
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
/// Rust version condition structure
pub struct RustVersionCondition {
    /// min version number
    pub min: Option<String>,
    /// max version number
    pub max: Option<String>,
    /// specific version number
    pub equal: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
/// Files modified (input/output) condition structure
pub struct FilesFilesModifiedCondition {
    /// input files
    pub input: Vec<String>,
    /// output files
    pub output: Vec<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
/// Control how condition checks are evaluated
pub enum ConditionType {
    /// All conditions must pass
    And,
    /// Any condition must pass
    Or,
    /// Any condition group must pass, but each group will be validated as an AND
    GroupOr,
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
/// Holds condition attributes
pub struct TaskCondition {
    /// condition type (AND/OR) by default AND
    pub condition_type: Option<ConditionType>,
    /// Failure message
    pub fail_message: Option<String>,
    /// Profile names (development, ...)
    pub profiles: Option<Vec<String>>,
    /// As defined in the cfg target_os
    pub os: Option<Vec<String>>,
    /// Platform names (linux, windows, mac)
    pub platforms: Option<Vec<String>>,
    /// Channel names (stable, beta, nightly)
    pub channels: Option<Vec<String>>,
    /// Environment variables which must be defined
    pub env_set: Option<Vec<String>>,
    /// Environment variables which must not be defined
    pub env_not_set: Option<Vec<String>>,
    /// Environment variables and their values
    pub env: Option<IndexMap<String, String>>,
    /// Environment variables which are defined as true
    pub env_true: Option<Vec<String>>,
    /// Environment variables which are defined as false
    pub env_false: Option<Vec<String>>,
    /// Environment variables and the values which they are required to contain
    pub env_contains: Option<IndexMap<String, String>>,
    /// Rust version condition
    pub rust_version: Option<RustVersionCondition>,
    /// Files exist
    pub files_exist: Option<Vec<String>>,
    /// Files which do not exist
    pub files_not_exist: Option<Vec<String>>,
    /// Files modified since last execution
    pub files_modified: Option<FilesFilesModifiedCondition>,
}

impl TaskCondition {
    pub fn get_condition_type(&self) -> ConditionType {
        match self.condition_type {
            Some(ref value) => value.clone(),
            None => ConditionType::And,
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
/// Env file path and attributes
pub struct EnvFileInfo {
    /// The file path as string
    pub path: String,
    /// The path base directory (relative paths are from this base path)
    pub base_path: Option<String>,
    /// The profile name this file is relevant to
    pub profile: Option<String>,
    /// If true, only set the env vars if not already defined
    pub defaults_only: Option<bool>,
}

impl EnvFileInfo {
    /// Creates and returns a new instance.
    pub fn new(path: String) -> EnvFileInfo {
        EnvFileInfo {
            path,
            base_path: None,
            profile: None,
            defaults_only: None,
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
/// Holds the env file path and attributes
pub enum EnvFile {
    /// The file path as string
    Path(String),
    /// Extended info object for env file
    Info(EnvFileInfo),
}

#[derive(Serialize, Deserialize, Debug, Clone)]
/// Env value provided by a script
pub struct EnvValueScript {
    /// The script to execute to get the env value
    pub script: Vec<String>,
    /// True/False to enable multi line env values
    pub multi_line: Option<bool>,
    /// The condition to validate
    pub condition: Option<TaskCondition>,
    /// The explicit environment variables this script depends on
    pub depends_on: Option<Vec<String>>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
/// Env value provided by decoding other values
pub struct EnvValueDecode {
    /// The source value (can be an env expression)
    pub source: String,
    /// The default value in case no decode mapping was found, if not provided it will default to the source value
    pub default_value: Option<String>,
    /// The decoding mapping
    pub mapping: HashMap<String, String>,
    /// The condition to validate
    pub condition: Option<TaskCondition>,
}

#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
/// Enables to unset env variables
pub struct EnvValueUnset {
    /// If true, the env variable will be unset, else ignored
    pub unset: bool,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
/// Env value set if condition is met
pub struct EnvValueConditioned {
    /// The value to set (can be an env expression)
    pub value: String,
    /// The condition to validate
    pub condition: Option<TaskCondition>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
/// Env value holding a list of paths based on given glob definitions
pub struct EnvValuePathGlob {
    /// The glob used to fetch all paths
    pub glob: String,
    /// True to include files (default is true if undefined)
    pub include_files: Option<bool>,
    /// True to include directories (default is true if undefined)
    pub include_dirs: Option<bool>,
    /// Enables to respect ignore files
    pub ignore_type: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
/// Holds the env value or script
pub enum EnvValue {
    /// The value as string
    Value(String),
    /// The value as boolean
    Boolean(bool),
    /// The value as number
    Number(isize),
    /// The value as a list of strings
    List(Vec<String>),
    /// Unset env
    Unset(EnvValueUnset),
    /// Script which will return the value
    Script(EnvValueScript),
    /// Env decoding info
    Decode(EnvValueDecode),
    /// Conditional env value
    Conditional(EnvValueConditioned),
    /// Path glob
    PathGlob(EnvValuePathGlob),
    /// Profile env
    Profile(IndexMap<String, EnvValue>),
}

/// Arguments used to check whether a crate or rustup component is installed.
///
/// Deserialize into an array of strings. Allows both a single string (which will
/// become a single-element array) or a sequence of strings.
#[derive(Debug, Serialize, Clone, PartialEq, Eq)]
#[serde(transparent)]
pub struct TestArg {
    /// Content of the arguments
    pub inner: Vec<String>,
}

impl std::ops::Deref for TestArg {
    type Target = Vec<String>;
    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl std::ops::DerefMut for TestArg {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.inner
    }
}

impl<'de> serde::de::Deserialize<'de> for TestArg {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::de::Deserializer<'de>,
    {
        struct StringVecVisitor;
        impl<'de> serde::de::Visitor<'de> for StringVecVisitor {
            type Value = TestArg;

            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                formatter.write_str("A string or an array of strings")
            }

            fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
            where
                E: serde::de::Error,
            {
                Ok(TestArg {
                    inner: vec![s.to_string()],
                })
            }

            fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
            where
                A: serde::de::SeqAccess<'de>,
            {
                let mut v = Vec::with_capacity(seq.size_hint().unwrap_or(0));
                while let Some(s) = seq.next_element()? {
                    v.push(s);
                }

                Ok(TestArg { inner: v })
            }
        }
        deserializer.deserialize_any(StringVecVisitor)
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
/// Holds instructions how to install the cargo plugin
pub struct InstallCargoPluginInfo {
    /// The provided crate to install
    pub crate_name: Option<String>,
    /// Minimal version
    pub min_version: Option<String>,
    /// Optional alternate 'install' command
    pub install_command: Option<String>,
    /// Optional add force flag (if needed), default is true
    pub force: Option<bool>,
}

impl PartialEq for InstallCargoPluginInfo {
    fn eq(&self, other: &InstallCargoPluginInfo) -> bool {
        let mut same = match self.crate_name {
            Some(ref crate_name) => match other.crate_name {
                Some(ref other_crate_name) => crate_name == other_crate_name,
                None => false,
            },
            None => match other.crate_name {
                None => true,
                _ => false,
            },
        };
        if !same {
            return false;
        }

        same = match self.min_version {
            Some(ref min_version) => match other.min_version {
                Some(ref other_min_version) => min_version == other_min_version,
                None => false,
            },
            None => match other.min_version {
                None => true,
                _ => false,
            },
        };
        if !same {
            return false;
        }

        same = match self.install_command {
            Some(ref install_command) => match other.install_command {
                Some(ref other_install_command) => install_command == other_install_command,
                None => false,
            },
            None => match other.install_command {
                None => true,
                _ => false,
            },
        };
        if !same {
            return false;
        }

        match self.force {
            Some(ref force) => match other.force {
                Some(ref other_force) => force == other_force,
                None => false,
            },
            None => match other.force {
                None => true,
                _ => false,
            },
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
/// Holds instructions how to install the crate
pub struct InstallCrateInfo {
    /// The provided crate to install
    pub crate_name: String,
    /// If defined, the component to install via rustup
    pub rustup_component_name: Option<String>,
    /// The binary file name to be used to test if the crate is already installed
    pub binary: String,
    /// Test arguments that will be used to check that the crate is installed.
    pub test_arg: TestArg,
    /// Minimal version
    pub min_version: Option<String>,
    /// Exact version
    pub version: Option<String>,
    /// Optional alternate 'install' command
    pub install_command: Option<String>,
    /// Optional add force flag (if needed), default is true
    pub force: Option<bool>,
}

impl PartialEq for InstallCrateInfo {
    fn eq(&self, other: &InstallCrateInfo) -> bool {
        if self.crate_name != other.crate_name
            || self.binary != other.binary
            || self.test_arg != other.test_arg
        {
            return false;
        }

        let mut same = match self.rustup_component_name {
            Some(ref rustup_component_name) => match other.rustup_component_name {
                Some(ref other_rustup_component_name) => {
                    if rustup_component_name == other_rustup_component_name {
                        true
                    } else {
                        false
                    }
                }
                None => false,
            },
            None => match other.rustup_component_name {
                None => true,
                _ => false,
            },
        };
        if !same {
            return false;
        }

        same = match self.min_version {
            Some(ref min_version) => match other.min_version {
                Some(ref other_min_version) => min_version == other_min_version,
                None => false,
            },
            None => match other.min_version {
                None => true,
                _ => false,
            },
        };
        if !same {
            return false;
        }

        same = match self.version {
            Some(ref version) => match other.version {
                Some(ref other_version) => version == other_version,
                None => false,
            },
            None => match other.version {
                None => true,
                _ => false,
            },
        };
        if !same {
            return false;
        }

        same = match self.install_command {
            Some(ref install_command) => match other.install_command {
                Some(ref other_install_command) => install_command == other_install_command,
                None => false,
            },
            None => match other.install_command {
                None => true,
                _ => false,
            },
        };
        if !same {
            return false;
        }

        match self.force {
            Some(ref force) => match other.force {
                Some(ref other_force) => force == other_force,
                None => false,
            },
            None => match other.force {
                None => true,
                _ => false,
            },
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
/// Holds instructions how to install a rustup component
pub struct InstallRustupComponentInfo {
    /// The component to install via rustup
    pub rustup_component_name: String,
    /// The binary file name to be used to test if the crate is already installed
    pub binary: Option<String>,
    /// Test argument that will be used to check that the crate is installed
    pub test_arg: Option<TestArg>,
}

impl PartialEq for InstallRustupComponentInfo {
    fn eq(&self, other: &InstallRustupComponentInfo) -> bool {
        if self.rustup_component_name != other.rustup_component_name {
            false
        } else {
            let same = match self.binary {
                Some(ref value) => match other.binary {
                    Some(ref other_value) => value == other_value,
                    None => false,
                },
                None => match other.binary {
                    None => true,
                    _ => false,
                },
            };

            if same {
                self.test_arg == other.test_arg
            } else {
                false
            }
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
/// Install crate name or params
pub enum InstallCrate {
    /// Enables to prevent installation flow
    Enabled(bool),
    /// The value as string
    Value(String),
    /// Install crate params
    CrateInfo(InstallCrateInfo),
    /// Install rustup component params
    RustupComponentInfo(InstallRustupComponentInfo),
    /// Install cargo plugin info
    CargoPluginInfo(InstallCargoPluginInfo),
}

impl PartialEq for InstallCrate {
    fn eq(&self, other: &InstallCrate) -> bool {
        match self {
            InstallCrate::Enabled(value) => match other {
                InstallCrate::Enabled(other_value) => value == other_value,
                _ => false,
            },
            InstallCrate::Value(value) => match other {
                InstallCrate::Value(other_value) => value == other_value,
                _ => false,
            },
            InstallCrate::CargoPluginInfo(info) => match other {
                InstallCrate::CargoPluginInfo(other_info) => info == other_info,
                _ => false,
            },
            InstallCrate::CrateInfo(info) => match other {
                InstallCrate::CrateInfo(other_info) => info == other_info,
                _ => false,
            },
            InstallCrate::RustupComponentInfo(info) => match other {
                InstallCrate::RustupComponentInfo(other_info) => info == other_info,
                _ => false,
            },
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(untagged)]
/// Holds the run task name/s
pub enum RunTaskName {
    /// Single task name
    Single(String),
    /// Multiple task names
    Multiple(Vec<String>),
}

#[derive(Serialize, Deserialize, Debug, Clone)]
/// Holds the run task information
pub struct RunTaskDetails {
    /// The task name
    pub name: RunTaskName,
    /// True to fork the task to a new sub process
    pub fork: Option<bool>,
    /// True to run all tasks in parallel (default false)
    pub parallel: Option<bool>,
    /// Cleanup task name
    pub cleanup_task: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
/// Holds the run task routing information
pub struct RunTaskRoutingInfo {
    /// The task name
    pub name: RunTaskName,
    /// True to fork the task to a new sub process
    pub fork: Option<bool>,
    /// True to run all tasks in parallel (default false)
    pub parallel: Option<bool>,
    /// Cleanup task name
    pub cleanup_task: Option<String>,
    /// if provided all condition values must be met in order for the task to be invoked
    pub condition: Option<TaskCondition>,
    /// if script exit code is not 0, the task will not be invoked
    pub condition_script: Option<ConditionScriptValue>,
    /// The script runner arguments before the script file path
    pub condition_script_runner_args: Option<Vec<String>>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
/// Run task info
pub enum RunTaskInfo {
    /// Task name
    Name(String),
    /// Run Task Info
    Details(RunTaskDetails),
    /// Task conditional selector
    Routing(Vec<RunTaskRoutingInfo>),
}

#[derive(Serialize, Deserialize, Debug, Clone)]
/// Holds watch options
pub struct WatchOptions {
    /// Watch version to install if not already installed
    pub version: Option<String>,
    /// Postpone first run until a file changes
    pub postpone: Option<bool>,
    /// Ignore a glob/gitignore-style pattern
    pub ignore_pattern: Option<MaybeArray<String>>,
    /// Do not use .gitignore files
    pub no_git_ignore: Option<bool>,
    /// Show paths that changed
    pub why: Option<bool>,
    /// Select which files/folders to watch
    pub watch: Option<Vec<String>>,
}

#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
#[serde(untagged)]
/// Could be an array or single value
pub enum MaybeArray<T> {
    /// Single value
    Single(T),
    /// Multiple values
    Multiple(Vec<T>),
}

impl PartialEq for WatchOptions {
    fn eq(&self, other: &WatchOptions) -> bool {
        let mut same = match self.version {
            Some(ref value) => match other.version {
                Some(ref other_value) => value == other_value,
                None => false,
            },
            None => match other.version {
                None => true,
                _ => false,
            },
        };

        same = if same {
            match self.postpone {
                Some(ref value) => match other.postpone {
                    Some(ref other_value) => value == other_value,
                    None => false,
                },
                None => match other.postpone {
                    None => true,
                    _ => false,
                },
            }
        } else {
            false
        };

        same = if same {
            match self.ignore_pattern {
                Some(ref value) => match other.ignore_pattern {
                    Some(ref other_value) => value == other_value,
                    None => false,
                },
                None => match other.ignore_pattern {
                    None => true,
                    _ => false,
                },
            }
        } else {
            false
        };

        same = if same {
            match self.no_git_ignore {
                Some(ref value) => match other.no_git_ignore {
                    Some(ref other_value) => value == other_value,
                    None => false,
                },
                None => match other.no_git_ignore {
                    None => true,
                    _ => false,
                },
            }
        } else {
            false
        };

        if same {
            match self.watch {
                Some(ref value) => match other.watch {
                    Some(ref other_value) => value == other_value,
                    None => false,
                },
                None => match other.watch {
                    None => true,
                    _ => false,
                },
            }
        } else {
            false
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
/// Holds watch options or simple true/false value
pub enum TaskWatchOptions {
    /// True/False to enable/disable watch
    Boolean(bool),
    /// Extended configuration for watch
    Options(WatchOptions),
}

impl PartialEq for TaskWatchOptions {
    fn eq(&self, other: &TaskWatchOptions) -> bool {
        match self {
            TaskWatchOptions::Boolean(value) => match other {
                TaskWatchOptions::Boolean(other_value) => value == other_value,
                _ => false,
            },
            TaskWatchOptions::Options(info) => match other {
                TaskWatchOptions::Options(other_info) => info == other_info,
                _ => false,
            },
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
/// Holds deprecation info such as true/false/message
pub enum DeprecationInfo {
    /// True/False flag (true is deprecated)
    Boolean(bool),
    /// Deprecation message
    Message(String),
}

impl PartialEq for DeprecationInfo {
    fn eq(&self, other: &DeprecationInfo) -> bool {
        match self {
            DeprecationInfo::Boolean(value) => match other {
                DeprecationInfo::Boolean(other_value) => value == other_value,
                _ => false,
            },
            DeprecationInfo::Message(message) => match other {
                DeprecationInfo::Message(other_message) => message == other_message,
                _ => false,
            },
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
/// Script file name
pub struct FileScriptValue {
    /// Script file name
    pub file: String,
    /// True for absolute path (default false)
    pub absolute_path: Option<bool>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
/// Script content split to parts to enable a more fine tuned extension capability
pub struct ScriptSections {
    /// Script section
    pub pre: Option<String>,
    /// Script section
    pub main: Option<String>,
    /// Script section
    pub post: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
/// Script value (text, file name, ...)
pub enum ScriptValue {
    /// The script text as single line
    SingleLine(String),
    /// The script text lines
    Text(Vec<String>),
    /// Script file name
    File(FileScriptValue),
    /// Script content split to multiple parts to enable fine tuned extension
    Sections(ScriptSections),
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
/// Condition script value (not as advanced as normal script value)
pub enum ConditionScriptValue {
    /// The script text as single line
    SingleLine(String),
    /// The script text lines
    Text(Vec<String>),
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
/// Holds a single task configuration such as command and dependencies list
pub struct Task {
    /// if true, it should ignore all data in base task
    pub clear: Option<bool>,
    /// Task description
    pub description: Option<String>,
    /// Category name used to document the task
    pub category: Option<String>,
    /// if true, the command/script of this task will not be invoked, dependencies however will be
    pub disabled: Option<bool>,
    /// if true, the task is hidden from the list of available tasks and also cannot be invoked directly from cli
    pub private: Option<bool>,
    /// if not false, this task is defined as deprecated
    pub deprecated: Option<DeprecationInfo>,
    /// Extend any task based on the defined name
    pub extend: Option<String>,
    /// set to false to notify cargo-make that this is not a workspace and should not call task for every member (same as --no-workspace CLI flag)
    pub workspace: Option<bool>,
    /// Optional plugin used to execute the task
    pub plugin: Option<String>,
    /// set to true to watch for file changes and invoke the task operation
    pub watch: Option<TaskWatchOptions>,
    /// if provided all condition values must be met in order for the task to be invoked (will not stop dependencies)
    pub condition: Option<TaskCondition>,
    /// if script exit code is not 0, the command/script of this task will not be invoked, dependencies however will be
    pub condition_script: Option<ConditionScriptValue>,
    /// The script runner arguments before the script file path
    pub condition_script_runner_args: Option<Vec<String>>,
    /// if true, any error while executing the task will be printed but will not break the build
    pub ignore_errors: Option<bool>,
    /// DEPRECATED, replaced with ignore_errors
    pub force: Option<bool>,
    /// The env files to setup before running the task commands
    pub env_files: Option<Vec<EnvFile>>,
    /// The env vars to setup before running the task commands
    pub env: Option<IndexMap<String, EnvValue>>,
    /// The working directory for the task to execute its command/script
    pub cwd: Option<String>,
    /// if defined, task points to another task and all other properties are ignored
    pub alias: Option<String>,
    /// acts like alias if runtime OS is Linux (takes precedence over alias)
    pub linux_alias: Option<String>,
    /// acts like alias if runtime OS is Windows (takes precedence over alias)
    pub windows_alias: Option<String>,
    /// acts like alias if runtime OS is Mac (takes precedence over alias)
    pub mac_alias: Option<String>,
    /// if defined, the provided crate will be installed (if needed) before running the task
    pub install_crate: Option<InstallCrate>,
    /// additional cargo install arguments
    pub install_crate_args: Option<Vec<String>>,
    /// if defined, the provided script will be executed before running the task
    pub install_script: Option<ScriptValue>,
    /// The command to execute
    pub command: Option<String>,
    /// The command args
    pub args: Option<Vec<String>>,
    /// If command is not defined, and script is defined, the provided script will be executed
    pub script: Option<ScriptValue>,
    /// The script runner (defaults to cmd in windows and sh for other platforms)
    pub script_runner: Option<String>,
    /// The script runner arguments before the script file path
    pub script_runner_args: Option<Vec<String>>,
    /// The script file extension
    pub script_extension: Option<String>,
    /// The task name to execute
    pub run_task: Option<RunTaskInfo>,
    /// A list of tasks to execute before this task
    pub dependencies: Option<Vec<DependencyIdentifier>>,
    /// The rust toolchain used to invoke the command or install the needed crates/components
    pub toolchain: Option<ToolchainSpecifier>,
    /// override task if runtime OS is Linux (takes precedence over alias)
    pub linux: Option<PlatformOverrideTask>,
    /// override task if runtime OS is Windows (takes precedence over alias)
    pub windows: Option<PlatformOverrideTask>,
    /// override task if runtime OS is Mac (takes precedence over alias)
    pub mac: Option<PlatformOverrideTask>,
}

/// A toolchain, defined either as a string (following the rustup syntax)
/// or a ToolchainBoundedSpecifier.
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
#[serde(untagged)]
pub enum ToolchainSpecifier {
    /// A string specifying the channel name of the toolchain
    Simple(String),
    /// A toolchain with a minimum version bound
    Bounded(ToolchainBoundedSpecifier),
}

impl From<String> for ToolchainSpecifier {
    fn from(channel: String) -> Self {
        Self::Simple(channel)
    }
}

impl From<&str> for ToolchainSpecifier {
    fn from(channel: &str) -> Self {
        channel.to_string().into()
    }
}

impl std::fmt::Display for ToolchainSpecifier {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Simple(ref channel) => write!(formatter, "{}", channel),
            Self::Bounded(ref spec) => write!(formatter, "{}", spec),
        }
    }
}

impl ToolchainSpecifier {
    /// Return the channel of the toolchain to look for
    pub fn channel(&self) -> &str {
        match self {
            Self::Simple(ref channel) => &channel,
            Self::Bounded(ToolchainBoundedSpecifier { ref channel, .. }) => channel,
        }
    }

    /// Return the minimal version, if any, to look for
    pub fn min_version(&self) -> Option<&str> {
        match self {
            Self::Simple(_) => None,
            Self::Bounded(ToolchainBoundedSpecifier {
                ref min_version, ..
            }) => Some(min_version),
        }
    }
}

/// A toolchain with a minimum version bound
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
pub struct ToolchainBoundedSpecifier {
    /// The channel of the toolchain to use
    pub channel: String,
    /// The minimum version to match
    pub min_version: String,
}

impl std::fmt::Display for ToolchainBoundedSpecifier {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(formatter, "{} >= {}", self.channel, self.min_version)
    }
}

/// A dependency, defined either as a string or as a Dependency object
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
#[serde(untagged)]
pub enum DependencyIdentifier {
    /// A full dependency definition (potentially in a different file)
    Definition(TaskIdentifier),
    /// A string dependency definition (its name in the current file)
    Name(String),
}

impl From<&str> for DependencyIdentifier {
    fn from(name: &str) -> Self {
        DependencyIdentifier::Name(name.to_string())
    }
}

impl DependencyIdentifier {
    /// Get the name of a dependency
    pub fn name(&self) -> &str {
        match self {
            DependencyIdentifier::Definition(identifier) => &identifier.name,
            DependencyIdentifier::Name(name) => name,
        }
    }

    /// Adorn the TaskIdentifier with a namespace
    pub fn with_namespace(self, namespace: &str) -> Self {
        match self {
            DependencyIdentifier::Definition(mut identifier) => {
                identifier.name = get_namespaced_task_name(namespace, &identifier.name);
                DependencyIdentifier::Definition(identifier)
            }
            DependencyIdentifier::Name(name) => {
                DependencyIdentifier::Name(get_namespaced_task_name(namespace, &name))
            }
        }
    }
}

/// An identifier for a task
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
pub struct TaskIdentifier {
    /// The task name to execute
    pub name: String,
    /// The path to the makefile the task resides in
    pub path: Option<String>,
}

impl std::fmt::Display for TaskIdentifier {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self.path {
            Some(path) => write!(formatter, "{}:{}", path, self.name),
            None => write!(formatter, "{}", self.name),
        }
    }
}

impl TaskIdentifier {
    /// Create a new TaskIdentifier referencing a task in the current file
    pub fn from_name(name: &str) -> Self {
        Self {
            name: name.to_string(),
            path: None,
        }
    }
}

impl Into<TaskIdentifier> for DependencyIdentifier {
    fn into(self) -> TaskIdentifier {
        match self {
            DependencyIdentifier::Definition(identifier) => identifier,
            DependencyIdentifier::Name(name) => TaskIdentifier { name, path: None },
        }
    }
}

impl Task {
    /// Creates and returns a new instance.
    pub fn new() -> Task {
        Default::default()
    }

    /// Apply modifications
    pub fn apply(self: &mut Task, modify_config: &ModifyConfig) {
        match modify_config.private {
            Some(value) => {
                if value {
                    self.private = Some(true);
                }
            }
            None => (),
        };

        match modify_config.namespace {
            Some(ref namespace) => {
                if namespace.len() > 0 {
                    if self.extend.is_some() {
                        self.extend = Some(get_namespaced_task_name(
                            namespace,
                            &self.extend.clone().unwrap(),
                        ));
                    }

                    if self.alias.is_some() {
                        self.alias = Some(get_namespaced_task_name(
                            namespace,
                            &self.alias.clone().unwrap(),
                        ));
                    }

                    if self.linux_alias.is_some() {
                        self.linux_alias = Some(get_namespaced_task_name(
                            namespace,
                            &self.linux_alias.clone().unwrap(),
                        ));
                    }

                    if self.windows_alias.is_some() {
                        self.windows_alias = Some(get_namespaced_task_name(
                            namespace,
                            &self.windows_alias.clone().unwrap(),
                        ));
                    }

                    if self.mac_alias.is_some() {
                        self.mac_alias = Some(get_namespaced_task_name(
                            namespace,
                            &self.mac_alias.clone().unwrap(),
                        ));
                    }

                    if self.run_task.is_some() {
                        let mut run_task = self.run_task.clone().unwrap();

                        run_task = match run_task {
                            RunTaskInfo::Name(value) => {
                                RunTaskInfo::Name(get_namespaced_task_name(namespace, &value))
                            }
                            RunTaskInfo::Details(mut run_task_details) => {
                                match run_task_details.name {
                                    RunTaskName::Single(ref name) => {
                                        run_task_details.name = RunTaskName::Single(
                                            get_namespaced_task_name(namespace, name),
                                        )
                                    }
                                    RunTaskName::Multiple(ref names) => {
                                        let mut updated_names = vec![];
                                        for name in names {
                                            updated_names
                                                .push(get_namespaced_task_name(namespace, name));
                                        }

                                        run_task_details.name =
                                            RunTaskName::Multiple(updated_names);
                                    }
                                };

                                RunTaskInfo::Details(run_task_details)
                            }
                            RunTaskInfo::Routing(mut routing_info_vector) => {
                                for routing_info in &mut routing_info_vector {
                                    match routing_info.name {
                                        RunTaskName::Single(ref name) => {
                                            routing_info.name = RunTaskName::Single(
                                                get_namespaced_task_name(namespace, name),
                                            )
                                        }
                                        RunTaskName::Multiple(ref names) => {
                                            let mut updated_names = vec![];
                                            for name in names {
                                                updated_names.push(get_namespaced_task_name(
                                                    namespace, name,
                                                ));
                                            }

                                            routing_info.name =
                                                RunTaskName::Multiple(updated_names);
                                        }
                                    };
                                }

                                RunTaskInfo::Routing(routing_info_vector)
                            }
                        };

                        self.run_task = Some(run_task);
                    }

                    if let Some(dependencies) = &self.dependencies {
                        self.dependencies = Some(
                            dependencies
                                .iter()
                                .map(|identifier| identifier.to_owned().with_namespace(namespace))
                                .collect(),
                        );
                    }
                }
            }
            None => (),
        };
    }

    /// Copies values from the task into self.
    ///
    /// # Arguments
    ///
    /// * `task` - The task to copy from
    pub fn extend(self: &mut Task, task: &Task) {
        let override_values = match task.clear {
            Some(value) => value,
            None => false,
        };

        if task.clear.is_some() {
            self.clear = task.clear.clone();
        }

        if task.description.is_some() {
            self.description = task.description.clone();
        } else if override_values {
            self.description = None;
        }

        if task.category.is_some() {
            self.category = task.category.clone();
        } else if override_values {
            self.category = None;
        }

        if task.disabled.is_some() {
            self.disabled = task.disabled.clone();
        } else if override_values {
            self.disabled = None;
        }

        if task.private.is_some() {
            self.private = task.private.clone();
        } else if override_values {
            self.private = None;
        }

        if task.deprecated.is_some() {
            self.deprecated = task.deprecated.clone();
        } else if override_values {
            self.deprecated = None;
        }

        if task.extend.is_some() {
            self.extend = task.extend.clone();
        } else if override_values {
            self.extend = None;
        }

        if task.workspace.is_some() {
            self.workspace = task.workspace.clone();
        } else if override_values {
            self.workspace = None;
        }

        if task.plugin.is_some() {
            self.plugin = task.plugin.clone();
        } else if override_values {
            self.plugin = None;
        }

        if task.watch.is_some() {
            self.watch = task.watch.clone();
        } else if override_values {
            self.watch = None;
        }

        if task.condition.is_some() {
            self.condition = task.condition.clone();
        } else if override_values {
            self.condition = None;
        }

        if task.condition_script.is_some() {
            self.condition_script = task.condition_script.clone();
        } else if override_values {
            self.condition_script = None;
        }

        if task.condition_script_runner_args.is_some() {
            self.condition_script_runner_args = task.condition_script_runner_args.clone();
        } else if override_values {
            self.condition_script_runner_args = None;
        }

        if task.ignore_errors.is_some() {
            self.ignore_errors = task.ignore_errors.clone();
        } else if override_values {
            self.ignore_errors = None;
        }

        if task.force.is_some() {
            self.force = task.force.clone();
        } else if override_values {
            self.force = None;
        }

        if task.env_files.is_some() {
            self.env_files = task.env_files.clone();
        } else if override_values {
            self.env_files = None;
        }

        if task.env.is_some() {
            self.env = task.env.clone();
        } else if override_values {
            self.env = None;
        }

        if task.cwd.is_some() {
            self.cwd = task.cwd.clone();
        } else if override_values {
            self.cwd = None;
        }

        if task.alias.is_some() {
            self.alias = task.alias.clone();
        } else if override_values {
            self.alias = None;
        }

        if task.linux_alias.is_some() {
            self.linux_alias = task.linux_alias.clone();
        } else if override_values {
            self.linux_alias = None;
        }

        if task.windows_alias.is_some() {
            self.windows_alias = task.windows_alias.clone();
        } else if override_values {
            self.windows_alias = None;
        }

        if task.mac_alias.is_some() {
            self.mac_alias = task.mac_alias.clone();
        } else if override_values {
            self.mac_alias = None;
        }

        if task.install_crate.is_some() {
            self.install_crate = task.install_crate.clone();
        } else if override_values {
            self.install_crate = None;
        }

        if task.install_crate_args.is_some() {
            self.install_crate_args = task.install_crate_args.clone();
        } else if override_values {
            self.install_crate_args = None;
        }

        if task.install_script.is_some() {
            self.install_script =
                extend_script_value(self.install_script.clone(), task.install_script.clone());
        } else if override_values {
            self.install_script = None;
        }

        if task.command.is_some() {
            self.command = task.command.clone();
        } else if override_values {
            self.command = None;
        }

        if task.args.is_some() {
            self.args = task.args.clone();
        } else if override_values {
            self.args = None;
        }

        if task.script.is_some() {
            self.script = extend_script_value(self.script.clone(), task.script.clone());
        } else if override_values {
            self.script = None;
        }

        if task.script_runner.is_some() {
            self.script_runner = task.script_runner.clone();
        } else if override_values {
            self.script_runner = None;
        }

        if task.script_runner_args.is_some() {
            self.script_runner_args = task.script_runner_args.clone();
        } else if override_values {
            self.script_runner_args = None;
        }

        if task.script_extension.is_some() {
            self.script_extension = task.script_extension.clone();
        } else if override_values {
            self.script_extension = None;
        }

        if task.run_task.is_some() {
            self.run_task = task.run_task.clone();
        } else if override_values {
            self.run_task = None;
        }

        if task.dependencies.is_some() {
            self.dependencies = task.dependencies.clone();
        } else if override_values {
            self.dependencies = None;
        }

        if task.toolchain.is_some() {
            self.toolchain = task.toolchain.clone();
        } else if override_values {
            self.toolchain = None;
        }

        if task.linux.is_some() {
            self.linux = task.linux.clone();
        } else if override_values {
            self.linux = None;
        }

        if task.windows.is_some() {
            self.windows = task.windows.clone();
        } else if override_values {
            self.windows = None;
        }

        if task.mac.is_some() {
            self.mac = task.mac.clone();
        } else if override_values {
            self.mac = None;
        }
    }

    /// Returns true if the task ignore_errors attribute is defined and true
    pub fn should_ignore_errors(self: &Task) -> bool {
        match self.ignore_errors {
            Some(value) => value,
            None => match self.force {
                Some(value) => {
                    legacy::show_deprecated_attriute_warning("force", "ignore_errors");

                    value
                }
                None => false,
            },
        }
    }

    /// Returns the override task definition based on the current platform.
    fn get_override(self: &Task) -> Option<PlatformOverrideTask> {
        let platform_name = get_platform_name();
        if platform_name == "windows" {
            match self.windows {
                Some(ref value) => Some(value.clone()),
                _ => None,
            }
        } else if platform_name == "mac" {
            match self.mac {
                Some(ref value) => Some(value.clone()),
                _ => None,
            }
        } else {
            match self.linux {
                Some(ref value) => Some(value.clone()),
                _ => None,
            }
        }
    }

    /// Returns a new task based on the override information and current platform.
    pub fn get_normalized_task(self: &mut Task) -> Task {
        match self.get_override() {
            Some(ref mut override_task) => {
                override_task.extend(self);

                Task {
                    clear: self.clear.clone(),
                    description: self.description.clone(),
                    category: self.category.clone(),
                    disabled: override_task.disabled.clone(),
                    private: override_task.private.clone(),
                    deprecated: override_task.deprecated.clone(),
                    extend: override_task.extend.clone(),
                    workspace: self.workspace.clone(),
                    plugin: override_task.plugin.clone(),
                    watch: override_task.watch.clone(),
                    condition: override_task.condition.clone(),
                    condition_script: override_task.condition_script.clone(),
                    condition_script_runner_args: override_task
                        .condition_script_runner_args
                        .clone(),
                    ignore_errors: override_task.ignore_errors.clone(),
                    force: override_task.force.clone(),
                    env_files: override_task.env_files.clone(),
                    env: override_task.env.clone(),
                    cwd: override_task.cwd.clone(),
                    alias: None,
                    linux_alias: None,
                    windows_alias: None,
                    mac_alias: None,
                    install_crate: override_task.install_crate.clone(),
                    install_crate_args: override_task.install_crate_args.clone(),
                    install_script: override_task.install_script.clone(),
                    command: override_task.command.clone(),
                    args: override_task.args.clone(),
                    script: override_task.script.clone(),
                    script_runner: override_task.script_runner.clone(),
                    script_runner_args: override_task.script_runner_args.clone(),
                    script_extension: override_task.script_extension.clone(),
                    run_task: override_task.run_task.clone(),
                    dependencies: override_task.dependencies.clone(),
                    toolchain: override_task.toolchain.clone(),
                    linux: None,
                    windows: None,
                    mac: None,
                }
            }
            None => self.clone(),
        }
    }

    /// Returns the alias value based on the current platform and task definition.
    pub fn get_alias(self: &Task) -> Option<String> {
        let alias = if cfg!(windows) {
            match self.windows_alias {
                Some(ref value) => Some(value),
                _ => None,
            }
        } else if cfg!(target_os = "macos") || cfg!(target_os = "ios") {
            match self.mac_alias {
                Some(ref value) => Some(value),
                _ => None,
            }
        } else {
            match self.linux_alias {
                Some(ref value) => Some(value),
                _ => None,
            }
        };

        match alias {
            Some(os_alias) => Some(os_alias.clone()),
            _ => match self.alias {
                Some(ref alias) => Some(alias.clone()),
                _ => None,
            },
        }
    }

    /// Returns the amount of actions defined on the task
    pub fn get_actions_count(self: &Task) -> u8 {
        let mut actions_count = 0;

        if self.run_task.is_some() {
            actions_count = actions_count + 1;
        }
        if self.command.is_some() {
            actions_count = actions_count + 1;
        }
        if self.script.is_some() {
            actions_count = actions_count + 1;
        }

        actions_count
    }

    /// Returns true if the task has any actions on its own
    /// or if it modifies the environment in any way.
    pub fn is_actionable(self: &Task) -> bool {
        if self.disabled.unwrap_or(false) {
            return false;
        }

        let actions_count = self.get_actions_count();
        if actions_count > 0 {
            return true;
        }

        if self.install_crate.is_some() || self.install_script.is_some() {
            return true;
        }

        let mut actionable = match self.env {
            Some(ref value) => value.len() > 0,
            None => false,
        };
        if actionable {
            return true;
        }

        actionable = match self.env_files {
            Some(ref value) => value.len() > 0,
            None => false,
        };
        if actionable {
            return true;
        }

        actionable = match self.dependencies {
            Some(ref value) => value.len() > 0,
            None => false,
        };
        if actionable {
            return true;
        }

        actionable = match self.watch {
            Some(ref options) => match options {
                TaskWatchOptions::Boolean(value) => *value,
                _ => true,
            },
            None => false,
        };

        actionable
    }

    /// Returns true if the task is valid
    pub fn is_valid(self: &Task) -> bool {
        let actions_count = self.get_actions_count();

        if actions_count <= 1 {
            true
        } else {
            false
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
/// Holds a single task configuration for a specific platform as an override of another task
pub struct PlatformOverrideTask {
    /// if true, it should ignore all data in base task
    pub clear: Option<bool>,
    /// if true, the command/script of this task will not be invoked, dependencies however will be
    pub disabled: Option<bool>,
    /// if true, the task is hidden from the list of available tasks and also cannot be invoked directly from cli
    pub private: Option<bool>,
    /// if not false, this task is defined as deprecated
    pub deprecated: Option<DeprecationInfo>,
    /// Extend any task based on the defined name
    pub extend: Option<String>,
    /// Optional plugin used to execute the task
    pub plugin: Option<String>,
    /// set to true to watch for file changes and invoke the task operation
    pub watch: Option<TaskWatchOptions>,
    /// if provided all condition values must be met in order for the task to be invoked (will not stop dependencies)
    pub condition: Option<TaskCondition>,
    /// if script exit code is not 0, the command/script of this task will not be invoked, dependencies however will be
    pub condition_script: Option<ConditionScriptValue>,
    /// The script runner arguments before the script file path
    pub condition_script_runner_args: Option<Vec<String>>,
    /// if true, any error while executing the task will be printed but will not break the build
    pub ignore_errors: Option<bool>,
    /// DEPRECATED, replaced with ignore_errors
    pub force: Option<bool>,
    /// The env files to setup before running the task commands
    pub env_files: Option<Vec<EnvFile>>,
    /// The env vars to setup before running the task commands
    pub env: Option<IndexMap<String, EnvValue>>,
    /// The working directory for the task to execute its command/script
    pub cwd: Option<String>,
    /// if defined, the provided crate will be installed (if needed) before running the task
    pub install_crate: Option<InstallCrate>,
    /// additional cargo install arguments
    pub install_crate_args: Option<Vec<String>>,
    /// if defined, the provided script will be executed before running the task
    pub install_script: Option<ScriptValue>,
    /// The command to execute
    pub command: Option<String>,
    /// The command args
    pub args: Option<Vec<String>>,
    /// If command is not defined, and script is defined, the provided script will be executed
    pub script: Option<ScriptValue>,
    /// The script runner (defaults to cmd in windows and sh for other platforms)
    pub script_runner: Option<String>,
    /// The script runner arguments before the script file path
    pub script_runner_args: Option<Vec<String>>,
    /// The script file extension
    pub script_extension: Option<String>,
    /// The task name to execute
    pub run_task: Option<RunTaskInfo>,
    /// A list of tasks to execute before this task
    pub dependencies: Option<Vec<DependencyIdentifier>>,
    /// The rust toolchain used to invoke the command or install the needed crates/components
    pub toolchain: Option<ToolchainSpecifier>,
}

impl PlatformOverrideTask {
    /// Copies values from the task into self.
    ///
    /// # Arguments
    ///
    /// * `task` - The task to copy from
    pub fn extend(self: &mut PlatformOverrideTask, task: &mut Task) {
        let copy_values = match self.clear {
            Some(value) => !value,
            None => true,
        };

        if copy_values {
            if self.disabled.is_none() && task.disabled.is_some() {
                self.disabled = task.disabled.clone();
            }

            if self.private.is_none() && task.private.is_some() {
                self.private = task.private.clone();
            }

            if self.deprecated.is_none() && task.deprecated.is_some() {
                self.deprecated = task.deprecated.clone();
            }

            if self.extend.is_none() && task.extend.is_some() {
                self.extend = task.extend.clone();
            }

            if self.plugin.is_none() && task.plugin.is_some() {
                self.plugin = task.plugin.clone();
            }

            if self.watch.is_none() && task.watch.is_some() {
                self.watch = task.watch.clone();
            }

            if self.condition.is_none() && task.condition.is_some() {
                self.condition = task.condition.clone();
            }

            if self.condition_script.is_none() && task.condition_script.is_some() {
                self.condition_script = task.condition_script.clone();
            }

            if self.condition_script_runner_args.is_none()
                && task.condition_script_runner_args.is_some()
            {
                self.condition_script_runner_args = task.condition_script_runner_args.clone();
            }

            if self.ignore_errors.is_none() && task.ignore_errors.is_some() {
                self.ignore_errors = task.ignore_errors.clone();
            }

            if self.force.is_none() && task.force.is_some() {
                self.force = task.force.clone();
            }

            if self.env_files.is_none() && task.env_files.is_some() {
                self.env_files = task.env_files.clone();
            }

            if self.env.is_none() && task.env.is_some() {
                self.env = task.env.clone();
            }

            if self.cwd.is_none() && task.cwd.is_some() {
                self.cwd = task.cwd.clone();
            }

            if self.install_crate.is_none() && task.install_crate.is_some() {
                self.install_crate = task.install_crate.clone();
            }

            if self.install_crate_args.is_none() && task.install_crate_args.is_some() {
                self.install_crate_args = task.install_crate_args.clone();
            }

            if self.install_script.is_none() && task.install_script.is_some() {
                self.install_script =
                    extend_script_value(self.install_script.clone(), task.install_script.clone());
            }

            if self.command.is_none() && task.command.is_some() {
                self.command = task.command.clone();
            }

            if self.args.is_none() && task.args.is_some() {
                self.args = task.args.clone();
            }

            if self.script.is_none() && task.script.is_some() {
                self.script = extend_script_value(None, task.script.clone());
            }

            if self.script_runner.is_none() && task.script_runner.is_some() {
                self.script_runner = task.script_runner.clone();
            }

            if self.script_runner_args.is_none() && task.script_runner_args.is_some() {
                self.script_runner_args = task.script_runner_args.clone();
            }

            if self.script_extension.is_none() && task.script_extension.is_some() {
                self.script_extension = task.script_extension.clone();
            }

            if self.run_task.is_none() && task.run_task.is_some() {
                self.run_task = task.run_task.clone();
            }

            if self.dependencies.is_none() && task.dependencies.is_some() {
                self.dependencies = task.dependencies.clone();
            }

            if self.toolchain.is_none() && task.toolchain.is_some() {
                self.toolchain = task.toolchain.clone();
            }
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
/// Extend with more fine tuning options
pub struct ExtendOptions {
    /// Path to another makefile
    pub path: String,
    /// Enable optional extend (default to false)
    pub optional: Option<bool>,
    /// Relative to option, sub as current makefile, git root, crate root, workspace root, etc...
    /// Possible values: (makefile, git, crate, workspace)
    pub relative: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
/// Holds makefile extend value
pub enum Extend {
    /// Path to another makefile
    Path(String),
    /// Extend options for more fine tune control
    Options(ExtendOptions),
    /// Multiple extends list
    List(Vec<ExtendOptions>),
}

#[derive(Serialize, Deserialize, Debug, Clone)]
/// Holds properties to modify the core tasks
pub struct ModifyConfig {
    /// If true, all core tasks will be set to private (default false)
    pub private: Option<bool>,
    /// If set to some value, all core tasks are modified to: namespace::name for example default::build
    pub namespace: Option<String>,
}

impl ModifyConfig {
    /// Returns true if config modifications is needed based on the current state
    pub fn is_modifications_defined(self: &ModifyConfig) -> bool {
        if self.private.unwrap_or(false) {
            true
        } else {
            match self.namespace {
                Some(ref value) => value.len() > 0,
                None => false,
            }
        }
    }

    /// Returns the namespace prefix for task names
    pub fn get_namespace_prefix(self: &ModifyConfig) -> String {
        match self.namespace {
            Some(ref value) => get_namespaced_task_name(value, ""),
            None => "".to_string(),
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, Copy, Hash, PartialEq, Eq)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
/// Unstable cargo-make feature
pub enum UnstableFeature {
    /// Gracefully shutdown and then kill the running command on Ctrl+C signal
    CtrlCHandling,
}

impl UnstableFeature {
    /// Creates the env. variable name associated to the feature
    pub fn to_env_name(&self) -> String {
        let mut feature = serde_json::to_string(&self).unwrap();
        feature = feature.replace("\"", "");
        format!("CARGO_MAKE_UNSTABLE_FEATURE_{feature}", feature = feature)
    }

    /// Is the corresponding env. variable set?
    pub fn is_env_set(&self) -> bool {
        envmnt::is(self.to_env_name())
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
/// Holds the configuration found in the makefile toml config section.
pub struct ConfigSection {
    /// If true, the default core tasks will not be loaded
    pub skip_core_tasks: Option<bool>,
    /// Modify core tasks config
    pub modify_core_tasks: Option<ModifyConfig>,
    /// Init task name which will be invoked at the start of every run
    pub init_task: Option<String>,
    /// End task name which will be invoked at the end of every run
    pub end_task: Option<String>,
    /// The name of the task to run in case of any error during the invocation of the flow
    pub on_error_task: Option<String>,
    /// The name of the task which runs legacy migration flows
    pub legacy_migration_task: Option<String>,
    /// Additional profile names to load
    pub additional_profiles: Option<Vec<String>>,
    /// Minimum cargo-make/makers version
    pub min_version: Option<String>,
    /// The task.workspace default value
    pub default_to_workspace: Option<bool>,
    /// do not load git env info (save on perf)
    pub skip_git_env_info: Option<bool>,
    /// do not load rust env info (save on perf)
    pub skip_rust_env_info: Option<bool>,
    /// do not load current crate env info (save on perf)
    pub skip_crate_env_info: Option<bool>,
    /// True to reduce console output for non CI execution
    pub reduce_output: Option<bool>,
    /// True to print time summary at the end of the flow
    pub time_summary: Option<bool>,
    /// Automatically load cargo aliases as cargo-make tasks
    pub load_cargo_aliases: Option<bool>,
    /// If true (default false) disable all automatic/defined installation instructions
    pub disable_install: Option<bool>,
    /// The project information member (used by workspaces)
    pub main_project_member: Option<String>,
    /// Invoked while loading the descriptor file but before loading any extended descriptor
    pub load_script: Option<ScriptValue>,
    /// acts like load_script if runtime OS is Linux (takes precedence over load_script)
    pub linux_load_script: Option<ScriptValue>,
    /// acts like load_script if runtime OS is Windows (takes precedence over load_script)
    pub windows_load_script: Option<ScriptValue>,
    /// acts like load_script if runtime OS is Mac (takes precedence over load_script)
    pub mac_load_script: Option<ScriptValue>,
    /// Enables unstable cargo-make features
    pub unstable_features: Option<IndexSet<UnstableFeature>>,
}

impl ConfigSection {
    /// Creates and returns a new instance.
    pub fn new() -> ConfigSection {
        Default::default()
    }

    /// Apply modifications
    pub fn apply(self: &mut ConfigSection, modify_config: &ModifyConfig) {
        match modify_config.namespace {
            Some(ref namespace) => {
                if self.init_task.is_some() {
                    self.init_task = Some(get_namespaced_task_name(
                        namespace,
                        &self.init_task.clone().unwrap(),
                    ));
                }

                if self.end_task.is_some() {
                    self.end_task = Some(get_namespaced_task_name(
                        namespace,
                        &self.end_task.clone().unwrap(),
                    ));
                }

                if self.on_error_task.is_some() {
                    self.on_error_task = Some(get_namespaced_task_name(
                        namespace,
                        &self.on_error_task.clone().unwrap(),
                    ));
                }

                if self.legacy_migration_task.is_some() {
                    self.legacy_migration_task = Some(get_namespaced_task_name(
                        namespace,
                        &self.legacy_migration_task.clone().unwrap(),
                    ));
                }
            }
            None => (),
        }
    }

    /// Copies values from the config section into self.
    ///
    /// # Arguments
    ///
    /// * `task` - The task to copy from
    pub fn extend(self: &mut ConfigSection, extended: &mut ConfigSection) {
        if extended.skip_core_tasks.is_some() {
            self.skip_core_tasks = extended.skip_core_tasks.clone();
        }

        if extended.modify_core_tasks.is_some() {
            self.modify_core_tasks = extended.modify_core_tasks.clone();
        }

        if extended.init_task.is_some() {
            self.init_task = extended.init_task.clone();
        }

        if extended.end_task.is_some() {
            self.end_task = extended.end_task.clone();
        }

        if extended.on_error_task.is_some() {
            self.on_error_task = extended.on_error_task.clone();
        }

        if extended.legacy_migration_task.is_some() {
            self.legacy_migration_task = extended.legacy_migration_task.clone();
        }

        if extended.additional_profiles.is_some() {
            self.additional_profiles = extended.additional_profiles.clone();
        }

        if extended.min_version.is_some() {
            self.min_version = extended.min_version.clone();
        }

        if extended.default_to_workspace.is_some() {
            self.default_to_workspace = extended.default_to_workspace.clone();
        }

        if extended.skip_git_env_info.is_some() {
            self.skip_git_env_info = extended.skip_git_env_info.clone();
        }

        if extended.skip_rust_env_info.is_some() {
            self.skip_rust_env_info = extended.skip_rust_env_info.clone();
        }

        if extended.skip_crate_env_info.is_some() {
            self.skip_crate_env_info = extended.skip_crate_env_info.clone();
        }

        if extended.reduce_output.is_some() {
            self.reduce_output = extended.reduce_output.clone();
        }

        if extended.time_summary.is_some() {
            self.time_summary = extended.time_summary.clone();
        }

        if extended.load_cargo_aliases.is_some() {
            self.load_cargo_aliases = extended.load_cargo_aliases.clone();
        }

        if extended.disable_install.is_some() {
            self.disable_install = extended.disable_install.clone();
        }

        if extended.main_project_member.is_some() {
            self.main_project_member = extended.main_project_member.clone();
        }

        if extended.load_script.is_some() {
            self.load_script =
                extend_script_value(self.load_script.clone(), extended.load_script.clone());
        }

        if extended.linux_load_script.is_some() {
            self.linux_load_script = extend_script_value(
                self.linux_load_script.clone(),
                extended.linux_load_script.clone(),
            );
        }

        if extended.windows_load_script.is_some() {
            self.windows_load_script = extend_script_value(
                self.windows_load_script.clone(),
                extended.windows_load_script.clone(),
            );
        }

        if extended.mac_load_script.is_some() {
            self.mac_load_script = extend_script_value(
                self.mac_load_script.clone(),
                extended.mac_load_script.clone(),
            );
        }

        if let Some(extended_unstable_features) = extended.unstable_features.clone() {
            if let Some(unstable_features) = &mut self.unstable_features {
                unstable_features.extend(extended_unstable_features);
            } else {
                self.unstable_features = Some(extended_unstable_features);
            }
        }
    }

    /// Returns the load script based on the current platform
    pub fn get_load_script(self: &ConfigSection) -> Option<ScriptValue> {
        let platform_name = get_platform_name();

        if platform_name == "windows" {
            if self.windows_load_script.is_some() {
                self.windows_load_script.clone()
            } else {
                self.load_script.clone()
            }
        } else if platform_name == "mac" {
            if self.mac_load_script.is_some() {
                self.mac_load_script.clone()
            } else {
                self.load_script.clone()
            }
        } else {
            if self.linux_load_script.is_some() {
                self.linux_load_script.clone()
            } else {
                self.load_script.clone()
            }
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
/// Holds the entire configuration such as task definitions and env vars
pub struct Config {
    /// Runtime config
    pub config: ConfigSection,
    /// The env files to setup before running the flow
    pub env_files: Vec<EnvFile>,
    /// The env vars to setup before running the flow
    pub env: IndexMap<String, EnvValue>,
    /// The env scripts to execute before running the flow
    pub env_scripts: Vec<String>,
    /// All task definitions
    pub tasks: IndexMap<String, Task>,
    /// All plugin definitions
    pub plugins: Option<Plugins>,
}

impl Config {
    /// Apply modifications
    pub fn apply(self: &mut Config, modify_config: &ModifyConfig) {
        self.config.apply(&modify_config);

        let namespace = match modify_config.namespace {
            Some(ref namespace) => namespace,
            None => "",
        };

        let mut modified_tasks = IndexMap::<String, Task>::new();

        for (key, value) in self.tasks.iter() {
            let namespaced_task = get_namespaced_task_name(namespace, &key);
            let mut task = value.clone();

            task.apply(&modify_config);

            modified_tasks.insert(namespaced_task, task);
        }

        self.tasks = modified_tasks;
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
/// Holds the entire externally read configuration such as task definitions and env vars where all values are optional
pub struct ExternalConfig {
    /// Path to another toml file to extend
    pub extend: Option<Extend>,
    /// Runtime config
    pub config: Option<ConfigSection>,
    /// The env files to setup before running the flow
    pub env_files: Option<Vec<EnvFile>>,
    /// The env vars to setup before running the flow
    pub env: Option<IndexMap<String, EnvValue>>,
    /// The env scripts to execute before running the flow
    pub env_scripts: Option<Vec<String>>,
    /// All task definitions
    pub tasks: Option<IndexMap<String, Task>>,
    /// All plugin definitions
    pub plugins: Option<Plugins>,
}

impl ExternalConfig {
    /// Creates and returns a new instance.
    pub fn new() -> ExternalConfig {
        Default::default()
    }
}

#[derive(Serialize, Clone, Debug)]
/// Execution plan step to execute
pub struct Step {
    /// The task name
    pub name: String,
    /// The task config
    pub config: Task,
}

#[derive(Debug)]
/// Execution plan which defines all steps to run and the order to run them
pub struct ExecutionPlan {
    /// A list of steps to execute
    pub steps: Vec<Step>,
}

#[derive(Debug)]
/// Command info
pub struct CommandSpec {
    /// The command to execute
    pub command: String,
    /// The command args
    pub args: Option<Vec<String>>,
}