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.

85 lines
2.9 KiB

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