packages feed

cabal-buck2-0.6.0.0: buck2/prelude/erlang/shell/src/user_default.erl

%% Copyright (c) Meta Platforms, Inc. and affiliates.
%%
%% This source code is licensed under both the MIT license found in the
%% LICENSE-MIT file in the root directory of this source tree and the Apache
%% License, Version 2.0 found in the LICENSE-APACHE file in the root directory
%% of this source tree.

%% % @format
-module(user_default).
-wacov(ignore).

-compile(warn_missing_spec_all).

-export([
    c/1, c/2, c/3,
    l/1
]).

-import(common_util, [filename_all_to_filename/1]).

-type c_ret() :: {ok, module()} | error.
-type c_options() :: [compile:option()] | compile:option().

-spec c(module()) -> c_ret() | {error, non_existing}.
c(Module) ->
    c(Module, []).

-spec c(module(), c_options()) -> c_ret() | {error, non_existing}.
c(Module, Options) ->
    c(Module, Options, fun(_) -> true end).

-spec c(module(), c_options(), fun((compile:option()) -> boolean())) -> c_ret() | {error, non_existing}.
c(Module, _Options, _Filter) ->
    case code:which(Module) of
        non_existing ->
            {error, non_existing};
        _ ->
            case shell_buck2_utils:rebuild_modules([Module]) of
                ok ->
                    ok = ct_daemon:push_module(Module),
                    code:purge(Module),
                    code:load_file(Module),
                    {ok, Module};
                error ->
                    error
            end
    end.

-spec l(module()) ->
    code:load_ret()
    | {error, not_found | {ambiguous, [file:filename_all()]}}.
l(Module) ->
    case shell_buck2_module_search:find_module(Module) of
        available ->
            c:l(Module);
        {source, RelSource} ->
            AbsSource = filename:absname(RelSource),
            Paths = [filename_all_to_filename(Path) || Path <- shell_buck2_utils:get_additional_paths(AbsSource)],
            ok = code:add_paths(Paths),
            ok = ct_daemon:push_paths(Paths),
            c:l(Module);
        Error ->
            Error
    end.