| 243 | } |
| 244 | |
| 245 | void generate_project(const std::string &type) { |
| 246 | const auto name = escape_project_name(fs::current_path().stem().string()); |
| 247 | if (fs::exists(fs::current_path() / "cmake.toml")) { |
| 248 | throw std::runtime_error("Cannot initialize a project when cmake.toml already exists!"); |
| 249 | } |
| 250 | |
| 251 | // Check if the folder is empty before creating any files |
| 252 | auto is_empty = fs::is_empty(fs::current_path()); |
| 253 | |
| 254 | // Automatically generate .gitattributes to not skew the statistics of the repo |
| 255 | generate_gitfile(".gitattributes", {"/**/CMakeLists.txt linguist-generated", "/**/cmkr.cmake linguist-vendored"}); |
| 256 | |
| 257 | // Generate .gitignore with reasonable defaults for CMake |
| 258 | generate_gitfile(".gitignore", {"build*/", "cmake-build*/", "CMakerLists.txt", "CMakeLists.txt.user"}); |
| 259 | |
| 260 | tsl::ordered_map<std::string, std::string> variables = { |
| 261 | {"@name", name}, |
| 262 | {"@type", type}, |
| 263 | }; |
| 264 | |
| 265 | if (!is_empty) { |
| 266 | // Make a backup of an existing CMakeLists.txt if it exists |
| 267 | std::error_code ec; |
| 268 | fs::rename("CMakeLists.txt", "CMakeLists.txt.bak", ec); |
| 269 | // Create an empty cmake.toml for migration purporses |
| 270 | create_file("cmake.toml", format(toml_migration, variables)); |
| 271 | return; |
| 272 | } |
| 273 | |
| 274 | if (type == "executable") { |
| 275 | create_file("cmake.toml", format(toml_executable, variables)); |
| 276 | create_file("src/" + name + "/main.cpp", format(cpp_executable, variables)); |
| 277 | } else if (type == "static" || type == "shared" || type == "library") { |
| 278 | create_file("cmake.toml", format(toml_library, variables)); |
| 279 | create_file("src/" + name + "/" + name + ".cpp", format(cpp_library, variables)); |
| 280 | create_file("include/" + name + "/" + name + ".hpp", format(hpp_library, variables)); |
| 281 | } else if (type == "interface") { |
| 282 | create_file("cmake.toml", format(toml_interface, variables)); |
| 283 | create_file("include/" + name + "/" + name + ".hpp", format(hpp_interface, variables)); |
| 284 | } else { |
| 285 | throw std::runtime_error("Unknown project type " + type + "! Supported types are: executable, library, shared, static, interface"); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | struct CommandEndl { |
| 290 | std::stringstream &ss; |
no test coverage detected