refactor.sh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/bin/bash
  2. #
  3. # Copyright (C) 2013-2020 Mattia Basaglia
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Lesser General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Lesser General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. ################################################################################
  18. # This script is to refactor the old class names to the new ones #
  19. # eg: occurrences of Color_Dialog become color_widgets::ColorDialog #
  20. # This script does very simple text replacements and overwrites existing files #
  21. # Use with care #
  22. # Usage: #
  23. # ./refactor.sh /path/to/sources #
  24. # #
  25. ################################################################################
  26. old_classes=(
  27. Color_Delegate
  28. Color_Dialog
  29. Color_List_Widget
  30. Color_Preview
  31. Color_Selector
  32. Color_Wheel
  33. Gradient_Slider
  34. Hue_Slider
  35. )
  36. old_enums=(
  37. Button_Mode
  38. Display_Mode
  39. Update_Mode
  40. Display_Enum
  41. Display_Flags
  42. )
  43. file_extensions=(
  44. ui
  45. cpp
  46. hpp
  47. C
  48. H
  49. h
  50. cxx
  51. hxx
  52. )
  53. function new_class_name()
  54. {
  55. echo "$1" | sed -e 's/_//g' -r -e 's/^/color_widgets::/'
  56. }
  57. function new_enum_name()
  58. {
  59. echo "$1" | sed -e 's/_//g'
  60. }
  61. directory="$1"
  62. if [ -z "$directory" ]
  63. then
  64. echo "Usage: $0 (directory)"
  65. exit 1
  66. fi
  67. find_extensions=""
  68. for ext in ${file_extensions[@]}
  69. do
  70. find_extensions="$find_extensions -o -name '*.$ext'"
  71. done
  72. find_extensions="$(echo "$find_extensions" | sed -r 's/ -o //')"
  73. find_command="find \""$directory"\" -type f -a \( $find_extensions \) -print"
  74. files="$(bash -c "$find_command")"
  75. replacements=""
  76. for class in ${old_classes[@]}
  77. do
  78. replacements="$replacements $class $(new_class_name $class)"
  79. done
  80. for enum in ${old_enums[@]}
  81. do
  82. replacements="$replacements $enum $(new_enum_name $enum)"
  83. done
  84. for file in $files
  85. do
  86. replace $replacements -- "$file"
  87. done