metaforce/hecl/driver/ToolInit.hpp

78 lines
2.2 KiB
C++
Raw Normal View History

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 {
const std::string* m_dir = nullptr;
2015-06-10 02:40:03 +00:00
2018-12-08 05:18:42 +00:00
public:
explicit ToolInit(const ToolPassInfo& info) : ToolBase(info) {
2018-12-08 05:18:42 +00:00
hecl::Sstat theStat;
const std::string* dir;
2018-12-08 05:18:42 +00:00
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)) {
LogModule.report(logvisor::Fatal, FMT_STRING("unable to stat '{}'"), *dir);
2018-12-08 05:18:42 +00:00
return;
}
}
if (!S_ISDIR(theStat.st_mode)) {
LogModule.report(logvisor::Fatal, FMT_STRING("'{}' is not a directory"), *dir);
2018-12-08 05:18:42 +00:00
return;
2015-06-10 02:40:03 +00:00
}
std::string testPath = *dir + "/.hecl/beacon";
2018-12-08 05:18:42 +00:00
if (!hecl::Stat(testPath.c_str(), &theStat)) {
LogModule.report(logvisor::Fatal, FMT_STRING("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
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;
LogModule.report(logvisor::Info, FMT_STRING("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("NAME");
2018-12-08 05:18:42 +00:00
help.beginWrap();
help.wrap("hecl-init - Initialize a brand-new project database\n");
2018-12-08 05:18:42 +00:00
help.endWrap();
help.secHead("SYNOPSIS");
2018-12-08 05:18:42 +00:00
help.beginWrap();
help.wrap("hecl init [<dir>]\n");
2018-12-08 05:18:42 +00:00
help.endWrap();
help.secHead("DESCRIPTION");
2018-12-08 05:18:42 +00:00
help.beginWrap();
help.wrap("Creates a ");
help.wrapBold(".hecl");
help.wrap(" directory within the selected directory with an initialized database index. "
"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
help.secHead("OPTIONS");
help.optionHead("<dir>", "group directory path");
2018-12-08 05:18:42 +00:00
help.beginWrap();
help.wrap("Directory to create new project database in. If not specified, current directory is used.\n");
2018-12-08 05:18:42 +00:00
help.endWrap();
}
2015-06-10 02:40:03 +00:00
std::string_view toolName() const override { return "init"sv; }
2015-06-10 02:40:03 +00:00
};