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.



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

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



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

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



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/xcutils/uitest.rb', line 186

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

#appended(string, other) ⇒ Object



180
181
182
183
184
# File 'lib/xcutils/uitest.rb', line 180

def appended(string, other)
  return other if string == nil
  return string if string.include? other
  return string << " #{other}"
end

#copy_template_filesObject



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/xcutils/uitest.rb', line 157

def copy_template_files
  template_path = "./#{@project_name}/#{@uitest_name}"
  
  if !Dir.exist?(template_path)
    puts "Copying template files into project #{@project_name}"
    path = File.expand_path('../../../templates', __FILE__)
    FileUtils.copy_entry path, "./#{@project_name}"
  else
    puts "Skip to copy template files. Files are already exsit".gray
  end

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

  @project.targets.each { |target|
    target_group = @project.groups.select { |e| e.path == target.name }.first

    if target_group.groups.select { |e| e.path == @uitest_name }.first.nil?
      template_group = target_group.new_group(nil, @uitest_name)
      add_files("#{template_path}/*", template_group, target)
    end
  }
end

#generate_build_configurationObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/xcutils/uitest.rb', line 86

def generate_build_configuration
  @project.targets.each { |target|
    target_config = target.build_configurations.select { |e| e.name == @taget_config }.first
    target_main_sb = nil
    
    target.build_configurations.each { |e|
      break if e.name == @uitest_name

      puts "Configure build configuration #{e.name} in #{target.name}"

      path = e.build_settings['INFOPLIST_FILE']
      plist = Plist.parse_xml(path)
      plist_main_sb = plist['UIMainStoryboardFile']
      build_settings_main_sb = e.build_settings['MAIN_STORYBOARD_FILE']
      variable_name = '$(MAIN_STORYBOARD_FILE)'
      is_not_variable = plist_main_sb != variable_name

      e.build_settings['MAIN_STORYBOARD_FILE'] = is_not_variable ? plist_main_sb : (target_main_sb != nil ? target_main_sb : build_settings_main_sb)
      
      if target_main_sb.nil?
        target_main_sb = build_settings_main_sb
      end

      plist['UIMainStoryboardFile'] = variable_name
    
      if is_not_variable
        File.write(path, plist.to_plist)
      end

      file_names_string = e.build_settings['EXCLUDED_SOURCE_FILE_NAMES']
      file_names_string = appended(file_names_string, '$(SRCROOT)/$(PRODUCT_NAME)/UITestRunner/*')
      file_names_string = appended(file_names_string, '$(SRCROOT)/$(PRODUCT_NAME)/UITestRunner/**/*')
      e.build_settings['EXCLUDED_SOURCE_FILE_NAMES'] = file_names_string
    }
    
    test_config = target.build_configurations.select { |e| e.name == @uitest_name }.first

    if test_config.nil?
      puts "Generating build configuration #{@uitest_name} into #{target.name}"
      new_config = project.new(Xcodeproj::Project::Object::XCBuildConfiguration)
      new_config.name = @uitest_name
      new_config.target_configuration_reference = target_config.base_configuration_reference
      target.build_configurations << new_config
    else
      puts "Configure build configuration #{@uitest_name} in #{target.name}"
      new_config = test_config
    end

    new_config.build_settings.update(target_config.build_settings)
    new_config.build_settings['MAIN_STORYBOARD_FILE'] = @uitest_name
    new_config.build_settings['EXCLUDED_SOURCE_FILE_NAMES'] = nil
  }
end

#generate_project_configurationObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/xcutils/uitest.rb', line 70

def generate_project_configuration
  puts "Generating project configuration #{@uitest_name} into #{@project_name}"
    
  configuration_list = @project.root_object.build_configuration_list
    
  if configuration_list.build_settings(@uitest_name) != nil
    puts "Skip to generate project configuration for #{@uitest_name}. It is already installed in the project #{@project_name}".gray
    return
  end
    
  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
end

#generate_scheme_fileObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/xcutils/uitest.rb', line 140

def generate_scheme_file
  scheme_name = "#{@project_name} #{@uitest_name}"
  
  if File.exist?(scheme_path(scheme_name))
    puts "Skip to generate. A scheme file is already exsit, #{scheme_name}".gray
    return
  end
  
  puts "Generating scheme file #{scheme_path(scheme_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

#open_projectObject



61
62
63
64
65
66
67
68
# File 'lib/xcutils/uitest.rb', line 61

def open_project
  puts "🤖 Installing #{@uitest_name} into #{@project_name}".green

  if !Dir.exist?(@project_path)
    abort "😭 Abort to install. Project #{project_name} does not exist".red
  end
  @project = Xcodeproj::Project.open(@project_path)
end

#runObject



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/xcutils/uitest.rb', line 47

def run
  puts "Input: project_name: #{@project_name}, target_config: #{@taget_config}, target_scheme: #{@target_scheme}"
  open_project
  generate_project_configuration
  generate_build_configuration
  generate_scheme_file
  copy_template_files

  if !@project.nil?
    @project.save()
    puts "🦾 Installing Completed".green
  end
end

#scheme_path(name) ⇒ Object



221
222
223
# File 'lib/xcutils/uitest.rb', line 221

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

#set_custom_module(item) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/xcutils/uitest.rb', line 208

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



40
41
42
43
44
45
# File 'lib/xcutils/uitest.rb', line 40

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