2
0

code-cleaner.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  2. # Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. #
  4. # Permission is hereby granted, free of charge, to any person
  5. # obtaining a copy of this software and associated documentation
  6. # files (the "Software"), to deal in the Software without
  7. # restriction, including without limitation the rights to use,
  8. # copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following
  11. # conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be
  14. # included in all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  18. # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  20. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21. # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23. # OTHER DEALINGS IN THE SOFTWARE.
  24. require 'optparse'
  25. require 'ostruct'
  26. $extension = [".cpp", ".h"]
  27. $files_list = Array.new
  28. #------------------------------------------------------------------------------
  29. def validate_command_line(args)
  30. if args.length != 2
  31. return false
  32. end
  33. if args[0] != "--path"
  34. return false
  35. end
  36. return true
  37. end
  38. #------------------------------------------------------------------------------
  39. def parse_command_line(args)
  40. banner = "Usage: crown-android.rb --target <android-target> --name <project-name> --path <project-path>\n"
  41. if not validate_command_line(args)
  42. print banner
  43. exit
  44. end
  45. options = OpenStruct.new
  46. OptionParser.new do |opts|
  47. opts.banner = banner
  48. opts.on("-p", "--path PATH", "Code Path to clean") do |p|
  49. options.path = p
  50. end
  51. opts.on_tail("-h", "--help", "Show this message") do
  52. puts opts
  53. exit
  54. end
  55. end.parse!(args)
  56. return options
  57. end
  58. #------------------------------------------------------------------------------
  59. def lists_files(path)
  60. files = Dir.entries(path)
  61. for entry in files
  62. if entry == "." || entry == ".."
  63. next
  64. end
  65. tmp = path + '/' + entry
  66. if File.directory?(tmp)
  67. lists_files(tmp)
  68. elsif File.file?(tmp)
  69. ext = File.extname(tmp)
  70. if ext == $extension[0] || ext == $extension[1] || ext == $extension[2]
  71. $files_list.push(tmp);
  72. end
  73. end
  74. end
  75. end
  76. #------------------------------------------------------------------------------
  77. def clean_file(file_name)
  78. lines = []
  79. # Copies each file's line
  80. File.open(file_name) do |file|
  81. file.each_line do |line|
  82. lines << line
  83. end
  84. end
  85. clean = true
  86. lines.each_index do |index|
  87. line = lines[index]
  88. #match one or more spaces/tabs at the end of the line -> nothing
  89. new_line = line.gsub(/[ \t]+$/, "")
  90. if(new_line != line)
  91. if(clean)
  92. puts "Cleaning '#{file_name}'"
  93. clean = false
  94. end
  95. lines[index] = new_line
  96. end
  97. end
  98. if(!clean)
  99. File.open(file_name, 'w') do |file|
  100. lines.each { |line| file.write(line) }
  101. end
  102. end
  103. end
  104. opts = parse_command_line(ARGV)
  105. lists_files(opts.path)
  106. $files_list.each { |file| clean_file(file) }