Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

84 rindas
2.9 KiB

pirms 15 gadiem
  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. {d, 'BUILD_TIME', Built}]) of
  18. up_to_date ->
  19. ok;
  20. error ->
  21. io:format("Failed to compile rebar files!\n"),
  22. halt(1)
  23. end,
  24. %% Make sure file:consult can parse the .app file
  25. case file:consult("ebin/rebar.app") of
  26. {ok, _} ->
  27. ok;
  28. {error, Reason} ->
  29. io:format("Invalid syntax in ebin/rebar.app: ~p\n", [Reason]),
  30. halt(1)
  31. end,
  32. %% Add ebin/ to our path
  33. true = code:add_path("ebin"),
  34. %% Run rebar to do proper .app validation and such
  35. rebar:main(["compile"] ++ Args),
  36. %% Read the contents of the files in ebin and templates; note that we place
  37. %% all the beam files at the top level of the code archive so that code loading
  38. %% works properly.
  39. Files = load_files("*", "ebin") ++ load_files("priv/templates/*", "."),
  40. case zip:create("mem", Files, [memory]) of
  41. {ok, {"mem", ZipBin}} ->
  42. %% Archive was successfully created. Prefix that binary with our
  43. %% header and write to "rebar" file
  44. Script = <<"#!/usr/bin/env escript\n", ZipBin/binary>>,
  45. case file:write_file("rebar", Script) of
  46. ok ->
  47. ok;
  48. {error, WriteError} ->
  49. io:format("Failed to write rebar script: ~p\n", [WriteError]),
  50. halt(1)
  51. end;
  52. {error, ZipError} ->
  53. io:format("Failed to construct rebar script archive: ~p\n", [ZipError]),
  54. halt(1)
  55. end,
  56. %% Finally, update executable perms for our script
  57. [] = os:cmd("chmod u+x rebar"),
  58. %% Add a helpful message
  59. io:format("Congratulations! You now have a self-contained script called \"rebar\" in\n"
  60. "your current working directory. Place this script anywhere in your path\n"
  61. "and you can use rebar to build OTP-compliant apps.\n").
  62. build_time() ->
  63. {{Y, M, D}, {H, Min, S}} = calendar:now_to_universal_time(now()),
  64. lists:flatten(io_lib:format("~4..0w~2..0w~2..0w_~2..0w~2..0w~2..0w", [Y, M, D, H, Min, S])).
  65. load_files(Wildcard, Dir) ->
  66. [read_file(Filename, Dir) || Filename <- filelib:wildcard(Wildcard, Dir)].
  67. read_file(Filename, Dir) ->
  68. {ok, Bin} = file:read_file(filename:join(Dir, Filename)),
  69. {Filename, Bin}.