Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

100 rader
3.4 KiB

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