2018-10-17 02:50:51 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* 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
|
2018-10-17 02:50:51 +00:00
|
|
|
* 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
|
2018-10-17 02:50:51 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-09-04 06:50:18 +00:00
|
|
|
#include "create_project.h"
|
2018-10-17 02:50:51 +00:00
|
|
|
#include <drogon/DrTemplateBase.h>
|
2018-09-04 06:50:18 +00:00
|
|
|
#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> ¶meters)
|
|
|
|
{
|
2018-10-14 07:56:54 +00:00
|
|
|
if (parameters.size() < 1)
|
2018-09-04 06:50:18 +00:00
|
|
|
{
|
2018-10-14 07:56:54 +00:00
|
|
|
std::cout << "please input project name" << std::endl;
|
2018-09-04 06:50:18 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2018-10-14 07:56:54 +00:00
|
|
|
auto pName = parameters[0];
|
2018-09-04 06:50:18 +00:00
|
|
|
createProject(pName);
|
|
|
|
}
|
2018-10-14 07:56:54 +00:00
|
|
|
static void newCmakeFile(std::ofstream &cmakeFile, const std::string &projectName)
|
2018-09-04 06:50:18 +00:00
|
|
|
{
|
2018-10-15 14:46:53 +00:00
|
|
|
HttpViewData data;
|
|
|
|
data.insert("ProjectName",projectName);
|
2018-10-17 02:50:51 +00:00
|
|
|
auto templ=DrTemplateBase::newTemplate("cmake.csp");
|
|
|
|
cmakeFile << templ->genText(data);
|
2018-10-14 07:56:54 +00:00
|
|
|
}
|
|
|
|
static void newMainFile(std::ofstream &mainFile)
|
|
|
|
{
|
2018-10-17 02:50:51 +00:00
|
|
|
auto templ=DrTemplateBase::newTemplate("demoMain");
|
|
|
|
mainFile << templ->genText();
|
2018-10-14 07:56:54 +00:00
|
|
|
}
|
|
|
|
static void newGitIgFile(std::ofstream &gitFile)
|
|
|
|
{
|
2018-10-17 02:50:51 +00:00
|
|
|
auto templ=DrTemplateBase::newTemplate("gitignore.csp");
|
|
|
|
gitFile << templ->genText();
|
2018-09-04 06:50:18 +00:00
|
|
|
}
|
2018-10-21 06:52:41 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2018-10-17 02:50:51 +00:00
|
|
|
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
|
|
|
|
2018-12-01 13:29:31 +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)
|
|
|
|
{
|
2018-10-17 02:50:51 +00:00
|
|
|
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();
|
|
|
|
}
|
2018-09-04 06:50:18 +00:00
|
|
|
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);
|
2018-09-04 06:50:18 +00:00
|
|
|
//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);
|
2018-09-04 06:50:18 +00:00
|
|
|
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);
|
2018-10-21 06:52:41 +00:00
|
|
|
std::ofstream uuidFile("cmake_modules/FindUUID.cmake", std::ofstream::out);
|
|
|
|
newUuidFindFile(uuidFile);
|
2018-12-01 13:29:31 +00:00
|
|
|
std::ofstream mysqlFile("cmake_modules/FindMySQL.cmake", std::ofstream::out);
|
|
|
|
newMySQLFindFile(mysqlFile);
|
2018-10-21 06:52:41 +00:00
|
|
|
|
2018-10-14 07:56:54 +00:00
|
|
|
std::ofstream gitFile(".gitignore", std::ofstream::out);
|
2018-09-04 06:50:18 +00:00
|
|
|
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
|
|
|
}
|