153 lines
4.0 KiB
C++
153 lines
4.0 KiB
C++
// CoreDeploy.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
|
|
//
|
|
|
|
#include <map>
|
|
#include <vector>
|
|
#include <string>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <filesystem>
|
|
|
|
#include "../UNSWebServerCore/ServerLogger.h"
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
static const fs::path src = "F:\\UNSWebServerCore\\UNSWebServerCore\\";
|
|
|
|
/**
|
|
* @brief 查找指定目录下所有包含 UNSWSC_DLL_EXPORT 宏的 .h 头文件
|
|
*
|
|
* @param source_dir 目标源代码文件夹路径
|
|
* @return std::vector<std::string> 匹配到的文件名列表(纯文件名,不含路径)
|
|
*/
|
|
std::vector<std::string> FileExportHeaders(const std::string& source_dir)
|
|
{
|
|
std::vector<std::string> matching_files;
|
|
const std::string target_macro = "UNSWSC_DLL_EXPORT";
|
|
|
|
fs::path dir_path(source_dir);
|
|
|
|
// 校验路径有效性
|
|
std::error_code ec;
|
|
if (!fs::exists(dir_path, ec) || !fs::is_directory(dir_path, ec))
|
|
return matching_files;
|
|
|
|
// 递归遍历目录(跳过因权限不足无法访问的文件夹)
|
|
auto options = fs::directory_options::skip_permission_denied;
|
|
for (const auto& entry : fs::recursive_directory_iterator(dir_path, options, ec))
|
|
{
|
|
// 1. 过滤:必须是普通文件且扩展名为 .h
|
|
if (entry.is_regular_file(ec) && entry.path().extension() == ".h")
|
|
{
|
|
std::ifstream file(entry.path(), std::ios::in);
|
|
if (!file.is_open())
|
|
continue;
|
|
|
|
// 2. 逐行读取并搜索关键字
|
|
std::string line;
|
|
while (std::getline(file, line))
|
|
{
|
|
if (line.find(target_macro) != std::string::npos)
|
|
{
|
|
// 找到匹配项,提取纯文件名并加入列表
|
|
matching_files.push_back(entry.path().filename().string());
|
|
break; // 找到后跳出当前文件的读取,避免重复添加
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return matching_files;
|
|
}
|
|
|
|
std::map<std::string, std::map<bool, std::vector<std::string>>> lib_files =
|
|
{
|
|
{
|
|
"Debug",
|
|
{
|
|
{ true, { "UNSWebServerCored.lib" } },
|
|
{ false, { "UNSWebServerCored.dll", "UNSWebServerCored.pdb" } }
|
|
}
|
|
},
|
|
{
|
|
"Release",
|
|
{
|
|
{ true, { "UNSWebServerCore.lib" } },
|
|
{ false, { "UNSWebServerCore.dll", "UNSWebServerCore.pdb" } }
|
|
}
|
|
},
|
|
};
|
|
|
|
bool CopyHeaderFile(const fs::path& dest, const std::vector<std::string>& files)
|
|
{
|
|
SCLOGF_INFO("Header Copy Begin: {} -> {}", src, dest);
|
|
for (const auto& file : files)
|
|
{
|
|
std::error_code ec;
|
|
fs::copy_file((src / file), (dest / file), fs::copy_options::overwrite_existing, ec);
|
|
if (ec)
|
|
{
|
|
SCLOGF_ERROR("Header [{}] Copy Failed: {} ({})", file, ec.value(), ec.message());
|
|
return false;
|
|
}
|
|
}
|
|
SCLOGF_INFO("Header Copy Success, All File Copied To {}", dest);
|
|
return true;
|
|
}
|
|
|
|
bool CopyLibraryFile(const fs::path& dest, const std::string& arch_text)
|
|
{
|
|
SCLOGF_INFO("Library Copy Begin: {} -> {}", src, dest);
|
|
for (const auto& [build, f_info] : lib_files)
|
|
{
|
|
fs::path dst_build_path = ((dest / "..") / arch_text) / build;
|
|
for (const auto& [lib, files] : f_info)
|
|
{
|
|
fs::path dst_path = (lib ? dest : dst_build_path);
|
|
for (const auto& file : files)
|
|
{
|
|
std::error_code ec;
|
|
fs::path file_path = (((src / "..") / arch_text) / build) / file;
|
|
fs::copy_file(file_path, (dst_path / file), fs::copy_options::overwrite_existing, ec);
|
|
if (ec)
|
|
{
|
|
SCLOGF_ERROR("Library [{}] Copy Failed: {} ({})", file, ec.value(), ec.message());
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
SCLOGF_INFO("Library Copy Success, All File Copied To {}", dest);
|
|
return true;
|
|
}
|
|
|
|
bool DeployCore(const fs::path& header_path, const fs::path& library_path = "")
|
|
{
|
|
if (header_path.empty())
|
|
{
|
|
SCLOGF_FATAL("ARG ERROR: Header IS Empty");
|
|
return false;
|
|
}
|
|
auto header_files = FileExportHeaders(src.string());
|
|
if (!CopyHeaderFile(header_path, header_files))
|
|
{
|
|
SCLOGF_FATAL("Header Copy Failed.");
|
|
return false;
|
|
}
|
|
if (!CopyLibraryFile((library_path.empty() ? header_path : library_path), "x64"))
|
|
{
|
|
SCLOGF_FATAL("Library Copy Failed.");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
int main()
|
|
{
|
|
SCLOG_CONSOLE_INIT(uns::llAll);
|
|
|
|
DeployCore("F:\\SVProjects\\SVUpdate\\OTAServer\\unswsc", "F:\\SVProjects\\SVUpdate\\OTAServer");
|
|
|
|
SCLOG_CLOSE();
|
|
return 0;
|
|
} |