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
use cargo_toml::Manifest;
use radix_common::prelude::*;
use radix_engine::utils::{extract_definition, ExtractSchemaError};
use radix_engine_interface::{blueprints::package::PackageDefinition, types::Level};
use radix_rust::prelude::{IndexMap, IndexSet};
use std::error::Error;
use std::path::{Path, PathBuf};
use std::process::{Command, ExitStatus, Stdio};
use std::{env, io};

const MANIFEST_FILE: &str = "Cargo.toml";
const BUILD_TARGET: &str = "wasm32-unknown-unknown";
const SCRYPTO_NO_SCHEMA: &str = "scrypto/no-schema";
const SCRYPTO_COVERAGE: &str = "scrypto/coverage";

#[derive(Debug)]
pub enum ScryptoCompilerError {
    /// Returns IO Error which occurred during compilation and optional context information.
    IOError(io::Error, Option<String>),
    /// Returns IO Error which occurred during compilation, path of a file related to that fail and
    /// optional context information.
    IOErrorWithPath(io::Error, PathBuf, Option<String>),
    /// Returns process exit status in case of 'cargo build' fail.
    CargoBuildFailure(ExitStatus),
    /// Returns `cargo metadata` command stderr output, path to Cargo.toml for which cargo metadata
    /// command failed and process exit status.
    CargoMetadataFailure(String, PathBuf, ExitStatus),
    /// Returns path to Cargo.toml for which results of cargo metadata command is not not valid json
    /// or target directory field is missing.
    CargoTargetDirectoryResolutionError(String),
    /// Compiler is unable to generate target binary file name.
    CargoTargetBinaryResolutionError,
    /// Returns path to Cargo.toml which was failed to load.
    CargoManifestLoadFailure(String),
    /// Returns path to Cargo.toml which cannot be found.
    CargoManifestFileNotFound(String),
    /// Provided package ID is not a member of the workspace.
    CargoWrongPackageId(String),
    /// Returns WASM Optimization error.
    WasmOptimizationError(wasm_opt::OptimizationError),
    /// Returns error occured during schema extraction.
    SchemaExtractionError(ExtractSchemaError),
    /// Returns error occured during schema encoding.
    SchemaEncodeError(EncodeError),
    /// Returned when trying to compile workspace without any scrypto packages.
    NothingToCompile,
}

#[derive(Clone)]
pub struct ScryptoCompilerInputParams {
    /// Path to Cargo.toml file, if not specified current directory will be used.
    pub manifest_path: Option<PathBuf>,
    /// Path to directory where compilation artifacts are stored, if not specified default location will by used.
    pub target_directory: Option<PathBuf>,
    /// Compilation profile. If not specified default profile: Release will be used.
    pub profile: Profile,
    /// List of environment variables to set or unest during compilation. Optional field.
    pub environment_variables: IndexMap<String, EnvironmentVariableAction>,
    /// List of features, used for 'cargo build --features'. Optional field.
    pub features: IndexSet<String>,
    /// If set to true then '--no-default-features' option is passed to 'cargo build'. Defult value is false.
    pub no_default_features: bool,
    /// If set to true then '--all-features' option is passed to 'cargo build'. Defult value is false.
    pub all_features: bool,
    /// List of packages to compile, used for 'cargo build --package'. Optional field.
    pub package: IndexSet<String>,
    /// List of custom options, passed as 'cargo build' arguments without any modifications. Optional field.
    /// Add each option as separate entry (for instance: '-j 1' must be added as two entires: '-j' and '1' one by one).
    pub custom_options: IndexSet<String>,
    /// If specified optimizes the built wasm using Binaryen's wasm-opt tool.
    /// Default configuration is equivalent to running the following commands in the CLI:
    /// wasm-opt -0z --strip-debug --strip-dwarf --strip-procedures $some_path $some_path
    pub wasm_optimization: Option<wasm_opt::OptimizationOptions>,
}
impl Default for ScryptoCompilerInputParams {
    /// Definition of default `ScryptoCompiler` configuration.
    fn default() -> Self {
        let wasm_optimization = Some(
            wasm_opt::OptimizationOptions::new_optimize_for_size_aggressively()
                .add_pass(wasm_opt::Pass::StripDebug)
                .add_pass(wasm_opt::Pass::StripDwarf)
                .add_pass(wasm_opt::Pass::StripProducers)
                .to_owned(),
        );
        let mut ret = Self {
            manifest_path: None,
            target_directory: None,
            profile: Profile::Release,
            environment_variables: indexmap!(),
            features: indexset!(),
            no_default_features: false,
            all_features: false,
            package: indexset!(),
            custom_options: indexset!(),
            wasm_optimization,
        };
        // Apply default log level features
        ret.features
            .extend(Self::log_level_to_scrypto_features(Level::default()).into_iter());
        ret
    }
}
impl ScryptoCompilerInputParams {
    pub fn log_level_to_scrypto_features(log_level: Level) -> Vec<String> {
        let mut ret = Vec::new();
        if Level::Error <= log_level {
            ret.push(String::from("scrypto/log-error"));
        }
        if Level::Warn <= log_level {
            ret.push(String::from("scrypto/log-warn"));
        }
        if Level::Info <= log_level {
            ret.push(String::from("scrypto/log-info"));
        }
        if Level::Debug <= log_level {
            ret.push(String::from("scrypto/log-debug"));
        }
        if Level::Trace <= log_level {
            ret.push(String::from("scrypto/log-trace"));
        }
        ret
    }
}

#[derive(Debug, Default, Clone)]
pub enum Profile {
    #[default]
    Release,
    Debug,
    Test,
    Bench,
    Custom(String),
}
impl Profile {
    fn as_command_args(&self) -> Vec<String> {
        vec![
            String::from("--profile"),
            match self {
                Profile::Release => String::from("release"),
                Profile::Debug => String::from("dev"),
                Profile::Test => String::from("test"),
                Profile::Bench => String::from("bench"),
                Profile::Custom(name) => name.clone(),
            },
        ]
    }
    fn as_target_directory_name(&self) -> String {
        match self {
            Profile::Release => String::from("release"),
            Profile::Debug => String::from("debug"),
            Profile::Test => String::from("debug"),
            Profile::Bench => String::from("release"),
            Profile::Custom(name) => name.clone(),
        }
    }
}
#[derive(Debug, PartialEq, Eq)]
pub struct ParseProfileError;
impl fmt::Display for ParseProfileError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_fmt(format_args!("{:?}", self))
    }
}
impl Error for ParseProfileError {}

