Class: Slather::Project

Inherits:
Xcodeproj::Project show all
Defined in:
lib/slather/project.rb

Constant Summary collapse

@@ymlfile =
'.slather.yml'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Xcodeproj::Project

#slather_setup_for_coverage

Instance Attribute Details

#archObject

Returns the value of attribute arch.



46
47
48
# File 'lib/slather/project.rb', line 46

def arch
  @arch
end

#binary_basenameObject

Returns the value of attribute binary_basename.



46
47
48
# File 'lib/slather/project.rb', line 46

def binary_basename
  @binary_basename
end

#binary_fileObject

Returns the value of attribute binary_file.



46
47
48
# File 'lib/slather/project.rb', line 46

def binary_file
  @binary_file
end

#build_directoryObject

Returns the value of attribute build_directory.



46
47
48
# File 'lib/slather/project.rb', line 46

def build_directory
  @build_directory
end

#cdn_assetsObject

Returns the value of attribute cdn_assets.



46
47
48
# File 'lib/slather/project.rb', line 46

def cdn_assets
  @cdn_assets
end

#ci_serviceObject

Returns the value of attribute ci_service.



46
47
48
# File 'lib/slather/project.rb', line 46

def ci_service
  @ci_service
end

#configurationObject

Returns the value of attribute configuration.



46
47
48
# File 'lib/slather/project.rb', line 46

def configuration
  @configuration
end

#coverage_access_tokenObject

Returns the value of attribute coverage_access_token.



46
47
48
# File 'lib/slather/project.rb', line 46

def coverage_access_token
  @coverage_access_token
end

#coverage_serviceObject

Returns the value of attribute coverage_service.



46
47
48
# File 'lib/slather/project.rb', line 46

def coverage_service
  @coverage_service
end

#decimalsObject

Returns the value of attribute decimals.



46
47
48
# File 'lib/slather/project.rb', line 46

def decimals
  @decimals
end

#ignore_listObject

Returns the value of attribute ignore_list.



46
47
48
# File 'lib/slather/project.rb', line 46

def ignore_list
  @ignore_list
end

#input_formatObject

Returns the value of attribute input_format.



46
47
48
# File 'lib/slather/project.rb', line 46

def input_format
  @input_format
end

#llvm_versionObject

Returns the value of attribute llvm_version.



46
47
48
# File 'lib/slather/project.rb', line 46

def llvm_version
  @llvm_version
end

#output_directoryObject

Returns the value of attribute output_directory.



46
47
48
# File 'lib/slather/project.rb', line 46

def output_directory
  @output_directory
end

#schemeObject

Returns the value of attribute scheme.



46
47
48
# File 'lib/slather/project.rb', line 46

def scheme
  @scheme
end

#show_htmlObject

Returns the value of attribute show_html.



46
47
48
# File 'lib/slather/project.rb', line 46

def show_html
  @show_html
end

#source_directoryObject

Returns the value of attribute source_directory.



46
47
48
# File 'lib/slather/project.rb', line 46

def source_directory
  @source_directory
end

#source_filesObject

Returns the value of attribute source_files.



46
47
48
# File 'lib/slather/project.rb', line 46

def source_files
  @source_files
end

#verbose_modeObject

Returns the value of attribute verbose_mode.



46
47
48
# File 'lib/slather/project.rb', line 46

def verbose_mode
  @verbose_mode
end

#workspaceObject

Returns the value of attribute workspace.



46
47
48
# File 'lib/slather/project.rb', line 46

def workspace
  @workspace
end

#xcodeprojObject

Returns the value of attribute xcodeproj.



46
47
48
# File 'lib/slather/project.rb', line 46

def xcodeproj
  @xcodeproj
end

Class Method Details

.open(xcodeproj) ⇒ Object



52
53
54
55
56
# File 'lib/slather/project.rb', line 52

def self.open(xcodeproj)
  proj = super
  proj.xcodeproj = xcodeproj
  proj
end

.ymlObject



324
325
326
# File 'lib/slather/project.rb', line 324

def self.yml
  @yml ||= File.exist?(yml_filename) ? YAML.load_file(yml_filename) : {}
end

.yml_filenameObject



320
321
322
# File 'lib/slather/project.rb', line 320

def self.yml_filename
  @@ymlfile
end

.yml_filename=(var) ⇒ Object



316
317
318
# File 'lib/slather/project.rb', line 316

def self.yml_filename=(var)
  @@ymlfile = var
end

Instance Method Details

#configureObject



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/slather/project.rb', line 328

