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.

121 regels
3.9 KiB

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