Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

128 рядки
4.1 KiB

14 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
15 роки тому
13 роки тому
15 роки тому
13 роки тому
13 роки тому
13 роки тому
  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
  72. case os:type() of
  73. {unix,_} ->
  74. [] = os:cmd("chmod u+x rebar"),
  75. ok;
  76. _ ->
  77. ok
  78. end,
  79. %% Add a helpful message
  80. io:format("Congratulations! You now have a self-contained script called"
  81. " \"rebar\" in\n"
  82. "your current working directory. "
  83. "Place this script anywhere in your path\n"
  84. "and you can use rebar to build OTP-compliant apps.\n").
  85. rm(Path) ->
  86. NativePath = filename:nativename(Path),
  87. Cmd = case os:type() of
  88. {unix,_} -> "rm -f ";
  89. {win32,_} -> "del /q "
  90. end,
  91. [] = os:cmd(Cmd ++ NativePath),
  92. ok.
  93. build_time() ->
  94. {{Y, M, D}, {H, Min, S}} = calendar:now_to_universal_time(now()),
  95. lists:flatten(io_lib:format("~4..0w~2..0w~2..0w_~2..0w~2..0w~2..0w",
  96. [Y, M, D, H, Min, S])).
  97. load_files(Wildcard, Dir) ->
  98. [read_file(Filename, Dir) || Filename <- filelib:wildcard(Wildcard, Dir)].
  99. read_file(Filename, Dir) ->
  100. {ok, Bin} = file:read_file(filename:join(Dir, Filename)),
  101. {Filename, Bin}.
  102. vcs_info([]) ->
  103. "No VCS info available.";
  104. vcs_info([{Id, Dir, Cmd} | Rest]) ->
  105. case filelib:is_dir(Dir) of
  106. true ->
  107. lists:concat([Id, " ", string:strip(os:cmd(Cmd), both, $\n)]);
  108. false ->
  109. vcs_info(Rest)
  110. end.