|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'nokogiri' |
| 4 | +require 'set' |
| 5 | +require_relative '../cmd' |
| 6 | +module PmdTester |
| 7 | + # This class is responsible for generation dynamic configuration |
| 8 | + # according to the difference between base and patch branch of Pmd. |
| 9 | + # Attention: we only consider java rulesets now. |
| 10 | + class RuleSetBuilder |
| 11 | + ALL_RULE_SETS = Set['bestpractices', 'codestyle', 'design', 'documentation', |
| 12 | + 'errorprone', 'multithreading', 'performance', 'security'].freeze |
| 13 | + PATH_TO_PMD_JAVA_BASED_RULES = |
| 14 | + 'pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule' |
| 15 | + PATH_TO_PMD_XPATH_BASED_RULES = 'pmd-java/src/main/resources/category/java' |
| 16 | + PATH_TO_ALL_JAVA_RULES = 'config/all-java.xml' |
| 17 | + PATH_TO_DYNAMIC_CONFIG = 'target/dynamic-config.xml' |
| 18 | + NO_JAVA_RULES_CHANGED_MESSAGE = 'No java rules have been changed!' |
| 19 | + |
| 20 | + def initialize(options) |
| 21 | + @options = options |
| 22 | + end |
| 23 | + |
| 24 | + def build |
| 25 | + filenames = diff_filenames |
| 26 | + rule_sets = get_rule_sets(filenames) |
| 27 | + output_filter_set(rule_sets) |
| 28 | + build_config_file(rule_sets) |
| 29 | + end |
| 30 | + |
| 31 | + def output_filter_set(rule_sets) |
| 32 | + if rule_sets == ALL_RULE_SETS |
| 33 | + # if `rule_sets` contains all rule sets, than no need to filter the baseline |
| 34 | + @options.filter_set = nil |
| 35 | + else |
| 36 | + @options.filter_set = rule_sets |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + def diff_filenames |
| 41 | + filenames = nil |
| 42 | + Dir.chdir(@options.local_git_repo) do |
| 43 | + base = @options.base_branch |
| 44 | + patch = @options.patch_branch |
| 45 | + # We only need to support git here, since PMD's repo is using git. |
| 46 | + diff_cmd = "git diff --name-only #{base}..#{patch} -- pmd/core pmd/java" |
| 47 | + filenames = Cmd.execute(diff_cmd) |
| 48 | + end |
| 49 | + filenames.split("\n") |
| 50 | + end |
| 51 | + |
| 52 | + def get_rule_sets(filenames) |
| 53 | + rule_sets = Set[] |
| 54 | + filenames.each do |filename| |
| 55 | + match_data = %r{#{PATH_TO_PMD_JAVA_BASED_RULES}/([^/]+)/[^/]+Rule.java}.match(filename) |
| 56 | + if match_data.nil? |
| 57 | + match_data = %r{#{PATH_TO_PMD_XPATH_BASED_RULES}/([^/]+).xml}.match(filename) |
| 58 | + end |
| 59 | + |
| 60 | + category = if match_data.nil? |
| 61 | + nil |
| 62 | + else |
| 63 | + match_data[1] |
| 64 | + end |
| 65 | + |
| 66 | + if category.nil? |
| 67 | + rule_sets = ALL_RULE_SETS |
| 68 | + break |
| 69 | + else |
| 70 | + rule_sets.add(category) |
| 71 | + end |
| 72 | + end |
| 73 | + rule_sets |
| 74 | + end |
| 75 | + |
| 76 | + def build_config_file(rule_sets) |
| 77 | + if rule_sets.empty? |
| 78 | + puts NO_JAVA_RULES_CHANGED_MESSAGE |
| 79 | + exit 0 |
| 80 | + end |
| 81 | + |
| 82 | + doc = Nokogiri::XML(File.read(PATH_TO_ALL_JAVA_RULES)) |
| 83 | + doc.search('rule').each do |rule| |
| 84 | + rule.remove unless match_ref?(rule, rule_sets) |
| 85 | + end |
| 86 | + |
| 87 | + description = doc.at_css('description') |
| 88 | + description.content = 'The ruleset generated by PmdTester dynamically' |
| 89 | + |
| 90 | + write_dynamic_file(doc) |
| 91 | + end |
| 92 | + |
| 93 | + def match_ref?(rule_node, rule_sets) |
| 94 | + rule_sets.each do |rule_set| |
| 95 | + return true unless rule_node['ref'].index(rule_set).nil? |
| 96 | + end |
| 97 | + |
| 98 | + false |
| 99 | + end |
| 100 | + |
| 101 | + def write_dynamic_file(doc) |
| 102 | + File.open(PATH_TO_DYNAMIC_CONFIG, 'w') do |x| |
| 103 | + x << doc.to_s.gsub(/\n\s+\n/, "\n") |
| 104 | + end |
| 105 | + @options.base_config = PATH_TO_DYNAMIC_CONFIG |
| 106 | + @options.patch_config = PATH_TO_DYNAMIC_CONFIG |
| 107 | + end |
| 108 | + end |
| 109 | +end |
0 commit comments