@@ -24,46 +24,52 @@ def initialize(projects, options, branch_config, branch_name)
2424 def get_pmd_binary_file
2525 logger . info "#{ @pmd_branch_name } : Start packaging PMD"
2626 Dir . chdir ( @local_git_repo ) do
27- current_head_sha = Cmd . execute ( 'git rev-parse HEAD' )
28- current_branch_sha = Cmd . execute ( "git rev-parse #{ @pmd_branch_name } " )
27+ build_branch_sha = Cmd . execute ( "git rev-parse #{ @pmd_branch_name } ^{commit}" )
2928
30- @pmd_version = determine_pmd_version
29+ checkout_build_branch # needs a clean working tree, otherwise fails
3130
32- # in case we are already on the correct branch
33- # and a binary zip already exists...
34- if current_head_sha == current_branch_sha &&
35- File . exist? ( "pmd-dist/target/pmd-bin-#{ @pmd_version } .zip" )
36- logger . warn "#{ @pmd_branch_name } : Skipping packaging - zip for " \
37- "#{ @pmd_version } already exists"
31+ raise "Wrong branch #{ get_last_commit_sha } " unless build_branch_sha == get_last_commit_sha
32+
33+ logger . debug "#{ @pmd_branch_name } : PMD Version is #{ @pmd_version } " \
34+ "(sha=#{ build_branch_sha } )"
35+ distro_path = saved_distro_path ( build_branch_sha )
36+ if File . directory? ( distro_path )
37+ logger . info "#{ @pmd_branch_name } : Skipping packaging - saved version exists " \
38+ " in #{ distro_path } "
3839 else
39- build_pmd
40+ build_pmd ( into_dir : distro_path )
4041 end
4142
42- @pmd_branch_details . branch_last_sha = get_last_commit_sha
43+ # we're still on the build branch
44+ @pmd_branch_details . branch_last_sha = build_branch_sha
4345 @pmd_branch_details . branch_last_message = get_last_commit_message
44-
45- logger . info "#{ @pmd_branch_name } : Extracting the zip"
46- unzip_cmd = "unzip -qo pmd-dist/target/pmd-bin-#{ @pmd_version } .zip -d #{ @pwd } /target"
47- Cmd . execute ( unzip_cmd )
4846 end
4947 logger . info "#{ @pmd_branch_name } : Packaging PMD completed"
5048 end
5149
52- def build_pmd
53- logger . info "#{ @pmd_branch_name } : Checking out the branch"
54- checkout_cmd = "git checkout #{ @pmd_branch_name } "
55- Cmd . execute ( checkout_cmd )
56-
57- # determine the version again - it might be different in the other branch
58- @pmd_version = determine_pmd_version
50+ # builds pmd on currently checked out branch
51+ def build_pmd ( into_dir :)
52+ # in CI there might have been a build performed already. In that case
53+ # we reuse the build result, otherwise we build PMD freshly
54+ pmd_dist_target = "pmd-dist/target/pmd-bin-#{ @pmd_version } .zip"
55+ if File . exist? ( "#{ @local_git_repo } /#{ pmd_dist_target } " )
56+ # that's a warning, because we don't know, whether this build really
57+ # belongs to the current branch or whether it's from a previous branch.
58+ # In CI, that's not a problem, because the workspace is always fresh.
59+ logger . warn "#{ @pmd_branch_name } : Reusing already existing #{ pmd_dist_target } "
60+ else
61+ logger . info "#{ @pmd_branch_name } : Building PMD #{ @pmd_version } ..."
62+ package_cmd = './mvnw clean package' \
63+ ' -Dmaven.test.skip=true' \
64+ ' -Dmaven.javadoc.skip=true' \
65+ ' -Dmaven.source.skip=true' \
66+ ' -Dcheckstyle.skip=true'
67+ Cmd . execute ( package_cmd )
68+ end
5969
60- logger . info "#{ @pmd_branch_name } : Building PMD #{ @pmd_version } ..."
61- package_cmd = './mvnw clean package' \
62- ' -Dmaven.test.skip=true' \
63- ' -Dmaven.javadoc.skip=true' \
64- ' -Dmaven.source.skip=true' \
65- ' -Dcheckstyle.skip=true'
66- Cmd . execute ( package_cmd )
70+ logger . info "#{ @pmd_branch_name } : Extracting the zip"
71+ Cmd . execute ( "unzip -qo #{ pmd_dist_target } -d pmd-dist/target/exploded" )
72+ Cmd . execute ( "mv pmd-dist/target/exploded/pmd-bin-#{ @pmd_version } #{ into_dir } " )
6773 end
6874
6975 def determine_pmd_version
@@ -73,7 +79,7 @@ def determine_pmd_version
7379 end
7480
7581 def get_last_commit_sha
76- get_last_commit_sha_cmd = 'git rev-parse HEAD'
82+ get_last_commit_sha_cmd = 'git rev-parse HEAD^{commit} '
7783 Cmd . execute ( get_last_commit_sha_cmd )
7884 end
7985
@@ -84,7 +90,7 @@ def get_last_commit_message
8490
8591 def generate_pmd_report ( project )
8692 error_recovery_options = @error_recovery ? 'PMD_JAVA_OPTS="-Dpmd.error_recovery -ea" ' : ''
87- run_path = "target/pmd-bin- #{ @pmd_version } /bin/run.sh"
93+ run_path = "#{ saved_distro_path ( @pmd_branch_details . branch_last_sha ) } /bin/run.sh"
8894 pmd_cmd = "#{ error_recovery_options } " \
8995 "#{ run_path } pmd -d #{ project . local_source_path } -f xml " \
9096 "-R #{ project . get_config_path ( @pmd_branch_name ) } " \
@@ -149,5 +155,39 @@ def build
149155 get_pmd_binary_file
150156 generate_pmd_reports
151157 end
158+
159+ private
160+
161+ def checkout_build_branch
162+ logger . info "#{ @pmd_branch_name } : Checking out the branch"
163+ # note that this would fail if the tree is dirty
164+ Cmd . execute ( "git checkout #{ @pmd_branch_name } " )
165+
166+ # determine the version
167+ @pmd_version = determine_pmd_version
168+
169+ return unless wd_has_dirty_git_changes
170+
171+ # working dir is dirty....
172+ # we don't allow this because we need the SHA to address the zip file
173+ logger . error "#{ @pmd_branch_name } : Won\' t build without a clean working tree, " \
174+ 'commit your changes'
175+ end
176+
177+ def work_dir
178+ "#{ @pwd } /target"
179+ end
180+
181+ # path to the unzipped distribution
182+ # e.g. <cwd>/pmd-bin-<version>-<branch>-<sha>
183+ def saved_distro_path ( build_sha )
184+ "#{ work_dir } /pmd-bin-#{ @pmd_version } " \
185+ "-#{ PmdBranchDetail . branch_filename ( @pmd_branch_name ) } " \
186+ "-#{ build_sha } "
187+ end
188+
189+ def wd_has_dirty_git_changes
190+ !Cmd . execute ( 'git status --porcelain' ) . empty?
191+ end
152192 end
153193end
0 commit comments