Fork of daniellemaywood.uk/gleam — Wasm codegen work
3.1 kB
89 lines
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: 2022 The Gleam contributors
3
4#[cfg(test)]
5mod generated_tests;
6
7use camino::Utf8PathBuf;
8use gleam_core::{
9 build::{
10 ErlangAppCodegenConfiguration, Mode, NullTelemetry, Outcome, StaleTracker, Target,
11 TargetCodegenConfiguration,
12 },
13 config::PackageConfig,
14 io::{FileSystemReader, FileSystemWriter},
15 warning::{VectorWarningEmitterIO, WarningEmitter},
16};
17use std::{
18 collections::{HashMap, HashSet},
19 rc::Rc,
20};
21
22pub fn prepare(path: &str) -> String {
23 let root = Utf8PathBuf::from(path).canonicalize_utf8().unwrap();
24
25 let toml = std::fs::read_to_string(root.join("gleam.toml")).unwrap();
26 let config: PackageConfig = toml::from_str(&toml).unwrap();
27
28 let target = match config.target {
29 Target::Erlang => TargetCodegenConfiguration::Erlang {
30 app_file: Some(ErlangAppCodegenConfiguration {
31 include_dev_deps: true,
32 package_name_overrides: HashMap::new(),
33 }),
34 },
35 Target::JavaScript => TargetCodegenConfiguration::JavaScript {
36 emit_typescript_definitions: config.javascript.typescript_declarations,
37 emit_source_maps: config.javascript.source_maps,
38 prelude_location: Utf8PathBuf::from("../prelude.mjs"),
39 },
40 Target::Cranelift => TargetCodegenConfiguration::Cranelift {},
41 };
42
43 let ids = gleam_core::uid::UniqueIdGenerator::new();
44 let mut modules = im::HashMap::new();
45 let warnings = VectorWarningEmitterIO::default();
46 let warning_emitter = WarningEmitter::new(Rc::new(warnings.clone()));
47 let filesystem = test_helpers_rs::to_in_memory_filesystem(&root);
48 let initial_files = filesystem.files();
49 let root = Utf8PathBuf::from("");
50 let out = Utf8PathBuf::from("/out/lib/the_package");
51 let lib = Utf8PathBuf::from("/out/lib");
52 let mut compiler = gleam_core::build::PackageCompiler::new(
53 &config,
54 Mode::Dev,
55 &root,
56 &out,
57 &lib,
58 &target,
59 ids,
60 filesystem.clone(),
61 );
62 compiler.write_entrypoint = false;
63 compiler.write_metadata = true;
64 compiler.compile_beam_bytecode = false;
65 compiler.copy_native_files = false;
66 let result = compiler.compile(
67 &warning_emitter,
68 &mut modules,
69 &mut im::HashMap::new(),
70 &mut StaleTracker::default(),
71 &mut HashSet::new(),
72 &NullTelemetry,
73 );
74 match result {
75 Outcome::Ok(_) => {
76 for path in initial_files {
77 if filesystem.is_file(&path) {
78 filesystem.delete_file(&path).unwrap();
79 }
80 }
81 let files = filesystem.into_contents();
82 let warnings = warnings.take();
83 test_helpers_rs::TestCompileOutput { files, warnings }.as_overview_text()
84 }
85 Outcome::TotalFailure(error) | Outcome::PartialFailure(_, error) => {
86 test_helpers_rs::normalise_diagnostic(&error.pretty_string())
87 }
88 }
89}