diff --git a/lib/gcovinator.rb b/lib/gcovinator.rb index 408851b..00fdb19 100644 --- a/lib/gcovinator.rb +++ b/lib/gcovinator.rb @@ -12,7 +12,7 @@ module Gcovinator class << self - def run(build_dir, source_dirs, files, output_dir) + def run(build_dir, source_dirs, files, output_dir, prefix) build_dir = Pathname.new(File.expand_path(build_dir)).cleanpath.to_s source_dirs = ["."] if source_dirs.empty? source_dirs = source_dirs.map do |s| @@ -23,6 +23,7 @@ module Gcovinator Pathname.new(File.expand_path(f)).cleanpath.to_s end output_dir = Pathname.new(File.expand_path(output_dir)).cleanpath.to_s + prefix = Pathname.new(File.expand_path(prefix)).cleanpath.to_s file_coverages = {} Dir.mktmpdir do |dir| Dir.chdir(dir) do @@ -38,7 +39,7 @@ module Gcovinator end FileUtils.mkdir_p(output_dir) file_reports = file_coverages.each_with_index.map do |(source_file_name, file_coverage), i| - FileReport.new(source_file_name, file_coverage, source_dirs, output_dir, sprintf("s%04d.html", i)) + FileReport.new(source_file_name, file_coverage, prefix, output_dir, sprintf("s%04d.html", i)) end IndexReport.new(output_dir, file_reports) end diff --git a/lib/gcovinator/cli.rb b/lib/gcovinator/cli.rb index b1a530c..89b34f2 100644 --- a/lib/gcovinator/cli.rb +++ b/lib/gcovinator/cli.rb @@ -11,6 +11,7 @@ module Gcovinator build_dir = "." source_dirs = [] output_dir = "coverage" + prefix = "." OptionParser.new do |opts| @@ -35,6 +36,10 @@ module Gcovinator output_dir = o end + opts.on("-p PREFIX", "--prefix PREFIX", "Prefix path to strip from source file paths. Defaults to '.' if not specified.") do |p| + prefix = p + end + opts.on("-s SRCDIR", "--source-dir SRCDIR", "Specify a source directory. Reports will only be generated for sources under a specified source directory. Multiple source directories may be specified. Defaults to '.' if not specified.") do |s| source_dirs << s end @@ -46,7 +51,7 @@ module Gcovinator end.parse!(argv) - Gcovinator.run(build_dir, source_dirs, argv, output_dir) + Gcovinator.run(build_dir, source_dirs, argv, output_dir, prefix) end end diff --git a/lib/gcovinator/file_report.rb b/lib/gcovinator/file_report.rb index 1a14571..37475d6 100644 --- a/lib/gcovinator/file_report.rb +++ b/lib/gcovinator/file_report.rb @@ -8,8 +8,8 @@ module Gcovinator attr_reader :source_file_name attr_reader :report_file_name - def initialize(source_file_name, file_coverage, source_dirs, output_dir, report_file_name) - @source_file_name = clean_source_file_name(source_file_name, source_dirs) + def initialize(source_file_name, file_coverage, prefix, output_dir, report_file_name) + @source_file_name = clean_source_file_name(source_file_name, prefix) @report_file_name = report_file_name @total_lines = 0 @covered_lines = 0 @@ -20,12 +20,10 @@ module Gcovinator private - def clean_source_file_name(source_file_name, source_dirs) - source_dirs.each do |source_dir| - prefix_test = "#{source_dir}/" - if source_file_name.start_with?(prefix_test) - return source_file_name[prefix_test.size, source_file_name.size] - end + def clean_source_file_name(source_file_name, prefix) + prefix_test = "#{prefix}/" + if source_file_name.start_with?(prefix_test) + return source_file_name[prefix_test.size, source_file_name.size] end source_file_name end