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.

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