選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

144 行
4.7 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"}]),
  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. %% Read the contents of the files in ebin and templates; note that we
  50. %% place all the beam files at the top level of the code archive so
  51. %% that code loading works properly.
  52. Files = load_files("*", "ebin") ++ load_files("priv/templates/*", "."),
  53. case zip:create("mem", Files, [memory]) of
  54. {ok, {"mem", ZipBin}} ->
  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. io:format("Failed to construct rebar script archive: ~p\n",
  71. [ZipError]),
  72. halt(1)
  73. end,
  74. %% Finally, update executable perms for our script on *nix,
  75. %% or write out script files on win32.
  76. case os:type() of
  77. {unix,_} ->
  78. [] = os:cmd("chmod u+x rebar"),
  79. ok;
  80. {win32,_} ->
  81. write_windows_scripts(),
  82. ok;
  83. _ ->
  84. ok
  85. end,
  86. %% Add a helpful message
  87. io:format("Congratulations! You now have a self-contained script called"
  88. " \"rebar\" in\n"
  89. "your current working directory. "
  90. "Place this script anywhere in your path\n"
  91. "and you can use rebar to build OTP-compliant apps.\n").
  92. rm(Path) ->
  93. NativePath = filename:nativename(Path),
  94. Cmd = case os:type() of
  95. {unix,_} -> "rm -f ";
  96. {win32,_} -> "del /q "
  97. end,
  98. [] = os:cmd(Cmd ++ NativePath),
  99. ok.
  100. build_time() ->
  101. {{Y, M, D}, {H, Min, S}} = calendar:now_to_universal_time(now()),
  102. lists:flatten(io_lib:format("~4..0w~2..0w~2..0w_~2..0w~2..0w~2..0w",
  103. [Y, M, D, H, Min, S])).
  104. load_files(Wildcard, Dir) ->
  105. [read_file(Filename, Dir) || Filename <- filelib:wildcard(Wildcard, Dir)].
  106. read_file(Filename, Dir) ->
  107. {ok, Bin} = file:read_file(filename:join(Dir, Filename)),
  108. {Filename, Bin}.
  109. vcs_info([]) ->
  110. "No VCS info available.";
  111. vcs_info([{Id, Dir, Cmd} | Rest]) ->
  112. case filelib:is_dir(Dir) of
  113. true ->
  114. lists:concat([Id, " ", string:strip(os:cmd(Cmd), both, $\n)]);
  115. false ->
  116. vcs_info(Rest)
  117. end.
  118. write_windows_scripts() ->
  119. CmdScript=
  120. "@echo off\r\n"
  121. "setlocal\r\n"
  122. "set rebarscript=%~f0\r\n"
  123. "escript.exe \"%rebarscript:.cmd=%\" %*\r\n",
  124. ok = file:write_file("rebar.cmd", CmdScript).