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.

147 line
4.8 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. %% Compile all src/*.erl to ebin
  23. case make:files(filelib:wildcard("src/*.erl"),
  24. [{outdir, "ebin"}, {i, "include"},
  25. DebugFlag,
  26. {d, 'BUILD_TIME', Built},
  27. {d, 'VCS_INFO', VcsInfo}]) of
  28. up_to_date ->
  29. ok;
  30. error ->
  31. io:format("Failed to compile rebar files!\n"),
  32. halt(1)
  33. end,
  34. %% Make sure file:consult can parse the .app file
  35. case file:consult("ebin/rebar.app") of
  36. {ok, _} ->
  37. ok;
  38. {error, Reason} ->
  39. io:format("Invalid syntax in ebin/rebar.app: ~p\n", [Reason]),
  40. halt(1)
  41. end,
  42. %% Add ebin/ to our path
  43. true = code:add_path("ebin"),
  44. %% Run rebar to do proper .app validation and such
  45. rebar:main(["compile"] ++ Args),
  46. %% Read the contents of the files in ebin and templates; note that we
  47. %% place all the beam files at the top level of the code archive so
  48. %% that code loading works properly.
  49. Files = load_files("*", "ebin") ++ load_files("priv/templates/*", "."),
  50. case zip:create("mem", Files, [memory]) of
  51. {ok, {"mem", ZipBin}} ->
  52. %% Archive was successfully created. Prefix that binary with our
  53. %% header and write to "rebar" file.
  54. %% Without -noshell -noinput escript consumes all input that would
  55. %% otherwise go to the shell for the next command.
  56. Script = <<"#!/usr/bin/env escript\n%%! -noshell -noinput\n",
  57. ZipBin/binary>>,
  58. case file:write_file("rebar", Script) of
  59. ok ->
  60. ok;
  61. {error, WriteError} ->
  62. io:format("Failed to write rebar script: ~p\n",
  63. [WriteError]),
  64. halt(1)
  65. end;
  66. {error, ZipError} ->
  67. io:format("Failed to construct rebar script archive: ~p\n",
  68. [ZipError]),
  69. halt(1)
  70. end,
  71. %% Finally, update executable perms for our script on *nix,
  72. %% or write out script files on win32.
  73. case os:type() of
  74. {unix,_} ->
  75. [] = os:cmd("chmod u+x rebar"),
  76. ok;
  77. {win32,_} ->
  78. write_windows_scripts(),
  79. ok;
  80. _ ->
  81. ok
  82. end,
  83. %% Add a helpful message
  84. io:format("Congratulations! You now have a self-contained script called"
  85. " \"rebar\" in\n"
  86. "your current working directory. "
  87. "Place this script anywhere in your path\n"
  88. "and you can use rebar to build OTP-compliant apps.\n").
  89. rm(Path) ->
  90. NativePath = filename:nativename(Path),
  91. Cmd = case os:type() of
  92. {unix,_} -> "rm -f ";
  93. {win32,_} -> "del /q "
  94. end,
  95. [] = os:cmd(Cmd ++ NativePath),
  96. ok.
  97. build_time() ->
  98. {{Y, M, D}, {H, Min, S}} = calendar:now_to_universal_time(now()),
  99. lists:flatten(io_lib:format("~4..0w~2..0w~2..0w_~2..0w~2..0w~2..0w",
  100. [Y, M, D, H, Min, S])).
  101. load_files(Wildcard, Dir) ->
  102. [read_file(Filename, Dir) || Filename <- filelib:wildcard(Wildcard, Dir)].
  103. read_file(Filename, Dir) ->
  104. {ok, Bin} = file:read_file(filename:join(Dir, Filename)),
  105. {Filename, Bin}.
  106. vcs_info([]) ->
  107. "No VCS info available.";
  108. vcs_info([{Id, Dir, Cmd} | Rest]) ->
  109. case filelib:is_dir(Dir) of
  110. true ->
  111. lists:concat([Id, " ", string:strip(os:cmd(Cmd), both, $\n)]);
  112. false ->
  113. vcs_info(Rest)
  114. end.
  115. write_windows_scripts() ->
  116. PowershellScript=
  117. "$basedir = Split-Path -Parent $MyInvocation.MyCommand.Path\r\n"
  118. "$rebar = Join-Path $basedir \"rebar\"\r\n"
  119. "escript.exe $rebar $args\r\n",
  120. CmdScript=
  121. "@echo off\r\n"
  122. "setlocal\r\n"
  123. "set rebarscript=%~f0\r\n"
  124. "escript.exe \"%rebarscript:.bat=%i\" %*\r\n",
  125. file:write_file("rebar.cmd", CmdScript),
  126. UTF16BE = {utf16, big},
  127. file:write_file("rebar.ps1", [unicode:encoding_to_bom(UTF16BE),
  128. unicode:characters_to_binary(PowershellScript, utf8, UTF16BE)]).