2018-10-07 03:38:44 +00:00
|
|
|
#pragma once
|
2015-06-10 02:40:03 +00:00
|
|
|
|
|
|
|
#include "ToolBase.hpp"
|
2017-12-29 07:56:31 +00:00
|
|
|
#include <cstdio>
|
2015-06-10 02:40:03 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
class ToolInit final : public ToolBase {
|
2019-08-20 02:52:33 +00:00
|
|
|
const hecl::SystemString* m_dir = nullptr;
|
2015-06-10 02:40:03 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
public:
|
2019-08-20 02:49:24 +00:00
|
|
|
explicit ToolInit(const ToolPassInfo& info) : ToolBase(info) {
|
2018-12-08 05:18:42 +00:00
|
|
|
hecl::Sstat theStat;
|
|
|
|
const hecl::SystemString* dir;
|
|
|
|
if (info.args.size())
|
|
|
|
dir = &info.args.front();
|
|
|
|
else
|
|
|
|
dir = &info.cwd;
|
2015-06-10 02:40:03 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
if (hecl::Stat(dir->c_str(), &theStat)) {
|
|
|
|
hecl::MakeDir(dir->c_str());
|
|
|
|
if (hecl::Stat(dir->c_str(), &theStat)) {
|
2019-07-20 04:22:58 +00:00
|
|
|
LogModule.report(logvisor::Fatal, fmt(_SYS_STR("unable to stat '{}'")), *dir);
|
2018-12-08 05:18:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!S_ISDIR(theStat.st_mode)) {
|
2019-07-20 04:22:58 +00:00
|
|
|
LogModule.report(logvisor::Fatal, fmt(_SYS_STR("'{}' is not a directory")), *dir);
|
2018-12-08 05:18:42 +00:00
|
|
|
return;
|
2015-06-10 02:40:03 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
hecl::SystemString testPath = *dir + _SYS_STR("/.hecl/beacon");
|
|
|
|
if (!hecl::Stat(testPath.c_str(), &theStat)) {
|
2019-07-20 04:22:58 +00:00
|
|
|
LogModule.report(logvisor::Fatal, fmt(_SYS_STR("project already exists at '{}'")), *dir);
|
2018-12-08 05:18:42 +00:00
|
|
|
return;
|
2015-06-10 02:40:03 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
m_dir = dir;
|
|
|
|
}
|
2015-06-10 02:40:03 +00:00
|
|
|
|
2019-08-20 02:34:12 +00:00
|
|
|
int run() override {
|
2018-12-08 05:18:42 +00:00
|
|
|
if (!m_dir)
|
|
|
|
return 1;
|
|
|
|
size_t ErrorRef = logvisor::ErrorCount;
|
|
|
|
hecl::Database::Project proj((hecl::ProjectRootPath(*m_dir)));
|
|
|
|
if (logvisor::ErrorCount > ErrorRef)
|
|
|
|
return 1;
|
2019-07-20 04:22:58 +00:00
|
|
|
LogModule.report(logvisor::Info, fmt(_SYS_STR("initialized project at '{}/.hecl'")), *m_dir);
|
2018-12-08 05:18:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2015-06-10 02:40:03 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
static void Help(HelpOutput& help) {
|
|
|
|
help.secHead(_SYS_STR("NAME"));
|
|
|
|
help.beginWrap();
|
|
|
|
help.wrap(_SYS_STR("hecl-init - Initialize a brand-new project database\n"));
|
|
|
|
help.endWrap();
|
|
|
|
|
|
|
|
help.secHead(_SYS_STR("SYNOPSIS"));
|
|
|
|
help.beginWrap();
|
|
|
|
help.wrap(_SYS_STR("hecl init [<dir>]\n"));
|
|
|
|
help.endWrap();
|
|
|
|
|
|
|
|
help.secHead(_SYS_STR("DESCRIPTION"));
|
|
|
|
help.beginWrap();
|
|
|
|
help.wrap(_SYS_STR("Creates a "));
|
|
|
|
help.wrapBold(_SYS_STR(".hecl"));
|
|
|
|
help.wrap(_SYS_STR(" directory within the selected directory with an initialized database index. ")
|
2018-10-14 20:09:15 +00:00
|
|
|
_SYS_STR("This constitutes an empty HECL project, ready for making stuff!!\n"));
|
2018-12-08 05:18:42 +00:00
|
|
|
help.endWrap();
|
2015-06-10 02:40:03 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
help.secHead(_SYS_STR("OPTIONS"));
|
|
|
|
help.optionHead(_SYS_STR("<dir>"), _SYS_STR("group directory path"));
|
|
|
|
help.beginWrap();
|
|
|
|
help.wrap(_SYS_STR("Directory to create new project database in. If not specified, current directory is used.\n"));
|
|
|
|
help.endWrap();
|
|
|
|
}
|
2015-06-10 02:40:03 +00:00
|
|
|
|
2019-08-20 02:34:12 +00:00
|
|
|
hecl::SystemString toolName() const override { return _SYS_STR("init"); }
|
2015-06-10 02:40:03 +00:00
|
|
|
};
|