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.

188 rivejä
5.9 KiB

14 vuotta sitten
14 vuotta sitten
14 vuotta sitten
14 vuotta sitten
15 vuotta sitten
14 vuotta sitten
15 vuotta sitten
14 vuotta sitten
  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 to do proper .app validation and such
  48. rebar:main(["compile"] ++ Args),
  49. TempDir = make_temp_dir(),
  50. ok = copy_files(TempDir), %% Copy the ebin and priv/templates
  51. {ok, Dirs} = file:list_dir(TempDir),
  52. case zip:create("mem", Dirs, [memory, {cwd, TempDir}]) of
  53. {ok, {"mem", ZipBin}} ->
  54. ok = rebar_file_utils:rm_rf(TempDir),
  55. %% Archive was successfully created. Prefix that binary with our
  56. %% header and write to "rebar" file.
  57. %% Without -noshell -noinput escript consumes all input that would
  58. %% otherwise go to the shell for the next command.
  59. Script = <<"#!/usr/bin/env escript\n%%! -noshell -noinput\n",
  60. ZipBin/binary>>,
  61. case file:write_file("rebar", Script) of
  62. ok ->
  63. ok;
  64. {error, WriteError} ->
  65. io:format("Failed to write rebar script: ~p\n",
  66. [WriteError]),
  67. halt(1)
  68. end;
  69. {error, ZipError} ->
  70. ok = rebar_file_utils:rm_rf(TempDir),
  71. io:format("Failed to construct rebar script archive: ~p\n",
  72. [ZipError]),
  73. halt(1)
  74. end,
  75. %% Finally, update executable perms for our script on *nix,
  76. %% or write out script files on win32.
  77. case os:type() of
  78. {unix,_} ->
  79. [] = os:cmd("chmod u+x rebar"),
  80. ok;
  81. {win32,_} ->
  82. write_windows_scripts(),
  83. ok;
  84. _ ->
  85. ok
  86. end,
  87. %% Add a helpful message
  88. io:format("Congratulations! You now have a self-contained script called"
  89. " \"rebar\" in\n"
  90. "your current working directory. "
  91. "Place this script anywhere in your path\n"
  92. "and you can use rebar to build OTP-compliant apps.\n").
  93. make_temp_dir() ->
  94. case temp_name("rebar.") of
  95. {ok, TempDir} ->
  96. case file:make_dir(TempDir) of
  97. ok ->
  98. TempDir;
  99. Error ->
  100. io:format("Failed to create temporary directory: ~p~n",
  101. [Error]),
  102. halt(1),
  103. Error
  104. end;
  105. Error ->
  106. io:format("Failed to create temporary directory: ~p~n",
  107. [Error]),
  108. halt(1)
  109. end.
  110. temp_name(Prefix) ->
  111. temp_name(Prefix, 5).
  112. temp_name(_Prefix, 0) ->
  113. {error, eexist};
  114. temp_name(Prefix, N) ->
  115. Hash = erlang:phash2(make_ref()),
  116. Name = Prefix ++ integer_to_list(Hash),
  117. case filelib:is_file(Name) of
  118. false -> {ok, Name};
  119. true -> temp_name(Prefix, N-1)
  120. end.
  121. rm(Path) ->
  122. NativePath = filename:nativename(Path),
  123. Cmd = case os:type() of
  124. {unix,_} -> "rm -f ";
  125. {win32,_} -> "del /q "
  126. end,
  127. [] = os:cmd(Cmd ++ NativePath),
  128. ok.
  129. build_time() ->
  130. {{Y, M, D}, {H, Min, S}} = calendar:now_to_universal_time(now()),
  131. lists:flatten(io_lib:format("~4..0w~2..0w~2..0w_~2..0w~2..0w~2..0w",
  132. [Y, M, D, H, Min, S])).
  133. copy_files(Temp) ->
  134. BaseEbinDir = filename:join("rebar", "ebin"),
  135. BaseTemplatesDir = filename:join("priv", "templates"),
  136. EbinDir = filename:join(Temp, BaseEbinDir),
  137. TemplatesDir = filename:join(Temp, BaseTemplatesDir),
  138. %% prepare directory structure
  139. lists:foreach(
  140. fun(Dir) ->
  141. ok = filelib:ensure_dir(filename:join(Dir, "dummy"))
  142. end, [EbinDir, TemplatesDir]),
  143. %% copy content of ebin
  144. EbinSrc = filename:join(["ebin", "*"]),
  145. ok = rebar_file_utils:cp_r([EbinSrc], EbinDir),
  146. %% copy content of priv/templates
  147. TemplatesSrc = filename:join(BaseTemplatesDir, "*"),
  148. ok = rebar_file_utils:cp_r([TemplatesSrc], TemplatesDir).
  149. vcs_info([]) ->
  150. "No VCS info available.";
  151. vcs_info([{Id, Dir, Cmd} | Rest]) ->
  152. case filelib:is_dir(Dir) of
  153. true ->
  154. lists:concat([Id, " ", string:strip(os:cmd(Cmd), both, $\n)]);
  155. false ->
  156. vcs_info(Rest)
  157. end.
  158. write_windows_scripts() ->
  159. CmdScript=
  160. "@echo off\r\n"
  161. "setlocal\r\n"
  162. "set rebarscript=%~f0\r\n"
  163. "escript.exe \"%rebarscript:.cmd=%\" %*\r\n",
  164. ok = file:write_file("rebar.cmd", CmdScript).