2323
2424#include < iostream>
2525
26+ using void_f = std::function<void (void )>;
27+
2628const String invitation = " > " ;
2729
2830void completion_callback (const char *s, Strings &completions);
@@ -34,6 +36,14 @@ bool is_y(const String &s)
3436 return s[0 ] == ' y' || s[0 ] == ' Y' ;
3537}
3638
39+ void readline (String &d)
40+ {
41+ String s;
42+ std::getline (std::cin, s);
43+ if (!s.empty ())
44+ d = s;
45+ }
46+
3747auto read_packages (const String &s)
3848{
3949 auto &pdb = getPackagesDatabase ();
@@ -69,31 +79,35 @@ auto read_versions(const String &pkg)
6979 return spkgs;
7080}
7181
82+ bool y_n_branch (const String &s, const void_f &yf = void_f(), const void_f &nf = void_f())
83+ {
84+ std::cout << s << " (y/n) [n]: " ;
85+ String t;
86+ readline (t);
87+ bool y = is_y (t);
88+ if (yf)
89+ yf ();
90+ else if (nf)
91+ nf ();
92+ return y;
93+ }
94+
7295void command_init (const Strings &args)
7396{
97+ bool script = false ;
7498 String project_type = " e" ;
7599 Project p;
76100 p.name = fs::current_path ().filename ().string ();
77101
78- auto readline = [](String &d)
79- {
80- String s;
81- std::getline (std::cin, s);
82- if (!s.empty ())
83- d = s;
84- };
85-
86102 // interactive mode
87103 if (args.empty ())
88104 {
105+ script = y_n_branch (" Create script?" );
89106 std::cout << " Enter project name [" << p.name << " ]: " ;
90107 readline (p.name );
91108 std::cout << " Enter project type (e - executable, l - library) [" << project_type << " ]: " ;
92109 readline (project_type);
93- std::cout << " Add some dependencies (y/n) [n]: " ;
94- String add_deps;
95- readline (add_deps);
96- if (is_y (add_deps))
110+ y_n_branch (" Add some dependencies?" , [&]
97111 {
98112 std::cout << " Start entering dependency names. Press TAB to list matching packages, ESC to stop.\n " ;
99113
@@ -129,7 +143,7 @@ void command_init(const Strings &args)
129143 std::cout << e.what () << " \n " ;
130144 }
131145 }
132- }
146+ });
133147 }
134148 else
135149 {
@@ -142,65 +156,83 @@ void command_init(const Strings &args)
142156 boost::system::error_code ec;
143157 auto root = fs::current_path ();
144158
145- Config c;
146- c.allow_relative_project_names = true ;
147- // c.allow_local_dependencies = true;
159+ static const auto err_exist = " File or dir with such name already exist" ;
160+ static const auto int_main = " int main(int argc, char **argv)\n {\n return 0;\n }\n " s;
148161
149- yaml orig;
150- if (fs::is_regular_file (CPPAN_FILENAME))
162+ if (script)
151163 {
152- orig = load_yaml_config (path (CPPAN_FILENAME));
153- c.load (orig);
154- }
164+ auto n = p.name + " .cpp" ;
165+ if (fs::exists (root / n))
166+ throw std::runtime_error (err_exist);
167+ write_file (root / n, " /*\n " + dump_yaml_config (p.save ()) + " */\n\n " +
168+ int_main);
155169
156- // checks first
157- auto &projects = c.getProjects ();
158- if (projects.find (p.name ) != projects.end ())
159- throw std::runtime_error (" Project " + p.name + " already exists in the config" );
160-
161- if (fs::exists (root / p.name ) ||
162- fs::exists (root / p.name / " src" ) ||
163- fs::exists (root / p.name / " include" ) ||
164- fs::exists (root / p.name / " include" / p.name ) ||
165- fs::exists (root / p.name / " include" / p.name / (p.name + " .h" )) ||
166- fs::exists (root / p.name / " src" / (p.name + " .cpp" )) ||
167- 0 )
168- throw std::runtime_error (" File or dir with such name already exist" );
169-
170- // TODO: add deps includes from hints
171- // into header? cpp? <- cpp! to hide from users
172-
173- // create, no checks
174- fs::create_directories (root / p.name / " src" );
175- if (p.type == ProjectType::Library)
176- {
177- fs::create_directories (root / p.name / " include" / p.name );
178- write_file (root / p.name / " src" / (p.name + " .cpp" ), " #include <" + p.name + " /" + p.name + " .h>\n\n " );
179- write_file (root / p.name / " include" / p.name / (p.name + " .h" ), " //#include <something>\n\n " );
170+ if (y_n_branch (" Build project?" ))
171+ build (root / n);
180172 }
181173 else
182174 {
183- write_file (root / p. name / " src " / (p. name + " .cpp " ), " //#include <something> \n\n "
184- " int main(int argc, char **argv) \n { \n return 0; \n } \n " ) ;
185- }
175+ Config c;
176+ c. allow_relative_project_names = true ;
177+ // c.allow_local_dependencies = true;
186178
187- p.root_directory = p.name ;
179+ yaml orig;
180+ if (fs::is_regular_file (CPPAN_FILENAME))
181+ {
182+ orig = load_yaml_config (path (CPPAN_FILENAME));
183+ c.load (orig);
184+ }
188185
189- yaml y;
190- if (!fs::exists (CPPAN_FILENAME))
191- {
192- y = p.save ();
193- }
194- else
195- {
196- projects[p.name ] = p;
197- y = c.save ();
198- orig[" projects" ] = y[" projects" ];
199- y = orig;
200- }
201- dump_yaml_config (CPPAN_FILENAME, y);
186+ // checks first
187+ auto &projects = c.getProjects ();
188+ if (projects.find (p.name ) != projects.end ())
189+ throw std::runtime_error (" Project " + p.name + " already exists in the config" );
190+
191+ if (fs::exists (root / p.name ) ||
192+ fs::exists (root / p.name / " src" ) ||
193+ fs::exists (root / p.name / " include" ) ||
194+ fs::exists (root / p.name / " include" / p.name ) ||
195+ fs::exists (root / p.name / " include" / p.name / (p.name + " .h" )) ||
196+ fs::exists (root / p.name / " src" / (p.name + " .cpp" )) ||
197+ 0 )
198+ throw std::runtime_error (err_exist);
199+
200+ // TODO: add deps includes from hints
201+ // into header? cpp? <- cpp! to hide from users
202202
203- build ();
203+ // create, no checks
204+ fs::create_directories (root / p.name / " src" );
205+ if (p.type == ProjectType::Library)
206+ {
207+ fs::create_directories (root / p.name / " include" / p.name );
208+ write_file (root / p.name / " src" / (p.name + " .cpp" ), " #include <" + p.name + " /" + p.name + " .h>\n\n " );
209+ write_file (root / p.name / " include" / p.name / (p.name + " .h" ), " //#include <something>\n\n " );
210+ }
211+ else
212+ {
213+ write_file (root / p.name / " src" / (p.name + " .cpp" ), " //#include <something>\n\n "
214+ + int_main);
215+ }
216+
217+ p.root_directory = p.name ;
218+
219+ yaml y;
220+ if (!fs::exists (CPPAN_FILENAME))
221+ {
222+ y = p.save ();
223+ }
224+ else
225+ {
226+ projects[p.name ] = p;
227+ y = c.save ();
228+ orig[" projects" ] = y[" projects" ];
229+ y = orig;
230+ }
231+ dump_yaml_config (CPPAN_FILENAME, y);
232+
233+ if (y_n_branch (" Build project?" ))
234+ build ();
235+ }
204236}
205237
206238void completion_callback (const char *in, Strings &completions)
0 commit comments