impl FromStr for Profile {
    type Err = ParseProfileError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "release" => Ok(Profile::Release),
            "debug" => Ok(Profile::Debug),
            "test" => Ok(Profile::Test),
            "bench" => Ok(Profile::Bench),
            other => {
                if other.contains(' ') {
                    Err(ParseProfileError)
                } else {
                    Ok(Profile::Custom(other.to_string()))
                }
            }
        }
    }
}

#[derive(Clone)]
pub enum EnvironmentVariableAction {
    Set(String),
    Unset,
}

#[derive(Debug)]
pub struct BuildArtifacts {
    pub wasm: BuildArtifact<Vec<u8>>,
    pub package_definition: BuildArtifact<PackageDefinition>,
}

#[derive(Debug, Clone)]
pub struct BuildArtifact<T> {
    pub path: PathBuf,
    pub content: T,
}

#[derive(Debug, Clone)]
pub struct CompilerManifestDefinition {
    /// Path to Cargo.toml file.
    pub manifest_path: PathBuf,
    /// Path to directory where compilation artifacts are stored.
    pub target_directory: PathBuf,
    /// Path to target binary WASM file.
    pub target_binary_wasm_path: PathBuf,
    /// Path to target binary RPD file.
    pub target_binary_rpd_path: PathBuf,
}

/// Programmatic implementation of Scrypto compiler which is a wrapper around rust cargo tool.
/// To create an instance of `ScryptoCompiler` use `builder()` constructor which implements builder pattern,
/// provide any required parameter @see `ScryptoCompilerInputParams` and finally call `compile()` function.
/// `ScryptoCompiler` supports worspace compilation by providing workspace manifest as `manifest_path` parameter of
/// running compiler from directory containg workspace Cargo.toml file. Only packages with defined metadata group:
/// [package.metadata.scrypto] will be used during workspace compilation (so workspace manifest can contain also non
/// Scrypto packages). Alternativelly packages for workspace compilation can be provided in `package` input parameter,
/// metadata is not validated in that case.
/// Compilation results consists of list of `BuildArtifacts` which contains generated WASM file path and its content
/// and path to RPD file with package definition and `PackageDefinition` struct.
pub struct ScryptoCompiler {
    /// Scrypto compiler input parameters.
    input_params: ScryptoCompilerInputParams,
    /// Manifest definition used in 'cargo build' command calls. For workspace compilation this is a workspace manifest,
    /// for non-workspace compilation it is particular project manifest.
    /// 'cargo build' command will automatically build all workspace members for workspace compilation.
    main_manifest: CompilerManifestDefinition,
    /// List of manifest definitions in workspace compilation.
    manifests: Vec<CompilerManifestDefinition>,
}

impl ScryptoCompiler {
    pub fn builder() -> ScryptoCompilerBuilder {
        ScryptoCompilerBuilder::default()
    }

    // Internal constructor
    fn from_input_params(
        input_params: &mut ScryptoCompilerInputParams,
    ) -> Result<Self, ScryptoCompilerError> {
        let manifest_path = Self::get_manifest_path(&input_params.manifest_path)?;

        // If compiling workspace use only packages which defines [package.metadata.scrypto]
        // or only specified packages with --package parameter
        if let Some(workspace_members) = ScryptoCompiler::is_manifest_workspace(&manifest_path)? {
            // Verify if provided package names belongs to this workspace
            if !input_params.package.is_empty() {
                let wrong_packages: Vec<_> = input_params
                    .package
                    .iter()
                    .filter(|package| {
                        workspace_members
                            .iter()
                            .find(|(_, member_package_name, _)| &member_package_name == package)
                            .is_none()
                    })
                    .collect();
                if let Some(package) = wrong_packages.first() {
                    return Err(ScryptoCompilerError::CargoWrongPackageId(
                        package.to_string(),
                    ));
                }
            } else {
                input_params.package = workspace_members
                    .iter()
                    .filter_map(|(_, package, scrypto_metadata)| {
                        if scrypto_metadata.is_some() {
                            Some(package.clone())
                        } else {
                            None
                        }
                    })
                    .collect();
                if input_params.package.is_empty() {
                    return Err(ScryptoCompilerError::NothingToCompile);
                }
            }

            let manifests = workspace_members
                .into_iter()
                .filter_map(|(member_manifest_input_path, package, _)| {
                    if input_params.package.contains(&package) {
                        Some(
                            match ScryptoCompiler::get_manifest_path(&Some(
                                member_manifest_input_path,
                            )) {
                                Ok(member_manifest_path) => ScryptoCompiler::prepare_manifest_def(
                                    input_params,
                                    &member_manifest_path,
                                ),
                                Err(x) => Err(x),
                            },
                        )
                    } else {
                        None
                    }
                })
                .collect::<Result<Vec<CompilerManifestDefinition>, ScryptoCompilerError>>()?;

            Ok(Self {
                input_params: input_params.to_owned(),
                main_manifest: ScryptoCompiler::prepare_manifest_def(input_params, &manifest_path)?,
                manifests,
            })
        } else {
            Ok(Self {
                input_params: input_params.to_owned(),
                main_manifest: ScryptoCompiler::prepare_manifest_def(input_params, &manifest_path)?,
                manifests: Vec::new(),
            })
        }
    }

    // Generates target paths basing on manifest path
    fn prepare_manifest_def(
        input_params: &ScryptoCompilerInputParams,
        manifest_path: &Path,
    ) -> Result<CompilerManifestDefinition, ScryptoCompilerError> {
        let (target_directory, target_binary_wasm_path, target_binary_rpd_path) =
            ScryptoCompiler::prepare_paths_for_manifest(input_params, manifest_path)?;

        Ok(CompilerManifestDefinition {
            manifest_path: manifest_path.to_path_buf(),
            target_directory,
            target_binary_wasm_path,
            target_binary_rpd_path,
        })
    }

