Expose rebar:start/0 and rebar:run/2 #236

Closed
wants to merge 1 commit into
from

3 participants

@hyperthunk

this isn't enough - you need to change the behaviour of rebar_utils:abort/1 if you want to do this, otherwise the host emulator will shutdown whenever some rebar module (or plugin) calls this function.

@josevalim

@hyperthunk good point, I will work on it.

@hyperthunk

Might I politely suggest that you stop and have conversation with the maintainers about what they want to do? It's a shame to make lots of changes and have them rejected.

@josevalim
@hyperthunk

I usually talk with a project's developer(s) through the pull request. You've also mentioned this on the mailing lists, so I think you've done what you can. You could always look at their GitHub profiles to see if there's any other contact info.

@josevalim
@hyperthunk

Hi José, yeah that's pretty cool. I notice that the purpose of rebar_config_formats you're using is really just to get a different bit of code to evaluate the file.

Perhaps a less invasive change to rebar_config (and therefore one more likely to be potentially accepted) would be to add a public API call that takes a term list and generates a rebar config record. For example:

from_raw(OptList) ->
    #config { dir = rebar_utils:get_cwd(), opts = OptList }.

And then you could call this from elixir quite easily:

defmodule Mix.Rebar do
  def start do
    loaded = :application.loaded_applications
    unless List.keyfind(loaded, :rebar, 1), do: :rebar.start()
  end

  def run(args) do
    start

    try do
      opts = :rebar_config:from_raw(consult_exs("rebar.config"))
      :rebar.run(args, rebar_opts.concat(opts))
    catch
      { :error, :failed } -> exit(1)
      other ->
        IO.puts "Unexpected error in rebar: #{other}"
        exit(1)
    end
  end

  def consult_exs(file) do
    case File.read(file) do
      { :ok, contents } ->
        { res, _ } = Code.eval File.read!(file), [], file: file
        res
      other -> other
    end
  end

  defp rebar_opts do
    [plugins: [Mix.Rebar.Compiler]]
  end
end

Obviously there's some degree of duplicating the code to figure out what rebar.config file you really want, but when you're calling rebar from your own tool, I think that's not too bad anyway.

@josevalim
@hyperthunk

the abort thing has been resolved in rebar - take a look at master (delayed_abort or something like that)

@josevalim

Thanks for pinging me @hyperthunk. I will wait for the maintainers to express any interest on this before I rebase.

@dizzyd

So, to sum up what you're trying to accomplish: You want it to be possible to call rebar:start(RawArgs, BaseConfig)? Is there anything else you need?

@josevalim

Yes, the goal is for rebar to have a public Erlang API which allows me to specify which rebar "task" to run and a basic configuration.

@dizzyd

It looks like exposing rebar:run_aux/2 would be sufficient for your purposes -- do you agree?

@josevalim

@dizzyd I believe we would also need rebar:start() so we can properly start rebar before running a command. rebar:run_aux is indeed mainly what I need. I have simply renamed it to rebar:run for convenience.

@josevalim

It seems ok, but rebar:start() is doing more work than that. But if it will work regardless, I am fine. :)

@dizzyd

I've committed rebar:run/2. I believe it should do most of the stuff one would need from an Erlang API (of course, it doesn't handle command line variables since...there is no command line. :))

If there are specific things you'd like to see beyond this, please open a new PR/issue on rebar/rebar.

@dizzyd dizzyd closed this Nov 6, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment