drogon/drogon_ctl/create_project.cc

129 lines
3.7 KiB
C++
Raw Normal View History

/**
*
* create_project.cc
* An Tao
*
* Copyright 2018, An Tao. All rights reserved.
2018-12-07 15:50:18 +08: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 13:26:14 +08:00
* Drogon
*
*/
#include "create_project.h"
#include <drogon/DrTemplateBase.h>
#include <drogon/utils/Utilities.h>
#include <iostream>
#include <sys/stat.h>
#include <sys/types.h>
#ifndef _WIN32
#include <unistd.h>
#else
#include <io.h>
#include <direct.h>
#endif
#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);
}
2019-05-18 20:39:57 +08:00
static void newCmakeFile(std::ofstream &cmakeFile,
const std::string &projectName)
{
2018-10-15 22:46:53 +08:00
HttpViewData data;
2019-05-18 20:39:57 +08:00
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)
{
2019-05-18 20:39:57 +08:00
auto templ = DrTemplateBase::newTemplate("demoMain");
mainFile << templ->genText();
2018-10-14 15:56:54 +08:00
}
static void newGitIgFile(std::ofstream &gitFile)
{
2019-05-18 20:39:57 +08:00
auto templ = DrTemplateBase::newTemplate("gitignore.csp");
gitFile << templ->genText();
}
2018-09-25 22:45:47 +08:00
static void newConfigFile(std::ofstream &configFile)
{
2019-05-18 20:39:57 +08:00
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();
}
static void newTestMainFile(std::ofstream &mainFile)
{
auto templ = DrTemplateBase::newTemplate("test_main");
mainFile << templ->genText();
}
static void newTestCmakeFile(std::ofstream &testCmakeFile,
const std::string &projectName)
{
HttpViewData data;
data.insert("ProjectName", projectName);
auto templ = DrTemplateBase::newTemplate("test_cmake");
testCmakeFile << templ->genText(data);
}
void create_project::createProject(const std::string &projectName)
{
2020-03-01 11:50:47 +08:00
#ifdef _WIN32
if (_access(projectName.data(), 0) == 0)
#else
2018-10-14 15:56:54 +08:00
if (access(projectName.data(), 0) == 0)
2020-03-01 11:50:47 +08:00
#endif
2018-09-12 17:09:08 +08:00
{
2019-05-18 20:39:57 +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;
drogon::utils::createPath(projectName);
2020-03-01 11:50:47 +08:00
// 1.create CMakeLists.txt
#ifdef _WIN32
auto r = _chdir(projectName.data());
#else
2018-10-14 15:56:54 +08:00
auto r = chdir(projectName.data());
2020-03-01 11:50:47 +08:00
#endif
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);
drogon::utils::createPath("views");
drogon::utils::createPath("controllers");
drogon::utils::createPath("filters");
drogon::utils::createPath("plugins");
drogon::utils::createPath("build");
drogon::utils::createPath("models");
drogon::utils::createPath("test");
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);
std::ofstream testMainFile("test/test_main.cc", std::ofstream::out);
newTestMainFile(testMainFile);
std::ofstream testCmakeFile("test/CMakeLists.txt", std::ofstream::out);
newTestCmakeFile(testCmakeFile, projectName);
2018-09-15 22:58:48 +08:00
}