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
//! # types
//!
//! Defines the various types and aliases used by cargo-make.
//!

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

use crate::legacy;
use ci_info::types::CiInfo;
use indexmap::IndexMap;
use rust_info::types::RustInfo;

/// 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
}

#[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,
    /// Only print the execution plan
    pub print_only: bool,
    /// List all known steps
    pub list_all_steps: bool,
    /// 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,
}

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,
            print_only: false,
            list_all_steps: false,
            diff_execution_plan: false,
            disable_check_for_updates: false,
            experimental: false,
            arguments: None,
            output_format: "default".to_string(),
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
/// 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 {
        Cache {
            file_name: None,
            last_update_check: None,
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
/// 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 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 {
            file_name: None,
            log_level: None,
            default_task_name: None,
            update_check_minimum_interval: None,
            search_project_root: Some(false),
        }
    }
}

#[derive(Debug, Clone)]
/// Holds git info for the given repo directory
pub struct GitInfo {
    /// branch name
    pub branch: Option<String>,
    /// user.name
    pub user_name: Option<String>,
    /// user.email
    pub user_email: Option<String>,
}

impl GitInfo {
    /// Returns new instance
    pub fn new() -> GitInfo {
        GitInfo {
            branch: None,
            user_name: None,
            user_email: None,
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
/// 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>>,
}

impl Workspace {
    /// Creates and returns a new instance.
    pub fn new() -> Workspace {
        Workspace {
            members: None,
            exclude: None,
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
/// 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 {
        PackageInfo {
            name: None,
            version: None,
            description: None,
            license: None,
            documentation: None,
            homepage: None,
            repository: None,
        }
    }
}

#[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(Serialize, Deserialize, Debug, Clone)]
/// 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 {
        CrateInfo {
            package: None,
            workspace: None,
            dependencies: None,
        }
    }
}

#[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,
    /// additional command line arguments
    pub cli_arguments: Option<Vec<String>>,
}

#[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)]
/// Holds condition attributes
pub struct TaskCondition {
    /// Profile names (development, ...)
    pub profiles: 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>>,
    /// Rust version condition
    pub rust_version: Option<RustVersionCondition>,
}

#[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>,
}

#[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),
    /// Script which will return the value
    Script(EnvValueScript),
    /// 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 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,
}

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
        {
            false
        } else {
            match self.rustup_component_name {
                Some(ref value) => match other.rustup_component_name {
                    Some(ref other_value) => value == other_value,
                    None => false,
                },
                None => match other.rustup_component_name {
                    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 {
    /// The value as string
    Value(String),
    /// Install crate params
    CrateInfo(InstallCrateInfo),
    /// Install rustup component params
    RustupComponentInfo(InstallRustupComponentInfo),
}

impl PartialEq for InstallCrate {
    fn eq(&self, other: &InstallCrate) -> bool {
        match self {
            InstallCrate::Value(value) => match other {
                InstallCrate::Value(other_value) => value == other_value,
                _ => 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)]
/// Holds the run task information
pub struct RunTaskDetails {
    /// The task name
    pub name: String,
    /// True to fork the task to a new sub process
    pub fork: Option<bool>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
/// Holds the run task routing information
pub struct RunTaskRoutingInfo {
    /// The task name
    pub name: String,
    /// True to fork the task to a new sub process
    pub fork: Option<bool>,
    /// 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<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<String>,
    /// Do not use .gitignore files
    pub no_git_ignore: Option<bool>,
}

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
        };

        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
        }
    }
}

#[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)]
/// 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>,
    /// 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>,
    /// 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<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 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<Vec<String>>,
    /// 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<Vec<String>>,
    /// The script runner (defaults to cmd in windows and sh for other platforms)
    pub script_runner: Option<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<String>>,
    /// The rust toolchain used to invoke the command or install the needed crates/components
    pub toolchain: Option<String>,
    /// 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>,
}

impl Task {
    /// Creates and returns a new instance.
    pub fn new() -> Task {
        Task {
            clear: None,
            description: None,
            category: None,
            disabled: None,
            private: None,
            extend: None,
            workspace: None,
            watch: None,
            condition: None,
            condition_script: None,
            ignore_errors: None,
            force: None,
            env: None,
            cwd: None,
            alias: None,
            linux_alias: None,
            windows_alias: None,
            mac_alias: None,
            install_crate: None,
            install_crate_args: None,
            install_script: None,
            command: None,
            args: None,
            script: None,
            script_runner: None,
            script_extension: None,
            run_task: None,
            dependencies: None,
            toolchain: None,
            linux: None,
            windows: None,
            mac: None,
        }
    }

    /// 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) => {
                                run_task_details.name =
                                    get_namespaced_task_name(namespace, &run_task_details.name);

                                RunTaskInfo::Details(run_task_details)
                            }
                            RunTaskInfo::Routing(mut routing_info_vector) => {
                                for mut routing_info in &mut routing_info_vector {
                                    routing_info.name =
                                        get_namespaced_task_name(namespace, &routing_info.name);
                                }

                                RunTaskInfo::Routing(routing_info_vector)
                            }
                        };

                        self.run_task = Some(run_task);
                    }

                    if self.dependencies.is_some() {
                        let dependencies = self.dependencies.clone().unwrap();
                        let mut modified_dependencies = vec![];

                        for task in &dependencies {
                            modified_dependencies.push(get_namespaced_task_name(namespace, &task));
                        }

                        self.dependencies = Some(modified_dependencies);
                    }
                }
            }
            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.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.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.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.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 = 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 = 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_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(),
                    extend: override_task.extend.clone(),
                    workspace: self.workspace.clone(),
                    watch: override_task.watch.clone(),
                    condition: override_task.condition.clone(),
                    condition_script: override_task.condition_script.clone(),
                    ignore_errors: override_task.ignore_errors.clone(),
                    force: override_task.force.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_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 true if the task is valid
    pub fn is_valid(self: &Task) -> bool {
        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;
        }

        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>,
    /// Extend any task based on the defined name
    pub extend: 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<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 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<Vec<String>>,
    /// 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<Vec<String>>,
    /// The script runner (defaults to cmd in windows and sh for other platforms)
    pub script_runner: Option<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<String>>,
    /// The rust toolchain used to invoke the command or install the needed crates/components
    pub toolchain: Option<String>,
}

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.extend.is_none() && task.extend.is_some() {
                self.extend = task.extend.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.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.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 = 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 = task.script.clone();
            }

            if self.script_runner.is_none() && task.script_runner.is_some() {
                self.script_runner = task.script_runner.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>,
}