def configure
  begin
    configure_scheme
    configure_configuration
    configure_workspace
    configure_build_directory
    configure_ignore_list
    configure_ci_service
    configure_coverage_access_token
    configure_coverage_service
    configure_source_directory
    configure_output_directory
    configure_input_format
    configure_arch
    configure_binary_file
    configure_decimals

    self.llvm_version = `xcrun llvm-cov --version`.match(/LLVM version ([\d\.]+)/).captures[0]
  rescue => e
    puts e.message
    puts failure_help_string
    puts "\n"
    raise
  end

  if self.verbose_mode
    puts "\nProcessing coverage file: #{profdata_file}"
    if self.binary_file
      puts "Against binary files:"
      self.binary_file.each do |binary_file|
        puts "\t#{binary_file}"
      end
    else
      puts "No binary files found."
    end
    puts "\n"
  end
end

#configure_archObject



466
467
468
# File 'lib/slather/project.rb', line 466

def configure_arch
  self.arch ||= self.class.yml["arch"] if self.class.yml["arch"]
end

#configure_binary_fileObject



460
461
462
463
464
# File 'lib/slather/project.rb', line 460

def configure_binary_file
  if self.input_format == "profdata"
    self.binary_file = load_option_array("binary_file") || find_binary_files
  end
end

#configure_build_directoryObject



367
368
369
# File 'lib/slather/project.rb', line 367

def configure_build_directory
  self.build_directory ||= self.class.yml["build_directory"] || derived_data_path
end

#configure_ci_serviceObject



383
384
385
# File 'lib/slather/project.rb', line 383

def configure_ci_service
  self.ci_service ||= (ENV["CI_SERVICE"] || self.class.yml["ci_service"] || :other)
end

#configure_configurationObject



407
408
409
# File 'lib/slather/project.rb', line 407

def configure_configuration
  self.configuration ||= self.class.yml["configuration"] if self.class.yml["configuration"]
end

#configure_coverage_access_tokenObject



429
430
431
# File 'lib/slather/project.rb', line 429

def configure_coverage_access_token
  self.coverage_access_token ||= (ENV["COVERAGE_ACCESS_TOKEN"] || self.class.yml["coverage_access_token"] || "")
end

#configure_coverage_serviceObject



425
426
427
# File 'lib/slather/project.rb', line 425

def configure_coverage_service
  self.coverage_service ||= (self.class.yml["coverage_service"] || :terminal)
end

#configure_decimalsObject



411
412
413
414
415
# File 'lib/slather/project.rb', line 411

def configure_decimals
  return if self.decimals
  self.decimals ||= self.class.yml["decimals"] if self.class.yml["decimals"]
  self.decimals = self.decimals ? Integer(self.decimals) : 2
end

#configure_ignore_listObject



379
380
381
# File 'lib/slather/project.rb', line 379

def configure_ignore_list
  self.ignore_list ||= [(self.class.yml["ignore"] || [])].flatten
end

#configure_input_formatObject



387
388
389
# File 'lib/slather/project.rb', line 387

def configure_input_format
  self.input_format ||= (self.class.yml["input_format"] || "auto")
end

#configure_output_directoryObject



375
376
377
# File 'lib/slather/project.rb', line 375

def configure_output_directory
  self.output_directory ||= self.class.yml["output_directory"] if self.class.yml["output_directory"]
end

#configure_schemeObject



403
404
405
# File 'lib/slather/project.rb', line 403

def configure_scheme
  self.scheme ||= self.class.yml["scheme"] if self.class.yml["scheme"]
end

#configure_source_directoryObject



371
372
373
# File 'lib/slather/project.rb', line 371

def configure_source_directory
  self.source_directory ||= self.class.yml["source_directory"] if self.class.yml["source_directory"]
end

#configure_workspaceObject



417
418
419
# File 'lib/slather/project.rb', line 417

def configure_workspace
  self.workspace ||= self.class.yml["workspace"] if self.class.yml["workspace"]
end

#coverage_filesObject



96
97
98
99
100
101
102
# File 'lib/slather/project.rb', line 96

def coverage_files
  if self.input_format == "profdata"
    profdata_coverage_files
  else
    gcov_coverage_files
  end
end

#decimal_f(decimal_arg) ⇒ Object



470
471
472
473
474
475
# File 'lib/slather/project.rb', line 470

def decimal_f decimal_arg
  configure_decimals unless decimals
  decimal = "%.#{decimals}f" % decimal_arg
  return decimal if decimals == 2 # special case 2 for backwards compatibility
  decimal.to_f.to_s
end

#failure_help_stringObject



58
59
60
# File 'lib/slather/project.rb', line 58

def failure_help_string
  "\n\tAre you sure your project is generating coverage? Make sure you enable code coverage in the Test section of your Xcode scheme.\n\tDid you specify your Xcode scheme? (--scheme or 'scheme' in .slather.yml)\n\tIf you're using a workspace, did you specify it? (--workspace or 'workspace' in .slather.yml)\n\tIf you use a different Xcode configuration, did you specify it? (--configuration or 'configuration' in .slather.yml)"
