Updating tools (WIP)
[euphorik.git] / tools / tools.erl
1 #!/usr/bin/env escript
2
3 % TODO :
4 % * Create some unit tests with eunit before releasing in production.
5
6 main(Args) ->
7 file:set_cwd(".."),
8 case Args of
9 ["build"] -> build();
10 ["run"] -> run_debug();
11 ["pre"] -> in_preprod("gburri@euphorik.ch:/var/www/euphorik_preprod", "var/www/euphorik");
12 ["prod"] -> in_prod("gburri@euphorik.ch:/var/www/euphorik");
13 ["js"] -> verif_js();
14 ["version"] -> update_version();
15 _ ->
16 io:format(
17 "Usage : ~s (build | run | pre | prod | js | version)~n"
18 " prod : in production~n"
19 " pre : in preproduction, data are copied from production~n"
20 " js : check the JavaScript files~n"
21 " version : update the version into somes files from the VERSION file~n",
22 [escript:script_name()]
23 )
24 end.
25
26 % A simple fonction to log message.
27 log(Str) ->
28 io:format("===== ~s =====~n", [Str]).
29
30 % Execute an OS command and print the result to stdout.
31 cmd(Command) ->
32 cmd(Command, []).
33 cmd(Command, Params) ->
34 cmd(Command, Params, ".").
35 cmd(Command, Params, Dir) ->
36 InitialDir = file:get_cwd(),
37 file:set_cwd(Dir),
38 io:format("~s~n", [os:cmd(lists:flatten(io_lib:format(Command, Params)))]),
39 file:set_cwd(InitialDir).
40
41 -record(uri, {
42 host,
43 path % in absolute
44 }).
45
46 % Create an uri record from an uri string.
47 create_uri(Uri_str) ->
48 [Host, Path] = string:tokens(Uri_str, ":"),
49 #uri{host = Host, path = Path}.
50
51 build() ->
52 % Create the directory "var/database" if it doesn't exist.
53 {ok, Files_in_root} = file:list_dir("."),
54 Var_exists = lists:any(fun(Name) -> Name =:= "var" end, Files_in_root),
55 if not Var_exists ->
56 file:make_dir("var");
57 true -> ok
58 end,
59 {ok, Files_in_var} = file:list_dir("var"),
60 Database_exists = lists:any(fun(Name) -> Name =:= "database" end, Files_in_var),
61 if not Database_exists ->
62 file:make_dir("var/database");
63 true -> ok
64 end,
65 % Build all the Erlang modules.
66 cmd("make", [], "modules").
67
68 % Update the version into somes files from the VERSION file.
69 update_version() ->
70 log("Update the version tag"),
71 todo.
72
73 % Check the JavaScript files.
74 verif_js() ->
75 todo.
76
77 run_debug() ->
78 build(),
79 todo.
80
81 % Start the production processus.
82 in_prod(Uri) ->
83 compile_server_part(Uri),
84 make_var_directory(Uri),
85 copy_files(Uri),
86 define_files_rights(Uri),
87 update_server().
88
89 % Start the pre-production processus.
90 in_preprod(Uri, data_path) ->
91 compile_server_part(Uri),
92 make_var_directory(Uri),
93 copy_files(Uri),
94 define_files_rights(Uri),
95 start_server(),
96 update_server().
97
98 % Compile the Erlang modules.
99 compile_server_part(Uri) ->
100 todo.
101
102 % Create the 'var' folder if it doesn't exist.
103 make_var_directory(Uri) ->
104 todo.
105
106 % Copy files from developpement env to production server.
107 copy_files(Uri) ->
108 copy_static_part(Uri),
109 copy_packed_js(Uri).
110
111 % Copy all static files like modules, styles, pages, etc.
112 copy_static_part(Uri) ->
113 %~ creer_rep('modules')
114 %~ system("rsync -r --exclude 'euphorik_test.beam' modules/ebin #{@uri}:#{@rep}/modules")
115 %~ system("rsync -r modules/include #{@uri}:#{@rep}/modules")
116 todo.
117
118 % Minify and pack JavaScript in one file then copy it to ther server.
119 copy_packed_js(Uri) ->
120 todo.
121
122 % Define the rights for the copied folder et files.
123 define_files_rights(Uri) ->
124 todo.
125
126 % Start the server if it not already started (in preproduction case only).
127 start_server() ->
128 cmd("yaws --conf ./yaws.conf --sname yaws_dev --mnesiadir \"../var/database/\" -I debian_yaws_dev").
129
130 % Run a erlang script to
131 update_server() ->
132 todo.