drogon/drogon_ctl/create_project.cc

110 lines
3.1 KiB
C++
Raw Normal View History

/**
*
* create_project.cc
* An Tao
* @section LICENSE
*
* Copyright 2018, An Tao. All rights reserved.
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* @section DESCRIPTION
*
*/
#include "create_project.h"
#include <drogon/DrTemplateBase.h>
#include <iostream>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fstream>
using namespace drogon_ctl;
void create_project::handleCommand(std::vector<std::string> &parameters)
{
2018-10-14 15:56:54 +08:00
if (parameters.size() < 1)
{
2018-10-14 15:56:54 +08:00
std::cout << "please input project name" << std::endl;
exit(1);
}
2018-10-14 15:56:54 +08:00
auto pName = parameters[0];
createProject(pName);
}
2018-10-14 15:56:54 +08:00
static void newCmakeFile(std::ofstream &cmakeFile, const std::string &projectName)
{
2018-10-15 22:46:53 +08:00
HttpViewData data;
data.insert("ProjectName",projectName);
auto templ=DrTemplateBase::newTemplate("cmake.csp");
cmakeFile << templ->genText(data);
2018-10-14 15:56:54 +08:00
}
static void newMainFile(std::ofstream &mainFile)
{
auto templ=DrTemplateBase::newTemplate("demoMain");
mainFile << templ->genText();
2018-10-14 15:56:54 +08:00
}
static void newGitIgFile(std::ofstream &gitFile)
{
auto templ=DrTemplateBase::newTemplate("gitignore.csp");
gitFile << templ->genText();
}
static void newUuidFindFile(std::ofstream &uuidFile)
{
auto templ=DrTemplateBase::newTemplate("FindUUID.csp");
uuidFile << templ->genText();
}
2018-09-15 22:52:59 +08:00
static void newJsonFindFile(std::ofstream &jsonFile)
{
auto templ=DrTemplateBase::newTemplate("FindJsoncpp.csp");
jsonFile << templ->genText();
2018-09-15 22:52:59 +08:00
}
2018-09-25 22:45:47 +08:00
static void newConfigFile(std::ofstream &configFile)
{
auto templ=DrTemplateBase::newTemplate("config");
configFile << templ->genText();
2018-09-25 22:45:47 +08:00
}
2018-10-31 19:07:37 +08:00
static void newModelConfigFile(std::ofstream &configFile)
{
auto templ = DrTemplateBase::newTemplate("model_json");
configFile << templ->genText();
}
void create_project::createProject(const std::string &projectName)
{
2018-10-14 15:56:54 +08:00
if (access(projectName.data(), 0) == 0)
2018-09-12 17:09:08 +08:00
{
2018-10-14 15:56:54 +08:00
std::cerr << "The directory already exists, please use another project name!" << std::endl;
2018-09-12 17:09:08 +08:00
exit(1);
}
2018-10-14 15:56:54 +08:00
std::cout << "create a project named " << projectName << std::endl;
mkdir(projectName.data(), 0755);
//1.create CMakeLists.txt
2018-10-14 15:56:54 +08:00
auto r = chdir(projectName.data());
2018-09-25 21:46:26 +08:00
(void)(r);
2018-10-14 15:56:54 +08:00
std::ofstream cmakeFile("CMakeLists.txt", std::ofstream::out);
newCmakeFile(cmakeFile, projectName);
std::ofstream mainFile("main.cc", std::ofstream::out);
newMainFile(mainFile);
2018-10-14 15:56:54 +08:00
mkdir("views", 0755);
mkdir("controllers", 0755);
mkdir("filters", 0755);
mkdir("build", 0755);
2018-10-31 19:07:37 +08:00
mkdir("models", 0755);
2018-10-14 15:56:54 +08:00
mkdir("cmake_modules", 0755);
std::ofstream jsonFile("cmake_modules/FindJsoncpp.cmake", std::ofstream::out);
2018-09-15 22:52:59 +08:00
newJsonFindFile(jsonFile);
std::ofstream uuidFile("cmake_modules/FindUUID.cmake", std::ofstream::out);
newUuidFindFile(uuidFile);
2018-10-14 15:56:54 +08:00
std::ofstream gitFile(".gitignore", std::ofstream::out);
newGitIgFile(gitFile);
2018-10-14 15:56:54 +08:00
std::ofstream configFile("config.json", std::ofstream::out);
2018-09-25 22:45:47 +08:00
newConfigFile(configFile);
2018-10-31 19:07:37 +08:00
std::ofstream modelConfigFile("models/model.json", std::ofstream::out);
newModelConfigFile(modelConfigFile);
2018-09-15 22:58:48 +08:00
}