end

#find_binary_file_in_bundle(bundle_file) ⇒ Object



477
478
479
480
481
482
483
484
# File 'lib/slather/project.rb', line 477

def find_binary_file_in_bundle(bundle_file)
  if File.directory? bundle_file
    bundle_file_noext = File.basename(bundle_file, File.extname(bundle_file))
    Dir["#{bundle_file}/**/#{bundle_file_noext}"].first
  else
    bundle_file
  end
end

#find_binary_filesObject

Raises:

  • (StandardError)


486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
# File 'lib/slather/project.rb', line 486

def find_binary_files
  binary_basename = load_option_array("binary_basename")
  found_binaries = []

  # Get scheme info out of the xcodeproj
  if self.scheme
    schemes_path = Xcodeproj::XCScheme.shared_data_dir(self.path)
    xcscheme_path = "#{schemes_path + self.scheme}.xcscheme"

    # Try to look inside 'xcuserdata' if the scheme is not found in 'xcshareddata'
    if !File.file?(xcscheme_path)
      schemes_path = Xcodeproj::XCScheme.user_data_dir(self.path)
      xcscheme_path = "#{schemes_path + self.scheme}.xcscheme"
    end

    if self.workspace and !File.file?(xcscheme_path)
      # No scheme was found in the xcodeproj, check the workspace
      schemes_path = Xcodeproj::XCScheme.shared_data_dir(self.workspace)
      xcscheme_path = "#{schemes_path + self.scheme}.xcscheme"

      if !File.file?(xcscheme_path)
        schemes_path = Xcodeproj::XCScheme.user_data_dir(self.workspace)
        xcscheme_path = "#{schemes_path + self.scheme}.xcscheme"
      end
    end

    raise StandardError, "No scheme named '#{self.scheme}' found in #{self.path}" unless File.exist? xcscheme_path

    xcscheme = Xcodeproj::XCScheme.new(xcscheme_path)

    if self.configuration
      configuration = self.configuration
    else
      configuration = xcscheme.test_action.build_configuration
    end

    search_list = binary_basename || find_buildable_names(xcscheme)
    search_dir = profdata_coverage_dir

    if Slather.xcode_version[0] >= 9
      # Go from the directory containing Coverage.profdata back to the directory containing Products (back out of ProfileData/UUID-dir)
      search_dir = File.join(search_dir, '../..')
    end

    search_list.each do |search_for|
      found_product = Dir["#{search_dir}/Products/#{configuration}*/#{search_for}*"].sort { |x, y|
        # Sort the matches without the file extension to ensure better matches when there are multiple candidates
        # For example, if the binary_basename is Test then we want Test.app to be matched before Test Helper.app
        File.basename(x, File.extname(x)) <=> File.basename(y, File.extname(y))
      }.find { |path|
        next if path.end_with? ".dSYM"
        next if path.end_with? ".swiftmodule"

        if File.directory? path
          path = find_binary_file_in_bundle(path)
          next if path.nil?
        end

        matches_arch(path)
      }

      if found_product and File.directory? found_product
        found_binary = find_binary_file_in_bundle(found_product)
      else
        found_binary = found_product
      end

      if found_binary
        found_binaries.push(found_binary)
      end
    end
  else
    xctest_bundle = Dir["#{profdata_coverage_dir}/**/*.xctest"].reject { |bundle|
        # Ignore xctest bundles that are in the UI runner app
        bundle.include? "-Runner.app/PlugIns/"
    }.first

    # Find the matching binary file
    search_list = binary_basename || ['*']

    search_list.each do |search_for|
      xctest_bundle_file_directory = Pathname.new(xctest_bundle).dirname
      app_bundle = Dir["#{xctest_bundle_file_directory}/#{search_for}.app"].first
      matched_xctest_bundle = Dir["#{xctest_bundle_file_directory}/#{search_for}.xctest"].first
      dynamic_lib_bundle = Dir["#{xctest_bundle_file_directory}/#{search_for}.{framework,dylib}"].first

      if app_bundle != nil
        found_binary = find_binary_file_in_bundle(app_bundle)
      elsif matched_xctest_bundle != nil
        found_binary = find_binary_file_in_bundle(matched_xctest_bundle)
      elsif dynamic_lib_bundle != nil
        found_binary = find_binary_file_in_bundle(dynamic_lib_bundle)
      else
        found_binary = find_binary_file_in_bundle(xctest_bundle)
      end

      if found_binary
        found_binaries.push(found_binary)
      end
    end
  end

  raise StandardError, "No product binary found in #{profdata_coverage_dir}." unless found_binaries.count > 0

  found_binaries.map { |binary| File.expand_path(binary) }
end

#find_buildable_names(xcscheme) ⇒ Object



