Merge remote-tracking branch 'canonical/next'
[sinan.git] / src / sin_gen.erl
blob4f439ca3982f8ba58de3c389fe99bc79e5419b6c
1 %% -*- mode: Erlang; fill-column: 80; comment-column: 75; -*-
2 %%%---------------------------------------------------------------------------
3 %%% @author Eric Merritt <ericbmerritt@gmail.com>
4 %%% @doc
5 %%% Provides utitlities to generate a complient otp/erlang
6 %%% project when arguments are correctly passed to it.
7 %%% @end
8 %%% @copyright (C) 2007-2011 Erlware
9 %%%---------------------------------------------------------------------------
10 -module(sin_gen).
12 %% API
13 -export([gen/2]).
15 %%============================================================================
16 %% Types
17 %%============================================================================
19 -type env() :: [{Key::atom(), Value::term()}].
21 %%============================================================================
22 %% API
23 %%============================================================================
25 %% @doc Kicks off the generation process. Handles the individual steps in new
26 %% project generation.
27 -spec gen(sin_config:config(), [{Key::term(), Value::term()}]) -> ok.
28 gen(Config, Env) ->
29 build_out_skeleton(Config, Env).
31 %%============================================================================
32 %% Internal functions
33 %%============================================================================
34 %% @doc Given the project directory builds out the various directories required
35 %% for an application.
36 -spec build_out_skeleton(sin_config:config(), env()) -> ok.
37 build_out_skeleton(Config, Env) ->
38 ProjDir = get_env(project_dir, Env),
39 make_dir(Config, filename:join(ProjDir, "doc")),
40 make_dir(Config, filename:join(ProjDir, "config")),
41 build_out_applications(Config, Env).
43 %% @doc Given the project directory and a list of application names, builds out
44 %% the application directory structure.
45 -spec build_out_applications(sin_config:config(), env()) -> ok.
46 build_out_applications(Config, Env) ->
47 case get_env(single_app_project, Env) of
48 false ->
49 Apps = get_env(apps, Env),
50 build_out_applications(Config, Env, Apps);
51 true ->
52 ProjDir = get_env(project_dir, Env),
53 ProjVsn = get_env(project_version, Env),
54 ProjName = get_env(project_name, Env),
55 build_out_application(Config, Env, ProjDir, ProjName, ProjVsn),
56 build_out_build_config(Env)
57 end.
59 -spec build_out_applications(sin_config:config(), env(), AppNames::[string()]) -> ok.
60 build_out_applications(Config, Env, [AppName | T]) ->
61 ProjDir = get_env(project_dir, Env),
62 AppDir = filename:join([ProjDir, "lib", AppName]),
63 build_out_application(Config, Env, AppDir, AppName, "0.1.0"),
64 build_out_applications(Config, Env, T);
65 build_out_applications(_Config, Env, []) ->
66 build_out_build_config(Env).
68 %% @doc build out all the things required by an application
69 -spec build_out_application(sin_config:config(), env(), AppDir::string(),
70 AppName::string(), AppVsn::string()) -> ok.
71 build_out_application(Config, Env, AppDir, AppName, AppVsn) ->
72 EbinDir = make_dir(Config, filename:join(AppDir, "ebin")),
73 AppSrc = make_dir(Config, filename:join(AppDir, "src")),
74 make_dir(Config, filename:join(AppDir, "include")),
75 make_dir(Config, filename:join(AppDir, "doc")),
76 build_out_super(Env, AppSrc, AppName),
77 build_out_app_src(Env, AppSrc, AppName, AppVsn),
78 build_out_otp(Env, AppSrc, AppName),
79 build_out_app_doc(Env, EbinDir, AppName).
81 %% @doc Builds out the supervisor for the app.
82 -spec build_out_super(env(), AppSrc::string(), AppName::string()) -> ok.
83 build_out_super(Env, AppSrc, AppName) ->
84 FileName = filename:join(AppSrc, AppName ++ "_sup.erl"),
85 case filelib:is_file(FileName) of
86 true ->
87 ok;
88 false ->
89 sin_skel:supervisor(Env, FileName, AppName)
90 end.
92 %% @doc Builds out the app descriptor for the app.
93 -spec build_out_app_src(env(), EbinDir::string(),
94 AppName::string(), AppVsn::string()) -> ok.
95 build_out_app_src(Env, AppSrc, AppName, AppVsn) ->
96 FileName = filename:join(AppSrc, AppName ++ ".app.src"),
97 case filelib:is_file(FileName) of
98 true ->
99 ok;
100 false ->
101 sin_skel:app_info(Env, FileName, AppName, AppVsn)
102 end.
104 %% @doc Builds out the overview.edoc for the app.
105 -spec build_out_app_doc(env(), EbinDir::string(), AppName::string()) -> ok.
106 build_out_app_doc(Env, EbinDir, AppName) ->
107 FileName = filename:join(EbinDir, "overview.edoc"),
108 case filelib:is_file(FileName) of
109 true ->
111 false ->
112 sin_skel:edoc_overview(Env, FileName, AppName)
113 end.
115 %% @doc Build out the top level otp parts of the application.
116 -spec build_out_otp(env(), AppSrc::string(), AppName::string()) -> ok.
117 build_out_otp(Env, AppSrc, AppName) ->
118 FileName = filename:join(AppSrc, AppName ++ "_app.erl"),
119 case filelib:is_file(FileName) of
120 true ->
122 false ->
123 sin_skel:application(Env, FileName, AppName)
124 end.
126 %% @doc Builds the build config dir in the root of the project.
127 build_out_build_config(Env) ->
128 case get_env(wants_build_config, Env) of
129 true ->
130 ProjectDir = get_env(project_dir, Env),
131 ConfName = filename:join([ProjectDir, "sinan.config"]),
132 ConfigFile = filename:join([ProjectDir, "config", "sys.config"]),
133 sin_skel:build_config(Env, ConfName),
134 sin_skel:sysconfig(Env, ConfigFile);
135 false ->
137 end.
139 %% @doc Helper function that makes the specified directory and all parent
140 %% directories.
141 -spec make_dir(sin_config:config(), DirName::string()) -> ok.
142 make_dir(Config, DirName) ->
143 filelib:ensure_dir(DirName),
144 is_made(Config, DirName, file:make_dir(DirName)),
145 DirName.
147 %% @doc Helper function that makes sure a directory is made by testing the
148 %% output of file:make_dir().
149 is_made(Config, DirName, ok) ->
150 sin_log:verbose(Config, "~s created ok.", [DirName]);
151 is_made(Config, DirName, {error, eexist}) ->
152 sin_log:verbose(Config, "~s created ok.", [DirName]).
154 %% @doc Get the value from the environment.
155 -spec get_env(Key::string(), env()) -> ok.
156 get_env(Name, Env) ->
157 {value, {Name, Value}} = lists:keysearch(Name, 1, Env),
158 Value.