Fork of daniellemaywood.uk/gleam — Wasm codegen work
3.6 kB
124 lines
1use gleam_core::{
2 build::{Codegen, Mode, Options, Target},
3 Result,
4};
5
6#[cfg(target_os = "windows")]
7static ENTRYPOINT_FILENAME: &str = "entrypoint.ps1";
8#[cfg(not(target_os = "windows"))]
9static ENTRYPOINT_FILENAME: &str = "entrypoint.sh";
10
11#[cfg(target_os = "windows")]
12static ENTRYPOINT_TEMPLATE: &str = include_str!("../templates/erlang-shipment-entrypoint.ps1");
13#[cfg(not(target_os = "windows"))]
14static ENTRYPOINT_TEMPLATE: &str = include_str!("../templates/erlang-shipment-entrypoint.sh");
15
16// TODO: start in embedded mode
17// TODO: test
18
19/// Generate a directory of precompiled Erlang along with a start script.
20/// Suitable for deployment to a server.
21///
22/// For each Erlang application (aka package) directory these directories are
23/// copied across:
24/// - ebin
25/// - include
26/// - priv
27pub(crate) fn erlang_shipment() -> Result<()> {
28 let paths = crate::project_paths_at_current_directory();
29 let target = Target::Erlang;
30 let mode = Mode::Prod;
31 let build = paths.build_directory_for_target(mode, target);
32 let out = paths.erlang_shipment_directory();
33
34 crate::fs::mkdir(&out)?;
35
36 // Reset the directories to ensure we have a clean slate and no old code
37 crate::fs::delete_dir(&build)?;
38 crate::fs::delete_dir(&out)?;
39
40 // Build project in production mode
41 let built = crate::build::main(
42 Options {
43 warnings_as_errors: false,
44 codegen: Codegen::All,
45 mode,
46 target: Some(target),
47 },
48 crate::build::download_dependencies()?,
49 )?;
50
51 for entry in crate::fs::read_dir(&build)?.filter_map(Result::ok) {
52 let path = entry.path();
53
54 // We are only interested in package directories
55 if !path.is_dir() {
56 continue;
57 }
58
59 let name = path.file_name().expect("Directory name");
60 let build = build.join(name);
61 let out = out.join(name);
62 crate::fs::mkdir(&out)?;
63
64 // Copy desired package subdirectories
65 for subdirectory in ["ebin", "priv", "include"] {
66 let source = build.join(subdirectory);
67 if source.is_dir() {
68 let source = crate::fs::canonicalise(&source)?;
69 crate::fs::copy_dir(source, &out)?;
70 }
71 }
72 }
73
74 // Write entrypoint script
75 let entrypoint = out.join(ENTRYPOINT_FILENAME);
76 let text =
77 ENTRYPOINT_TEMPLATE.replace("$PACKAGE_NAME_FROM_GLEAM", &built.root_package.config.name);
78 crate::fs::write(&entrypoint, &text)?;
79 crate::fs::make_executable(&entrypoint)?;
80
81 crate::cli::print_exported(&built.root_package.config.name);
82
83 println!(
84 "
85Your Erlang shipment has been generated to {path}.
86
87It can be copied to a compatible server with Erlang installed and run with
88the {file} script.
89
90 {entrypoint}
91",
92 path = out,
93 file = ENTRYPOINT_FILENAME,
94 entrypoint = entrypoint,
95 );
96
97 Ok(())
98}
99
100pub fn hex_tarball() -> Result<()> {
101 let paths = crate::project_paths_at_current_directory();
102 let config = crate::config::root_config()?;
103 let data: Vec<u8> = crate::publish::build_hex_tarball(&paths, &config)?;
104
105 let path = paths.build_export_hex_tarball(&config.name, &config.version.to_string());
106 crate::fs::write_bytes(&path, &data)?;
107 println!(
108 "
109Your hex tarball has been generated in {}.
110",
111 &path
112 );
113 Ok(())
114}
115
116pub fn javascript_prelude() -> Result<()> {
117 print!("{}", gleam_core::javascript::PRELUDE);
118 Ok(())
119}
120
121pub fn typescript_prelude() -> Result<()> {
122 print!("{}", gleam_core::javascript::PRELUDE_TS_DEF);
123 Ok(())
124}