593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
# File 'lib/slather/project.rb', line 593

def find_buildable_names(xcscheme)
  found_buildable_names = []

  # enumerate code coverage targets
  begin
    code_coverage_targets = xcscheme.test_action.xml_element.elements['CodeCoverageTargets']
    targets = code_coverage_targets.map do |node|
      Xcodeproj::XCScheme::BuildableReference.new(node) if node.is_a?(REXML::Element)
    end.compact
    buildable_names = targets.each do |target|
      found_buildable_names.push(target.buildable_name)
    end
  rescue
    # just in case if there are no entries in the test action
  end

  # enumerate build action entries
  begin
    xcscheme.build_action.entries.each do |entry|
      buildable_name = entry.buildable_references[0].buildable_name

      if !buildable_name.end_with? ".a"
        # Can't run code coverage on static libraries
        found_buildable_names.push(buildable_name)
      end
    end
  rescue
    # xcodeproj will raise an exception if there are no entries in the build action
  end

  # enumerate test action entries
  begin
    xcscheme.test_action.testables.each do |entry|
      buildable_name = entry.buildable_references[0].buildable_name
      found_buildable_names.push(buildable_name)
    end
  rescue
    # just in case if there are no entries in the test action
  end

  # some items are both buildable and testable, so return only unique ones
  found_buildable_names.uniq
end

#find_source_filesObject



647
648
649
650
651
652
653
654
655
656
657
658
659
# File 'lib/slather/project.rb', line 647

def find_source_files
  source_files = load_option_array("source_files")
  return if source_files.nil?

  current_dir = Pathname("./").realpath
  paths = source_files.flat_map { |pattern| Dir.glob(pattern) }.uniq

  paths.map do |path|
    source_file_absolute_path = Pathname(path).realpath
    source_file_relative_path = source_file_absolute_path.relative_path_from(current_dir)
    self.ignore_list.any? { |ignore| File.fnmatch(ignore, source_file_relative_path) } ? nil : source_file_absolute_path
  end.compact
end

#first_product_nameObject



202
203
204
205
206
207
# File 'lib/slather/project.rb', line 202

def first_product_name
  first_product = self.products.first
  # If name is not available it computes it using
  # the path by dropping the 'extension' of the path.
  first_product.name || remove_extension(first_product.path)
end

#load_option_array(option) ⇒ Object



661
662
663
664
665
666
667
668
669
670
671
672
673
674
# File 'lib/slather/project.rb', line 661

def load_option_array(option)
  value = self.send(option.to_sym)
  # Only load if a value is not already set
  if !value
    value_yml = self.class.yml[option]
    # Need to check the type in the config file because it can be a string or array
    if value_yml and value_yml.is_a? Array
      value = value_yml
    elsif value_yml
      value = [value_yml]
    end
  end
  value
end

#matches_arch(binary_path) ⇒ Object



637
638
639
640
641
642
643
644
645
# File 'lib/slather/project.rb', line 637

def matches_arch(binary_path)
  if self.arch
    lipo_output = `lipo -info "#{binary_path}"`
    archs_in_binary = lipo_output.split(':').last.split(' ')
    archs_in_binary.include? self.arch
  else
    true
  end
end

#profdata_coverage_dirObject



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/slather/project.rb', line 209

def profdata_coverage_dir
  @profdata_coverage_dir ||= begin
    raise StandardError, "The specified build directory (#{self.build_directory}) does not exist" unless File.exist?(self.build_directory)
    dir = nil
    if self.scheme
      dir = Dir[File.join(build_directory,"/**/CodeCoverage/#{self.scheme}")].first
    else
      dir = Dir[File.join(build_directory,"/**/#{first_product_name}")].first
    end

    if dir == nil
      # Xcode 7.3 moved the location of Coverage.profdata
      dir = Dir[File.join(build_directory,"/**/CodeCoverage")].first
    end

    if dir == nil && Slather.xcode_version[0] >= 9
      # Xcode 9 moved the location of Coverage.profdata
      coverage_files = Dir[File.join(build_directory, "/**/ProfileData/*/Coverage.profdata")]

      if coverage_files.count == 0
        # Look up one directory
        # The ProfileData directory is next to Intermediates.noindex (in previous versions of Xcode the coverage was inside Intermediates)
        coverage_files = Dir[File.join(build_directory, "../**/ProfileData/*/Coverage.profdata")]
      end

      if coverage_files != nil && coverage_files.count != 0
        dir = Pathname.new(coverage_files.first).parent()
      end
    end

    raise StandardError, "No coverage directory found." unless dir != nil
    dir
  end
end

#remove_extension(path) ⇒ Object



198
199
200
# File 'lib/slather/project.rb', line 198

def remove_extension(path)
  path.split(".")[0..-2].join(".")
end