From b9f20ca43ba12c664fd1a221305955e49ca460d1 Mon Sep 17 00:00:00 2001 From: taynpg Date: Wed, 11 Sep 2024 16:55:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .clang-format | 17 ++++ .gitignore | 4 + .vscode/settings.json | 110 ++++++++++++++++++++++ CMakeLists.txt | 14 +++ main.cpp | 163 +++++++++++++++++++++++++++++++++ template/.clang-format | 17 ++++ template/.gitignore | 4 + template/.vscode/settings.json | 109 ++++++++++++++++++++++ template/CMakeLists.txt | 14 +++ template/main.cpp | 7 ++ 10 files changed, 459 insertions(+) create mode 100644 .clang-format create mode 100644 .gitignore create mode 100644 .vscode/settings.json create mode 100644 CMakeLists.txt create mode 100644 main.cpp create mode 100644 template/.clang-format create mode 100644 template/.gitignore create mode 100644 template/.vscode/settings.json create mode 100644 template/CMakeLists.txt create mode 100644 template/main.cpp diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..402f22e --- /dev/null +++ b/.clang-format @@ -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 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5b861d3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +build +.vs +.cache +cmake-* \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..786f532 --- /dev/null +++ b/.vscode/settings.json @@ -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" + } +} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..1a3c8a2 --- /dev/null +++ b/CMakeLists.txt @@ -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) diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..10ca34c --- /dev/null +++ b/main.cpp @@ -0,0 +1,163 @@ +#include +#include +#include + +#ifdef _WIN32 +#include +#elif defined(__clang__) && defined(__APPLE__) +#include +#include +#else +#include +#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 iterf(file); + std::istreambuf_iterator 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 ret(5, 0); + + ret[0] = static_cast(cp("CMakeLists.txt", "demo", project_name)); + ret[1] = static_cast(cp("main.cpp")); + ret[2] = static_cast(cp(".clang-format")); + ret[3] = static_cast(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(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 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; +} \ No newline at end of file diff --git a/template/.clang-format b/template/.clang-format new file mode 100644 index 0000000..402f22e --- /dev/null +++ b/template/.clang-format @@ -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 diff --git a/template/.gitignore b/template/.gitignore new file mode 100644 index 0000000..5b861d3 --- /dev/null +++ b/template/.gitignore @@ -0,0 +1,4 @@ +build +.vs +.cache +cmake-* \ No newline at end of file diff --git a/template/.vscode/settings.json b/template/.vscode/settings.json new file mode 100644 index 0000000..aee1bff --- /dev/null +++ b/template/.vscode/settings.json @@ -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" + } +} \ No newline at end of file diff --git a/template/CMakeLists.txt b/template/CMakeLists.txt new file mode 100644 index 0000000..57e1acd --- /dev/null +++ b/template/CMakeLists.txt @@ -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) diff --git a/template/main.cpp b/template/main.cpp new file mode 100644 index 0000000..157fa26 --- /dev/null +++ b/template/main.cpp @@ -0,0 +1,7 @@ +#include + +int main() +{ + std::cout << "Done" << std::endl; + return 0; +} \ No newline at end of file