Namdak Tonpa 10 years ago
parent
commit
431a4ee5c1
10 changed files with 89 additions and 16 deletions
  1. 1 1
      LICENSE
  2. 1 1
      include/mad.hrl
  3. BIN
      mad
  4. 3 1
      rebar.config
  5. 5 0
      src/mad.erl
  6. 12 3
      src/mad_bundle.erl
  7. 3 2
      src/mad_compile.erl
  8. 55 0
      src/mad_ling.erl
  9. 8 8
      src/mad_repl.erl
  10. 1 0
      src/mad_utils.erl

+ 1 - 1
LICENSE

@@ -1,4 +1,4 @@
-Copyright (c) 2013 Maxim Sokhatsky, Synrc Research Center
+Copyright (c) 2013—2015 Maxim Sokhatsky, Synrc Research Center
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal

+ 1 - 1
include/mad.hrl

@@ -1 +1 @@
--define(VERSION,"ce7201").
+-define(VERSION,"6225f8").

BIN
mad


+ 3 - 1
rebar.config

@@ -1,2 +1,4 @@
+%{sub_dirs,["deps/ling/apps"]}.
 {deps_dir,"deps"}.
-{deps, [{sh, ".*", {git, "git://github.com/synrc/sh", {tag, "1.4"}}}]}.
+{deps, [{ling, ".*", {git, "git://github.com/cloudozer/ling", {tag, "master"}}},
+        {sh, ".*",   {git, "git://github.com/synrc/sh",       {tag, "1.4"}}}]}.

+ 5 - 0
src/mad.erl

@@ -69,6 +69,11 @@ up(_Cwd,_ConfigFileName,_Config,Params) ->
     io:format("Up Params: ~p~n",[Params]),
     mad_deps:up(_Config,Params).
 
+ling(_Cwd,_ConfigFileName,_Config,Params) ->
+    io:format("Ling Params: ~p~n",[Params]),
+    Name = case Params of [] -> mad_utils:cwd(); E -> E end,
+    mad_ling:main(filename:basename(Name)).
+
 app(_Cwd,_ConfigFileName,_Config,Params) ->
     io:format("Create App Params: ~p~n",[Params]),
     mad_create:app(Params).

+ 12 - 3
src/mad_bundle.erl

@@ -2,9 +2,11 @@
 -copyright('Maxim Sokhatsky').
 -compile(export_all).
 
+id(X) -> X.
+
 main(App) ->
     EmuArgs = "-noshell -noinput",
-    Files = static() ++ beams(),
+    Files = static() ++ beams(fun filename:basename/1),
     escript:create(App,[shebang,{comment,""},{emu_args,EmuArgs},{archive,Files,[memory]}]),
     file:change_mode(App, 8#764),
     false.
@@ -16,7 +18,14 @@ static() -> Name = "static.gz",
         [ F || F <- mad_repl:wildcards(["{apps,deps}/*/priv/**","priv/**"]), not filelib:is_dir(F) ],
         [{compress,all},memory]), [ { Name, Bin } ].
 
