lint-up.rb 6.1 KB

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