131 lines
3.6 KiB
C++
131 lines
3.6 KiB
C++
#include <CLI11.hpp>
|
|
#include <boost/filesystem.hpp>
|
|
#include <boost/process.hpp>
|
|
#include <fmt/format.h>
|
|
#include <iostream>
|
|
#include <memory>
|
|
|
|
#if defined(_WIN32)
|
|
#include <windows.h>
|
|
#endif
|
|
|
|
#include "exec_cmd.h"
|
|
#include "read_config.h"
|
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
constexpr auto spitLinePre = "\n---------------------------------------------------";
|
|
constexpr auto spitLine = "---------------------------------------------------";
|
|
constexpr auto spitLineLn = "---------------------------------------------------\n";
|
|
|
|
int Clear(CmdInfo& info, const std::string& root, const std::string& operType)
|
|
{
|
|
const auto& cfg = info.data[operType];
|
|
if (cfg.size() != 1) {
|
|
std::cout << "Clear未匹配到唯一配置:" << operType << std::endl;
|
|
return 6;
|
|
}
|
|
std::cout << spitLineLn;
|
|
std::cout << "执行命令[" << operType << "]:\n";
|
|
std::cout << spitLineLn;
|
|
const auto& ags = cfg[0];
|
|
for (const auto& item : ags.args) {
|
|
if (item.empty()) {
|
|
continue;
|
|
}
|
|
fs::path p(root);
|
|
p.append(item);
|
|
if (fs::exists(p)) {
|
|
std::cout << "移除目录 => " << p.string() << std::endl;
|
|
fs::remove_all(p);
|
|
}
|
|
else {
|
|
std::cout << "目录不存在 => " << p.string() << std::endl;
|
|
}
|
|
}
|
|
std::cout << spitLineLn;
|
|
return 0;
|
|
}
|
|
|
|
int ConfigBuild(CmdInfo& info, const std::string& root, const std::string& operType)
|
|
{
|
|
auto cmdExe = boost::process::environment::find_executable("cmd");
|
|
|
|
std::vector<std::string> args = {"/c", "call"};
|
|
std::string vsbat = info.vsbat;
|
|
|
|
args.push_back(vsbat);
|
|
args.emplace_back("&&");
|
|
args.emplace_back("cmake");
|
|
|
|
const auto& cfg = info.data[operType];
|
|
if (cfg.size() != 1) {
|
|
std::cout << "ConfigBuild未匹配到唯一配置:" << operType << std::endl;
|
|
return 6;
|
|
}
|
|
|
|
const auto& ags = cfg[0];
|
|
for (const auto& arg : ags.args) {
|
|
args.push_back(arg);
|
|
}
|
|
|
|
std::cout << spitLineLn;
|
|
std::cout << "执行命令[" << operType << "]:\n";
|
|
std::cout << spitLineLn;
|
|
for (int i = 4; i < args.size(); i++) {
|
|
if (i > 4 && i % 2 == 0) {
|
|
std::cout << std::endl;
|
|
}
|
|
std::cout << args[i] << " ";
|
|
}
|
|
std::cout << spitLinePre << std::endl;
|
|
return ExecCMD::ExecuteCmd(cmdExe.string(), args, root);
|
|
}
|
|
|
|
int main(int argc, char** argv)
|
|
{
|
|
#if defined(_WIN32)
|
|
SetConsoleOutputCP(CP_UTF8);
|
|
#endif
|
|
|
|
CLI::App app{"cmake-vsenv构建工具。"};
|
|
argv = app.ensure_utf8(argv);
|
|
|
|
std::string rootDir;
|
|
app.add_option("-r, --root", rootDir, "项目根目录。");
|
|
|
|
std::string operType;
|
|
app.add_option("-t, --type", operType, "模式。")->required()->check(CLI::IsMember({"Debug", "Release"}));
|
|
|
|
std::string actionName;
|
|
app.add_option("-a, --action", actionName, "动作。")->required()->check(CLI::IsMember({"Config", "Build", "Clear"}));
|
|
|
|
CLI11_PARSE(app, argc, argv);
|
|
|
|
fs::path p(rootDir);
|
|
p.append("cmake-vsenv.json");
|
|
if (!fs::exists(p)) {
|
|
std::cerr << "不存在=>" << p.string() << std::endl;
|
|
return 2;
|
|
}
|
|
|
|
auto config = std::make_shared<ReadConfig>();
|
|
|
|
CmdInfo info;
|
|
auto acType = config->GetType(actionName);
|
|
if (!config->Read(info, acType, p.string())) {
|
|
std::cerr << "解析配置失败=>" << p.string() << std::endl;
|
|
return 3;
|
|
}
|
|
|
|
if (!info.data.count(operType)) {
|
|
std::cout << "获取" << operType << "配置失败。" << std::endl;
|
|
return 5;
|
|
}
|
|
|
|
if (actionName == "Clear") {
|
|
return Clear(info, rootDir, operType);
|
|
}
|
|
return ConfigBuild(info, rootDir, operType);
|
|
}
|