This commit is contained in:
Graham McIntire 2025-07-18 13:59:03 -05:00
parent 00a8f996f4
commit 3e6d73b553
No known key found for this signature in database
3 changed files with 25 additions and 22 deletions

View file

@ -8,7 +8,7 @@ defmodule Aprsme.EncodingUtils do
# The Gleam module is compiled with @ separator in the beam name
@gleam_module :aprsme@encoding
# Optional compile-time check to suppress warnings
@compile {:no_warn_undefined, {@gleam_module, :sanitize_string, 1}}
@compile {:no_warn_undefined, {@gleam_module, :to_float_safe, 1}}

View file

@ -1,25 +1,26 @@
defmodule Mix.Tasks.GleamCompile do
@shortdoc "Compiles Gleam files and copies them to the correct location"
@moduledoc false
use Mix.Task
@shortdoc "Compiles Gleam files and copies them to the correct location"
def run(_args) do
env = Mix.env()
# Check if compile.gleam task exists
task_exists = Code.ensure_loaded?(Mix.Tasks.Compile.Gleam)
if task_exists do
# First run the gleam compiler
Mix.Task.run("compile.gleam")
# Ensure output directory exists
ebin_dir = "_build/#{env}/lib/aprsme/ebin"
File.mkdir_p!(ebin_dir)
# Copy beam files from Gleam build to Elixir build
gleam_output = "build/#{env}/erlang/aprsme/_gleam_artefacts"
if File.exists?(gleam_output) do
gleam_output
|> File.ls!()
@ -33,22 +34,23 @@ defmodule Mix.Tasks.GleamCompile do
end
else
Mix.shell().info("Gleam compiler not available, compiling manually")
# Compile Gleam files manually using gleam binary if available
if System.find_executable("gleam") do
Mix.shell().info("Found gleam binary, compiling...")
{output, _exit_code} = System.cmd("gleam", ["build"], cd: File.cwd!())
Mix.shell().info(output)
# Copy from gleam build to elixir build
# Gleam always builds to dev directory regardless of MIX_ENV
# The compiled files go to the project's ebin directory
gleam_output = "build/dev/erlang/aprsme/ebin"
ebin_dir = "_build/#{env}/lib/aprsme/ebin"
File.mkdir_p!(ebin_dir)
# First, check if compile.gleam already created the aprsme@encoding.beam
dev_ebin = "_build/dev/lib/aprsme/ebin"
if File.exists?(Path.join(dev_ebin, "aprsme@encoding.beam")) do
Mix.shell().info("Found pre-compiled aprsme@encoding.beam")
src = Path.join(dev_ebin, "aprsme@encoding.beam")
@ -56,15 +58,17 @@ defmodule Mix.Tasks.GleamCompile do
File.copy!(src, dest)
Mix.shell().info("Copied aprsme@encoding.beam to test build")
end
if File.exists?(gleam_output) do
Mix.shell().info("Checking #{gleam_output} for beam files...")
beam_files = gleam_output
|> File.ls!()
|> Enum.filter(&String.ends_with?(&1, ".beam"))
beam_files =
gleam_output
|> File.ls!()
|> Enum.filter(&String.ends_with?(&1, ".beam"))
Mix.shell().info("Found #{length(beam_files)} beam files")
Enum.each(beam_files, fn beam_file ->
src = Path.join(gleam_output, beam_file)
dest = Path.join(ebin_dir, beam_file)
@ -78,10 +82,10 @@ defmodule Mix.Tasks.GleamCompile do
# Last resort: try to copy from dev build if available
dev_ebin = "_build/dev/lib/aprsme/ebin"
test_ebin = "_build/#{env}/lib/aprsme/ebin"
if File.exists?(dev_ebin) and env != :dev do
File.mkdir_p!(test_ebin)
dev_ebin
|> File.ls!()
|> Enum.filter(&String.starts_with?(&1, "aprs@"))
@ -93,7 +97,7 @@ defmodule Mix.Tasks.GleamCompile do
end
end
end
:ok
end
end
end

View file

@ -142,5 +142,4 @@ defmodule Aprsme.MixProject do
# {:aprs, github: "aprsme/aprs", branch: "main"}
# end
end
end