source_code_filter.rb 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/ruby
  2. # Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/
  3. require 'tempfile'
  4. require 'code_beauty_ruby.rb'
  5. def checkForHeader(fileName)
  6. foundHeader = false;
  7. foundSheBang = false;
  8. isCoffee = false;
  9. header = "# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/\n"
  10. if File.extname(fileName) == '.coffee'
  11. isCoffee = true
  12. end
  13. # read file
  14. file = File.open(fileName)
  15. t_file = Tempfile.new('Temp')
  16. allLines = IO.readlines(fileName)
  17. # detect file type
  18. if allLines[0] =~ /(^# Copyright)/ || allLines[1] =~ /(^# Copyright)/
  19. foundHeader = true
  20. elsif allLines[1] =~/^=begin/ #assume external script
  21. foundHeader = true
  22. end
  23. if allLines[0] =~ /(^#!\/)/
  24. foundSheBang = true
  25. end
  26. file.each do |line|
  27. # replace old header in script
  28. if file.lineno == 1 && foundSheBang && foundHeader
  29. t_file.puts header
  30. # insert new header
  31. elsif file.lineno == 1 && !foundSheBang && foundHeader
  32. t_file.puts header
  33. # insert new header
  34. elsif file.lineno == 1 && !foundSheBang && !foundHeader
  35. t_file.puts header
  36. t_file.puts line + "\n"
  37. # insert new header into script
  38. elsif file.lineno == 1 && foundSheBang && !foundHeader
  39. t_file.puts line
  40. t_file.puts header + "\n"
  41. # strip lines
  42. else
  43. t_file.puts line.rstrip
  44. end
  45. end
  46. # rename file
  47. t_file.rewind
  48. t_file.close
  49. FileUtils.cp(t_file.path, fileName)
  50. t_file.unlink
  51. # beautify ruby file
  52. if !isCoffee
  53. t_file = RBeautify.beautify_file(fileName)
  54. end
  55. end
  56. #folder array
  57. folder = ['app/assets/javascripts/app','app/controllers/', 'app/models/', 'app/helpers/', 'app/mailers/' ]
  58. folder.each do |folder|
  59. puts 'Working on folder' + folder.to_s
  60. rbfiles = File.join("../#{folder}**", "*.{rb,coffee}")
  61. d = Dir.glob(rbfiles)
  62. d.each {|fileName|
  63. puts "Working on #{fileName}"
  64. #check if file header is present
  65. checkForHeader(fileName)
  66. }
  67. end