112 lines
3.6 KiB
C++
112 lines
3.6 KiB
C++
#include <boost/algorithm/string.hpp>
|
|
#include <boost/nowide/fstream.hpp>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <zoost/zoost.h>
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
bool CopyDirectory(const std::string& from, const std::string& to)
|
|
{
|
|
try {
|
|
fs::path source_path(from);
|
|
fs::path target_path(to);
|
|
|
|
if (!fs::exists(source_path) || !fs::is_directory(source_path)) {
|
|
std::cerr << "源目录不存在或不是目录: " << from << std::endl;
|
|
return false;
|
|
}
|
|
if (!fs::exists(target_path)) {
|
|
fs::create_directories(target_path);
|
|
}
|
|
for (const auto& entry : fs::recursive_directory_iterator(source_path)) {
|
|
try {
|
|
fs::path relative_path = entry.path().lexically_relative(source_path);
|
|
fs::path full_target_path = target_path / relative_path;
|
|
|
|
if (fs::is_directory(entry.status())) {
|
|
fs::create_directories(full_target_path);
|
|
} else if (fs::is_regular_file(entry.status())) {
|
|
fs::create_directories(full_target_path.parent_path());
|
|
fs::copy_file(entry.path(), full_target_path, fs::copy_options::overwrite_existing);
|
|
}
|
|
|
|
} catch (const std::exception& e) {
|
|
std::cerr << "处理文件时出错: " << entry.path() << " - " << e.what() << std::endl;
|
|
continue;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
} catch (const std::exception& e) {
|
|
std::cerr << "复制目录时出错: " << e.what() << std::endl;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
int main(int argc, char** argv)
|
|
{
|
|
zoostCommon::SetOutputU8();
|
|
zoostCommon::SetStdLibrayU8();
|
|
|
|
std::string arg("default");
|
|
if (argc > 1) {
|
|
arg = std::string(argv[1]);
|
|
}
|
|
|
|
zoostPath zp;
|
|
std::string binPath;
|
|
if (!zp.GetBinaryPath(binPath)) {
|
|
std::cerr << "获取二进制目录失败。" << std::endl;
|
|
return 1;
|
|
}
|
|
|
|
auto from = fs::path(binPath).parent_path().append("template").append(arg);
|
|
if (!fs::exists(from)) {
|
|
std::cerr << "源目录不存在: " << from << std::endl;
|
|
return 1;
|
|
}
|
|
|
|
auto toPath = fs::current_path();
|
|
|
|
// toPath必须为空目录
|
|
if (!toPath.empty() && !fs::is_empty(toPath)) {
|
|
std::cerr << "目标目录不是空目录: " << toPath << std::endl;
|
|
return 1;
|
|
}
|
|
|
|
std::cout << "从: " << from << " 复制到: " << toPath << std::endl;
|
|
|
|
if (!CopyDirectory(from.string(), toPath.string())) {
|
|
std::cerr << "复制目录失败。" << std::endl;
|
|
return 1;
|
|
}
|
|
std::cout << "复制完成。" << std::endl;
|
|
|
|
// 进行替换操作。
|
|
auto cmakeFile = fs::path(toPath).append("CMakeLists.txt");
|
|
if (fs::exists(cmakeFile)) {
|
|
boost::nowide::ifstream inFile(cmakeFile);
|
|
if (!inFile.is_open()) {
|
|
std::cerr << "无法打开 CMakeLists.txt 文件进行读取。" << std::endl;
|
|
return 1;
|
|
}
|
|
std::string content((std::istreambuf_iterator<char>(inFile)), std::istreambuf_iterator<char>());
|
|
inFile.close();
|
|
|
|
boost::replace_all(content, "zedTemplate", fs::path(toPath).filename().string());
|
|
boost::nowide::ofstream outFile(cmakeFile);
|
|
if (!outFile.is_open()) {
|
|
std::cerr << "无法打开 CMakeLists.txt 文件进行写入。" << std::endl;
|
|
return 1;
|
|
}
|
|
outFile.write(content.c_str(), content.size());
|
|
outFile.close();
|
|
} else {
|
|
std::cerr << "CMakeLists.txt 文件不存在。" << std::endl;
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|