Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include both shell and ps1 entrypoint scripts with gleam export erlan… #4364

Merged
merged 4 commits into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 33 additions & 17 deletions compiler-cli/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand All @@ -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<u8> = crate::publish::build_hex_tarball(paths, &mut config)?;
Expand Down
Loading