commit
d4710d3da7
|
@ -18,3 +18,4 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR})
|
||||||
add_executable(drogon_ctl ${SRC_DIR} ${TEMPL_SRC})
|
add_executable(drogon_ctl ${SRC_DIR} ${TEMPL_SRC})
|
||||||
add_dependencies(drogon_ctl trantor makeVersion _drogon_ctl)
|
add_dependencies(drogon_ctl trantor makeVersion _drogon_ctl)
|
||||||
install(TARGETS drogon_ctl DESTINATION bin)
|
install(TARGETS drogon_ctl DESTINATION bin)
|
||||||
|
install(PROGRAMS $<TARGET_FILE_DIR:drogon_ctl>/drogon_ctl DESTINATION bin RENAME dg_ctl)
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
using namespace drogon_ctl;
|
using namespace drogon_ctl;
|
||||||
std::string create::detail()
|
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"
|
"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 view <csp file name> //create HttpView source files from csp file\n\n"
|
||||||
"drogon_ctl create controller [-s] <[namespace::]class_name> //"
|
"drogon_ctl create controller [-s] <[namespace::]class_name> //"
|
||||||
|
@ -29,7 +29,7 @@ std::string create::detail()
|
||||||
"create HttpController source files\n\n"
|
"create HttpController source files\n\n"
|
||||||
"drogon_ctl create controller -w <[namespace::]class_name> //"
|
"drogon_ctl create controller -w <[namespace::]class_name> //"
|
||||||
"create WebSocketController source files\n\n"
|
"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"
|
"create a filter named class_name\n\n"
|
||||||
"drogon_ctl create project <project_name> //"
|
"drogon_ctl create project <project_name> //"
|
||||||
"create a project named project_name\n\n"
|
"create a project named project_name\n\n"
|
||||||
|
|
|
@ -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;
|
std::cout << "The file you want to create already exists, overwrite it(y/n)?" << std::endl;
|
||||||
auto in = getchar();
|
auto in = getchar();
|
||||||
|
(void)getchar(); //get the return key
|
||||||
if (in != 'Y' && in != 'y')
|
if (in != 'Y' && in != 'y')
|
||||||
{
|
{
|
||||||
std::cout << "Abort!" << std::endl;
|
std::cout << "Abort!" << std::endl;
|
||||||
|
|
|
@ -13,32 +13,54 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "create_filter.h"
|
#include "create_filter.h"
|
||||||
|
#include <drogon/DrTemplateBase.h>
|
||||||
|
#include <drogon/utils/Utilities.h>
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <fstream>
|
||||||
|
#include <regex>
|
||||||
|
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
|
||||||
#include <fstream>
|
|
||||||
|
|
||||||
#include <drogon/DrTemplateBase.h>
|
|
||||||
using namespace drogon_ctl;
|
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");
|
auto templ = drogon::DrTemplateBase::newTemplate("filter_h");
|
||||||
HttpViewData data;
|
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);
|
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");
|
auto templ = drogon::DrTemplateBase::newTemplate("filter_cc");
|
||||||
HttpViewData data;
|
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);
|
file << templ->genText(data);
|
||||||
}
|
}
|
||||||
void create_filter::handleCommand(std::vector<std::string> ¶meters)
|
void create_filter::handleCommand(std::vector<std::string> ¶meters)
|
||||||
|
@ -47,7 +69,39 @@ void create_filter::handleCommand(std::vector<std::string> ¶meters)
|
||||||
{
|
{
|
||||||
std::cout << "Invalid parameters!" << std::endl;
|
std::cout << "Invalid parameters!" << std::endl;
|
||||||
}
|
}
|
||||||
auto className = parameters[0];
|
for (auto className : parameters)
|
||||||
createFilterHeaderFile(className);
|
{
|
||||||
createFilterSourceFile(className);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,5 @@ class create_filter : public DrObject<create_filter>, public CommandHandler
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::string _outputPath = ".";
|
std::string _outputPath = ".";
|
||||||
void createViewFiles(std::vector<std::string> &cspFileNames);
|
|
||||||
int createViewFile(const std::string &script_filename);
|
|
||||||
};
|
};
|
||||||
} // namespace drogon_ctl
|
} // namespace drogon_ctl
|
||||||
|
|
|
@ -1,14 +1,17 @@
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* {{className}}.cc
|
* {{filename}}.cc
|
||||||
*
|
|
||||||
* drogon_ctl
|
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "{{className}}.h"
|
#include "{{filename}}.h"
|
||||||
|
|
||||||
using namespace drogon;
|
using namespace drogon;
|
||||||
|
<%c++auto namespaceStr=@@.get<std::string>("namespaceString");
|
||||||
|
if(!namespaceStr.empty())
|
||||||
|
$$<<"using namespace "<<namespaceStr<<";\n";
|
||||||
|
%>
|
||||||
|
|
||||||
void {{className}}::doFilter(const HttpRequestPtr &req,
|
void {{className}}::doFilter(const HttpRequestPtr &req,
|
||||||
const FilterCallback &fcb,
|
const FilterCallback &fcb,
|
||||||
const FilterChainCallback &fccb)
|
const FilterChainCallback &fccb)
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* {{className}}.h
|
* {{filename}}.h
|
||||||
* drogon_ctl
|
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -9,7 +8,16 @@
|
||||||
|
|
||||||
#include <drogon/HttpFilter.h>
|
#include <drogon/HttpFilter.h>
|
||||||
using namespace drogon;
|
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}}>
|
class {{className}} : public HttpFilter<{{className}}>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -17,4 +25,8 @@ class {{className}} : public HttpFilter<{{className}}>
|
||||||
virtual void doFilter(const HttpRequestPtr &req,
|
virtual void doFilter(const HttpRequestPtr &req,
|
||||||
const FilterCallback &fcb,
|
const FilterCallback &fcb,
|
||||||
const FilterChainCallback &fccb) override;
|
const FilterChainCallback &fccb) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
<%c++for(size_t i=0;i<namespaceVector.size();i++){%>
|
||||||
|
}
|
||||||
|
<%c++}%>
|
Loading…
Reference in New Issue