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.

75 lines
2.5 KiB

  1. #!/usr/bin/env escript
  2. %% -*- erlang -*-
  3. main(Args) ->
  4. %% Get a string repr of build time
  5. Built = build_time(),
  6. %% Check for force=1 flag to force a rebuild
  7. case lists:member("force=1", Args) of
  8. true ->
  9. [] = os:cmd("rm -rf ebin/*.beam"),
  10. ok;
  11. false ->
  12. ok
  13. end,
  14. %% Compile all src/*.erl to ebin
  15. case make:files(filelib:wildcard("src/*.erl"), [{outdir, "ebin"}, {i, "include"},
  16. {d, 'BUILD_TIME', Built}]) of
  17. up_to_date ->
  18. ok;
  19. error ->
  20. io:format("Failed to compile rebar files!\n"),
  21. halt(1)
  22. end,
  23. %% Make sure file:consult can parse the .app file
  24. case file:consult("ebin/rebar.app") of
  25. {ok, _} ->
  26. ok;
  27. {error, Reason} ->
  28. io:format("Invalid syntax in ebin/rebar.app: ~p\n", [Reason]),
  29. halt(1)
  30. end,
  31. %% Add ebin/ to our path
  32. true = code:add_path("ebin"),
  33. %% Run rebar to do proper .app validation and such
  34. rebar:main(["compile"] ++ Args),
  35. %% Construct the archive of everything in ebin/ dir -- put it on the
  36. %% top-level of the zip file so that code loading works properly.
  37. Files = filelib:wildcard("*", "ebin"),
  38. case zip:create("mem", Files, [{cwd, "ebin"}, memory]) of
  39. {ok, {"mem", ZipBin}} ->
  40. %% Archive was successfully created. Prefix that binary with our
  41. %% header and write to "rebar" file
  42. Script = <<"#!/usr/bin/env escript\n", ZipBin/binary>>,
  43. case file:write_file("rebar", Script) of
  44. ok ->
  45. ok;
  46. {error, WriteError} ->
  47. io:format("Failed to write rebar script: ~p\n", [WriteError]),
  48. halt(1)
  49. end;
  50. {error, ZipError} ->
  51. io:format("Failed to construct rebar script archive: ~p\n", [ZipError]),
  52. halt(1)
  53. end,
  54. %% Finally, update executable perms for our script
  55. [] = os:cmd("chmod u+x rebar"),
  56. %% Add a helpful message
  57. io:format("Congratulations! You now have a self-contained script called \"rebar\" in\n"
  58. "your current working directory. Place this script anywhere in your path\n"
  59. "and you can use rebar to build OTP-compliant apps.\n").
  60. build_time() ->
  61. {{Y, M, D}, {H, Min, S}} = calendar:now_to_universal_time(now()),
  62. lists:flatten(io_lib:format("~4..0w~2..0w~2..0w_~2..0w~2..0w~2..0w", [Y, M, D, H, Min, S])).