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.

110 lines
3.4 KiB

  1. #!/usr/bin/env escript
  2. %% -*- mode: erlang;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 --tags"}]),
  10. %% Check for force=1 flag to force a rebuild
  11. case lists:member("force=1", Args) of
  12. true ->
  13. rm("ebin/*.beam");
  14. false ->
  15. rm("ebin/rebar_core.beam")
  16. end,
  17. %% Add check for debug flag
  18. DebugFlag = case lists:member("debug", Args) of
  19. true -> debug_info;
  20. false -> undefined
  21. end,
  22. %% Extract the system info of the version of OTP we use to compile rebar
  23. OtpInfo = string:strip(erlang:system_info(otp_release), both, $\n),
  24. %% Compile all src/*.erl to ebin
  25. case make:files(filelib:wildcard("src/*.erl"),
  26. [{outdir, "ebin"}, {i, "include"},
  27. DebugFlag,
  28. {d, 'BUILD_TIME', Built},
  29. {d, 'VCS_INFO', VcsInfo},
  30. {d, 'OTP_INFO', OtpInfo}]) of
  31. up_to_date ->
  32. ok;
  33. error ->
  34. io:format("Failed to compile rebar files!\n"),
  35. halt(1)
  36. end,
  37. %% Make sure file:consult can parse the .app file
  38. case file:consult("ebin/rebar.app") of
  39. {ok, _} ->
  40. ok;
  41. {error, Reason} ->
  42. io:format("Invalid syntax in ebin/rebar.app: ~p\n", [Reason]),
  43. halt(1)
  44. end,
  45. %% Add ebin/ to our path
  46. true = code:add_path("ebin"),
  47. %% Run rebar compile to do proper .app validation etc.
  48. %% and rebar escriptize to create the rebar script
  49. rebar:main(["compile", "escriptize"] ++ Args),
  50. %% Finally, update executable perms for our script on *nix,
  51. %% or write out script files on win32.
  52. case os:type() of
  53. {unix,_} ->
  54. [] = os:cmd("chmod u+x rebar"),
  55. ok;
  56. {win32,_} ->
  57. write_windows_scripts(),
  58. ok;
  59. _ ->
  60. ok
  61. end,
  62. %% Add a helpful message
  63. io:format("Congratulations! You now have a self-contained script called"
  64. " \"rebar\" in\n"
  65. "your current working directory. "
  66. "Place this script anywhere in your path\n"
  67. "and you can use rebar to build OTP-compliant apps.\n").
  68. rm(Path) ->
  69. NativePath = filename:nativename(Path),
  70. Cmd = case os:type() of
  71. {unix,_} -> "rm -f ";
  72. {win32,_} -> "del /q "
  73. end,
  74. [] = os:cmd(Cmd ++ NativePath),
  75. ok.
  76. build_time() ->
  77. {{Y, M, D}, {H, Min, S}} = calendar:now_to_universal_time(now()),
  78. lists:flatten(io_lib:format("~4..0w~2..0w~2..0w_~2..0w~2..0w~2..0w",
  79. [Y, M, D, H, Min, S])).
  80. vcs_info([]) ->
  81. "No VCS info available.";
  82. vcs_info([{Id, Dir, Cmd} | Rest]) ->
  83. case filelib:is_dir(Dir) of
  84. true ->
  85. lists:concat([Id, " ", string:strip(os:cmd(Cmd), both, $\n)]);
  86. false ->
  87. vcs_info(Rest)
  88. end.
  89. write_windows_scripts() ->
  90. CmdScript=
  91. "@echo off\r\n"
  92. "setlocal\r\n"
  93. "set rebarscript=%~f0\r\n"
  94. "escript.exe \"%rebarscript:.cmd=%\" %*\r\n",
  95. ok = file:write_file("rebar.cmd", CmdScript).