diff --git a/CHANGELOG.md b/CHANGELOG.md index db39aeec975..e61753b2917 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,11 @@ - Include a type annotation for the `main` function generated by `gleam new`. ([Drew Olson](https://github.com/drewolson)) +- Two entry point scripts are now always generated by `gleam export erlang-shipment`: + - `entrypoint.sh` for UNIX Shell + - `entrypoint.ps1` for PowerShell + ([Greg Burri](https://github.com/ummon)) + ### Language server - The language server now allows renaming of functions and constants across diff --git a/compiler-cli/src/export.rs b/compiler-cli/src/export.rs index 60e9429c58e..dc29f25b24e 100644 --- a/compiler-cli/src/export.rs +++ b/compiler-cli/src/export.rs @@ -6,15 +6,13 @@ use gleam_core::{ paths::ProjectPaths, }; -#[cfg(target_os = "windows")] -static ENTRYPOINT_FILENAME: &str = "entrypoint.ps1"; -#[cfg(not(target_os = "windows"))] -static ENTRYPOINT_FILENAME: &str = "entrypoint.sh"; +static ENTRYPOINT_FILENAME_POWERSHELL: &str = "entrypoint.ps1"; +static ENTRYPOINT_FILENAME_POSIX_SHELL: &str = "entrypoint.sh"; -#[cfg(target_os = "windows")] -static ENTRYPOINT_TEMPLATE: &str = include_str!("../templates/erlang-shipment-entrypoint.ps1"); -#[cfg(not(target_os = "windows"))] -static ENTRYPOINT_TEMPLATE: &str = include_str!("../templates/erlang-shipment-entrypoint.sh"); +static ENTRYPOINT_TEMPLATE_POWERSHELL: &str = + include_str!("../templates/erlang-shipment-entrypoint.ps1"); +static ENTRYPOINT_TEMPLATE_POSIX_SHELL: &str = + include_str!("../templates/erlang-shipment-entrypoint.sh"); // TODO: start in embedded mode // TODO: test @@ -78,12 +76,19 @@ pub(crate) fn erlang_shipment(paths: &ProjectPaths) -> Result<()> { } } - // Write entrypoint script - let entrypoint = out.join(ENTRYPOINT_FILENAME); - let text = - ENTRYPOINT_TEMPLATE.replace("$PACKAGE_NAME_FROM_GLEAM", &built.root_package.config.name); - crate::fs::write(&entrypoint, &text)?; - crate::fs::make_executable(&entrypoint)?; + // PowerShell entry point script. + write_entrypoint_script( + &out.join(ENTRYPOINT_FILENAME_POWERSHELL), + ENTRYPOINT_TEMPLATE_POWERSHELL, + &built.root_package.config.name, + )?; + + // POSIX Shell entry point script. + write_entrypoint_script( + &out.join(ENTRYPOINT_FILENAME_POSIX_SHELL), + ENTRYPOINT_TEMPLATE_POSIX_SHELL, + &built.root_package.config.name, + )?; crate::cli::print_exported(&built.root_package.config.name); @@ -92,15 +97,26 @@ pub(crate) fn erlang_shipment(paths: &ProjectPaths) -> Result<()> { Your Erlang shipment has been generated to {out}. It can be copied to a compatible server with Erlang installed and run with -the {ENTRYPOINT_FILENAME} script. - - {entrypoint} +one of the following scripts: + - {ENTRYPOINT_FILENAME_POWERSHELL} (PowerShell script) + - {ENTRYPOINT_FILENAME_POSIX_SHELL} (POSIX Shell script) ", ); Ok(()) } +fn write_entrypoint_script( + entrypoint_output_path: &Utf8PathBuf, + entrypoint_template_path: &str, + package_name: &str, +) -> Result<(), gleam_core::Error> { + let text = entrypoint_template_path.replace("$PACKAGE_NAME_FROM_GLEAM", package_name); + crate::fs::write(entrypoint_output_path, &text)?; + crate::fs::make_executable(entrypoint_output_path)?; + Ok(()) +} + pub fn hex_tarball(paths: &ProjectPaths) -> Result<()> { let mut config = crate::config::root_config(paths)?; let data: Vec = crate::publish::build_hex_tarball(paths, &mut config)?;