Class: Snapshot::TestCommandGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/snapshot/test_command_generator.rb

Overview

Responsible for building the fully working xcodebuild command

Class Method Summary collapse

Class Method Details

.actionsObject



49
50
51
52
53
54
55
56
# File 'lib/snapshot/test_command_generator.rb', line 49

def actions
  actions = []
  actions << :clean if Snapshot.config[:clean]
  actions << :build # https://github.com/fastlane/snapshot/issues/246
  actions << :test

  actions
end

.build_settingsObject



42
43
44
45
46
47
# File 'lib/snapshot/test_command_generator.rb', line 42

def build_settings
  build_settings = []
  build_settings << "FASTLANE_SNAPSHOT=YES"

  build_settings
end

.derived_data_pathObject



113
114
115
# File 'lib/snapshot/test_command_generator.rb', line 113

def derived_data_path
  Snapshot.cache[:derived_data_path] ||= (Snapshot.config[:derived_data_path] || Dir.mktmpdir("snapshot_derived"))
end

.destination(device_name) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/snapshot/test_command_generator.rb', line 89

def destination(device_name)
  os = device_name =~ /^Apple TV/ ? "tvOS" : "iOS"
  os_version = Snapshot.config[:ios_version] || Snapshot::LatestOsVersion.version(os)

  device = find_device(device_name, os_version)
  if device.nil?
    UI.user_error!("No device found named '#{device_name}' for version '#{os_version}'")
    return
  elsif device.os_version != os_version
    UI.important("Using device named '#{device_name}' with version '#{device.os_version}' because no match was found for version '#{os_version}'")
  end
  value = "platform=#{os} Simulator,id=#{device.udid},OS=#{os_version}"

  return ["-destination '#{value}'"]
end

.device_udid(device_name, os_version = ) ⇒ Object



83
84
85
86
87
# File 'lib/snapshot/test_command_generator.rb', line 83

def device_udid(device_name, os_version = Snapshot.config[:ios_version])
  device = find_device(device_name, os_version)

  device ? device.udid : nil
end

.find_device(device_name, os_version = ) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/snapshot/test_command_generator.rb', line 66

def find_device(device_name, os_version = Snapshot.config[:ios_version])
  # We might get this error message
  # > The requested device could not be found because multiple devices matched the request.
  #
  # This happens when you have multiple simulators for a given device type / iOS combination
  #   { platform:iOS Simulator, id:1685B071-AFB2-4DC1-BE29-8370BA4A6EBD, OS:9.0, name:iPhone 5 }
  #   { platform:iOS Simulator, id:A141F23B-96B3-491A-8949-813B376C28A7, OS:9.0, name:iPhone 5 }
  #
  simulators = FastlaneCore::DeviceManager.simulators
  # Sort devices with matching names by OS version, largest first, so that we can
  # pick the device with the newest OS in case an exact OS match is not available
  name_matches = simulators.find_all { |sim| sim.name.strip == device_name.strip }
                           .sort_by { |sim| Gem::Version.new(sim.os_version) }
                           .reverse
  name_matches.find { |sim| sim.os_version == os_version } || name_matches.first
end

.generate(device_type: nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/snapshot/test_command_generator.rb', line 5

def generate(device_type: nil)
  parts = prefix
  parts << "xcodebuild"
  parts += options
  parts += destination(device_type)
  parts += build_settings
  parts += actions
  parts += suffix
  parts += pipe

  parts
end

.optionsObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/snapshot/test_command_generator.rb', line 31

def options
  config = Snapshot.config

  options = []
  options += project_path_array
  options << "-sdk '#{config[:sdk]}'" if config[:sdk]
  options << "-derivedDataPath '#{derived_data_path}'"

  options
end

.pipeObject



62
63
64
# File 'lib/snapshot/test_command_generator.rb', line 62

def pipe
  ["| tee #{xcodebuild_log_path.shellescape} | xcpretty #{Snapshot.config[:xcpretty_args]}"]
end

.prefixObject



18
19
20
# File 'lib/snapshot/test_command_generator.rb', line 18

def prefix
  ["set -o pipefail &&"]
end

.project_path_arrayArray

Path to the project or workspace as parameter This will also include the scheme (if given)

Returns:

  • (Array)

    The array with all the components to join



25
26
27
28
29
# File 'lib/snapshot/test_command_generator.rb', line 25

def project_path_array
  proj = Snapshot.project.xcodebuild_parameters
  return proj if proj.count > 0
  UI.user_error!("No project/workspace found")
end

.suffixObject



58
59
60
# File 'lib/snapshot/test_command_generator.rb', line 58

def suffix
  []
end

.xcodebuild_log_pathObject



105
106
107
108
109
110
111
# File 'lib/snapshot/test_command_generator.rb', line 105

def xcodebuild_log_path
  file_name = "#{Snapshot.project.app_name}-#{Snapshot.config[:scheme]}.log"
  containing = File.expand_path(Snapshot.config[:buildlog_path])
  FileUtils.mkdir_p(containing)

  return File.join(containing, file_name)
end