#[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)]
/// 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>,
    /// Additional profile names to load
    pub additional_profiles: Option<Vec<String>>,
    /// Minimum cargo-make/makers version
    pub min_version: Option<String>,
    /// Invoked while loading the descriptor file but before loading any extended descriptor
    pub load_script: Option<Vec<String>>,
    /// acts like load_script if runtime OS is Linux (takes precedence over load_script)
    pub linux_load_script: Option<Vec<String>>,
    /// acts like load_script if runtime OS is Windows (takes precedence over load_script)
    pub windows_load_script: Option<Vec<String>>,
    /// acts like load_script if runtime OS is Mac (takes precedence over load_script)
    pub mac_load_script: Option<Vec<String>>,
}

impl ConfigSection {
    /// Creates and returns a new instance.
    pub fn new() -> ConfigSection {
        ConfigSection {
            skip_core_tasks: None,
            modify_core_tasks: None,
            init_task: None,
            end_task: None,
            on_error_task: None,
            additional_profiles: None,
            min_version: None,
            load_script: None,
            linux_load_script: None,
            windows_load_script: None,
            mac_load_script: None,
        }
    }

    /// 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(),
                    ));
                }
            }
            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.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.load_script.is_some() {
            self.load_script = extended.load_script.clone();
        }

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

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

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

    /// Returns the load script based on the current platform
    pub fn get_load_script(self: &ConfigSection) -> Option<Vec<String>> {
        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)]
/// Holds the entire configuration such as task definitions and env vars
pub struct Config {
    /// Runtime config
    pub config: ConfigSection,
    /// The env vars to setup before running the tasks
    pub env: IndexMap<String, EnvValue>,
    /// All task definitions
    pub tasks: IndexMap<String, Task>,
}

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)]
/// 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 vars to setup before running the tasks
    pub env: Option<IndexMap<String, EnvValue>>,
    /// All task definitions
    pub tasks: Option<IndexMap<String, Task>>,
}

impl ExternalConfig {
    /// Creates and returns a new instance.
    pub fn new() -> ExternalConfig {
        ExternalConfig {
            extend: None,
            config: None,
            env: None,
            tasks: None,
        }
    }
}

#[derive(Serialize, 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>>,
}