    fn get_default_target_directory(manifest_path: &Path) -> Result<String, ScryptoCompilerError> {
        let output = Command::new("cargo")
            .arg("metadata")
            .arg("--manifest-path")
            .arg(manifest_path)
            .arg("--format-version")
            .arg("1")
            .arg("--no-deps")
            .output()
            .map_err(|e| {
                ScryptoCompilerError::IOErrorWithPath(
                    e,
                    manifest_path.to_path_buf(),
                    Some(String::from("Cargo metadata for manifest failed.")),
                )
            })?;
        if output.status.success() {
            let parsed =
                serde_json::from_slice::<serde_json::Value>(&output.stdout).map_err(|_| {
                    ScryptoCompilerError::CargoTargetDirectoryResolutionError(
                        manifest_path.display().to_string(),
                    )
                })?;
            let target_directory = parsed
                .as_object()
                .and_then(|o| o.get("target_directory"))
                .and_then(|o| o.as_str())
                .ok_or(ScryptoCompilerError::CargoTargetDirectoryResolutionError(
                    manifest_path.display().to_string(),
                ))?;
            Ok(target_directory.to_owned())
        } else {
            Err(ScryptoCompilerError::CargoMetadataFailure(
                String::from_utf8_lossy(&output.stderr).to_string(),
                manifest_path.to_path_buf(),
                output.status,
            ))
        }
    }

    // Returns path to Cargo.toml (including the file)
    fn get_manifest_path(
        input_manifest_path: &Option<PathBuf>,
    ) -> Result<PathBuf, ScryptoCompilerError> {
        let manifest_path = match input_manifest_path.clone() {
            Some(mut path) => {
                if !path.ends_with(MANIFEST_FILE) {
                    path.push(MANIFEST_FILE);
                }
                path
            }
            None => {
                let mut path = env::current_dir().map_err(|e| {
                    ScryptoCompilerError::IOError(
                        e,
                        Some(String::from("Getting current directory failed.")),
                    )
                })?;
                path.push(MANIFEST_FILE);
                path
            }
        };

        if !manifest_path.exists() {
            Err(ScryptoCompilerError::CargoManifestFileNotFound(
                manifest_path.display().to_string(),
            ))
        } else {
            Ok(manifest_path)
        }
    }

    // If manifest is a workspace this function returns non-empty vector of tuple with workspace members (path),
    // package name and package scrypto metadata (content of section from Cargo.toml [package.metadata.scrypto]).
    fn is_manifest_workspace(
        manifest_path: &Path,
    ) -> Result<Option<Vec<(PathBuf, String, Option<cargo_toml::Value>)>>, ScryptoCompilerError>
    {
        let manifest = Manifest::from_path(&manifest_path).map_err(|_| {
            ScryptoCompilerError::CargoManifestLoadFailure(manifest_path.display().to_string())
        })?;
        if let Some(workspace) = manifest.workspace {
            if workspace.members.is_empty() {
                Ok(None)
            } else {
                Ok(Some(
                    workspace
                        .members
                        .iter()
                        .map(|i| {
                            let mut member_manifest_input_path = manifest_path.to_path_buf();
                            member_manifest_input_path.pop(); // Workspace Cargo.toml file
                            member_manifest_input_path.push(PathBuf::from(i));
                            member_manifest_input_path.push(MANIFEST_FILE); // Manifest Cargo.toml file

                            match Manifest::from_path(&member_manifest_input_path) {
                                Ok(manifest) => {
                                    let metadata = match &manifest.package().metadata {
                                        Some(cargo_toml::Value::Table(map)) => {
                                            map.get("scrypto").cloned()
                                        }
                                        _ => None,
                                    };
                                    Ok((
                                        member_manifest_input_path,
                                        manifest.package().name().to_string(),
                                        metadata,
                                    ))
                                }
                                Err(_) => Err(ScryptoCompilerError::CargoManifestLoadFailure(
                                    member_manifest_input_path.display().to_string(),
                                )),
                            }
                        })
                        .collect::<Result<Vec<_>, ScryptoCompilerError>>()?,
                ))
            }
        } else {
            Ok(None)
        }
    }

    fn get_target_binary_name(
        manifest_path: &Path,
    ) -> Result<Option<String>, ScryptoCompilerError> {
        // Find the binary name
        let manifest = Manifest::from_path(&manifest_path).map_err(|_| {
            ScryptoCompilerError::CargoManifestLoadFailure(manifest_path.display().to_string())
        })?;
        if let Some(w) = manifest.workspace {
            if !w.members.is_empty() {
                // For workspace compilation there is no binary file for the main manifest
                return Ok(None);
            }
        }
        let mut wasm_name = None;
        if let Some(lib) = manifest.lib {
            wasm_name = lib.name.clone();
        }
        if wasm_name.is_none() {
            if let Some(pkg) = manifest.package {
                wasm_name = Some(pkg.name.replace("-", "_"));
            }
        }
        Ok(Some(wasm_name.ok_or(
            ScryptoCompilerError::CargoTargetBinaryResolutionError,
        )?))
    }

    // Basing on manifest path returns target directory, target binary WASM path and target binary PRD path
    fn prepare_paths_for_manifest(
        input_params: &ScryptoCompilerInputParams,
        manifest_path: &Path,
    ) -> Result<(PathBuf, PathBuf, PathBuf), ScryptoCompilerError> {
        // Generate target directory
        let target_directory = if let Some(directory) = &input_params.target_directory {
            // If target directory is explicitly specified as compiler parameter then use it as is
            PathBuf::from(directory)
        } else {
            // If target directory is not specified as compiler parameter then get default
            // target directory basing on manifest file
            PathBuf::from(&Self::get_default_target_directory(&manifest_path)?)
        };

        let (target_binary_wasm_path, target_binary_rpd_path) =
            if let Some(target_binary_name) = Self::get_target_binary_name(&manifest_path)? {
                let mut target_binary_wasm_path = target_directory.clone();
                target_binary_wasm_path.push(BUILD_TARGET);
                target_binary_wasm_path.push(input_params.profile.as_target_directory_name());
                target_binary_wasm_path.push(target_binary_name.clone());
                target_binary_wasm_path.set_extension("wasm");

                let mut target_binary_rpd_path = target_directory.clone();
                target_binary_rpd_path.push(BUILD_TARGET);
                target_binary_rpd_path.push(Profile::Release.as_target_directory_name());
                target_binary_rpd_path.push(target_binary_name);
                target_binary_rpd_path.set_extension("rpd");

                (target_binary_wasm_path, target_binary_rpd_path)
            } else {
                // for workspace compilation these paths are empty
                (PathBuf::new(), PathBuf::new())
            };

        Ok((
            target_directory,
            target_binary_wasm_path,
            target_binary_rpd_path,
        ))
    }

