lint-up.rb 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. ## Script from https://github.com/tir38/android-lint-entropy-reducer at 07.05.2017
  2. # adapts to drone, use git username / token as parameter
  3. # TODO cleanup this script, it has a lot of unused stuff
  4. Encoding.default_external = Encoding::UTF_8
  5. Encoding.default_internal = Encoding::UTF_8
  6. puts "=================== starting Android Lint Entropy Reducer ===================="
  7. # ======================== SETUP ============================
  8. # User name for git commits made by this script.
  9. TRAVIS_GIT_USERNAME = String.new("Drone CI server")
  10. # File name and relative path of generated Lint report. Must match build.gradle file:
  11. # lintOptions {
  12. # htmlOutput file("[FILE_NAME].html")
  13. # }
  14. LINT_REPORT_FILE = String.new("app/build/reports/lint/lint.html")
  15. # File name and relative path of previous results of this script.
  16. PREVIOUS_LINT_RESULTS_FILE=String.new("scripts/analysis/lint-results.txt")
  17. # Flag to evaluate warnings. true = check warnings; false = ignore warnings
  18. CHECK_WARNINGS = true
  19. # File name and relative path to custom lint rules; Can be null or "".
  20. CUSTOM_LINT_FILE = String.new("")
  21. # ================ SETUP DONE; DON'T TOUCH ANYTHING BELOW ================
  22. require 'fileutils'
  23. require 'pathname'
  24. require 'open3'
  25. # since we need the xml-simple gem, and we want this script self-contained, let's grab it just when we need it
  26. begin
  27. gem "xml-simple"
  28. rescue LoadError
  29. system("gem install --user-install xml-simple")
  30. Gem.clear_paths
  31. end
  32. require 'xmlsimple'
  33. # add custom Lint jar
  34. if !CUSTOM_LINT_FILE.nil? &&
  35. CUSTOM_LINT_FILE.length > 0
  36. ENV["ANDROID_LINT_JARS"] = Dir.pwd + "/" + CUSTOM_LINT_FILE
  37. puts "adding custom lint rules to default set: "
  38. puts ENV["ANDROID_LINT_JARS"]
  39. end
  40. # run Lint
  41. puts "running Lint..."
  42. system './gradlew clean lintGplayDebug 1>/dev/null'
  43. # confirm that Lint ran w/out error
  44. result = $?.to_i
  45. if result != 0
  46. puts "FAIL: failed to run ./gradlew clean lintGplayDebug"
  47. exit 1
  48. end
  49. # find Lint report file
  50. lint_reports = Dir.glob(LINT_REPORT_FILE)
  51. if lint_reports.length == 0
  52. puts "Lint HTML report not found."
  53. exit 1
  54. end
  55. lint_report = String.new(lint_reports[0])
  56. # find error/warning count string in HTML report
  57. error_warning_string = ""
  58. File.open lint_report do |file|
  59. error_warning_string = file.find { |line| line =~ /([0-9]* error[s]? and )?[0-9]* warning[s]?/ }
  60. end
  61. # find number of errors
  62. error_string = error_warning_string.match(/[0-9]* error[s]?/)
  63. if (error_string.nil?)
  64. current_error_count = 0
  65. else
  66. current_error_count = error_string[0].match(/[0-9]*/)[0].to_i
  67. end
  68. puts "found errors: " + current_error_count.to_s
  69. # find number of warnings
  70. if CHECK_WARNINGS == true
  71. warning_string = error_warning_string.match(/[0-9]* warning[s]?/)[0]
  72. current_warning_count = warning_string.match(/[0-9]*/)[0].to_i
  73. puts "found warnings: " + current_warning_count.to_s
  74. end
  75. # get previous error and warning counts from last successful build
  76. previous_results = false
  77. previous_lint_reports = Dir.glob(PREVIOUS_LINT_RESULTS_FILE)
  78. if previous_lint_reports.nil? ||
  79. previous_lint_reports.length == 0
  80. previous_lint_report = File.new(PREVIOUS_LINT_RESULTS_FILE, "w") # create for writing to later
  81. else
  82. previous_lint_report = String.new(previous_lint_reports[0])
  83. previous_error_warning_string = ""
  84. File.open previous_lint_report do |file|
  85. previous_error_warning_string = file.find { |line| line =~ /([0-9]* error[s]? and )?[0-9]* warning[s]?/ }
  86. end
  87. unless previous_error_warning_string.nil?
  88. previous_results = true
  89. previous_error_string = previous_error_warning_string.match(/[0-9]* error[s]?/)
  90. if previous_error_string.nil?
  91. previous_error_string = "0 errors"
  92. else
  93. previous_error_string = previous_error_string[0]
  94. end
  95. previous_error_count = previous_error_string.match(/[0-9]*/)[0].to_i
  96. puts "previous errors: " + previous_error_count.to_s
  97. if CHECK_WARNINGS == true
  98. previous_warning_string = previous_error_warning_string.match(/[0-9]* warning[s]?/)
  99. if previous_warning_string.nil?
  100. previous_warning_string = "0 warnings"
  101. else
  102. previous_warning_string = previous_warning_string[0]
  103. end
  104. previous_warning_count = previous_warning_string.match(/[0-9]*/)[0].to_i
  105. puts "previous warnings: " + previous_warning_count.to_s
  106. end
  107. end
  108. end
  109. # compare previous error count with current error count
  110. if previous_results == true &&
  111. current_error_count > previous_error_count
  112. puts "FAIL: error count increased"
  113. exit 1
  114. end
  115. # compare previous warning count with current warning count
  116. if CHECK_WARNINGS == true &&
  117. previous_results == true &&
  118. current_warning_count > previous_warning_count
  119. puts "FAIL: warning count increased"
  120. exit 1
  121. end
  122. # check if warning and error count stayed the same
  123. if previous_results == true &&
  124. current_error_count == previous_error_count &&
  125. current_warning_count == previous_warning_count
  126. puts "SUCCESS: count stayed the same"
  127. exit 2
  128. end
  129. # either error count or warning count DECREASED
  130. # write new results to file (will overwrite existing, or create new)
  131. File.write(previous_lint_report, "DO NOT TOUCH; GENERATED BY DRONE\n" + error_warning_string)
  132. # update git user name and email for this script
  133. system ("git config --local user.name 'github-actions'")
  134. system ("git config --local user.email 'github-actions@github.com'")
  135. # add previous Lint result file to git
  136. system ('git add ' + PREVIOUS_LINT_RESULTS_FILE)
  137. # commit changes
  138. system('git commit -sm "Analysis: update lint results to reflect reduced error/warning count"')
  139. # push to origin
  140. system ('git push')
  141. puts "SUCCESS: count was reduced"
  142. exit 0 # success