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.

82 regels
2.8 KiB

  1. -module(rebar_prv_unlock).
  2. -behaviour(provider).
  3. -export([init/1,
  4. do/1,
  5. format_error/1]).
  6. -include("rebar.hrl").
  7. -include_lib("providers/include/providers.hrl").
  8. -define(PROVIDER, unlock).
  9. -define(DEPS, []).
  10. %% ===================================================================
  11. %% Public API
  12. %% ===================================================================
  13. -spec init(rebar_state:t()) -> {ok, rebar_state:t()}.
  14. init(State) ->
  15. State1 = rebar_state:add_provider(
  16. State,
  17. providers:create([{name, ?PROVIDER},
  18. {module, ?MODULE},
  19. {bare, true},
  20. {deps, ?DEPS},
  21. {example, ""},
  22. {short_desc, "Unlock dependencies."},
  23. {desc, "Unlock project dependencies. Mentioning no application "
  24. "will unlock all dependencies. To unlock specific dependencies, "
  25. "their name can be listed in the command."},
  26. {opts, [
  27. {package, undefined, undefined, string,
  28. "List of packages to unlock. If not specified, all dependencies are unlocked."}
  29. ]}
  30. ])
  31. ),
  32. {ok, State1}.
  33. do(State) ->
  34. Dir = rebar_state:dir(State),
  35. LockFile = filename:join(Dir, ?LOCK_FILE),
  36. case file:consult(LockFile) of
  37. {error, enoent} ->
  38. %% Our work is done.
  39. {ok, State};
  40. {error, Reason} ->
  41. ?PRV_ERROR({file,Reason});
  42. {ok, _} ->
  43. Locks = rebar_config:consult_lock_file(LockFile),
  44. {ok, NewLocks} = handle_unlocks(State, Locks, LockFile),
  45. {ok, rebar_state:set(State, {locks, default}, NewLocks)}
  46. end.
  47. -spec format_error(any()) -> iolist().
  48. format_error({file, Reason}) ->
  49. io_lib:format("Lock file editing failed for reason ~p", [Reason]);
  50. format_error(unknown_lock_format) ->
  51. "Lock file format unknown";
  52. format_error(Reason) ->
  53. io_lib:format("~p", [Reason]).
  54. handle_unlocks(State, Locks, LockFile) ->
  55. {Args, _} = rebar_state:command_parsed_args(State),
  56. Names = parse_names(rebar_utils:to_binary(proplists:get_value(package, Args, <<"">>))),
  57. case [Lock || Lock = {Name, _, _} <- Locks, not lists:member(Name, Names)] of
  58. [] ->
  59. file:delete(LockFile),
  60. {ok, []};
  61. _ when Names =:= [] -> % implicitly all locks
  62. file:delete(LockFile),
  63. {ok, []};
  64. NewLocks ->
  65. rebar_config:write_lock_file(LockFile, NewLocks),
  66. {ok, NewLocks}
  67. end.
  68. parse_names(Bin) ->
  69. case lists:usort(re:split(Bin, <<" *, *">>, [trim, unicode])) of
  70. [<<"">>] -> []; % nothing submitted
  71. Other -> Other
  72. end.