Modify the drogon_ctl command

This commit is contained in:
antao 2019-03-09 10:31:58 +08:00
parent 8575951052
commit d63423545b
7 changed files with 92 additions and 23 deletions

View File

@ -18,3 +18,4 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR})
add_executable(drogon_ctl ${SRC_DIR} ${TEMPL_SRC})
add_dependencies(drogon_ctl trantor makeVersion _drogon_ctl)
install(TARGETS drogon_ctl DESTINATION bin)
install(PROGRAMS $<TARGET_FILE_DIR:drogon_ctl>/drogon_ctl DESTINATION bin RENAME dg_ctl)

View File

@ -20,7 +20,7 @@
using namespace drogon_ctl;
std::string create::detail()
{
return "Use create command to create some source files of drogon webapp\n"
return "Use create command to create some source files of drogon webapp\n\n"
"Usage:drogon_ctl create <view|controller|filter|project|model> [-options] <object name>\n\n"
"drogon_ctl create view <csp file name> //create HttpView source files from csp file\n\n"
"drogon_ctl create controller [-s] <[namespace::]class_name> //"
@ -29,7 +29,7 @@ std::string create::detail()
"create HttpController source files\n\n"
"drogon_ctl create controller -w <[namespace::]class_name> //"
"create WebSocketController source files\n\n"
"drogon_ctl create filter <class_name> //"
"drogon_ctl create filter <[namespace::]class_name> //"
"create a filter named class_name\n\n"
"drogon_ctl create project <project_name> //"
"create a project named project_name\n\n"

View File

@ -274,6 +274,7 @@ void create_controller::createController(const std::string &className, Controlle
{
std::cout << "The file you want to create already exists, overwrite it(y/n)?" << std::endl;
auto in = getchar();
(void)getchar(); //get the return key
if (in != 'Y' && in != 'y')
{
std::cout << "Abort!" << std::endl;

View File

@ -13,32 +13,54 @@
*/
#include "create_filter.h"
#include <drogon/DrTemplateBase.h>
#include <drogon/utils/Utilities.h>
#include <string>
#include <iostream>
#include <unistd.h>
#include <fstream>
#include <regex>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fstream>
#include <drogon/DrTemplateBase.h>
using namespace drogon_ctl;
static void createFilterHeaderFile(const std::string &className)
static void createFilterHeaderFile(std::ofstream &file, const std::string &className, const std::string &fileName)
{
std::ofstream file(className + ".h", std::ofstream::out);
auto templ = drogon::DrTemplateBase::newTemplate("filter_h");
HttpViewData data;
data.insert("className", className);
if (className.find("::") != std::string::npos)
{
auto namespaceVector = splitString(className, "::");
data.insert("className", namespaceVector.back());
namespaceVector.pop_back();
data.insert("namespaceVector", namespaceVector);
}
else
{
data.insert("className", className);
}
data.insert("filename", fileName);
file << templ->genText(data);
}
static void createFilterSourceFile(const std::string &className)
static void createFilterSourceFile(std::ofstream &file, const std::string &className, const std::string &fileName)
{
std::ofstream file(className + ".cc", std::ofstream::out);
auto templ = drogon::DrTemplateBase::newTemplate("filter_cc");
HttpViewData data;
data.insert("className", className);
if (className.find("::") != std::string::npos)
{
auto pos = className.rfind("::");
data.insert("namespaceString", className.substr(0, pos));
data.insert("className", className.substr(pos + 2));
}
else
{
data.insert("className", className);
}
data.insert("filename", fileName);
file << templ->genText(data);
}
void create_filter::handleCommand(std::vector<std::string> &parameters)
@ -47,7 +69,39 @@ void create_filter::handleCommand(std::vector<std::string> &parameters)
{
std::cout << "Invalid parameters!" << std::endl;
}
auto className = parameters[0];
createFilterHeaderFile(className);
createFilterSourceFile(className);
for (auto className : parameters)
{
std::regex regex("::");
std::string fileName = std::regex_replace(className, regex, std::string("_"));
std::string headFileName = fileName + ".h";
std::string sourceFilename = fileName + ".cc";
{
std::ifstream iHeadFile(headFileName.c_str(), std::ifstream::in);
std::ifstream iSourceFile(sourceFilename.c_str(), std::ifstream::in);
if (iHeadFile || iSourceFile)
{
std::cout << "The file you want to create already exists, overwrite it(y/n)?" << std::endl;
auto in = getchar();
(void)getchar(); //get the return key
if (in != 'Y' && in != 'y')
{
std::cout << "Abort!" << std::endl;
exit(0);
}
}
}
std::ofstream oHeadFile(headFileName.c_str(), std::ofstream::out);
std::ofstream oSourceFile(sourceFilename.c_str(), std::ofstream::out);
if (!oHeadFile || !oSourceFile)
{
perror("");
exit(-1);
}
std::cout << "create a http fitler:" << className << std::endl;
createFilterHeaderFile(oHeadFile, className, fileName);
createFilterSourceFile(oSourceFile, className, fileName);
}
}

View File

@ -27,7 +27,5 @@ class create_filter : public DrObject<create_filter>, public CommandHandler
protected:
std::string _outputPath = ".";
void createViewFiles(std::vector<std::string> &cspFileNames);
int createViewFile(const std::string &script_filename);
};
} // namespace drogon_ctl

View File

@ -1,14 +1,17 @@
/**
*
* {{className}}.cc
*
* drogon_ctl
* {{filename}}.cc
*
*/
#include "{{className}}.h"
#include "{{filename}}.h"
using namespace drogon;
<%c++auto namespaceStr=@@.get<std::string>("namespaceString");
if(!namespaceStr.empty())
$$<<"using namespace "<<namespaceStr<<";\n";
%>
void {{className}}::doFilter(const HttpRequestPtr &req,
const FilterCallback &fcb,
const FilterChainCallback &fccb)

View File

@ -1,7 +1,6 @@
/**
*
* {{className}}.h
* drogon_ctl
* {{filename}}.h
*
*/
@ -9,7 +8,16 @@
#include <drogon/HttpFilter.h>
using namespace drogon;
<%c++
auto namespaceVector=@@.get<std::vector<std::string>>("namespaceVector");
if(namespaceVector.empty())
$$<<"\n";
for(auto &namespaceName:namespaceVector){%>
namespace {%namespaceName%}
{
<%c++}
$$<<"\n";%>
class {{className}} : public HttpFilter<{{className}}>
{
public:
@ -17,4 +25,8 @@ class {{className}} : public HttpFilter<{{className}}>
virtual void doFilter(const HttpRequestPtr &req,
const FilterCallback &fcb,
const FilterChainCallback &fccb) override;
};
};
<%c++for(size_t i=0;i<namespaceVector.size();i++){%>
}
<%c++}%>