metaforce/hecl/driver/main.cpp

228 lines
6.7 KiB
C++
Raw Normal View History

2015-05-15 22:39:43 +00:00
#include <stdio.h>
#include <string.h>
2015-05-26 04:42:20 +00:00
#include <stdlib.h>
2015-05-27 09:09:05 +00:00
#include <sys/param.h>
2015-05-20 05:22:32 +00:00
#include <regex>
#include <stdexcept>
2015-05-26 04:42:20 +00:00
#include <list>
2015-05-17 04:55:29 +00:00
#include <HECLDatabase.hpp>
2015-06-10 02:40:03 +00:00
#include "ToolBase.hpp"
#include "ToolInit.hpp"
#include "ToolSpec.hpp"
2015-06-12 09:08:49 +00:00
#include "ToolExtract.hpp"
2015-06-10 02:40:03 +00:00
#include "ToolAdd.hpp"
#include "ToolRemove.hpp"
#include "ToolGroup.hpp"
#include "ToolCook.hpp"
#include "ToolClean.hpp"
#include "ToolPackage.hpp"
#include "ToolHelp.hpp"
2015-05-15 22:39:43 +00:00
2015-05-26 04:42:20 +00:00
bool XTERM_COLOR = false;
2015-06-11 09:41:10 +00:00
/*
2015-06-10 02:40:03 +00:00
#define HECL_GIT 1234567
#define HECL_GIT_S "1234567"
#define HECL_BRANCH master
#define HECL_BRANCH_S "master"
2015-06-11 09:41:10 +00:00
*/
2015-06-10 02:40:03 +00:00
2015-05-20 05:22:32 +00:00
/* Main usage message */
2015-06-10 02:40:03 +00:00
static void printHelp(const HECL::SystemChar* pname)
2015-05-15 22:39:43 +00:00
{
2015-05-26 04:42:20 +00:00
if (XTERM_COLOR)
2015-06-10 02:40:03 +00:00
HECL::Printf(_S("" BOLD "HECL" NORMAL ""));
2015-05-26 04:42:20 +00:00
else
2015-06-10 02:40:03 +00:00
HECL::Printf(_S("HECL"));
2015-05-20 05:22:32 +00:00
#if HECL_GIT
2015-06-10 02:40:03 +00:00
HECL::Printf(_S(" Commit " HECL_GIT_S " " HECL_BRANCH_S "\nUsage: %s init|add|remove|group|cook|clean|package|help\n"), pname);
2015-05-20 05:22:32 +00:00
#elif HECL_VER
2015-06-10 02:40:03 +00:00
HECL::Printf(_S(" Version " HECL_VER_S "\nUsage: %s init|add|remove|group|cook|clean|package|help\n"), pname);
2015-05-20 05:22:32 +00:00
#else
2015-06-10 02:40:03 +00:00
HECL::Printf(_S("\nUsage: %s init|add|remove|group|cook|clean|package|help\n"), pname);
2015-05-20 05:22:32 +00:00
#endif
}
/* Regex patterns */
2015-06-10 02:40:03 +00:00
static const HECL::SystemRegex regOPEN(_S("-o([^\"]*|\\S*))"), std::regex::ECMAScript|std::regex::optimize);
static const HECL::SystemRegex regVERBOSE(_S("-v(v*)"), std::regex::ECMAScript|std::regex::optimize);
static const HECL::SystemRegex regFORCE(_S("-f"), std::regex::ECMAScript|std::regex::optimize);
2015-05-24 04:51:16 +00:00
#include "../blender/CBlenderConnection.hpp"
2015-06-10 02:40:03 +00:00
#if HECL_UCS2
int wmain(int argc, const wchar_t** argv)
#else
int main(int argc, const char** argv)
2015-06-10 02:40:03 +00:00
#endif
{
2015-05-26 04:42:20 +00:00
/* Xterm check */
const char* term = getenv("TERM");
if (!strncmp(term, "xterm", 5))
XTERM_COLOR = true;
//CBlenderConnection bconn(false);
//return 0;
2015-05-24 04:51:16 +00:00
2015-05-20 05:22:32 +00:00
/* Basic usage check */
if (argc == 1)
{
printHelp(argv[0]);
return 0;
}
else if (argc == 0)
{
2015-06-10 02:40:03 +00:00
printHelp(_S("hecl"));
return 0;
}
2015-05-20 05:22:32 +00:00
/* Assemble common tool pass info */
2015-06-10 02:40:03 +00:00
ToolPassInfo info;
2015-05-20 05:22:32 +00:00
info.pname = argv[0];
2015-06-10 02:40:03 +00:00
HECL::SystemChar cwdbuf[MAXPATHLEN];
if (HECL::Getcwd(cwdbuf, MAXPATHLEN))
2015-05-27 09:09:05 +00:00
info.cwd = cwdbuf;
2015-05-20 05:22:32 +00:00
/* Concatenate args */
2015-06-10 02:40:03 +00:00
std::list<HECL::SystemString> args;
2015-05-20 05:22:32 +00:00
for (int i=2 ; i<argc ; ++i)
2015-06-10 02:40:03 +00:00
args.push_back(HECL::SystemString(argv[i]));
2015-05-20 05:22:32 +00:00
if (!args.empty())
{
/* Extract output argument */
2015-06-10 02:40:03 +00:00
for (std::list<HECL::SystemString>::const_iterator it = args.begin() ; it != args.end() ;)
2015-05-20 05:22:32 +00:00
{
2015-06-10 02:40:03 +00:00
const HECL::SystemString& arg = *it;
HECL::SystemRegexMatch oMatch;
2015-05-26 04:42:20 +00:00
if (std::regex_search(arg, oMatch, regOPEN))
{
2015-06-10 02:40:03 +00:00
const HECL::SystemString& token = oMatch[1].str();
2015-05-26 04:42:20 +00:00
if (token.size())
{
if (info.output.empty())
info.output = oMatch[1].str();
it = args.erase(it);
}
else
{
it = args.erase(it);
if (it == args.end())
break;
if (info.output.empty())
info.output = *it;
it = args.erase(it);
}
continue;
}
++it;
2015-05-20 05:22:32 +00:00
}
/* Count verbosity */
2015-06-10 02:40:03 +00:00
for (std::list<HECL::SystemString>::const_iterator it = args.begin() ; it != args.end() ;)
2015-05-20 05:22:32 +00:00
{
2015-06-10 02:40:03 +00:00
const HECL::SystemString& arg = *it;
HECL::SystemRegexMatch vMatch;
2015-05-26 04:42:20 +00:00
if (std::regex_search(arg, vMatch, regVERBOSE))
{
++info.verbosityLevel;
info.verbosityLevel += vMatch[1].str().size();
it = args.erase(it);
continue;
}
++it;
2015-05-20 05:22:32 +00:00
}
/* Check force argument */
2015-06-10 02:40:03 +00:00
for (std::list<HECL::SystemString>::const_iterator it = args.begin() ; it != args.end() ;)
2015-05-20 05:22:32 +00:00
{
2015-06-10 02:40:03 +00:00
const HECL::SystemString& arg = *it;
2015-05-26 04:42:20 +00:00
if (std::regex_search(arg, regFORCE))
{
info.force = true;
it = args.erase(it);
continue;
}
++it;
2015-05-20 05:22:32 +00:00
}
/* Gather remaining args */
2015-06-10 02:40:03 +00:00
for (const HECL::SystemString& arg : args)
2015-05-26 04:42:20 +00:00
info.args.push_back(arg);
2015-05-20 05:22:32 +00:00
}
2015-06-11 04:55:06 +00:00
/* Attempt to find hecl project */
2015-06-12 04:02:23 +00:00
std::unique_ptr<HECL::ProjectRootPath> rootPath = HECL::SearchForProject(info.cwd);
2015-06-11 04:55:06 +00:00
std::unique_ptr<HECL::Database::Project> project;
2015-06-11 21:04:15 +00:00
if (rootPath.get())
2015-06-11 04:55:06 +00:00
{
try
{
project.reset(new HECL::Database::Project(*rootPath));
info.project = project.get();
}
catch (HECL::Exception& ex)
{
HECL::FPrintf(stderr,
_S("Unable to open discovered project at '%s':\n%s\n"),
rootPath->getAbsolutePath().c_str(), ex.swhat());
return -1;
}
}
2015-05-20 05:22:32 +00:00
/* Construct selected tool */
2015-06-10 02:40:03 +00:00
HECL::SystemString toolName(argv[1]);
2015-06-11 04:55:06 +00:00
HECL::ToLower(toolName);
std::unique_ptr<ToolBase> tool;
2015-05-20 05:22:32 +00:00
try
{
2015-06-10 02:40:03 +00:00
if (toolName == _S("init"))
2015-06-11 04:55:06 +00:00
tool.reset(new ToolInit(info));
2015-06-10 02:40:03 +00:00
else if (toolName == _S("spec"))
2015-06-11 04:55:06 +00:00
tool.reset(new ToolSpec(info));
2015-06-12 09:08:49 +00:00
else if (toolName == _S("extract"))
tool.reset(new ToolExtract(info));
2015-06-10 02:40:03 +00:00
else if (toolName == _S("add"))
2015-06-11 04:55:06 +00:00
tool.reset(new ToolAdd(info));
2015-06-10 02:40:03 +00:00
else if (toolName == _S("remove") || toolName == _S("rm"))
2015-06-11 04:55:06 +00:00
tool.reset(new ToolRemove(info));
2015-06-10 02:40:03 +00:00
else if (toolName == _S("group"))
2015-06-11 04:55:06 +00:00
tool.reset(new ToolGroup(info));
2015-06-10 02:40:03 +00:00
else if (toolName == _S("cook"))
2015-06-11 04:55:06 +00:00
tool.reset(new ToolCook(info));
2015-06-10 02:40:03 +00:00
else if (toolName == _S("clean"))
2015-06-11 04:55:06 +00:00
tool.reset(new ToolClean(info));
2015-06-10 02:40:03 +00:00
else if (toolName == _S("package") || toolName == _S("pack"))
2015-06-11 04:55:06 +00:00
tool.reset(new ToolPackage(info));
2015-06-10 02:40:03 +00:00
else if (toolName == _S("help"))
2015-06-11 04:55:06 +00:00
tool.reset(new ToolHelp(info));
2015-05-20 05:22:32 +00:00
else
2015-06-10 02:40:03 +00:00
throw HECL::Exception(_S("unrecognized tool '") + toolName + _S("'"));
2015-05-20 05:22:32 +00:00
}
2015-06-10 02:40:03 +00:00
catch (HECL::Exception& ex)
2015-05-20 05:22:32 +00:00
{
2015-06-10 02:40:03 +00:00
HECL::FPrintf(stderr,
_S("Unable to construct HECL tool '%s':\n%s\n"),
toolName.c_str(), ex.swhat());
2015-05-20 05:22:32 +00:00
return -1;
}
if (info.verbosityLevel)
2015-06-10 02:40:03 +00:00
HECL::Printf(_S("Constructed tool '%s' %d\n"), tool->toolName().c_str(), info.verbosityLevel);
2015-05-20 05:22:32 +00:00
/* Run tool */
int retval;
try
{
retval = tool->run();
}
2015-06-10 02:40:03 +00:00
catch (HECL::Exception& ex)
2015-05-20 05:22:32 +00:00
{
2015-06-10 02:40:03 +00:00
HECL::FPrintf(stderr, _S("Error running HECL tool '%s':\n%s\n"), toolName.c_str(), ex.swhat());
2015-05-20 05:22:32 +00:00
return -1;
}
2015-05-20 05:22:32 +00:00
return retval;
2015-05-15 22:39:43 +00:00
}