Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

118 рядки
3.7 KiB

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