|
| 1 | +#!/usr/bin/env ruby |
| 2 | + |
| 3 | +require 'etc' |
| 4 | +require 'fileutils' |
| 5 | +require 'find' |
| 6 | +require 'optparse' |
| 7 | + |
| 8 | +# arraw of source subfolders to exclude |
| 9 | +excludedFolders = [] |
| 10 | +extensionsToProcess = [] |
| 11 | +coveralls_cmd = "cpp-coveralls" |
| 12 | + |
| 13 | +excludeHeaders = false |
| 14 | + |
| 15 | +# create option parser |
| 16 | +opts = OptionParser.new |
| 17 | +opts.banner = "Usage: coveralls.rb [options]" |
| 18 | + |
| 19 | +opts.on('-e', '--exclude-folder FOLDER', 'Folder to exclude') do |v| |
| 20 | + excludedFolders << v |
| 21 | + coveralls_cmd.concat(" -e #{v}") |
| 22 | +end |
| 23 | + |
| 24 | +opts.on('-h', '--exclude-headers', 'Ignores headers') do |v| |
| 25 | + excludeHeaders = true |
| 26 | +end |
| 27 | + |
| 28 | +opts.on('-x', '--extension EXT', 'Source file extension to process') do |v| |
| 29 | + extensionsToProcess << v |
| 30 | + coveralls_cmd.concat(" -x #{v}") |
| 31 | +end |
| 32 | + |
| 33 | +opts.on_tail("-?", "--help", "Show this message") do |
| 34 | + puts opts |
| 35 | + exit |
| 36 | +end |
| 37 | + |
| 38 | +# parse the options |
| 39 | +begin |
| 40 | + opts.parse!(ARGV) |
| 41 | +rescue OptionParser::InvalidOption => e |
| 42 | + puts e |
| 43 | + puts opts |
| 44 | + exit(1) |
| 45 | +end |
| 46 | + |
| 47 | +# the folders |
| 48 | +workingDir = Dir.getwd |
| 49 | +derivedDataDir = "#{Etc.getpwuid.dir}/Library/Developer/Xcode/DerivedData/" |
| 50 | +outputDir = workingDir + "/gcov" |
| 51 | + |
| 52 | +# create gcov output folder |
| 53 | +FileUtils.mkdir outputDir |
| 54 | + |
| 55 | +# pattern to get source file from first line of gcov file |
| 56 | +GCOV_SOURCE_PATTERN = Regexp.new(/Source:(.*)/) |
| 57 | + |
| 58 | +# enumerate all gcda files underneath derivedData |
| 59 | +Find.find(derivedDataDir) do |gcda_file| |
| 60 | + |
| 61 | + if gcda_file.match(/\.gcda\Z/) |
| 62 | + |
| 63 | + #get just the folder name |
| 64 | + gcov_dir = File.dirname(gcda_file) |
| 65 | + |
| 66 | + # cut off absolute working dir to get relative source path |
| 67 | + relative_input_path = gcda_file.slice(derivedDataDir.length, gcda_file.length) |
| 68 | + puts "\nINPUT: #{relative_input_path}" |
| 69 | + |
| 70 | + #process the file |
| 71 | + result = %x( gcov '#{gcda_file}' -o '#{gcov_dir}' ) |
| 72 | + |
| 73 | + # filter the resulting output |
| 74 | + Dir.glob("*.gcov") do |gcov_file| |
| 75 | + |
| 76 | + firstLine = File.open(gcov_file).readline |
| 77 | + match = GCOV_SOURCE_PATTERN.match(firstLine) |
| 78 | + |
| 79 | + if (match) |
| 80 | + |
| 81 | + source_path = match[1] |
| 82 | + |
| 83 | + puts "source: #{source_path} - #{workingDir}" |
| 84 | + |
| 85 | + if (source_path.start_with? workingDir) |
| 86 | + |
| 87 | + # cut off absolute working dir to get relative source path |
| 88 | + relative_path = source_path.slice(workingDir.length+1, source_path.length) |
| 89 | + |
| 90 | + extension = File.extname(relative_path) |
| 91 | + extension = extension.slice(1, extension.length-1) |
| 92 | + |
| 93 | + puts "#{extension}" |
| 94 | + |
| 95 | + # get the path components |
| 96 | + path_comps = relative_path.split(File::SEPARATOR) |
| 97 | + |
| 98 | + shouldProcess = false |
| 99 | + exclusionMsg ="" |
| 100 | + |
| 101 | + if (excludedFolders.include?(path_comps[0])) |
| 102 | + exclusionMsg = "excluded via option" |
| 103 | + else |
| 104 | + if (excludeHeaders == true && extension == 'h') |
| 105 | + exclusionMsg = "excluded header" |
| 106 | + else |
| 107 | + if (extensionsToProcess.count == 0 || extensionsToProcess.include?(extension)) |
| 108 | + shouldProcess = true |
| 109 | + else |
| 110 | + exclusionMsg = "excluded extension" |
| 111 | + shouldProcess = false |
| 112 | + end |
| 113 | + end |
| 114 | + end |
| 115 | + |
| 116 | + if (shouldProcess) |
| 117 | + puts " - process: #{relative_path}" |
| 118 | + FileUtils.mv(gcov_file, outputDir) |
| 119 | + else |
| 120 | + puts " - ignore: #{relative_path} (#{exclusionMsg})" |
| 121 | + FileUtils.rm gcov_file |
| 122 | + end |
| 123 | + else |
| 124 | + puts " - ignore: #{gcov_file} (outside source folder)" |
| 125 | + FileUtils.rm gcov_file |
| 126 | + end |
| 127 | + end |
| 128 | + end |
| 129 | + end |
| 130 | +end |
| 131 | + |
| 132 | +#call the coveralls, exclude some files |
| 133 | +system coveralls_cmd |
| 134 | + |
| 135 | +#clean up |
| 136 | +FileUtils.rm_rf outputDir |
0 commit comments