Created
April 14, 2017 09:29
-
-
Save Liquidsoul/fb701513d131d82668fed5e79b6710a0 to your computer and use it in GitHub Desktop.
A fastlane action for `pmd cpd` to use for objective-C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Fastlane | |
module Actions | |
module SharedValues | |
PMD_CPD_OUTPUT_FILE = :PMD_CPD_OUTPUT_FILE | |
PMD_CPD_ISSUES_COUNT = :PMD_CPD_ISSUES_COUNT | |
end | |
class PmdCpdAction < Action | |
def self.run(params) | |
raise "'pmd' not found in your PATH. It can be installed using 'brew install pmd'".red unless (`which pmd` and $?.to_i == 0) | |
minimum_tokens=100 | |
language='ObjectiveC' | |
output_folder = params[:output_folder] || '' | |
require 'pathname' | |
output_path = Pathname.new(output_folder) + 'cpd_report.xml' | |
cpd_args = ["--encoding UTF-8", "--format net.sourceforge.pmd.cpd.XMLRenderer"] | |
cpd_args << "--minimum-tokens #{minimum_tokens}" | |
cpd_args << "--language #{language}" | |
cpd_args << params[:files].map { |e| "--files #{e}" } | |
command_prefix = [ | |
'cd', | |
File.expand_path('.').shellescape, | |
'&&' | |
].join(' ') | |
command = [ | |
command_prefix, | |
'pmd cpd', | |
cpd_args, | |
"> #{output_path}" | |
].join(' ') | |
return_hack = "; rvalue=$?; if [ $rvalue -eq 4 ]; then exit 0; else exit $rvalue; fi;" | |
command += return_hack | |
Action.sh command | |
self.count_issues(output_path) | |
Actions.lane_context[SharedValues::PMD_CPD_OUTPUT_FILE] = File.expand_path(output_path) | |
end | |
def self.count_issues(file) | |
issues_count = 0 | |
File.open(file).each do |line| | |
if line =~ /<duplication/ | |
issues_count += 1 | |
end | |
end | |
Actions.lane_context[SharedValues::PMD_CPD_ISSUES_COUNT] = issues_count | |
end | |
##################################################### | |
# @!group Documentation | |
##################################################### | |
def self.description | |
"Generate a duplication report for Objective-C code" | |
end | |
def self.available_options | |
[ | |
FastlaneCore::ConfigItem.new(key: :files, | |
env_name: "FL_PMD_CPD_PATHS", | |
description: "Array of paths to process", | |
is_string: false, | |
verify_block: proc do |paths| | |
raise "No file path for PmdCpdAction given, pass using `files: 'files'`".red unless (paths and not paths.empty?) | |
raise "'files' options must be an array".red unless paths.is_a?(Array) | |
for path in paths do | |
raise "Couldn't find file at path '#{path}'".red unless File.exist?(path) | |
end | |
end), | |
FastlaneCore::ConfigItem.new(key: :output_folder, | |
env_name: "FL_PMD_CPD_OUTPUT_FOLDER", | |
description: "Output folder path", | |
is_string: true, | |
optional: true, | |
verify_block: proc do |path| | |
raise "Invalid folder path for PmdCpdAction given, pass using `output_folder: 'folder_path'`".red unless (path and not path.empty?) | |
raise "Couldn't find folder at path '#{path}'".red unless File.exist?(path) | |
end), | |
] | |
end | |
def self.output | |
[ | |
['PMD_CPD_OUTPUT_FILE', 'The path to the report file'], | |
['PMD_CPD_ISSUES_COUNT', 'The number of issues found'], | |
] | |
end | |
def self.return_value | |
# If you method provides a return value, you can describe here what it does | |
end | |
def self.authors | |
# So no one will ever forget your contribution to fastlane :) You are awesome btw! | |
["Liquidsoul"] | |
end | |
def self.is_supported?(platform) | |
[:ios, :mac].include?(platform) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment