This commit is contained in:
taynpg 2024-09-11 16:55:10 +08:00
commit b9f20ca43b
10 changed files with 459 additions and 0 deletions

17
.clang-format Normal file
View File

@ -0,0 +1,17 @@
BasedOnStyle: LLVM
IndentWidth: 4
PointerAlignment: Left
AccessModifierOffset: -4
BreakBeforeBraces: Custom
BraceWrapping:
AfterFunction: true
AfterClass: true
Cpp11BracedListStyle: true
ReflowComments: true
SpacesBeforeTrailingComments: 3
TabWidth: 4
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ColumnLimit: 80
AllowShortBlocksOnASingleLine: Never
AllowShortFunctionsOnASingleLine: None
AllowShortEnumsOnASingleLine: false

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
build
.vs
.cache
cmake-*

110
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,110 @@
{
"files.autoSave": "onFocusChange",
"editor.fontSize": 14,
"editor.fontFamily": "'FiraCode Nerd Font Mono', 'FiraCode Nerd Font Mono', 'FiraCode Nerd Font Mono'",
"cmake.configureOnOpen": true,
"cmake.debugConfig": {
"console": "integratedTerminal",
"setupCommands": [
{
"description": "-gdb-set charset utf-8",
"text": "-gdb-set charset UTF-8"
},
{
"description": "Enable gdb pretty-printing",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"args": [
"D:/Demo"
]
},
"cmake.options.statusBarVisibility": "visible",
"cmake.generator": "Ninja",
"C_Cpp.default.compileCommands": "${workspaceRoot}/build/compile_commands.json",
"C_Cpp.default.cppStandard": "c++17",
"editor.inlayHints.enabled": "off",
"editor.unicodeHighlight.allowedLocales": {
"ja": true,
"zh-hant": true,
"zh-hans": true
},
"files.associations": {
"algorithm": "cpp",
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"cctype": "cpp",
"charconv": "cpp",
"chrono": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"concepts": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"exception": "cpp",
"filesystem": "cpp",
"format": "cpp",
"forward_list": "cpp",
"fstream": "cpp",
"functional": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"ios": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"iterator": "cpp",
"limits": "cpp",
"list": "cpp",
"locale": "cpp",
"map": "cpp",
"memory": "cpp",
"mutex": "cpp",
"new": "cpp",
"numeric": "cpp",
"optional": "cpp",
"ostream": "cpp",
"ratio": "cpp",
"regex": "cpp",
"set": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"stop_token": "cpp",
"streambuf": "cpp",
"string": "cpp",
"system_error": "cpp",
"thread": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"typeinfo": "cpp",
"unordered_map": "cpp",
"utility": "cpp",
"vector": "cpp",
"xfacet": "cpp",
"xhash": "cpp",
"xiosbase": "cpp",
"xlocale": "cpp",
"xlocbuf": "cpp",
"xlocinfo": "cpp",
"xlocmes": "cpp",
"xlocmon": "cpp",
"xlocnum": "cpp",
"xloctime": "cpp",
"xmemory": "cpp",
"xstddef": "cpp",
"xstring": "cpp",
"xtr1common": "cpp",
"xtree": "cpp",
"xutility": "cpp",
"variant": "cpp",
"xmemory0": "cpp"
}
}

14
CMakeLists.txt Normal file
View File

@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.16)
project(cmt LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
if (MSVC)
add_compile_options(/source-charset:utf-8)
endif()
message(STATUS "System: ${CMAKE_SYSTEM_NAME}")
message(STATUS "Compiler CXX ID: ${CMAKE_CXX_COMPILER_ID}")
add_executable(cmt main.cpp)

163
main.cpp Normal file
View File

@ -0,0 +1,163 @@
#include <filesystem>
#include <fstream>
#include <iostream>
#ifdef _WIN32
#include <windows.h>
#elif defined(__clang__) && defined(__APPLE__)
#include <mach-o/dyld.h>
#include <unistd.h>
#else
#include <unistd.h>
#endif
#ifndef PATH_MAX
#define PATH_MAX 256
#endif
namespace fs = std::filesystem;
/// @brief
/// @param source 原文件路径
/// @param dest 目标目录,注意是目录,已存在则会被覆盖
bool copy_file(const fs::path& source, const fs::path& dest,
const std::string& spre = "", const std::string& safter = "")
{
try {
if (spre.empty()) {
std::string name = fs::path(source).filename().string();
return fs::copy_file(source, fs::path(dest).append(name),
fs::copy_options::overwrite_existing);
}
// 读入文件
std::ifstream file(source.string());
if (!file.is_open()) {
std::cout << "打开文件失败:" << source.string() << std::endl;
return false;
}
std::istreambuf_iterator<char> iterf(file);
std::istreambuf_iterator<char> iter;
std::string content(iterf, iter);
// 替换
std::size_t start_pos = 0;
while ((start_pos = content.find(spre, start_pos)) !=
std::string::npos) {
content.replace(start_pos, spre.length(), safter);
start_pos += safter.length();
}
// 写入
fs::path desFile(dest);
desFile.append(source.filename().string());
std::ofstream ofile(desFile);
ofile << content;
return true;
} catch (const fs::filesystem_error& e) {
std::cerr << e.what() << '\n';
return false;
}
}
bool handle_work(const std::string& source_dir, const std::string& purpose)
{
if (!fs::exists(purpose)) {
fs::create_directories(purpose);
}
std::string project_name = fs::path(purpose).filename().string();
std::cout << "工程名:" << project_name << std::endl;
auto cp = [&](const std::string& relative_path,
const std::string& spre = "",
const std::string& safter = "") -> bool {
fs::path fsource(source_dir);
fsource.append(relative_path);
return copy_file(fsource.string(), purpose, spre, safter);
};
std::vector<int> ret(5, 0);
ret[0] = static_cast<int>(cp("CMakeLists.txt", "demo", project_name));
ret[1] = static_cast<int>(cp("main.cpp"));
ret[2] = static_cast<int>(cp(".clang-format"));
ret[3] = static_cast<int>(cp(".gitignore"));
fs::path vscodeConfig(source_dir);
vscodeConfig.append(".vscode/settings.json");
fs::path purpose2(purpose);
purpose2.append(".vscode");
if (!fs::exists(purpose2)) {
fs::create_directories(purpose2);
}
ret[4] = static_cast<int>(copy_file(vscodeConfig.string(), purpose2.string()));
auto r = std::find(ret.begin(), ret.end(), 0);
return r == ret.end();
}
std::string get_exe_path()
{
std::string path;
#ifdef _WIN32
char buffer[MAX_PATH];
DWORD length = GetModuleFileName(NULL, buffer, MAX_PATH);
if (length == 0) {
return "";
}
return std::string(buffer, length);
#elif defined(__clang__) && defined(__APPLE__)
uint32_t size = 0;
_NSGetExecutablePath(NULL, &size); // 获取路径缓冲区的大小
std::vector<char> buffer(size); // 创建缓冲区
if (_NSGetExecutablePath(buffer.data(), &size) != 0) {
return "";
}
return std::string(buffer.data());
#else
char buffer[PATH_MAX];
ssize_t len = readlink("/proc/self/exe", buffer, sizeof(buffer) - 1);
if (len == -1) {
return "";
}
buffer[len] = '\0'; // 确保字符串以null终止
return std::string(buffer);
#endif
}
int main(int argc, char* argv[])
{
if (argc < 2) {
std::cout << "第二个参数为创建的项目路径。" << std::endl;
return -1;
}
try {
fs::path cmdPath(argv[1]);
fs::path curWorkPath;
if (cmdPath.is_relative()) {
// std::cout << "相对路径:" << cmdPath.string() << std::endl;
curWorkPath = fs::current_path();
curWorkPath.append(cmdPath.string());
} else {
// std::cout << "绝对路径:" << cmdPath.string() << std::endl;
curWorkPath = cmdPath;
}
// 转换为标准路径:
fs::path standardPath = fs::absolute(curWorkPath);
std::cout << "创建工程目录为:" << standardPath.string() << std::endl;
fs::path exe_path(get_exe_path());
std::string template_dir = exe_path.parent_path().append("template").string();
if (handle_work(template_dir, standardPath.string())) {
std::cout << "创建工程成功....!!!&&&" << std::endl;
}
} catch (const fs::filesystem_error& err) {
std::cerr << err.what() << '\n';
return -1;
}
return 0;
}

17
template/.clang-format Normal file
View File

@ -0,0 +1,17 @@
BasedOnStyle: LLVM
IndentWidth: 4
PointerAlignment: Left
AccessModifierOffset: -4
BreakBeforeBraces: Custom
BraceWrapping:
AfterFunction: true
AfterClass: true
Cpp11BracedListStyle: true
ReflowComments: true
SpacesBeforeTrailingComments: 3
TabWidth: 4
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ColumnLimit: 80
AllowShortBlocksOnASingleLine: Never
AllowShortFunctionsOnASingleLine: None
AllowShortEnumsOnASingleLine: false

4
template/.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
build
.vs
.cache
cmake-*

109
template/.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,109 @@
{
"files.autoSave": "onFocusChange",
"editor.fontSize": 14,
"editor.fontFamily": "'FiraCode Nerd Font Mono', 'FiraCode Nerd Font Mono', 'FiraCode Nerd Font Mono'",
"cmake.configureOnOpen": true,
"cmake.debugConfig": {
"console": "integratedTerminal",
"setupCommands": [
{
"description": "-gdb-set charset utf-8",
"text": "-gdb-set charset UTF-8"
},
{
"description": "Enable gdb pretty-printing",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"args": [
]
},
"cmake.options.statusBarVisibility": "visible",
"cmake.generator": "Ninja",
"C_Cpp.default.compileCommands": "${workspaceRoot}/build/compile_commands.json",
"C_Cpp.default.cppStandard": "c++17",
"editor.inlayHints.enabled": "off",
"editor.unicodeHighlight.allowedLocales": {
"ja": true,
"zh-hant": true,
"zh-hans": true
},
"files.associations": {
"algorithm": "cpp",
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"cctype": "cpp",
"charconv": "cpp",
"chrono": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"concepts": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"exception": "cpp",
"filesystem": "cpp",
"format": "cpp",
"forward_list": "cpp",
"fstream": "cpp",
"functional": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"ios": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"iterator": "cpp",
"limits": "cpp",
"list": "cpp",
"locale": "cpp",
"map": "cpp",
"memory": "cpp",
"mutex": "cpp",
"new": "cpp",
"numeric": "cpp",
"optional": "cpp",
"ostream": "cpp",
"ratio": "cpp",
"regex": "cpp",
"set": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"stop_token": "cpp",
"streambuf": "cpp",
"string": "cpp",
"system_error": "cpp",
"thread": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"typeinfo": "cpp",
"unordered_map": "cpp",
"utility": "cpp",
"vector": "cpp",
"xfacet": "cpp",
"xhash": "cpp",
"xiosbase": "cpp",
"xlocale": "cpp",
"xlocbuf": "cpp",
"xlocinfo": "cpp",
"xlocmes": "cpp",
"xlocmon": "cpp",
"xlocnum": "cpp",
"xloctime": "cpp",
"xmemory": "cpp",
"xstddef": "cpp",
"xstring": "cpp",
"xtr1common": "cpp",
"xtree": "cpp",
"xutility": "cpp",
"variant": "cpp",
"xmemory0": "cpp"
}
}

14
template/CMakeLists.txt Normal file
View File

@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.16)
project(demo LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
if (MSVC)
add_compile_options(/source-charset:utf-8)
endif()
message(STATUS "System: ${CMAKE_SYSTEM_NAME}")
message(STATUS "Compiler CXX ID: ${CMAKE_CXX_COMPILER_ID}")
add_executable(demo main.cpp)

7
template/main.cpp Normal file
View File

@ -0,0 +1,7 @@
#include <iostream>
int main()
{
std::cout << "Done" << std::endl;
return 0;
}