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

Add utility function to fetch wrapper exe path at runtime #154

Merged
merged 1 commit into from
Sep 3, 2024
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
1 change: 1 addition & 0 deletions examples/cli_example/lib/example_cli_app.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule ExampleCliApp do
args = Burrito.Util.Args.get_arguments()

IO.puts("My arguments are: #{inspect(args)}")
IO.puts("I was started from: #{Burrito.Util.Args.get_bin_path()}")

IO.write("Testing Crypto (Generating ed25519 key-pair)...")
test_crypto()
Expand Down
14 changes: 14 additions & 0 deletions lib/util/args.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,18 @@ defmodule Burrito.Util.Args do
System.argv()
end
end

@doc """
Returns the path of the wrapper binary that launched this application.
If not currently inside a Burrito wrapped application, returns `:not_in_burrito`.
"""
@spec get_bin_path() :: binary() | :not_in_burrito
def get_bin_path() do
env_value = System.get_env("__BURRITO_BIN_PATH")
if env_value != nil do
env_value
else
:not_in_burrito
end
end
end
4 changes: 3 additions & 1 deletion src/erlang_launcher.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn get_erl_exe_name() []const u8 {
}
}

pub fn launch(install_dir: []const u8, env_map: *EnvMap, meta: *const MetaStruct, args_trimmed: []const []const u8) !void {
pub fn launch(install_dir: []const u8, env_map: *EnvMap, meta: *const MetaStruct, self_path: []const u8, args_trimmed: []const []const u8) !void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
const allocator = arena.allocator();

Expand Down Expand Up @@ -70,6 +70,7 @@ pub fn launch(install_dir: []const u8, env_map: *EnvMap, meta: *const MetaStruct
try env_map.put("RELEASE_ROOT", install_dir);
try env_map.put("RELEASE_SYS_CONFIG", config_sys_path_no_ext);
try env_map.put("__BURRITO", "1");
try env_map.put("__BURRITO_BIN_PATH", self_path);

var win_child_proc = std.process.Child.init(final_args, allocator);
win_child_proc.env_map = env_map;
Expand Down Expand Up @@ -105,6 +106,7 @@ pub fn launch(install_dir: []const u8, env_map: *EnvMap, meta: *const MetaStruct
try erl_env_map.put("RELEASE_ROOT", install_dir);
try erl_env_map.put("RELEASE_SYS_CONFIG", config_sys_path_no_ext);
try erl_env_map.put("__BURRITO", "1");
try erl_env_map.put("__BURRITO_BIN_PATH", self_path);

return std.process.execve(allocator, final_args, &erl_env_map);
}
Expand Down
3 changes: 2 additions & 1 deletion src/wrapper.zig
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ pub fn main() anyerror!void {
try maybe_install_musl_runtime();

// Trim args to only what we actually want to pass to erlang
const self_path = try std.fs.selfExePathAlloc(allocator);
const args_trimmed = args.?[1..];

// If this is not a production build, we always want a clean install
Expand Down Expand Up @@ -142,7 +143,7 @@ pub fn main() anyerror!void {

log.debug("Launching erlang...", .{});

try launcher.launch(install_dir, &env_map, &meta, args_trimmed);
try launcher.launch(install_dir, &env_map, &meta, self_path, args_trimmed);
}

fn do_payload_install(install_dir: []const u8, metadata_path: []const u8) !void {
Expand Down
Loading