From d63423545b03c3ff8b6a2722ccdc7c7860ffb3e6 Mon Sep 17 00:00:00 2001 From: antao Date: Sat, 9 Mar 2019 10:31:58 +0800 Subject: [PATCH] Modify the drogon_ctl command --- drogon_ctl/CMakeLists.txt | 1 + drogon_ctl/create.cc | 4 +- drogon_ctl/create_controller.cc | 1 + drogon_ctl/create_filter.cc | 78 +++++++++++++++++++++++++----- drogon_ctl/create_filter.h | 2 - drogon_ctl/templates/filter_cc.csp | 11 +++-- drogon_ctl/templates/filter_h.csp | 18 +++++-- 7 files changed, 92 insertions(+), 23 deletions(-) diff --git a/drogon_ctl/CMakeLists.txt b/drogon_ctl/CMakeLists.txt index 566d3db4..83d2dee8 100755 --- a/drogon_ctl/CMakeLists.txt +++ b/drogon_ctl/CMakeLists.txt @@ -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 $/drogon_ctl DESTINATION bin RENAME dg_ctl) diff --git a/drogon_ctl/create.cc b/drogon_ctl/create.cc index 1cd621fe..e601efc0 100755 --- a/drogon_ctl/create.cc +++ b/drogon_ctl/create.cc @@ -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 [-options] \n\n" "drogon_ctl create view //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 //" + "drogon_ctl create filter <[namespace::]class_name> //" "create a filter named class_name\n\n" "drogon_ctl create project //" "create a project named project_name\n\n" diff --git a/drogon_ctl/create_controller.cc b/drogon_ctl/create_controller.cc index b2727ee3..82234462 100755 --- a/drogon_ctl/create_controller.cc +++ b/drogon_ctl/create_controller.cc @@ -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; diff --git a/drogon_ctl/create_filter.cc b/drogon_ctl/create_filter.cc index 4dbd0204..c14f9055 100644 --- a/drogon_ctl/create_filter.cc +++ b/drogon_ctl/create_filter.cc @@ -13,32 +13,54 @@ */ #include "create_filter.h" +#include +#include + #include #include +#include +#include +#include #include #include -#include -#include -#include 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 ¶meters) @@ -47,7 +69,39 @@ void create_filter::handleCommand(std::vector ¶meters) { 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); + } } diff --git a/drogon_ctl/create_filter.h b/drogon_ctl/create_filter.h index 34449a9a..f1998be9 100644 --- a/drogon_ctl/create_filter.h +++ b/drogon_ctl/create_filter.h @@ -27,7 +27,5 @@ class create_filter : public DrObject, public CommandHandler protected: std::string _outputPath = "."; - void createViewFiles(std::vector &cspFileNames); - int createViewFile(const std::string &script_filename); }; } // namespace drogon_ctl diff --git a/drogon_ctl/templates/filter_cc.csp b/drogon_ctl/templates/filter_cc.csp index a0863a00..d32eae60 100644 --- a/drogon_ctl/templates/filter_cc.csp +++ b/drogon_ctl/templates/filter_cc.csp @@ -1,14 +1,17 @@ /** * - * {{className}}.cc - * - * drogon_ctl + * {{filename}}.cc * */ -#include "{{className}}.h" +#include "{{filename}}.h" using namespace drogon; +<%c++auto namespaceStr=@@.get("namespaceString"); +if(!namespaceStr.empty()) +$$<<"using namespace "< + void {{className}}::doFilter(const HttpRequestPtr &req, const FilterCallback &fcb, const FilterChainCallback &fccb) diff --git a/drogon_ctl/templates/filter_h.csp b/drogon_ctl/templates/filter_h.csp index c1883fac..08ae4c94 100644 --- a/drogon_ctl/templates/filter_h.csp +++ b/drogon_ctl/templates/filter_h.csp @@ -1,7 +1,6 @@ /** * - * {{className}}.h - * drogon_ctl + * {{filename}}.h * */ @@ -9,7 +8,16 @@ #include using namespace drogon; +<%c++ +auto namespaceVector=@@.get>("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; -}; \ No newline at end of file +}; + +<%c++for(size_t i=0;i +} +<%c++}%> \ No newline at end of file