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.

119 lines
3.9 KiB

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