You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 line
3.4 KiB

  1. %% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
  2. %% ex: ts=4 sw=4 et
  3. -module(rebar_prv_local_install).
  4. -behaviour(provider).
  5. -export([init/1,
  6. do/1,
  7. format_error/1]).
  8. -export([extract_escript/2]).
  9. -include("rebar.hrl").
  10. -include_lib("providers/include/providers.hrl").
  11. -include_lib("kernel/include/file.hrl").
  12. -define(PROVIDER, install).
  13. -define(NAMESPACE, local).
  14. -define(DEPS, []).
  15. %% ===================================================================
  16. %% Public API
  17. %% ===================================================================
  18. -spec init(rebar_state:t()) -> {ok, rebar_state:t()}.
  19. init(State) ->
  20. State1 =
  21. rebar_state:add_provider(State,
  22. providers:create([{name, ?PROVIDER},
  23. {module, ?MODULE},
  24. {bare, true},
  25. {namespace, ?NAMESPACE},
  26. {deps, ?DEPS},
  27. {example, "rebar3 unstable install"},
  28. {short_desc, "Extract libs from rebar3 escript along with a run script."},
  29. {desc, ""},
  30. {opts, []}])),
  31. {ok, State1}.
  32. -spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}.
  33. do(State) ->
  34. case os:type() of
  35. {win32, _} ->
  36. ?ERROR("Sorry, this feature is not yet available on Windows.", []),
  37. {ok, State};
  38. _ ->
  39. case rebar_state:escript_path(State) of
  40. undefined ->
  41. ?INFO("Already running from an unpacked rebar3. Nothing to do...", []),
  42. {ok, State};
  43. ScriptPath ->
  44. extract_escript(State, ScriptPath)
  45. end
  46. end.
  47. -spec format_error(any()) -> iolist().
  48. format_error({non_writeable, Dir}) ->
  49. io_lib:format("Could not write to ~p. Please ensure the path is writeable.",
  50. [Dir]);
  51. format_error(Reason) ->
  52. io_lib:format("~p", [Reason]).
  53. bin_contents(OutputDir) ->
  54. <<"#!/usr/bin/env sh
  55. erl -pz ", (rebar_utils:to_binary(OutputDir))/binary,"/*/ebin +sbtu +A1 -noshell -boot start_clean -s rebar3 main $REBAR3_ERL_ARGS -extra \"$@\"
  56. ">>.
  57. extract_escript(State, ScriptPath) ->
  58. {ok, Escript} = escript:extract(ScriptPath, []),
  59. {archive, Archive} = lists:keyfind(archive, 1, Escript),
  60. %% Extract contents of Archive to ~/.cache/rebar3/lib
  61. %% And add a rebar3 bin script to ~/.cache/rebar3/bin
  62. Opts = rebar_state:opts(State),
  63. OutputDir = filename:join(rebar_dir:global_cache_dir(Opts), "lib"),
  64. case filelib:ensure_dir(filename:join(OutputDir, "empty")) of
  65. ok ->
  66. ok;
  67. {error, Posix} when Posix == eaccess; Posix == enoent ->
  68. throw(?PRV_ERROR({non_writeable, OutputDir}))
  69. end,
  70. ?INFO("Extracting rebar3 libs to ~ts...", [OutputDir]),
  71. zip:extract(Archive, [{cwd, OutputDir}]),
  72. BinDir = filename:join(rebar_dir:global_cache_dir(Opts), "bin"),
  73. BinFile = filename:join(BinDir, "rebar3"),
  74. filelib:ensure_dir(BinFile),
  75. ?INFO("Writing rebar3 run script ~ts...", [BinFile]),
  76. file:write_file(BinFile, bin_contents(OutputDir)),
  77. ok = file:write_file_info(BinFile, #file_info{mode=33277}),
  78. ?INFO("Add to $PATH for use: export PATH=~ts:$PATH", [BinDir]),
  79. {ok, State}.