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.

112 lines
3.7 KiB

преди 15 години
преди 15 години
  1. #!/usr/bin/env escript
  2. %% -*- mode:erlang;tab-width:4;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. %% Compile all src/*.erl to ebin
  18. case make:files(filelib:wildcard("src/*.erl"), [{outdir, "ebin"}, {i, "include"},
  19. {d, 'BUILD_TIME', Built},
  20. {d, 'VCS_INFO', VcsInfo}]) of
  21. up_to_date ->
  22. ok;
  23. error ->
  24. io:format("Failed to compile rebar files!\n"),
  25. halt(1)
  26. end,
  27. %% Make sure file:consult can parse the .app file
  28. case file:consult("ebin/rebar.app") of
  29. {ok, _} ->
  30. ok;
  31. {error, Reason} ->
  32. io:format("Invalid syntax in ebin/rebar.app: ~p\n", [Reason]),
  33. halt(1)
  34. end,
  35. %% Add ebin/ to our path
  36. true = code:add_path("ebin"),
  37. %% Run rebar to do proper .app validation and such
  38. rebar:main(["compile"] ++ Args),
  39. %% Read the contents of the files in ebin and templates; note that we place
  40. %% all the beam files at the top level of the code archive so that code loading
  41. %% works properly.
  42. Files = load_files("*", "ebin") ++ load_files("priv/templates/*", "."),
  43. case zip:create("mem", Files, [memory]) of
  44. {ok, {"mem", ZipBin}} ->
  45. %% Archive was successfully created. Prefix that binary with our
  46. %% header and write to "rebar" file
  47. Script = <<"#!/usr/bin/env escript\n%%! -noshell -noinput\n", ZipBin/binary>>,
  48. case file:write_file("rebar", Script) of
  49. ok ->
  50. ok;
  51. {error, WriteError} ->
  52. io:format("Failed to write rebar script: ~p\n", [WriteError]),
  53. halt(1)
  54. end;
  55. {error, ZipError} ->
  56. io:format("Failed to construct rebar script archive: ~p\n", [ZipError]),
  57. halt(1)
  58. end,
  59. %% Finally, update executable perms for our script
  60. case os:type() of
  61. {unix,_} ->
  62. [] = os:cmd("chmod u+x rebar"),
  63. ok;
  64. _ ->
  65. ok
  66. end,
  67. %% Add a helpful message
  68. io:format("Congratulations! You now have a self-contained script called \"rebar\" in\n"
  69. "your current working directory. Place this script anywhere in your path\n"
  70. "and you can use rebar to build OTP-compliant apps.\n").
  71. rm(Path) ->
  72. NativePath = filename:nativename(Path),
  73. Cmd = case os:type() of
  74. {unix,_} -> "rm -f ";
  75. {win32,_} -> "del /q "
  76. end,
  77. [] = os:cmd(Cmd ++ NativePath),
  78. ok.
  79. build_time() ->
  80. {{Y, M, D}, {H, Min, S}} = calendar:now_to_universal_time(now()),
  81. lists:flatten(io_lib:format("~4..0w~2..0w~2..0w_~2..0w~2..0w~2..0w", [Y, M, D, H, Min, S])).
  82. load_files(Wildcard, Dir) ->
  83. [read_file(Filename, Dir) || Filename <- filelib:wildcard(Wildcard, Dir)].
  84. read_file(Filename, Dir) ->
  85. {ok, Bin} = file:read_file(filename:join(Dir, Filename)),
  86. {Filename, Bin}.
  87. vcs_info([]) ->
  88. "No VCS info available.";
  89. vcs_info([{Id, Dir, Cmd} | Rest]) ->
  90. case filelib:is_dir(Dir) of
  91. true ->
  92. lists:concat([Id, " ", string:strip(os:cmd(Cmd), both, $\n)]);
  93. false ->
  94. vcs_info(Rest)
  95. end.