Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

135 Zeilen
4.4 KiB

vor 13 Jahren
vor 13 Jahren
vor 15 Jahren
vor 13 Jahren
vor 15 Jahren
vor 13 Jahren
  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. %% Types dict:dict() and digraph:digraph() have been introduced in Erlang 17.
  26. %% At the same time, their counterparts dict() and digraph() are to be deprecated
  27. %% in Erlang 18. namespaced_types option is used to select proper type name
  28. %% depending of the OTP version used.
  29. NamespacedTypes = case is_otp(OtpInfo, "^[0-9]+") of
  30. true -> {d, namespaced_types};
  31. false -> undefined
  32. end,
  33. %% Compile all src/*.erl to ebin
  34. case make:files(filelib:wildcard("src/*.erl"),
  35. [{outdir, "ebin"}, {i, "include"},
  36. DebugFlag,
  37. NamespacedTypes,
  38. {d, 'BUILD_TIME', Built},
  39. {d, 'VCS_INFO', VcsInfo},
  40. {d, 'OTP_INFO', OtpInfo}]) of
  41. up_to_date ->
  42. ok;
  43. error ->
  44. io:format("Failed to compile rebar files!\n"),
  45. halt(1)
  46. end,
  47. %% Make sure file:consult can parse the .app file
  48. case file:consult("ebin/rebar.app") of
  49. {ok, _} ->
  50. ok;
  51. {error, Reason} ->
  52. io:format("Invalid syntax in ebin/rebar.app: ~p\n", [Reason]),
  53. halt(1)
  54. end,
  55. %% Add ebin/ to our path
  56. true = code:add_path("ebin"),
  57. %% Run rebar compile to do proper .app validation etc.
  58. %% and rebar escriptize to create the rebar script
  59. RebarArgs = Args -- ["debug"], %% Avoid trying to run 'debug' command
  60. rebar:main(["compile", "escriptize"] ++ RebarArgs),
  61. %% Finally, update executable perms for our script on *nix,
  62. %% or write out script files on win32.
  63. case os:type() of
  64. {unix,_} ->
  65. [] = os:cmd("chmod u+x rebar"),
  66. ok;
  67. {win32,_} ->
  68. write_windows_scripts(),
  69. ok;
  70. _ ->
  71. ok
  72. end,
  73. %% Add a helpful message
  74. io:format("Congratulations! You now have a self-contained script called"
  75. " \"rebar\" in\n"
  76. "your current working directory. "
  77. "Place this script anywhere in your path\n"
  78. "and you can use rebar to build OTP-compliant apps.\n").
  79. is_otp(OtpInfo, Regex) ->
  80. case re:run(OtpInfo, Regex, [{capture, none}]) of
  81. match -> true;
  82. nomatch -> false
  83. end.
  84. rm(Path) ->
  85. NativePath = filename:nativename(Path),
  86. Cmd = case os:type() of
  87. {unix,_} -> "rm -f ";
  88. {win32,_} -> "del /q "
  89. end,
  90. [] = os:cmd(Cmd ++ NativePath),
  91. ok.
  92. build_time() ->
  93. {{Y, M, D}, {H, Min, S}} = calendar:now_to_universal_time(now()),
  94. lists:flatten(io_lib:format("~4..0w~2..0w~2..0w_~2..0w~2..0w~2..0w",
  95. [Y, M, D, H, Min, S])).
  96. vcs_info([]) ->
  97. "No VCS info available.";
  98. vcs_info([{Id, Dir, VsnCmd, StatusCmd} | Rest]) ->
  99. case filelib:is_dir(Dir) of
  100. true ->
  101. Vsn = string:strip(os:cmd(VsnCmd), both, $\n),
  102. Status = case string:strip(os:cmd(StatusCmd), both, $\n) of
  103. [] ->
  104. "";
  105. _ ->
  106. "-dirty"
  107. end,
  108. lists:concat([Id, " ", Vsn, Status]);
  109. false ->
  110. vcs_info(Rest)
  111. end.
  112. write_windows_scripts() ->
  113. CmdScript=
  114. "@echo off\r\n"
  115. "setlocal\r\n"
  116. "set rebarscript=%~f0\r\n"
  117. "escript.exe \"%rebarscript:.cmd=%\" %*\r\n",
  118. ok = file:write_file("rebar.cmd", CmdScript).