drogon/drogon_ctl/create_project.cc

119 lines
3.4 KiB
C++
Raw Normal View History

/**
*
* create_project.cc
* An Tao
*
* Copyright 2018, An Tao. All rights reserved.
2018-12-07 07:50:18 +00:00
* https://github.com/an-tao/drogon
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
2018-11-16 05:26:14 +00:00
* Drogon
*
*/
#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 07:56:54 +00:00
if (parameters.size() < 1)
{
2018-10-14 07:56:54 +00:00
std::cout << "please input project name" << std::endl;
exit(1);
}
2018-10-14 07:56:54 +00:00
auto pName = parameters[0];
createProject(pName);
}
2018-10-14 07:56:54 +00:00
static void newCmakeFile(std::ofstream &cmakeFile, const std::string &projectName)
{
2018-10-15 14:46:53 +00:00
HttpViewData data;
data.insert("ProjectName",projectName);
auto templ=DrTemplateBase::newTemplate("cmake.csp");
cmakeFile << templ->genText(data);
2018-10-14 07:56:54 +00:00
}
static void newMainFile(std::ofstream &mainFile)
{
auto templ=DrTemplateBase::newTemplate("demoMain");
mainFile << templ->genText();
2018-10-14 07:56:54 +00: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 14:52:59 +00:00
static void newJsonFindFile(std::ofstream &jsonFile)
{
auto templ=DrTemplateBase::newTemplate("FindJsoncpp.csp");
jsonFile << templ->genText();
2018-09-15 14:52:59 +00:00
}
2018-09-25 14:45:47 +00:00
static void newMySQLFindFile(std::ofstream &jsonFile)
{
auto templ = DrTemplateBase::newTemplate("FindMySQL.csp");
jsonFile << templ->genText();
}
2018-09-25 14:45:47 +00:00
static void newConfigFile(std::ofstream &configFile)
{
auto templ=DrTemplateBase::newTemplate("config");
configFile << templ->genText();
2018-09-25 14:45:47 +00:00
}
2018-10-31 11:07:37 +00: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 07:56:54 +00:00
if (access(projectName.data(), 0) == 0)
2018-09-12 09:09:08 +00:00
{
2018-10-14 07:56:54 +00:00
std::cerr << "The directory already exists, please use another project name!" << std::endl;
2018-09-12 09:09:08 +00:00
exit(1);
}
2018-10-14 07:56:54 +00:00
std::cout << "create a project named " << projectName << std::endl;
mkdir(projectName.data(), 0755);
//1.create CMakeLists.txt
2018-10-14 07:56:54 +00:00
auto r = chdir(projectName.data());
2018-09-25 13:46:26 +00:00
(void)(r);
2018-10-14 07:56:54 +00: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 07:56:54 +00:00
mkdir("views", 0755);
mkdir("controllers", 0755);
mkdir("filters", 0755);
mkdir("build", 0755);
2018-10-31 11:07:37 +00:00
mkdir("models", 0755);
2018-10-14 07:56:54 +00:00
mkdir("cmake_modules", 0755);
std::ofstream jsonFile("cmake_modules/FindJsoncpp.cmake", std::ofstream::out);
2018-09-15 14:52:59 +00:00
newJsonFindFile(jsonFile);
std::ofstream uuidFile("cmake_modules/FindUUID.cmake", std::ofstream::out);
newUuidFindFile(uuidFile);
std::ofstream mysqlFile("cmake_modules/FindMySQL.cmake", std::ofstream::out);
newMySQLFindFile(mysqlFile);
2018-10-14 07:56:54 +00:00
std::ofstream gitFile(".gitignore", std::ofstream::out);
newGitIgFile(gitFile);
2018-10-14 07:56:54 +00:00
std::ofstream configFile("config.json", std::ofstream::out);
2018-09-25 14:45:47 +00:00
newConfigFile(configFile);
2018-10-31 11:07:37 +00:00
std::ofstream modelConfigFile("models/model.json", std::ofstream::out);
newModelConfigFile(modelConfigFile);
2018-09-15 14:58:48 +00:00
}