drogon/drogon_ctl/create_project.cc

96 lines
2.7 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);
}
2019-05-18 12:39:57 +00:00
static void newCmakeFile(std::ofstream &cmakeFile,
const std::string &projectName)
{
2018-10-15 14:46:53 +00:00
HttpViewData data;
2019-05-18 12:39:57 +00:00
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)
{
2019-05-18 12:39:57 +00:00
auto templ = DrTemplateBase::newTemplate("demoMain");
mainFile << templ->genText();
2018-10-14 07:56:54 +00:00
}
static void newGitIgFile(std::ofstream &gitFile)
{
2019-05-18 12:39:57 +00:00
auto templ = DrTemplateBase::newTemplate("gitignore.csp");
gitFile << templ->genText();
}
2018-09-25 14:45:47 +00:00
static void newConfigFile(std::ofstream &configFile)
{
2019-05-18 12:39:57 +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();
}
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
{
2019-05-18 12:39:57 +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);
2019-05-18 12:39:57 +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);
newMainFile(mainFile);
2018-10-14 07:56:54 +00:00
mkdir("views", 0755);
mkdir("controllers", 0755);
mkdir("filters", 0755);
2019-03-29 07:34:14 +00:00
mkdir("plugins", 0755);
2018-10-14 07:56:54 +00:00
mkdir("build", 0755);
2018-10-31 11:07:37 +00:00
mkdir("models", 0755);
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
}