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.

59 lines
2.0 KiB

преди 15 години
  1. #!/usr/bin/env escript
  2. %% -*- erlang -*-
  3. main(Args) ->
  4. %% Compile all src/*.erl to ebin
  5. case make:files(filelib:wildcard("src/*.erl"), [{outdir, "ebin"}, {i, "include"}]) of
  6. up_to_date ->
  7. ok;
  8. error ->
  9. io:format("Failed to compile rebar files!\n"),
  10. halt(1)
  11. end,
  12. %% Make sure file:consult can parse the .app file
  13. case file:consult("ebin/rebar.app") of
  14. {ok, _} ->
  15. ok;
  16. {error, Reason} ->
  17. io:format("Invalid syntax in ebin/rebar.app: ~p\n", [Reason]),
  18. halt(1)
  19. end,
  20. %% Add ebin/ to our path
  21. true = code:add_path("ebin"),
  22. %% Run rebar to do proper .app validation and such
  23. rebar:main(["compile"] ++ Args),
  24. %% Construct the archive of everything in ebin/ dir -- put it on the
  25. %% top-level of the zip file so that code loading works properly.
  26. Files = filelib:wildcard("*", "ebin"),
  27. case zip:create("mem", Files, [{cwd, "ebin"}, memory]) of
  28. {ok, {"mem", ZipBin}} ->
  29. %% Archive was successfully created. Prefix that binary with our
  30. %% header and write to "rebar" file
  31. Script = <<"#!/usr/bin/env escript\n", ZipBin/binary>>,
  32. case file:write_file("rebar", Script) of
  33. ok ->
  34. ok;
  35. {error, WriteError} ->
  36. io:format("Failed to write rebar script: ~p\n", [WriteError]),
  37. halt(1)
  38. end;
  39. {error, ZipError} ->
  40. io:format("Failed to construct rebar script archive: ~p\n", [ZipError]),
  41. halt(1)
  42. end,
  43. %% Finally, update executable perms for our script
  44. [] = os:cmd("chmod u+x rebar"),
  45. %% Add a helpful message
  46. io:format("Congratulations! You now have a self-contained script called \"rebar\" in\n"
  47. "your current working directory. Place this script anywhere in your path\n"
  48. "and you can use rebar to build OTP-compliant apps.\n").