Class: XcodeUtils::Command::UITest::Install

Inherits:
XcodeUtils::Command::UITest show all
Defined in:
lib/xcutils/uitest.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Install

Returns a new instance of Install.



28
29
30
31
32
33
34
35
36
# File 'lib/xcutils/uitest.rb', line 28

def initialize(argv)
  @project_name = argv.option('project-name', nil)
  @taget_config = argv.option('target-config', nil)
  @target_scheme = argv.option('target-scheme', nil)
  @uitest_name = 'UITestRunner'
  @project_path = "./#{@project_name}.xcodeproj"
  super
  @additional_args = argv.remainder!
end

Class Method Details

.optionsObject



20
21
22
23
24
25
26
# File 'lib/xcutils/uitest.rb', line 20

def self.options
  [
    ['--project-name=""', 'The name of your xcode project'],
    ['--target-config=""', 'The name of build configuration to be copied to uitestrunner build configuration'],
    ['--target-scheme=""', 'The name of build scheme to be copied to uitestrunner build scheme'],
  ]
end

Instance Method Details

#add_files(direc, current_group, main_target) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/xcutils/uitest.rb', line 142

def add_files(direc, current_group, main_target)
  Dir.glob(direc) do |item|
      next if item == '.' or item == '.DS_Store'
              if File.directory?(item)
          group_name = File.basename(item)
          created_group = current_group.new_group(nil, group_name)
          add_files("#{item}/*", created_group, main_target)
      else
        i = current_group.new_file(File.basename(item))
        
        if item.include? ".swift" or item.include? ".m"
          main_target.add_file_references([i])
        end

        if item.include? ".storyboard" or item.include? ".xib"
          set_custom_module(item)
          main_target.add_resources([i])
        end
      end
  end
end

#copy_template_filesObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/xcutils/uitest.rb', line 116

def copy_template_files
  template_path = "./#{@project_name}/#{@uitest_name}"
  
  if Dir.exist?(template_path)
    puts "Template files already exist in the project #{@project_name}"
    return
  end

  puts "Copying template files into project #{@project_name}"

  path = File.expand_path('../../../templates', __FILE__)
  FileUtils.copy_entry path, "./#{@project_name}"

  puts "Adding references for files and resources into project #{@project_name}"

  project = Xcodeproj::Project.open(@project_path)

  project.targets.each { |target|
    target_group = project.groups.select { |e| e.path == target.name }.first
    template_group = target_group.new_group(nil, @uitest_name)
    add_files("#{template_path}/*", template_group, target)
  }

  project.save()
end

#generate_build_configurationObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/xcutils/uitest.rb', line 51

def generate_build_configuration
  puts "Input: project_name: #{@project_name}, target_config: #{@taget_config}, target_scheme: #{@target_scheme}"
    
  project = Xcodeproj::Project.open(@project_path)
  configuration_list = project.root_object.build_configuration_list
    
  if configuration_list.build_settings(@uitest_name) != nil
    puts "uitest already install in the project #{@project_name}"
    return
  end
    
  puts "Initializing #{@uitest_name}"
  puts "Generating project configuration"
    
  configuration = project.new(Xcodeproj::Project::Object::XCBuildConfiguration)
  configuration.name = @uitest_name
  configuration.build_settings = configuration_list.build_settings(@taget_config)
  configuration_list.build_configurations << configuration
    
  project.targets.each do |target|
    break if target.build_configurations.select { |e| e.name == @uitest_name }.count > 0
    
    puts "Generating build configuration #{target.name}:#{@uitest_name}"
    
    base_config = target.build_configurations.select { |e| e.name == @taget_config }.first
    org_main_storyboard_file = nil
    
    target.build_configurations.each { |e|
      path = e.build_settings['INFOPLIST_FILE']
      obj = Plist.parse_xml(path)
      var_name = '$(MAIN_STORYBOARD_FILE)'
      e.build_settings['MAIN_STORYBOARD_FILE'] = obj['UIMainStoryboardFile'] != var_name ? obj['UIMainStoryboardFile'] : org_main_storyboard_file
      obj['UIMainStoryboardFile'] = '$(MAIN_STORYBOARD_FILE)'
    
      if org_main_storyboard_file == nil
        org_main_storyboard_file = e.build_settings['MAIN_STORYBOARD_FILE']
      end
    
      File.write(path, obj.to_plist)
    }
    
    new_config = project.new(Xcodeproj::Project::Object::XCBuildConfiguration)
    new_config.name = @uitest_name
    new_config.base_configuration_reference = base_config.base_configuration_reference
    new_config.build_settings.update(base_config.build_settings)
    new_config.build_settings['MAIN_STORYBOARD_FILE'] = @uitest_name
    
    target.build_configurations << new_config
    
    puts "Generating scheme #{target.name} #{@uitest_name}"
  
    project.save()
  end
end

#generate_schemeObject



106
107
108
109
110
111
112
113
114
# File 'lib/xcutils/uitest.rb', line 106

def generate_scheme
  scheme_name = "#{@project_name} #{@uitest_name}"
  
  FileUtils.copy_file(scheme_path(@target_scheme), scheme_path(scheme_name))
  
  scheme = Xcodeproj::XCScheme.new(scheme_path(scheme_name))
  scheme.launch_action.build_configuration = @uitest_name
  scheme.save_as(@project_path, scheme_name)
end

#runObject



45
46
47
48
49
# File 'lib/xcutils/uitest.rb', line 45

def run
  generate_build_configuration
  generate_scheme
  copy_template_files
end

#scheme_path(name) ⇒ Object



177
178
179
# File 'lib/xcutils/uitest.rb', line 177

def scheme_path(name)
  return "#{@project_path}/xcshareddata/xcschemes/#{name}.xcscheme"
end

#set_custom_module(item) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/xcutils/uitest.rb', line 164

def set_custom_module(item)
  doc = Nokogiri::XML(File.open(item))
  nodes = doc.xpath("//viewController")

  nodes.each { |e|
    e['customModule'] = @project_name
  }

  if nodes.count > 0
    File.write(item, doc.to_xml)
  end
end

#validate!Object



38
39
40
41
42
43
# File 'lib/xcutils/uitest.rb', line 38

def validate!
  super
  help! 'Argument --project-name is required.' unless @project_name
  help! 'Argument --target-config is required.' unless @taget_config
  help! 'Argument --target-scheme is required.' unless @target_scheme
end