Advertisement
DinoSource

Untitled

Mar 2nd, 2025
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.79 KB | Source Code | 0 0
  1. #!/bin/sh
  2.  
  3. DEVELOP="${HOME}/dev/cpp"
  4.  
  5. project_directory="${DEVELOP}/${1}"
  6.  
  7. cd ${DEVELOP} && mkdir -p ${1} && cd ${project_directory} && \
  8. touch .gitignore .gitmodules CMakeLists.txt README.md
  9.  
  10. cat > .gitignore << EOF
  11. # Ignore build artefacts in 'build' directory
  12. build/
  13.  
  14. # Ignore 'lib' directory
  15. lib/
  16.  
  17. # Ignore temporary files created by Google Test Framework in 'Testing' directory
  18. Testing/
  19.  
  20. # Ignore VS Code config files in '.vscode' directory
  21. .vscode/
  22.  
  23. # Ignore binaries
  24. *.out
  25. *.exe
  26. main
  27. app
  28.  
  29. # Ignore third-parties in 'external' directory in the root of the project
  30. /external/*
  31.  
  32. # except of CMakeLists.txt file
  33. !/external/CMakeLists.txt
  34.  
  35. #Ignore googletest
  36. googletest
  37. /external/googletest/
  38.  
  39. # Ignore unnecessary CMake, Make, clangd and conan stuff
  40. .cache/
  41. CMakeFiles/
  42. CMakeCache.txt
  43. DartConfiguration.tcl
  44.  
  45. *.cmake
  46. !/cmake/UpdateSubmodules.cmake
  47. Makefile
  48. compile_commands.json
  49. EOF
  50.  
  51. cat > .gitmodules << EOF
  52. [submodule "external/googletest"]
  53.     path = external/googletest
  54.     url = https://github.com/google/googletest.git
  55. EOF
  56.  
  57. cat > CMakeLists.txt << EOF
  58. cmake_minimum_required(VERSION 3.30.0)
  59. project("$1" VERSION 0.0.1
  60.     DESCRIPTION "$1"
  61.     LANGUAGES CXX)
  62.  
  63. # Disable response files
  64. set(CMAKE_CXX_USE_RESPONSE_FILE_FOR_INCLUDES OFF)
  65.  
  66. # Enable C++23 compiler support
  67. set(CMAKE_CXX_STANDARD 23)
  68. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  69. set(CMAKE_CXX_EXTENSIONS OFF)
  70.  
  71. # Update the submodules here
  72. include(cmake/UpdateSubmodules.cmake)
  73.  
  74. # Enable testing for this project
  75. include(CTest)
  76.  
  77. # Add subdirectories with code
  78. add_subdirectory(external)
  79. add_subdirectory(test)
  80. add_subdirectory(src)
  81. EOF
  82.  
  83. cd ${project_directory} && mkdir -p build cmake external src test && \
  84. cd ${project_directory}/cmake && touch UpdateSubmodules.cmake
  85.  
  86. cat > UpdateSubmodules.cmake << EOF
  87. # Adapted from https://cliutils.gitlab.io/modern-cmake/chapters/projects/submodule.html
  88. find_package(Git QUIET)
  89. if(GIT_FOUND)
  90.     option(UPDATE_SUBMODULES "Check submodules during build" ON)
  91.     if(NOT UPDATE_SUBMODULES)
  92.         return()
  93.     endif()
  94.     execute_process(COMMAND \${GIT_EXECUTABLE} submodule
  95.                     WORKING_DIRECTORY \${CMAKE_CURRENT_SOURCE_DIR}
  96.                     OUTPUT_VARIABLE EXISTING_SUBMODULES
  97.                     RESULT_VARIABLE RETURN_CODE
  98.                     OUTPUT_STRIP_TRAILING_WHITESPACE)
  99.     message(STATUS "Updating git submodules:\n\${EXISTING_SUBMODULES}")
  100.     execute_process(COMMAND \${GIT_EXECUTABLE} submodule update --init --recursive
  101.                     WORKING_DIRECTORY \${CMAKE_CURRENT_SOURCE_DIR}
  102.                     RESULT_VARIABLE RETURN_CODE)
  103.     if(NOT RETURN_CODE EQUAL "0")
  104.         message(WARNING "Cannot update submodules. Git command failed with \${RETURN_CODE}")
  105.         return()
  106.     endif()
  107.     message(STATUS "Git submodules updated successfully")
  108. endif()
  109. EOF
  110.  
  111. cd ${project_directory} && mkdir -p .vscode && cd ${project_directory}/.vscode && \
  112. touch launch.json
  113.  
  114. cat > launch.json << EOF
  115. {
  116.     "version": "0.2.0",
  117.     "configurations": [
  118.         {
  119.             "type": "lldb",
  120.             "request": "launch",
  121.             "name": "Debug",
  122.             "program": "\${workspaceFolder}/build/test/${1}_test",
  123.             "args": [],
  124.             "cwd": "\${workspaceFolder}"
  125.         }
  126.     ]
  127. }
  128. EOF
  129.  
  130. cd ${project_directory}/external && touch CMakeLists.txt
  131. cat > CMakeLists.txt << EOF
  132. cmake_minimum_required(VERSION 3.30.0)
  133.  
  134. set(CMAKE_CXX_STANDARD 23)
  135. add_subdirectory(googletest)
  136. EOF
  137.  
  138. cd ${project_directory}/src && touch CMakeLists.txt main.cpp solution.h btree.h
  139. cat > CMakeLists.txt << EOF
  140. cmake_minimum_required(VERSION 3.30.0)
  141.  
  142. set(SOURCES solution.h main.cpp)
  143. add_executable(app \${SOURCES})
  144. EOF
  145.  
  146. cat > main.cpp << EOF
  147. int main() {}
  148. EOF
  149.  
  150. cat > btree.h << EOF
  151. struct TreeNode {
  152.     int val;
  153.     TreeNode* left;
  154.     TreeNode* right;
  155.  
  156.     TreeNode()
  157.         : val{0}
  158.         , left{nullptr}
  159.         , right{nullptr} {}
  160.  
  161.     explicit TreeNode(int x)
  162.         : val{x}
  163.         , left{nullptr}
  164.         , right{nullptr} {}
  165.  
  166.     TreeNode(int x, TreeNode* left, TreeNode* right)
  167.         : val{x}
  168.         , left{left}
  169.         , right{right} {}
  170. };
  171. EOF
  172.  
  173. cat > solution.h << EOF
  174. #ifndef MY_VERY_AWESOME_HEADER_${1^^}_H
  175. #define MY_VERY_AWESOME_HEADER_${1^^}_H
  176.  
  177. #include "btree.h"
  178. #include <string>
  179. #include <vector>
  180.  
  181. class Solution {
  182. public:
  183.     auto ${2} {
  184.         return {};
  185.     }
  186. };
  187.  
  188. #endif // MY_VERY_AWESOME_HEADER_${1^^}_H
  189. EOF
  190.  
  191. cd ${project_directory}/test && touch CMakeLists.txt
  192. cd ${project_directory}/test && touch $1_test.cpp
  193. cat > CMakeLists.txt << EOF
  194. cmake_minimum_required(VERSION 3.30.0)
  195.  
  196. # Enable C++23 compiler support
  197. set(CMAKE_CXX_STANDARD 23)
  198. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  199. set(CMAKE_CXX_EXTENSIONS OFF)
  200.  
  201. set(TEST_SOLUTION "$1_test")
  202.  
  203. # BUILD_TESTING variable is created by include(CTest)
  204. # It is set to ON by default
  205. if (BUILD_TESTING)
  206.     set(SOURCES
  207.         "../src/solution.h"
  208.         "$1_test.cpp")
  209.  
  210.     add_executable(\${TEST_SOLUTION} \${SOURCES})
  211.  
  212.     target_link_libraries(\${TEST_SOLUTION} PRIVATE GTest::gtest_main)
  213.  
  214.     include(GoogleTest)
  215.     # Finds all the Google tests associated with the executable
  216.     gtest_discover_tests(\${TEST_SOLUTION})
  217.  
  218.     target_include_directories(\${TEST_SOLUTION} PUBLIC test src)
  219. endif()
  220. EOF
  221.  
  222. cat > $1_test.cpp << EOF
  223. #include <gtest/gtest.h>
  224. #include "../src/solution.h"
  225.  
  226. [[maybe_unused]] Solution sol;
  227.  
  228. TEST(TestTopic, $1_test_1) {
  229.     auto actual_result {1};
  230.     auto expected_result {1};
  231.  
  232.     EXPECT_EQ(actual_result, expected_result);
  233. }
  234.  
  235. TEST(TestTopic, $1_test_2) {
  236.     auto actual_result {1};
  237.     auto expected_result {1};
  238.  
  239.     EXPECT_EQ(actual_result, expected_result);
  240. }
  241.  
  242. TEST(TestTopic, $1_test_3) {
  243.     auto actual_result {1};
  244.     auto expected_result {1};
  245.  
  246.     EXPECT_EQ(actual_result, expected_result);
  247. }
  248. EOF
  249.  
  250. cd ${project_directory} && touch xconfig.sh xbuild.sh xtests.sh xapp.sh
  251.  
  252. cat > xconfig.sh << EOF
  253. # You have to define DEVELOP variable as a path to your development
  254. # directory in your .bashrc (or .zshrc if you use zsh) file
  255.  
  256. # Environment variables CC and CXX have to be defined in your .bashrc
  257. # (or .zshrc if you're using zsh) file as well.
  258. # Usually path to compiler looks like "/usr/bin/gcc" or "/usr/bin/clang".
  259. # However, it might differ if you've installed it in other directory.
  260.  
  261. /usr/bin/cmake --no-warn-unused-cli \\
  262.     -DCMAKE_BUILD_TYPE:STRING=Debug \\
  263.     -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE \\
  264.     -DCMAKE_C_COMPILER:FILEPATH=\$CC \\
  265.     -DCMAKE_CXX_COMPILER:FILEPATH=\$CXX \\
  266.     -S$DEVELOP/$1 \\
  267.     -B$DEVELOP/$1/build \\
  268.     -G Ninja
  269. EOF
  270.  
  271. cat > xbuild.sh << EOF
  272. # You have to define DEVELOP variable as a path to your development
  273. # directory in your .bashrc (or .zshrc if you use zsh) file
  274.  
  275. /usr/bin/cmake --build $DEVELOP/$1/build --config Debug --target all --
  276. EOF
  277.  
  278. cat > xtests.sh << EOF
  279. # You have to define DEVELOP variable as a path to your development
  280. # directory in your .bashrc (or .zshrc if you use zsh) file
  281.  
  282. $DEVELOP/$1/build/test/$1_test
  283. EOF
  284.  
  285. cat > xapp.sh << EOF
  286. # You have to define DEVELOP variable as a path to your development
  287. # directory in your .bashrc (or .zshrc if you use zsh) file
  288.  
  289. $DEVELOP/$1/build/src/app
  290. EOF
  291.  
  292. cd ${project_directory} && \
  293.     git init && \
  294.     git config --global init.defaultBranch master && \
  295.     git config --global user.email "[email protected]" && \
  296.     git config --global user.name "Awesome Guy" && \
  297.     git config --global core.editor nvim && \
  298.     git add --all && \
  299.     git commit -m "Initial commit" && \
  300.     git submodule add -f https://github.com/google/googletest.git external/googletest && \
  301.     git add --all && \
  302.     git commit -m "Add googletest as a git submodule" && \
  303.     bash ./xconfig.sh && \
  304.     bash ./xbuild.sh && \
  305.     nvim ${project_directory}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement