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.

119 lines
3.8 KiB

преди 14 години
преди 15 години
преди 14 години
преди 13 години
преди 13 години
преди 13 години
преди 13 години
преди 15 години
преди 13 години
преди 15 години
преди 13 години
преди 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. RebarArgs = Args -- ["debug"], %% Avoid trying to run 'debug' command
  51. rebar:main(["compile", "escriptize"] ++ RebarArgs),
  52. %% Finally, update executable perms for our script on *nix,
  53. %% or write out script files on win32.
  54. case os:type() of
  55. {unix,_} ->
  56. [] = os:cmd("chmod u+x rebar"),
  57. ok;
  58. {win32,_} ->
  59. write_windows_scripts(),
  60. ok;
  61. _ ->
  62. ok
  63. end,
  64. %% Add a helpful message
  65. io:format("Congratulations! You now have a self-contained script called"
  66. " \"rebar\" in\n"
  67. "your current working directory. "
  68. "Place this script anywhere in your path\n"
  69. "and you can use rebar to build OTP-compliant apps.\n").
  70. rm(Path) ->
  71. NativePath = filename:nativename(Path),
  72. Cmd = case os:type() of
  73. {unix,_} -> "rm -f ";
  74. {win32,_} -> "del /q "
  75. end,
  76. [] = os:cmd(Cmd ++ NativePath),
  77. ok.
  78. build_time() ->
  79. {{Y, M, D}, {H, Min, S}} = calendar:now_to_universal_time(now()),
  80. lists:flatten(io_lib:format("~4..0w~2..0w~2..0w_~2..0w~2..0w~2..0w",
  81. [Y, M, D, H, Min, S])).
  82. vcs_info([]) ->
  83. "No VCS info available.";
  84. vcs_info([{Id, Dir, VsnCmd, StatusCmd} | Rest]) ->
  85. case filelib:is_dir(Dir) of
  86. true ->
  87. Vsn = string:strip(os:cmd(VsnCmd), both, $\n),
  88. Status = case string:strip(os:cmd(StatusCmd), both, $\n) of
  89. [] ->
  90. "";
  91. _ ->
  92. "-dirty"
  93. end,
  94. lists:concat([Id, " ", Vsn, Status]);
  95. false ->
  96. vcs_info(Rest)
  97. end.
  98. write_windows_scripts() ->
  99. CmdScript=
  100. "@echo off\r\n"
  101. "setlocal\r\n"
  102. "set rebarscript=%~f0\r\n"
  103. "escript.exe \"%rebarscript:.cmd=%\" %*\r\n",
  104. ok = file:write_file("rebar.cmd", CmdScript).