Fork of daniellemaywood.uk/gleam — Wasm codegen work
1.7 kB
58 lines
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: 2024 The Gleam contributors
3
4#[cfg(test)]
5mod generated_tests;
6
7use camino::Utf8PathBuf;
8use gleam_core::{
9 analyse::TargetSupport,
10 build::{Codegen, Compile, Mode, NullTelemetry, Options, ProjectCompiler, Telemetry},
11 config::PackageConfig,
12 io::{FileSystemReader, FileSystemWriter},
13 paths::ProjectPaths,
14 warning::VectorWarningEmitterIO,
15};
16use std::rc::Rc;
17
18pub fn prepare(path: &str, mode: Mode) -> String {
19 let root = Utf8PathBuf::from(path).canonicalize_utf8().unwrap();
20 let filesystem = test_helpers_rs::to_in_memory_filesystem(&root);
21 let initial_files = filesystem.files();
22
23 let toml = std::fs::read_to_string(root.join("gleam.toml")).unwrap();
24 let config: PackageConfig = toml::from_str(&toml).unwrap();
25 let warnings = VectorWarningEmitterIO::default();
26 let telemetry: &'static dyn Telemetry = &NullTelemetry;
27
28 let options = Options {
29 mode,
30 target: None,
31 compile: Compile::All,
32 codegen: Codegen::All,
33 warnings_as_errors: false,
34 root_target_support: TargetSupport::Enforced,
35 no_print_progress: true,
36 };
37
38 let compiler = ProjectCompiler::new(
39 config,
40 options,
41 vec![],
42 telemetry,
43 Rc::new(warnings.clone()),
44 ProjectPaths::new(root),
45 filesystem.clone(),
46 );
47
48 compiler.compile().unwrap();
49
50 for path in initial_files {
51 if filesystem.is_file(&path) {
52 filesystem.delete_file(&path).unwrap();
53 }
54 }
55 let files = filesystem.into_contents();
56 let warnings = warnings.take();
57 test_helpers_rs::TestCompileOutput { files, warnings }.as_overview_text()
58}