Skip to content

Commit 5918333

Browse files
committed
Implement script initialization.
1 parent 783b898 commit 5918333

File tree

7 files changed

+125
-81
lines changed

7 files changed

+125
-81
lines changed

src/client/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ file(GLOB cppan_src "*")
1111
add_executable(client ${cppan_src})
1212
target_link_libraries(client common
1313
pvt.cppan.demo.boost.program_options
14-
#pvt.cppan.demo.gnu.readline.readline
1514
pvt.cppan.demo.yhirose.cpp_linenoise
1615
)
1716

src/client/init.cpp

Lines changed: 95 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
#include <iostream>
2525

26+
using void_f = std::function<void(void)>;
27+
2628
const String invitation = "> ";
2729

2830
void 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+
3747
auto 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+
7295
void 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

206238
void completion_callback(const char *in, Strings &completions)

src/common/checks.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ struct CheckParameters
3131
StringSet libraries;
3232
StringSet flags;
3333

34+
// TODO: pass all found includes to this test
35+
// it is possible only in sequential mode
36+
bool all_includes = false;
37+
3438
void writeHeadersBefore(Context &ctx) const;
3539
void writeHeadersAfter(Context &ctx) const;
3640
void writeBefore(Context &ctx) const;

src/common/filesystem.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,13 @@ void copy_dir(const path &src, const path &dst)
163163

164164
void remove_files_like(const Files &files, const String &regex)
165165
{
166+
boost::system::error_code ec;
166167
std::regex r(regex);
167168
for (auto &f : files)
168169
{
169170
if (!std::regex_match(f.filename().string(), r))
170171
continue;
171-
fs::remove(f);
172+
fs::remove(f, ec);
172173
}
173174
}
174175

src/common/resolver.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -229,20 +229,22 @@ void Resolver::download_and_unpack()
229229
return;
230230
}
231231

232-
// verify before any actions, so stop on error
233-
if (Settings::get_local_settings().verify_all)
234-
verify(d);
235-
236-
// remove existing version dir
237-
cleanPackages(d.target_name);
238-
239-
// dl
232+
// Do this before we clean previous package version!
233+
// This is useful when we have network issues during download,
234+
// so we won't lost existing package.
240235
LOG_INFO(logger, "Downloading: " << d.target_name << "...");
241236

242237
// maybe d.target_name instead of version_dir.string()?
243-
path fn = make_archive_name(version_dir.string());
238+
path fn = make_archive_name((temp_directory_path("dl") / d.target_name).string());
244239
download(d, fn);
245240

241+
// verify before cleaning old pkg
242+
if (Settings::get_local_settings().verify_all)
243+
verify(d, fn);
244+
245+
// remove existing version dir
246+
cleanPackages(d.target_name);
247+
246248
rd.downloads++;
247249
write_file(hash_file, d.sha256);
248250

@@ -254,6 +256,7 @@ void Resolver::download_and_unpack()
254256
}
255257
catch (...)
256258
{
259+
fs::remove(fn);
257260
fs::remove_all(version_dir);
258261
throw;
259262
}

src/common/verifier.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void verify(const String &target_name)
3535
verify(pkg);
3636
}
3737

38-
void verify(const Package &pkg)
38+
void verify(const Package &pkg, path fn)
3939
{
4040
LOG_INFO(logger, "Verifying : " << pkg.target_name << "...");
4141

@@ -57,15 +57,20 @@ void verify(const Package &pkg)
5757
// download & prepare cppan sources
5858
// we also resolve dependency here
5959
{
60-
LOG_DEBUG(logger, "Resolving : " << pkg.target_name << "...");
61-
LOG_DEBUG(logger, "Downloading: " << pkg.target_name << "...");
60+
bool rm = fn.empty();
61+
if (fn.empty())
62+
{
63+
LOG_DEBUG(logger, "Resolving : " << pkg.target_name << "...");
64+
LOG_DEBUG(logger, "Downloading: " << pkg.target_name << "...");
6265

63-
auto fn = dir_cppan / make_archive_name();
64-
resolve_and_download(pkg, fn);
66+
fn = dir_cppan / make_archive_name();
67+
resolve_and_download(pkg, fn);
68+
}
6569

6670
LOG_DEBUG(logger, "Unpacking : " << pkg.target_name << "...");
6771
unpack_file(fn, dir_cppan);
68-
fs::remove(fn);
72+
if (rm)
73+
fs::remove(fn);
6974
}
7075

7176
// only after cppan resolve step

src/common/verifier.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@
1919
#include "package.h"
2020

2121
void verify(const String &target_name);
22-
void verify(const Package &pkg);
22+
void verify(const Package &pkg, path fn = path());

0 commit comments

Comments
 (0)