No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

83 líneas
2.8 KiB

hace 15 años
  1. #!/usr/bin/env escript
  2. %% -*- erlang -*-
  3. main(Args) ->
  4. %% Get a string repr of build time
  5. Built = build_time(),
  6. %% Check for force=1 flag to force a rebuild
  7. case lists:member("force=1", Args) of
  8. true ->
  9. [] = os:cmd("rm -rf ebin/*.beam"),
  10. ok;
  11. false ->
  12. ok
  13. end,
  14. %% Compile all src/*.erl to ebin
  15. case make:files(filelib:wildcard("src/*.erl"), [{outdir, "ebin"}, {i, "include"},
  16. {d, 'BUILD_TIME', Built}]) of
  17. up_to_date ->
  18. ok;
  19. error ->
  20. io:format("Failed to compile rebar files!\n"),
  21. halt(1)
  22. end,
  23. %% Make sure file:consult can parse the .app file
  24. case file:consult("ebin/rebar.app") of
  25. {ok, _} ->
  26. ok;
  27. {error, Reason} ->
  28. io:format("Invalid syntax in ebin/rebar.app: ~p\n", [Reason]),
  29. halt(1)
  30. end,
  31. %% Add ebin/ to our path
  32. true = code:add_path("ebin"),
  33. %% Run rebar to do proper .app validation and such
  34. rebar:main(["compile"] ++ Args),
  35. %% Read the contents of the files in ebin and templates; note that we place
  36. %% all the beam files at the top level of the code archive so that code loading
  37. %% works properly.
  38. Files = load_files("*", "ebin") ++ load_files("priv/templates/*", "."),
  39. case zip:create("mem", Files, [memory]) of
  40. {ok, {"mem", ZipBin}} ->
  41. %% Archive was successfully created. Prefix that binary with our
  42. %% header and write to "rebar" file
  43. Script = <<"#!/usr/bin/env escript\n", ZipBin/binary>>,
  44. case file:write_file("rebar", Script) of
  45. ok ->
  46. ok;
  47. {error, WriteError} ->
  48. io:format("Failed to write rebar script: ~p\n", [WriteError]),
  49. halt(1)
  50. end;
  51. {error, ZipError} ->
  52. io:format("Failed to construct rebar script archive: ~p\n", [ZipError]),
  53. halt(1)
  54. end,
  55. %% Finally, update executable perms for our script
  56. [] = os:cmd("chmod u+x rebar"),
  57. %% Add a helpful message
  58. io:format("Congratulations! You now have a self-contained script called \"rebar\" in\n"
  59. "your current working directory. Place this script anywhere in your path\n"
  60. "and you can use rebar to build OTP-compliant apps.\n").
  61. build_time() ->
  62. {{Y, M, D}, {H, Min, S}} = calendar:now_to_universal_time(now()),
  63. lists:flatten(io_lib:format("~4..0w~2..0w~2..0w_~2..0w~2..0w~2..0w", [Y, M, D, H, Min, S])).
  64. load_files(Wildcard, Dir) ->
  65. [read_file(Filename, Dir) || Filename <- filelib:wildcard(Wildcard, Dir)].
  66. read_file(Filename, Dir) ->
  67. {ok, Bin} = file:read_file(filename:join(Dir, Filename)),
  68. {Filename, Bin}.