    // Prepares OS command arguments
    fn prepare_command(&mut self, command: &mut Command, for_package_extract: bool) {
        let mut features: Vec<[&str; 2]> = self
            .input_params
            .features
            .iter()
            .map(|f| ["--features", f])
            .collect();
        if let Some(idx) = features
            .iter()
            .position(|[_tag, value]| *value == SCRYPTO_NO_SCHEMA)
        {
            if for_package_extract {
                features.remove(idx);
            }
        } else if !for_package_extract {
            features.push(["--features", SCRYPTO_NO_SCHEMA]);
        }

        let mut remove_cargo_rustflags_env = false;
        if for_package_extract {
            if let Some(idx) = features
                .iter()
                .position(|[_tag, value]| *value == SCRYPTO_COVERAGE)
            {
                // for schema extract 'scrypto/coverage' flag must be removed
                features.remove(idx);
                remove_cargo_rustflags_env = true;
            }
        }

        let features: Vec<&str> = features.into_iter().flatten().collect();

        let package: Vec<&str> = self
            .input_params
            .package
            .iter()
            .map(|p| ["--package", p])
            .flatten()
            .collect();

        command
            .arg("build")
            .arg("--target")
            .arg(BUILD_TARGET)
            .arg("--target-dir")
            .arg(&self.main_manifest.target_directory)
            .arg("--manifest-path")
            .arg(&self.main_manifest.manifest_path)
            .args(package)
            .args(features);

        if for_package_extract {
            command.arg("--release");
        } else {
            command.args(self.input_params.profile.as_command_args());
        }

        if self.input_params.no_default_features {
            command.arg("--no-default-features");
        }
        if self.input_params.all_features {
            command.arg("--all_features");
        }

        self.input_params
            .environment_variables
            .iter()
            .for_each(|(name, action)| {
                match action {
                    EnvironmentVariableAction::Set(value) => {
                        // CARGO_ENCODED_RUSTFLAGS for coverage build must be removed for 1st phase compilation
                        if !(remove_cargo_rustflags_env && name == "CARGO_ENCODED_RUSTFLAGS") {
                            command.env(name, value);
                        }
                    }
                    EnvironmentVariableAction::Unset => {
                        command.env_remove(name);
                    }
                };
            });

        command.args(self.input_params.custom_options.iter());
    }

    fn wasm_optimize(&self, wasm_path: &Path) -> Result<(), ScryptoCompilerError> {
        if let Some(wasm_opt_config) = &self.input_params.wasm_optimization {
            wasm_opt_config
                .run(wasm_path, wasm_path)
                .map_err(ScryptoCompilerError::WasmOptimizationError)
        } else {
            Ok(())
        }
    }

    pub fn compile_with_stdio<T: Into<Stdio>>(
        &mut self,
        stdin: Option<T>,
        stdout: Option<T>,
        stderr: Option<T>,
    ) -> Result<Vec<BuildArtifacts>, ScryptoCompilerError> {
        let mut command = Command::new("cargo");
        // Stdio streams used only for 1st phase compilation due to lack of Copy trait.
        if let Some(s) = stdin {
            command.stdin(s);
        }
        if let Some(s) = stdout {
            command.stdout(s);
        }
        if let Some(s) = stderr {
            command.stderr(s);
        }
        let package_definitions = self.compile_internal_phase_1(&mut command)?;

        let mut command = Command::new("cargo");
        let wasms = self.compile_internal_phase_2(&mut command)?;

        Ok(package_definitions
            .iter()
            .zip(wasms.iter())
            .map(|(package, wasm)| BuildArtifacts {
                wasm: wasm.clone(),
                package_definition: package.clone(),
            })
            .collect())
    }

    // Two phase compilation:
    //  1st phase compiles with schema (without "scrypto/no-schema" feature) and release profile
    //      and then extracts package definition rpd file
    //  2nd phase compiles without schema (with "scrypto/no-schema" feature) and user specified profile
    pub fn compile(&mut self) -> Result<Vec<BuildArtifacts>, ScryptoCompilerError> {
        let mut command = Command::new("cargo");
        let package_definitions = self.compile_internal_phase_1(&mut command)?;

        let mut command = Command::new("cargo");
        let wasms = self.compile_internal_phase_2(&mut command)?;

        Ok(package_definitions
            .iter()
            .zip(wasms.iter())
            .map(|(package, wasm)| BuildArtifacts {
                wasm: wasm.clone(),
                package_definition: package.clone(),
            })
            .collect())
    }

    // 1st compilation phase: compile with schema and extract schema to .rpd file
    fn compile_internal_phase_1(
        &mut self,
        command: &mut Command,
    ) -> Result<Vec<BuildArtifact<PackageDefinition>>, ScryptoCompilerError> {
        self.prepare_command_phase_1(command);
        self.cargo_command_call(command)?;

        // compilation post-processing for all manifests
        if self.manifests.is_empty() {
            // non-workspace compilation
            Ok(vec![self.compile_internal_phase_1_postprocess(
                &self.main_manifest,
            )?])
        } else {
            // workspace compilation
            Ok(self
                .manifests
                .iter()
                .map(|manifest| self.compile_internal_phase_1_postprocess(&manifest))
                .collect::<Result<Vec<_>, ScryptoCompilerError>>()?)
        }
    }

    // used for unit tests
    fn prepare_command_phase_1(&mut self, command: &mut Command) {
        self.prepare_command(command, true); // build with schema and release profile
    }