-beams() ->
-    [ { filename:basename(F), read_file(F) } || F <-
+beams() -> beams(fun id/1).
+beams(Fun) ->
+    [ { Fun(F), read_file(F) } || F <-
         lists:concat([filelib:wildcard(X)||X <-
         [ "ebin/*","{apps,deps}/*/ebin/*","sys.config",".applist"]])].
+
+privs() -> privs(fun id/1).
+privs(Fun) ->
+    [ { Fun(F), read_file(F) } || F <-
+        lists:concat([filelib:wildcard(X)||X <-
+        [ F || F <- mad_repl:wildcards(["{apps,deps}/*/priv/**","priv/**"]), not filelib:is_dir(F) ]])].

+ 3 - 2
src/mad_compile.erl

@@ -87,8 +87,9 @@ is_compiled(BeamFile, File) -> mad_utils:last_modified(BeamFile) >= mad_utils:la
     Dirs = mad_utils:sub_dirs(Cwd, ConfigFile, Conf),
     %io:format("Compile Apps: ~p~n",[Dirs]),
     case Dirs of
-        [] -> mad_compile:dep(Cwd, Conf, ConfigFile, Cwd);
-        Apps -> mad_compile:deps(Cwd, Conf, ConfigFile, Apps) end.
+           [] -> mad_compile:dep(Cwd,  Conf, ConfigFile, Cwd);
+         Apps -> mad_compile:dep(Cwd,  Conf, ConfigFile, Cwd),
+                 mad_compile:deps(Cwd, Conf, ConfigFile, Apps) end.
 
 'compile-deps'(Cwd, ConfigFile, Conf) ->
     mad_compile:deps(Cwd, Conf, ConfigFile, mad_utils:get_value(deps, Conf, [])).

+ 55 - 0
src/mad_ling.erl

@@ -0,0 +1,55 @@
+-module(mad_ling).
+-description("LING Erlang Virtual Machine Bundle Packaging").
+-copyright('Cloudozer, LLP').
+-compile(export_all).
+
+main(App) ->
+    io:format("Bundle Name: ~p~n",[mad_repl:local_app()]),
+    io:format("System: ~p~n",     [mad_repl:system()]),
+    io:format("Apps: ~p~n",       [mad_repl:applist()]),
+    io:format("Overlay: ~p~n",    [overlay()]),
+    io:format("Files: ~p~n",      [[B||{B,_} <- bundle()]]),
+    add_apps(),
+    false.
+
+bundle()         -> mad_bundle:beams(fun filename:basename/1) ++ mad_bundle:privs().
+cache_dir()      -> ".railing".
+overlay()        -> filelib:wildcard("deps/ling/apps/*/ebin/*.beam").
+local_map(Bucks) -> list_to_binary(lists:map(fun({B,M,_}) ->
+                    io_lib:format("~s /~s\n",[M,B]) end, Bucks)).
+
+bundle_name() ->
+    case file:get_cwd() of
+		 {ok,"/"} -> "himmel";
+		 {ok,Cwd} -> filename:basename(Cwd) end.
+
+add_apps() ->
+	StartBoot = lists:concat([code:root_dir(),"/bin/start.boot"]),
+	Bucks     = [ {boot, "/boot", [local_map, StartBoot]} ]
+	         ++ [ lib(A) || A <- mad_repl:applist() ].
+
+lib(A) -> A.
+
+embed_fs(Bucks)  ->
+    EmbedFsPath   = lists:concat([cache_dir(),"/embed.fs"]),
+    {ok, EmbedFs} = file:open(EmbedFsPath, [write]),
+    BuckCount = length(Bucks),
+    BinCount = lists:foldl(fun({_,_,Bins},Count) -> Count + length(Bins) end,0,Bucks),
+    file:write(EmbedFs, <<BuckCount:32,BinCount:32>>),
+    lists:foreach(fun({Buck,_,Bins}) ->
+		  BuckName = binary:list_to_bin(atom_to_list(Buck)),
+          BuckNameSize = size(BuckName),
+		  BuckBinCount = length(Bins),
+          file:write(EmbedFs, <<BuckNameSize, BuckName/binary, BuckBinCount:32>>),
+          lists:foreach(fun
+                (local_map) -> write_bin(EmbedFs, "local.map", local_map(Bucks));
+				(Bin) -> write_bin(EmbedFs, filename:basename(Bin), element(2,file:read_file(Bin)))
+		  end,Bins)
+	end,Bucks),
+    file:close(EmbedFs).
+
+write_bin(Dev, Bin, Data) ->
+	Name = binary:list_to_bin(Bin),
+	NameSize = size(Name),
+	DataSize = size(Data),
+	file:write(Dev, <<NameSize, Name/binary, DataSize:32, Data/binary>>).

+ 8 - 8
src/mad_repl.erl

@@ -6,12 +6,12 @@ disabled() -> [].
 system() -> [compiler,syntax_tools,sasl,tools,mnesia,reltool,xmerl,crypto,kernel,stdlib,
              wx,webtool,ssl,runtime_tools,public_key,observer,inets,asn1,et,eunit,hipe,os_mon].
 
-local_app() -> 
+local_app() ->
     case filename:basename(filelib:wildcard("ebin/*.app"),".app") of
          [] -> [];
-         A -> [list_to_atom(A)] end.
+         A -> list_to_atom(A) end.
 
-applist() -> 
+applist() ->
     Name = ".applist",
     case file:read_file(Name) of
          {ok,Binary} -> parse_applist(Binary); 
@@ -22,7 +22,7 @@ applist() ->
 
 wildcards(List) -> lists:concat([filelib:wildcard(X)||X<-List]).
 
-parse_applist(AppList) -> 
+parse_applist(AppList) ->
    Res = string:tokens(string:strip(string:strip(binary_to_list(AppList),right,$]),left,$[),","),
    [ list_to_atom(R) || R <-Res ]  -- disabled().
 
@@ -59,10 +59,10 @@ load_apps(Params,_,_Acc) -> [ application:ensure_all_started(list_to_atom(A))||A
 
 cwd() -> {ok, Cwd} = file:get_cwd(), Cwd.
 
-main(Params,RebarConfig) -> 
-    SystemPath = filelib:wildcard(code:root_dir() ++ 
-      "/lib/{"++ string:join([atom_to_list(X)||X<-mad_repl:system()],",") ++ "}-*/ebin"),
-    UserPath = wildcards(["{apps,deps}/*/ebin","ebin"]),
+main(Params,RebarConfig) ->
+    SystemPath = filelib:wildcard(code:root_dir() ++ "/lib/{"
+              ++ string:join([atom_to_list(X)||X<-mad_repl:system()],",") ++ "}-*/ebin"),
+    UserPath   = wildcards(["{apps,deps}/*/ebin","ebin"]),
     code:set_path(SystemPath++UserPath),
     code:add_path(filename:join([cwd(),filename:basename(escript:script_name())])),
     load(),

+ 1 - 0
src/mad_utils.erl

@@ -84,6 +84,7 @@ atomize("sta"++_) -> start;
 atomize("att"++_) -> attach;
 atomize("sto"++_) -> stop;
 atomize("cle"++_) -> clean;
+atomize("lin"++_) -> ling;
 atomize("rel"++_) -> release;
 atomize(Else) -> Else.