You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

373 lines
9.6 KiB

  1. #!/bin/bash
  2. ScriptFile=$(readlink -f "${BASH_SOURCE[0]}")
  3. ScriptDir=$(dirname "${ScriptFile}")
  4. IgnoreVsCodeSettings=1
  5. UseGit=0
  6. Verbose=0
  7. Update=0
  8. CMakeModules=""
  9. CMakeModulesForced=""
  10. Operations=()
  11. function Log()
  12. {
  13. printf "$@\n"
  14. }
  15. function Verbose()
  16. {
  17. if [[ $Verbose -eq 1 ]]; then
  18. Log "$@"
  19. fi
  20. }
  21. function Error()
  22. {
  23. >&2 printf "$@\n"
  24. }
  25. function Panic()
  26. {
  27. Error "$@"
  28. exit 1
  29. }
  30. function PrintHelp()
  31. {
  32. printf "This is a tool to create a new project group or projects inside an exsisting group.
  33. Parameters:
  34. -g|--group <group-directory>
  35. Create a new project group at the given directory.
  36. -i|--interface <name> <group-directory>
  37. Create a new interface library project with the given name at the given directory.
  38. -l|--library <name> <group-directory>
  39. Create a new library project with the given name at the given directory.
  40. -e|--executable <name> <group-directory>
  41. Create a new executable project with the given name at the given directory.
  42. -m|--modules <remote>
  43. -M|--force-modules <remote>
  44. Add the CMake Modules from the given remote repository as submodule of the new project.
  45. --git
  46. Initialize a new git repository when creating a new project or group.
  47. -u|--update
  48. Update an exsisting project.
  49. -s|--vscode
  50. Add default settings for visual studio code
  51. -v|--verbose
  52. Print extra debug output.
  53. -?|-h|--help
  54. Print this help.
  55. "
  56. }
  57. function Copy()
  58. {
  59. srcDir="$1"
  60. dstDir="$2"
  61. file="$3"
  62. oldName="$4"
  63. newName="$5"
  64. tmpSrc="$srcDir$file"
  65. tmpDst="$dstDir$file"
  66. if [[ -n "$oldName$newName" ]]; then
  67. tmpDst=${tmpDst//$oldName/$newName}
  68. fi
  69. tmpDir=$(dirname $tmpDst)
  70. Verbose " Copy .$file to $tmpDst"
  71. mkdir -p "$tmpDir" \
  72. || Panic "Unable to create directory: $tmpDir!"
  73. cp "$tmpSrc" "$tmpDst" \
  74. || Panic "Unable to copy file: $tmpSrc > $tmpDst!"
  75. if [[ -n "$oldName$newName" ]]; then
  76. oldUpper=$(echo $oldName | awk -F: '{ print toupper($1) }')
  77. newUpper=$(echo $newName | awk -F: '{ print toupper($1) }')
  78. sed -i -e "s/$oldName/$newName/g" "$tmpDst" \
  79. || Panic "Unable to replace names!"
  80. sed -i -e "s/$oldUpper/$newUpper/g" "$tmpDst" \
  81. || Panic "Unable to replace names!"
  82. fi
  83. }
  84. function CreateGroup()
  85. {
  86. dir="$1"
  87. Log "\nCreate Project Group: $dir"
  88. # Create directory
  89. Log " Create directory: $dir"
  90. mkdir -p "$dir" \
  91. || Panic "Unable to create project group directory: $dir!"
  92. # Create git repository
  93. Log " Create git repository"
  94. git -C $dir init \
  95. || Panic "Git init failed!"
  96. # Add cmake modules
  97. Log " Add git submodule for CMake modules"
  98. if [[ -n "$CMakeModules" ]]; then
  99. git -C $dir submodule add $CMakeModulesForced "$CMakeModules" "cmake/modules" \
  100. || Panic "Git submodule add failed!"
  101. git -C $dir/cmake/modules fetch --all \
  102. || Panic "Git submodule fetch failed!"
  103. git -C $dir/cmake/modules reset --hard origin/master \
  104. || Panic "Git submodule reset failed!"
  105. fi
  106. # Copy files
  107. Log " Copy files"
  108. srcDir="$ScriptDir"
  109. for file in $(find $srcDir -type f); do
  110. # Ignore unrelated files
  111. if [[ $file == $ScriptFile ]] \
  112. || [[ $file == $srcDir/build/* ]] \
  113. || [[ $file == $srcDir/.git/* ]] \
  114. || [[ $file == $srcDir/.gitmodules ]] \
  115. || [[ $file == $srcDir/projects/* ]] \
  116. || [[ $file == $srcDir/cmake/* ]]
  117. then
  118. relFile=.${file/$srcDir/}
  119. Verbose " Ignore $relFile"
  120. # Ignore .vscode settings
  121. elif [[ IgnoreVsCodeSettings -eq 1 ]] \
  122. && [[ $file == $srcDir/.vscode/* ]]
  123. then
  124. relFile=.${file/$srcDir/}
  125. Verbose " Ignore $relFile"
  126. # Copy related files
  127. else
  128. Copy "$srcDir" "$dir" "${file/$srcDir/}"
  129. fi
  130. done
  131. }
  132. function CreateProject()
  133. {
  134. name="$1"
  135. groupdir="$2"
  136. src="$3"
  137. dir="$groupdir/projects/$name"
  138. Log "\nCreate Project $1 in director $2"
  139. # Create directory
  140. Log " Create directory: $dir"
  141. mkdir -p "$dir" \
  142. || Panic "Unable to create directory: $dir!"
  143. # Create/Update git repository
  144. if [[ $Update -eq 1 ]]; then
  145. if [[ -e "$dir/cmake/modules/.git" ]]; then
  146. Log " Update git submodule for CMake modules"
  147. git -C "$dir/cmake/modules" fetch origin \
  148. || Panic "Unable to fetch cmake module!"
  149. git -C "$dir/cmake/modules" checkout master \
  150. || Panic "Unable to checkout cmake module!"
  151. git -C "$dir/cmake/modules" reset --hard origin/master \
  152. || Panic "Unable to reset cmake module!"
  153. fi
  154. elif [[ $UseGit -eq 1 ]]; then
  155. Log " Create git repository"
  156. git -C $dir init \
  157. || Panic "Git init failed!"
  158. # Add cmake modules
  159. if [[ -n "$CMakeModules" ]]; then
  160. Log " Add git submodule for CMake modules"
  161. git -C $dir submodule add $CMakeModulesForced "$CMakeModules" "cmake/modules" \
  162. || Panic "Git submodule add failed!"
  163. git -C $dir/cmake/modules fetch --all \
  164. || Panic "Git submodule fetch failed!"
  165. git -C $dir/cmake/modules reset --hard origin/master \
  166. || Panic "Git submodule reset failed!"
  167. fi
  168. fi
  169. # Copy files
  170. Log " Copy files"
  171. Copy "$ScriptDir/cmake" "$groupdir/cmake" "/Find$src.cmake" "$src" "$name"
  172. srcDir="$ScriptDir/projects/$src"
  173. for file in $(find $srcDir -type f); do
  174. targetFile="${file/$srcDir/}"
  175. targetFile="${dir}${targetFile}"
  176. targetFile="${targetFile//$src/$name}"
  177. # exclude build directory and cmake modules
  178. if [[ $file == $srcDir/build/* ]] \
  179. || [[ $file == $srcDir/cmake/modules/* ]]
  180. then
  181. relFile=.${file/$srcDir/}
  182. Verbose " Ignore $relFile"
  183. # Ignore .vscode settings
  184. elif [[ IgnoreVsCodeSettings -eq 1 ]] \
  185. && [[ $file == $srcDir/.vscode/* ]]
  186. then
  187. relFile=.${file/$srcDir/}
  188. Verbose " Ignore $relFile"
  189. # ignore git files
  190. elif [[ $UseGit -eq 0 ]] \
  191. && [[ $file == $srcDir/.git* ]]
  192. then
  193. relFile=.${file/$srcDir/}
  194. Verbose " Ignore $relFile"
  195. # update
  196. elif [[ $Update -eq 1 ]] \
  197. && [[ ! -f ${targetFile} ]]
  198. then
  199. relFile=.${file/$srcDir/}
  200. Verbose " Ignore $relFile"
  201. # copy
  202. else
  203. Copy "$srcDir" "$dir" "${file/$srcDir/}" "$src" "$name"
  204. fi
  205. done
  206. }
  207. # Parse arguments
  208. while [ $# -gt 0 ]; do
  209. case $1 in
  210. "-g" | "--group" )
  211. if [ $# -lt 2 ]; then
  212. Panic "Parameter $1 expects exactly one parameter!"
  213. fi
  214. tmp=$(readlink -m "$2")
  215. Operations+=("grp:$tmp")
  216. shift
  217. ;;
  218. "-i" | "--interface" )
  219. if [ $# -lt 3 ]; then
  220. Panic "Parameter $1 expects exactly two parameter!"
  221. fi
  222. tmp=$(readlink -m "$3")
  223. Operations+=("intf:$2:$tmp")
  224. shift
  225. shift
  226. ;;
  227. "-l" | "--library" )
  228. if [ $# -lt 3 ]; then
  229. Panic "Parameter $1 expects exactly two parameter!"
  230. fi
  231. tmp=$(readlink -m "$3")
  232. Operations+=("lib:$2:$tmp")
  233. shift
  234. shift
  235. ;;
  236. "-e" | "--executable" )
  237. if [ $# -lt 3 ]; then
  238. Panic "Parameter $1 expects exactly two parameter!"
  239. fi
  240. tmp=$(readlink -m "$3")
  241. Operations+=("exe:$2:$tmp")
  242. shift
  243. shift
  244. ;;
  245. "-m" | "--modules" )
  246. if [ $# -lt 2 ]; then
  247. Panic "Parameter $1 expects exactly one parameter!"
  248. fi
  249. CMakeModules="$2"
  250. shift
  251. ;;
  252. "-M" | "--force-modules" )
  253. if [ $# -lt 2 ]; then
  254. Panic "Parameter $1 expects exactly one parameter!"
  255. fi
  256. CMakeModules="$2"
  257. CMakeModulesForced="--force"
  258. shift
  259. ;;
  260. "-u" | "--update")
  261. Update=1
  262. ;;
  263. "-s" | "--vscode")
  264. IgnoreVsCodeSettings=0
  265. ;;
  266. "--git" )
  267. UseGit=1
  268. ;;
  269. "-v" | "--verbose" )
  270. Verbose=1
  271. ;;
  272. "-h" | "-?" | "--help" )
  273. PrintHelp
  274. exit 0
  275. ;;
  276. * )
  277. Panic "Invalid or unknown parameter: $1"
  278. ;;
  279. esac
  280. shift
  281. done
  282. # Execute operations
  283. for data in "${Operations[@]}"; do
  284. op=$(echo $data | awk -F: '{ print $1 }')
  285. case $op in
  286. grp)
  287. dir=$(echo $data | awk -F: '{ print $2 }')
  288. CreateGroup "$dir"
  289. ;;
  290. intf)
  291. name=$(echo $data | awk -F: '{ print $2 }')
  292. dir=$(echo $data | awk -F: '{ print $3 }')
  293. CreateProject "$name" "$dir" "libinterface"
  294. ;;
  295. lib)
  296. name=$(echo $data | awk -F: '{ print $2 }')
  297. dir=$(echo $data | awk -F: '{ print $3 }')
  298. CreateProject "$name" "$dir" "libhelloworld"
  299. ;;
  300. exe)
  301. name=$(echo $data | awk -F: '{ print $2 }')
  302. dir=$(echo $data | awk -F: '{ print $3 }')
  303. CreateProject "$name" "$dir" "helloworld"
  304. ;;
  305. *)
  306. Panic "Invalid or unknown operation: $op"
  307. ;;
  308. esac
  309. done