    fn compile_internal_phase_1_postprocess(
        &self,
        manifest_def: &CompilerManifestDefinition,
    ) -> Result<BuildArtifact<PackageDefinition>, ScryptoCompilerError> {
        let path = manifest_def.target_binary_rpd_path.with_extension("wasm");
        let code = std::fs::read(&path).map_err(|e| {
            ScryptoCompilerError::IOErrorWithPath(
                e,
                path,
                Some(String::from("Read WASM file for RPD extract failed.")),
            )
        })?;

        let package_definition =
            extract_definition(&code).map_err(ScryptoCompilerError::SchemaExtractionError)?;

        std::fs::write(
            &manifest_def.target_binary_rpd_path,
            manifest_encode(&package_definition)
                .map_err(ScryptoCompilerError::SchemaEncodeError)?,
        )
        .map_err(|err| {
            ScryptoCompilerError::IOErrorWithPath(
                err,
                manifest_def.target_binary_rpd_path.clone(),
                Some(String::from("RPD file write failed.")),
            )
        })?;

        Ok(BuildArtifact {
            path: manifest_def.target_binary_rpd_path.clone(),
            content: package_definition,
        })
    }

    // 2nd compilation phase: compile without schema and with optional wasm optimisations - this is the final .wasm file
    fn compile_internal_phase_2(
        &mut self,
        command: &mut Command,
    ) -> Result<Vec<BuildArtifact<Vec<u8>>>, ScryptoCompilerError> {
        self.prepare_command_phase_2(command);
        self.cargo_command_call(command)?;

        // compilation post-processing for all manifests
        if self.manifests.is_empty() {
            // non-workspace compilation
            Ok(vec![self.compile_internal_phase_2_postprocess(
                &self.main_manifest,
            )?])
        } else {
            // workspace compilation
            Ok(self
                .manifests
                .iter()
                .map(|manifest| self.compile_internal_phase_2_postprocess(&manifest))
                .collect::<Result<Vec<_>, ScryptoCompilerError>>()?)
        }
    }

    // used for unit tests
    fn prepare_command_phase_2(&mut self, command: &mut Command) {
        self.prepare_command(command, false); // build without schema and with userchoosen profile
    }

    fn compile_internal_phase_2_postprocess(
        &self,
        manifest_def: &CompilerManifestDefinition,
    ) -> Result<BuildArtifact<Vec<u8>>, ScryptoCompilerError> {
        self.wasm_optimize(&manifest_def.target_binary_wasm_path.clone())?;

        let code = std::fs::read(&manifest_def.target_binary_wasm_path).map_err(|e| {
            ScryptoCompilerError::IOErrorWithPath(
                e,
                manifest_def.target_binary_wasm_path.clone(),
                Some(String::from("Read WASM file failed.")),
            )
        })?;
        Ok(BuildArtifact {
            path: manifest_def.target_binary_wasm_path.clone(),
            content: code,
        })
    }

    fn cargo_command_call(&mut self, command: &mut Command) -> Result<(), ScryptoCompilerError> {
        let status = command.status().map_err(|e| {
            ScryptoCompilerError::IOError(e, Some(String::from("Cargo build command failed.")))
        })?;
        status
            .success()
            .then_some(())
            .ok_or(ScryptoCompilerError::CargoBuildFailure(status))
    }

    /// Returns information about the main manifest
    pub fn get_main_manifest_definition(&self) -> CompilerManifestDefinition {
        self.main_manifest.clone()
    }
}

#[derive(Default)]
pub struct ScryptoCompilerBuilder {
    input_params: ScryptoCompilerInputParams,
}

impl ScryptoCompilerBuilder {
    pub fn manifest_path(&mut self, path: impl Into<PathBuf>) -> &mut Self {
        self.input_params.manifest_path = Some(path.into());
        self
    }

    pub fn target_directory(&mut self, directory: impl Into<PathBuf>) -> &mut Self {
        self.input_params.target_directory = Some(directory.into());

        self
    }

    pub fn profile(&mut self, profile: Profile) -> &mut Self {
        self.input_params.profile = profile;
        self
    }

    pub fn env(&mut self, name: &str, action: EnvironmentVariableAction) -> &mut Self {
        self.input_params
            .environment_variables
            .insert(name.to_string(), action);
        self
    }

    pub fn feature(&mut self, name: &str) -> &mut Self {
        self.input_params.features.insert(name.to_string());
        self
    }

    pub fn no_default_features(&mut self) -> &mut Self {
        self.input_params.no_default_features = true;
        self
    }

    pub fn all_features(&mut self) -> &mut Self {
        self.input_params.all_features = true;
        self
    }

    pub fn package(&mut self, name: &str) -> &mut Self {
        self.input_params.package.insert(name.to_string());
        self
    }

    pub fn scrypto_macro_trace(&mut self) -> &mut Self {
        self.input_params
            .features
            .insert(String::from("scrypto/trace"));
        self
    }

    pub fn log_level(&mut self, log_level: Level) -> &mut Self {
        // Firstly clear any log level previously set
        let all_features = ScryptoCompilerInputParams::log_level_to_scrypto_features(Level::Trace);
        all_features.iter().for_each(|log_level| {
            self.input_params.features.swap_remove(log_level);
        });

        // Now set log level provided by the user
        if Level::Error <= log_level {
            self.input_params
                .features
                .insert(String::from("scrypto/log-error"));
        }
        if Level::Warn <= log_level {
            self.input_params
                .features
                .insert(String::from("scrypto/log-warn"));
        }
        if Level::Info <= log_level {
            self.input_params
                .features
                .insert(String::from("scrypto/log-info"));
        }
        if Level::Debug <= log_level {
            self.input_params
                .features
                .insert(String::from("scrypto/log-debug"));
        }
        if Level::Trace <= log_level {
            self.input_params
                .features
                .insert(String::from("scrypto/log-trace"));
        }
        self
    }

    pub fn coverage(&mut self) -> &mut Self {
        self.input_params
            .features
            .insert(String::from(SCRYPTO_COVERAGE));
        self
    }

    pub fn optimize_with_wasm_opt(
        &mut self,
        options: Option<wasm_opt::OptimizationOptions>,
    ) -> &mut Self {
        self.input_params.wasm_optimization = options;
        self
    }

    pub fn custom_options(&mut self, options: &[&str]) -> &mut Self {
        self.input_params
            .custom_options
            .extend(options.iter().map(|item| item.to_string()));
        self
    }

    pub fn build(&mut self) -> Result<ScryptoCompiler, ScryptoCompilerError> {
        ScryptoCompiler::from_input_params(&mut self.input_params)
    }

    pub fn compile(&mut self) -> Result<Vec<BuildArtifacts>, ScryptoCompilerError> {
        self.build()?.compile()
    }

    pub fn compile_with_stdio<T: Into<Stdio>>(
        &mut self,
        stdin: Option<T>,
        stdout: Option<T>,
        stderr: Option<T>,
    ) -> Result<Vec<BuildArtifacts>, ScryptoCompilerError> {
        self.build()?.compile_with_stdio(stdin, stdout, stderr)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // helper function
    fn cmd_to_string(cmd: &Command) -> String {
        let args = cmd
            .get_args()
            .into_iter()
            .map(|arg| arg.to_str().unwrap())
            .collect::<Vec<_>>()
            .join(" ");
        let envs = cmd
            .get_envs()
            .into_iter()
            .map(|(name, value)| {
                if let Some(value) = value {
                    format!("{}={}", name.to_str().unwrap(), value.to_str().unwrap())
                } else {
                    format!("{}", name.to_str().unwrap())
                }
            })
            .collect::<Vec<_>>()
            .join(" ");
        let mut ret = envs;
        if !ret.is_empty() {
            ret.push(' ');
        }
        ret.push_str(cmd.get_program().to_str().unwrap());
        ret.push(' ');
        ret.push_str(&args);
        ret
    }

    #[test]
    fn test_target_binary_path_target() {
        let target_dir = "./tests/target";
        let compiler = ScryptoCompiler::builder()
            .manifest_path("./tests/assets/scenario_1/blueprint")
            .target_directory(target_dir)
            .custom_options(&["-j", "1"])
            .build()
            .unwrap();

        assert_eq!(
            "./tests/target/wasm32-unknown-unknown/release/test_blueprint.wasm",
            compiler
                .main_manifest
                .target_binary_wasm_path
                .display()
                .to_string()
        );
    }

    #[test]
    fn test_command_output_default() {
        // Arrange
        let mut manifest_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        let mut default_target_path = manifest_path.clone();
        manifest_path.push("Cargo.toml");
        default_target_path.pop(); // ScryptoCompiler dir
        default_target_path.push("target");
        let mut cmd_phase_1 = Command::new("cargo");
        let mut cmd_phase_2 = Command::new("cargo");

        // Act
        ScryptoCompiler::builder()
            .build()
            .unwrap()
            .prepare_command_phase_1(&mut cmd_phase_1);
        ScryptoCompiler::builder()
            .build()
            .unwrap()
            .prepare_command_phase_2(&mut cmd_phase_2);

        // Assert
        assert_eq!(cmd_to_string(&cmd_phase_1),
            format!("cargo build --target wasm32-unknown-unknown --target-dir {} --manifest-path {} --features scrypto/log-error --features scrypto/log-warn --features scrypto/log-info --release", default_target_path.display(), manifest_path.display()));
        assert_eq!(cmd_to_string(&cmd_phase_2),
            format!("cargo build --target wasm32-unknown-unknown --target-dir {} --manifest-path {} --features scrypto/log-error --features scrypto/log-warn --features scrypto/log-info --features scrypto/no-schema --profile release", default_target_path.display(), manifest_path.display()));
    }

    #[test]
    fn test_command_output_with_manifest_path() {
        // Arrange
        let mut manifest_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        let mut default_target_path = manifest_path.clone();
        manifest_path.push("tests/assets/scenario_1/blueprint/Cargo.toml");
        default_target_path.push("tests/assets/scenario_1/target");
        let mut cmd_phase_1 = Command::new("cargo");
        let mut cmd_phase_2 = Command::new("cargo");

        // Act
        ScryptoCompiler::builder()
            .manifest_path(&manifest_path)
            .build()
            .unwrap()
            .prepare_command_phase_1(&mut cmd_phase_1);
        ScryptoCompiler::builder()
            .manifest_path(&manifest_path)
            .build()
            .unwrap()
            .prepare_command_phase_2(&mut cmd_phase_2);

        // Assert
        assert_eq!(cmd_to_string(&cmd_phase_1),
            format!("cargo build --target wasm32-unknown-unknown --target-dir {} --manifest-path {} --features scrypto/log-error --features scrypto/log-warn --features scrypto/log-info --release", default_target_path.display(), manifest_path.display()));
        assert_eq!(cmd_to_string(&cmd_phase_2),
            format!("cargo build --target wasm32-unknown-unknown --target-dir {} --manifest-path {} --features scrypto/log-error --features scrypto/log-warn --features scrypto/log-info --features scrypto/no-schema --profile release", default_target_path.display(), manifest_path.display()));
    }

    #[test]
    fn test_command_output_target_directory() {
        // Arrange
        let mut manifest_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        manifest_path.push("Cargo.toml");
        let target_path = PathBuf::from("/tmp/build");
        let mut cmd_phase_1 = Command::new("cargo");
        let mut cmd_phase_2 = Command::new("cargo");

        // Act
        ScryptoCompiler::builder()
            .target_directory(&target_path)
            .build()
            .unwrap()
            .prepare_command_phase_1(&mut cmd_phase_1);
        ScryptoCompiler::builder()
            .target_directory(&target_path)
            .build()
            .unwrap()
            .prepare_command_phase_2(&mut cmd_phase_2);

        // Assert
        assert_eq!(cmd_to_string(&cmd_phase_1),
            format!("cargo build --target wasm32-unknown-unknown --target-dir {} --manifest-path {} --features scrypto/log-error --features scrypto/log-warn --features scrypto/log-info --release", target_path.display(), manifest_path.display()));
        assert_eq!(cmd_to_string(&cmd_phase_2),
            format!("cargo build --target wasm32-unknown-unknown --target-dir {} --manifest-path {} --features scrypto/log-error --features scrypto/log-warn --features scrypto/log-info --features scrypto/no-schema --profile release", target_path.display(), manifest_path.display()));
    }

    #[test]
    fn test_command_output_features() {
        // Arrange
        let mut manifest_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        let mut default_target_path = manifest_path.clone();
        manifest_path.push("Cargo.toml");
        default_target_path.pop(); // ScryptoCompiler dir
        default_target_path.push("target");
        let mut cmd_phase_1 = Command::new("cargo");
        let mut cmd_phase_2 = Command::new("cargo");

        // Act
        ScryptoCompiler::builder()
            .log_level(Level::Trace)
            .feature("feature_1")
            .no_default_features()
            .build()
            .unwrap()
            .prepare_command_phase_1(&mut cmd_phase_1);
        ScryptoCompiler::builder()
            .log_level(Level::Trace)
            .feature("feature_1")
            .no_default_features()
            .build()
            .unwrap()
            .prepare_command_phase_2(&mut cmd_phase_2);

        // Assert
        assert_eq!(cmd_to_string(&cmd_phase_1),
            format!("cargo build --target wasm32-unknown-unknown --target-dir {} --manifest-path {} --features scrypto/log-error --features scrypto/log-warn --features scrypto/log-info --features scrypto/log-debug --features scrypto/log-trace --features feature_1 --release --no-default-features", default_target_path.display(), manifest_path.display()));
        assert_eq!(cmd_to_string(&cmd_phase_2),
            format!("cargo build --target wasm32-unknown-unknown --target-dir {} --manifest-path {} --features scrypto/log-error --features scrypto/log-warn --features scrypto/log-info --features scrypto/log-debug --features scrypto/log-trace --features feature_1 --features scrypto/no-schema --profile release --no-default-features", default_target_path.display(), manifest_path.display()));
    }

    #[test]
    fn test_command_output_lower_log_level_than_default() {
        // Arrange
        let mut manifest_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        let mut default_target_path = manifest_path.clone();
        manifest_path.push("Cargo.toml");
        default_target_path.pop(); // ScryptoCompiler dir
        default_target_path.push("target");
        let mut cmd_phase_1 = Command::new("cargo");
        let mut cmd_phase_2 = Command::new("cargo");

        // Act
        ScryptoCompiler::builder()
            .log_level(Level::Error)
            .build()
            .unwrap()
            .prepare_command_phase_1(&mut cmd_phase_1);
        ScryptoCompiler::builder()
            .log_level(Level::Error)
            .build()
            .unwrap()
            .prepare_command_phase_2(&mut cmd_phase_2);

        // Assert
        assert_eq!(cmd_to_string(&cmd_phase_1),
            format!("cargo build --target wasm32-unknown-unknown --target-dir {} --manifest-path {} --features scrypto/log-error --release", default_target_path.display(), manifest_path.display()));
        assert_eq!(cmd_to_string(&cmd_phase_2),
            format!("cargo build --target wasm32-unknown-unknown --target-dir {} --manifest-path {} --features scrypto/log-error --features scrypto/no-schema --profile release", default_target_path.display(), manifest_path.display()));
    }
    #[test]
    fn test_command_output_workspace() {
        // Arrange
        let mut manifest_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        let mut default_target_path = manifest_path.clone();
        manifest_path.push("tests/assets/scenario_1/Cargo.toml");
        default_target_path.push("tests/assets/scenario_1/target");
        let mut cmd_phase_1 = Command::new("cargo");
        let mut cmd_phase_2 = Command::new("cargo");

        // Act
        ScryptoCompiler::builder()
            .manifest_path(&manifest_path)
            .build()
            .unwrap()
            .prepare_command_phase_1(&mut cmd_phase_1);
        ScryptoCompiler::builder()
            .manifest_path(&manifest_path)
            .build()
            .unwrap()
            .prepare_command_phase_2(&mut cmd_phase_2);

        // Assert
        assert_eq!(cmd_to_string(&cmd_phase_1),
            format!("cargo build --target wasm32-unknown-unknown --target-dir {} --manifest-path {} --package test_blueprint --package test_blueprint_2 --package test_blueprint_3 --features scrypto/log-error --features scrypto/log-warn --features scrypto/log-info --release", default_target_path.display(), manifest_path.display()));
        assert_eq!(cmd_to_string(&cmd_phase_2),
            format!("cargo build --target wasm32-unknown-unknown --target-dir {} --manifest-path {} --package test_blueprint --package test_blueprint_2 --package test_blueprint_3 --features scrypto/log-error --features scrypto/log-warn --features scrypto/log-info --features scrypto/no-schema --profile release", default_target_path.display(), manifest_path.display()));
    }

    #[test]
    fn test_command_output_workspace_with_packages() {
        // Arrange
        let mut manifest_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        let mut default_target_path = manifest_path.clone();
        manifest_path.push("tests/assets/scenario_1/Cargo.toml");
        default_target_path.push("tests/assets/scenario_1/target");
        let mut cmd_phase_1 = Command::new("cargo");
        let mut cmd_phase_2 = Command::new("cargo");

        // Act
        ScryptoCompiler::builder()
            .manifest_path(&manifest_path)
            .package("test_blueprint")
            .package("test_blueprint_3")
            .build()
            .unwrap()
            .prepare_command_phase_1(&mut cmd_phase_1);
        ScryptoCompiler::builder()
            .manifest_path(&manifest_path)
            .package("test_blueprint")
            .package("test_blueprint_3")
            .build()
            .unwrap()
            .prepare_command_phase_2(&mut cmd_phase_2);

        // Assert
        assert_eq!(cmd_to_string(&cmd_phase_1),
            format!("cargo build --target wasm32-unknown-unknown --target-dir {} --manifest-path {} --package test_blueprint --package test_blueprint_3 --features scrypto/log-error --features scrypto/log-warn --features scrypto/log-info --release", default_target_path.display(), manifest_path.display()));
        assert_eq!(cmd_to_string(&cmd_phase_2),
            format!("cargo build --target wasm32-unknown-unknown --target-dir {} --manifest-path {} --package test_blueprint --package test_blueprint_3 --features scrypto/log-error --features scrypto/log-warn --features scrypto/log-info --features scrypto/no-schema --profile release", default_target_path.display(), manifest_path.display()));
    }

    #[test]
    fn test_command_output_profiles() {
        // Arrange
        let mut manifest_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        let mut default_target_path = manifest_path.clone();
        manifest_path.push("Cargo.toml");
        default_target_path.pop(); // ScryptoCompiler dir
        default_target_path.push("target");
        let mut cmd_phase_1 = Command::new("cargo");
        let mut cmd_phase_2 = Command::new("cargo");

        // Act
        ScryptoCompiler::builder()
            .profile(Profile::Debug)
            .build()
            .unwrap()
            .prepare_command_phase_1(&mut cmd_phase_1);
        ScryptoCompiler::builder()
            .profile(Profile::Debug)
            .build()
            .unwrap()
            .prepare_command_phase_2(&mut cmd_phase_2);

        // Assert
        assert_eq!(cmd_to_string(&cmd_phase_1),
            format!("cargo build --target wasm32-unknown-unknown --target-dir {} --manifest-path {} --features scrypto/log-error --features scrypto/log-warn --features scrypto/log-info --release", default_target_path.display(), manifest_path.display()));
        assert_eq!(cmd_to_string(&cmd_phase_2),
            format!("cargo build --target wasm32-unknown-unknown --target-dir {} --manifest-path {} --features scrypto/log-error --features scrypto/log-warn --features scrypto/log-info --features scrypto/no-schema --profile dev", default_target_path.display(), manifest_path.display()));
    }

    #[test]
    fn test_command_output_no_schema_check() {
        // Arrange
        let mut manifest_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        let mut default_target_path = manifest_path.clone();
        manifest_path.push("Cargo.toml");
        default_target_path.pop(); // ScryptoCompiler dir
        default_target_path.push("target");
        let mut cmd_phase_1 = Command::new("cargo");
        let mut cmd_phase_2 = Command::new("cargo");

        // Act
        // Ensure that no-schema is properly used across both phase compilation, even if specified explicitly by the user.
        ScryptoCompiler::builder()
            .feature(SCRYPTO_NO_SCHEMA)
            .build()
            .unwrap()
            .prepare_command_phase_1(&mut cmd_phase_1);
        ScryptoCompiler::builder()
            .feature(SCRYPTO_NO_SCHEMA)
            .build()
            .unwrap()
            .prepare_command_phase_2(&mut cmd_phase_2);

        // Assert
        assert_eq!(cmd_to_string(&cmd_phase_1),
            format!("cargo build --target wasm32-unknown-unknown --target-dir {} --manifest-path {} --features scrypto/log-error --features scrypto/log-warn --features scrypto/log-info --release", default_target_path.display(), manifest_path.display()));
        assert_eq!(cmd_to_string(&cmd_phase_2),
            format!("cargo build --target wasm32-unknown-unknown --target-dir {} --manifest-path {} --features scrypto/log-error --features scrypto/log-warn --features scrypto/log-info --features scrypto/no-schema --profile release", default_target_path.display(), manifest_path.display()));
    }

    #[test]
    fn test_command_coverage() {
        // Arrange
        let mut manifest_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        let mut target_path = manifest_path.clone();
        manifest_path.push("Cargo.toml");
        target_path.pop(); // ScryptoCompiler dir
        target_path.push("coverage");
        let mut cmd_phase_1 = Command::new("cargo");
        let mut cmd_phase_2 = Command::new("cargo");

        // Act
        ScryptoCompiler::builder()
            .coverage()
            .target_directory(target_path.clone())
            .build()
            .unwrap()
            .prepare_command_phase_1(&mut cmd_phase_1);
        ScryptoCompiler::builder()
            .coverage()
            .target_directory(target_path.clone())
            .build()
            .unwrap()
            .prepare_command_phase_2(&mut cmd_phase_2);

        // Assert
        assert_eq!(cmd_to_string(&cmd_phase_1),
            format!("cargo build --target wasm32-unknown-unknown --target-dir {} --manifest-path {} --features scrypto/log-error --features scrypto/log-warn --features scrypto/log-info --release", target_path.display(), manifest_path.display()));
        assert_eq!(cmd_to_string(&cmd_phase_2),
            format!("cargo build --target wasm32-unknown-unknown --target-dir {} --manifest-path {} --features scrypto/log-error --features scrypto/log-warn --features scrypto/log-info --features scrypto/coverage --features scrypto/no-schema --profile release", target_path.display(), manifest_path.display()));
    }

    #[test]
    fn test_command_coverage_with_env() {
        // Arrange
        let mut manifest_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        let mut target_path = manifest_path.clone();
        manifest_path.push("Cargo.toml");
        target_path.pop(); // ScryptoCompiler dir
        target_path.push("coverage");
        let action = EnvironmentVariableAction::Set(String::from(
            "-Clto=off\x1f-Cinstrument-coverage\x1f-Zno-profiler-runtime\x1f--emit=llvm-ir",
        ));
        let mut cmd_phase_1 = Command::new("cargo");
        let mut cmd_phase_2 = Command::new("cargo");

        // Act
        ScryptoCompiler::builder()
            .coverage()
            .target_directory(target_path.clone())
            .env("CARGO_ENCODED_RUSTFLAGS", action.clone()) // CARGO_ENCODED_RUSTFLAGS must be removed for 1st phase
            .build()
            .unwrap()
            .prepare_command_phase_1(&mut cmd_phase_1);
        ScryptoCompiler::builder()
            .coverage()
            .target_directory(target_path.clone())
            .env("CARGO_ENCODED_RUSTFLAGS", action.clone())
            .build()
            .unwrap()
            .prepare_command_phase_2(&mut cmd_phase_2);

        // Assert
        assert_eq!(cmd_to_string(&cmd_phase_1),
            format!("cargo build --target wasm32-unknown-unknown --target-dir {} --manifest-path {} --features scrypto/log-error --features scrypto/log-warn --features scrypto/log-info --release", target_path.display(), manifest_path.display()));
        assert_eq!(cmd_to_string(&cmd_phase_2),
            format!("CARGO_ENCODED_RUSTFLAGS=-Clto=off\x1f-Cinstrument-coverage\x1f-Zno-profiler-runtime\x1f--emit=llvm-ir cargo build --target wasm32-unknown-unknown --target-dir {} --manifest-path {} --features scrypto/log-error --features scrypto/log-warn --features scrypto/log-info --features scrypto/coverage --features scrypto/no-schema --profile release", target_path.display(), manifest_path.display()));
    }
}