mad_repl.erl 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. -module(mad_repl).
  2. -copyright('Maxim Sokhatsky').
  3. -compile(export_all).
  4. disabled() -> [].
  5. system() -> [compiler,syntax_tools,sasl,tools,mnesia,reltool,xmerl,crypto,kernel,stdlib,ssh,eldap,
  6. wx,webtool,ssl,runtime_tools,public_key,observer,inets,asn1,et,eunit,hipe,os_mon].
  7. local_app() ->
  8. case filename:basename(filelib:wildcard("ebin/*.app"),".app") of
  9. [] -> [];
  10. A -> list_to_atom(A) end.
  11. applist() ->
  12. Name = ".applist",
  13. case file:read_file(Name) of
  14. {ok,Binary} -> parse_applist(Binary);
  15. {error,_} ->
  16. case mad_repl:load_file(Name) of
  17. {error,_} -> mad_resolve:main([]);
  18. {ok,Plan} -> parse_applist(Plan) end end.
  19. wildcards(List) -> lists:concat([filelib:wildcard(X)||X<-List]).
  20. parse_applist(AppList) ->
  21. Res = string:tokens(string:strip(string:strip(binary_to_list(AppList),right,$]),left,$[),","),
  22. [ list_to_atom(R) || R <-Res ] -- disabled().
  23. load_config() ->
  24. Config = wildcards(["sys.config",lists:concat(["etc/",mad:host(),"/sys.config"])]),
  25. Apps = case Config of
  26. [] -> case mad_repl:load_file("sys.config") of
  27. {error,_} -> [];
  28. {ok,Bin} -> parse(binary_to_list(Bin)) end;
  29. File -> case file:consult(hd(File)) of
  30. {error,_} -> [];
  31. {ok,[A]} -> A end end,
  32. load_config(Apps, []).
  33. load_config([H|T], Apps2) ->
  34. App2 = case H of
  35. {App,Cfg} -> [application:set_env(App,K,V) || {K,V} <- Cfg], [H];
  36. File when is_list(File) ->
  37. Apps = case file:consult(File) of
  38. {error,_} -> [];
  39. {ok,[A]} -> A end,
  40. load_config(Apps, []);
  41. _ -> []
  42. end,
  43. load_config(T, Apps2 ++ App2);
  44. load_config([], Apps2) ->
  45. Apps2.
  46. acc_start(A,Acc) ->
  47. case application:start(A) of
  48. {error,{already_started,_}} -> Acc;
  49. {error,{_,{{M,_F,_},_Ret}}} -> [M|Acc];
  50. {error,{_Reason,Name}} when is_atom(_Reason) -> [Name|Acc];
  51. ok -> Acc;
  52. _ -> Acc end.
  53. load_apps([],_,_Acc) ->
  54. Res = lists:foldl(fun(A,Acc) -> case lists:member(A,system()) of
  55. true -> acc_start(A,Acc);
  56. _ -> case load_config(A) of
  57. [] -> acc_start(A,Acc);
  58. _E -> acc_start(_E,Acc) end end end,[], applist()),
  59. case Res of
  60. [] -> ok;
  61. _ -> mad:info("~nApps couldn't be loaded: ~p~n",[Res]) end;
  62. load_apps(["applist"],Config,Acc) -> load_apps([],Config,Acc);
  63. load_apps(Params,_,_Acc) -> [ application:ensure_all_started(list_to_atom(A))||A<-Params].
  64. cwd() -> case file:get_cwd() of {ok, Cwd} -> Cwd; _ -> "." end.
  65. sh(Params) ->
  66. { _Cwd,_ConfigFileName,_Config } = mad_utils:configs(),
  67. SystemPath = filelib:wildcard(code:root_dir() ++ "/lib/{"
  68. ++ string:join([atom_to_list(X)||X<-mad_repl:system()],",") ++ "}-*/ebin"),
  69. UserPath = wildcards(["{apps,deps}/*/ebin","ebin"]),
  70. code:set_path(SystemPath++UserPath),
  71. code:add_path(filename:join([cwd(),filename:basename(escript:script_name())])),
  72. load(),
  73. Config = load_config(),
  74. Driver = mad_utils:get_value(shell_driver,_Config,user_drv),
  75. pre(Driver),
  76. case os:type() of
  77. {win32,nt} -> shell:start();
  78. _ -> Driver:start() end,
  79. post(Driver),
  80. load_apps(Params,Config,[]),
  81. case Params of
  82. ["applist"] -> skip;
  83. _ -> timer:sleep(infinity) end.
  84. load() ->
  85. ets_created(),
  86. {ok,Sections} = escript:extract(escript:script_name(),[]),
  87. [Bin] = [B||{archive,B}<-Sections],
  88. unfold_zips(Bin).
  89. unfold_zips(Bin) ->
  90. {ok,Unzip} = zip:unzip(Bin,[memory]),
  91. [ begin
  92. ets:insert(filesystem,{U,FileBin}),
  93. case U of
  94. "static.gz" -> unfold_zips(FileBin);
  95. _ -> skip end
  96. end || {U,FileBin} <- Unzip].
  97. ets_created() ->
  98. case ets:info(filesystem) of
  99. undefined -> ets:new(filesystem,[set,named_table,{keypos,1},public]);
  100. _ -> skip end.
  101. load_file(Name) ->
  102. ets_created(),
  103. case ets:lookup(filesystem,Name) of
  104. [{Name,Bin}] -> {ok,Bin};
  105. _ -> {error,etsfs} end.
  106. load_config(A) when is_atom(A) -> load_config(atom_to_list(A));
  107. load_config(A) when is_list(A) ->
  108. AppFile = A ++".app",
  109. Name = wildcards(["{apps,deps}/*/ebin/"++AppFile,"ebin/"++AppFile]),
  110. case file:read_file(Name) of
  111. {ok,Bin} -> parse(binary_to_list(Bin));
  112. {error,_} -> case ets:lookup(filesystem,AppFile) of
  113. [{Name,Bin}] -> parse(binary_to_list(Bin));
  114. _ -> [] end end.
  115. parse(String) ->
  116. {ok,Tokens,_EndLine} = erl_scan:string(String),
  117. {ok,AbsForm} = erl_parse:parse_exprs(Tokens),
  118. {value,Value,_Bs} = erl_eval:exprs(AbsForm, erl_eval:new_bindings()),
  119. Value.
  120. % we need to call printing before starting driver for user_drv
  121. % but for start_kjell we should call after, that's why we have pre and post here.
  122. pre(start_kjell) -> [];
  123. pre(user_drv) -> unregister(user), appconfig(user_drv);
  124. pre(Driver) -> appconfig(Driver).
  125. post(start_kjell) -> appconfig(start_kjell);
  126. post(_) -> [].
  127. print(Label,Value,start_kjell) -> io:requests([{put_chars,Label ++ normalize(length(Label)+1,Value) ++ "\n\r"}]);
  128. print(Label,Value,_) -> mad:info("~s~p~n",[Label,Value]).
  129. normalize(Padding,V) -> [ case X of 10 -> [13,10]; E -> E end || X <- lists:flatten(pp(Padding,V) )].
  130. pp(Padding,V) -> k_io_lib_pretty:print(V, Padding, 80, 30, 60, fun(_,_)-> no end).
  131. appconfig(Driver) ->
  132. print("Configuration: ", load_config(), Driver),
  133. print("Applications: ", applist(), Driver).