Class: Fastlane::SetupIos

Inherits:
Setup
  • Object
show all
Defined in:
lib/fastlane/setup/setup_ios.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Setup

#is_android?, #is_ios?, #show_analytics

Instance Attribute Details

#app_identifierObject

Returns the value of attribute app_identifier.



14
15
16
# File 'lib/fastlane/setup/setup_ios.rb', line 14

def app_identifier
  @app_identifier
end

#app_nameObject

Returns the value of attribute app_name.



15
16
17
# File 'lib/fastlane/setup/setup_ios.rb', line 15

def app_name
  @app_name
end

#apple_idObject

Returns the value of attribute apple_id.



6
7
8
# File 'lib/fastlane/setup/setup_ios.rb', line 6

def apple_id
  @apple_id
end

#dev_portal_teamObject

Returns the value of attribute dev_portal_team.



11
12
13
# File 'lib/fastlane/setup/setup_ios.rb', line 11

def dev_portal_team
  @dev_portal_team
end

#itc_refObject

Returns the value of attribute itc_ref.



9
10
11
# File 'lib/fastlane/setup/setup_ios.rb', line 9

def itc_ref
  @itc_ref
end

#itc_teamObject

Returns the value of attribute itc_team.



12
13
14
# File 'lib/fastlane/setup/setup_ios.rb', line 12

def itc_team
  @itc_team
end

#portal_refObject

Returns the value of attribute portal_ref.



8
9
10
# File 'lib/fastlane/setup/setup_ios.rb', line 8

def portal_ref
  @portal_ref
end

#projectObject

Returns the value of attribute project.



5
6
7
# File 'lib/fastlane/setup/setup_ios.rb', line 5

def project
  @project
end

#toolsObject

the tools that are already enabled



4
5
6
# File 'lib/fastlane/setup/setup_ios.rb', line 4

def tools
  @tools
end

Instance Method Details

#ask_for_app_identifierObject



154
155
156
# File 'lib/fastlane/setup/setup_ios.rb', line 154

def ask_for_app_identifier
  self.app_identifier = ask('App Identifier (com.krausefx.app): '.yellow)
end

#ask_for_apple_idObject



150
151
152
# File 'lib/fastlane/setup/setup_ios.rb', line 150

def ask_for_apple_id
  self.apple_id ||= ask('Your Apple ID (e.g. [email protected]): '.yellow)
end

#ask_to_enable_other_toolsObject



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/fastlane/setup/setup_ios.rb', line 87

def ask_to_enable_other_tools
  if self.itc_ref.nil? && self.portal_ref.nil?
    wants_to_create_app = agree('Would you like to create your app on iTunes Connect and the Developer Portal? (y/n)', true)
    if wants_to_create_app
      create_app_if_necessary
      detect_if_app_is_available # check if the app was, in fact, created.
    end
  end
  if self.itc_ref && self.portal_ref
    wants_to_setup_deliver = agree("Do you want to setup 'deliver', which is used to upload app screenshots, app metadata and app updates to the App Store? This requires the app to be in the App Store already. (y/n)".yellow, true)
    enable_deliver if wants_to_setup_deliver
  end
end

#copy_existing_filesObject



139
140
141
142
143
144
145
146
147
148
# File 'lib/fastlane/setup/setup_ios.rb', line 139

def copy_existing_files
  files_to_copy.each do |current|
    current = File.join(File.expand_path('..', FastlaneFolder.path), current)
    next unless File.exist?(current)
    file_name = File.basename(current)
    to_path = File.join(folder, file_name)
    UI.success("Moving '#{current}' to '#{to_path}'")
    FileUtils.mv(current, to_path)
  end
end

#create_app_if_necessaryObject



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/fastlane/setup/setup_ios.rb', line 193

def create_app_if_necessary
  UI.important "Creating the app on iTunes Connect and the Apple Developer Portal"
  require 'produce'
  config = {} # this has to be done like this
  FastlaneCore::Project.detect_projects(config)
  project = FastlaneCore::Project.new(config)

  produce_options_hash = {
    app_name: project.app_name,
    app_identifier: self.app_identifier
  }
  Produce.config = FastlaneCore::Configuration.create(Produce::Options.available_options, produce_options_hash)
  begin
    ENV['PRODUCE_APPLE_ID'] = Produce::Manager.start_producing
  rescue => exception
    if exception.to_s.include?("The App Name you entered has already been used")
      UI.important("It looks like that #{project.app_name} has already been taken by someone else, please enter an alternative.")
      Produce.config[:app_name] = ask("App Name: ".yellow)
      Produce.config[:skip_devcenter] = true # since we failed on iTC
      ENV['PRODUCE_APPLE_ID'] = Produce::Manager.start_producing
    end
  end
end

#default_setupObject



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/fastlane/setup/setup_ios.rb', line 66

def default_setup
  copy_existing_files
  generate_appfile(manually: false)
  detect_installed_tools # after copying the existing files
  if self.itc_ref.nil? && self.portal_ref.nil?
    create_app_if_necessary
  end
  enable_deliver
  generate_fastfile(manually: false)
  show_analytics
end

#detect_if_app_is_availableObject

Detect if the app was created on the Dev Portal / iTC



179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/fastlane/setup/setup_ios.rb', line 179

def detect_if_app_is_available
  require 'spaceship'

  UI.important "Verifying if app is available on the Apple Developer Portal and iTunes Connect..."
  UI.message "Starting login with user '#{self.apple_id}'"
  Spaceship.(self.apple_id, nil)
  self.dev_portal_team = Spaceship.select_team
  self.portal_ref = Spaceship::App.find(self.app_identifier)

  Spaceship::Tunes.(@apple_id, nil)
  self.itc_team = Spaceship::Tunes.select_team
  self.itc_ref = Spaceship::Application.find(self.app_identifier)
end

#detect_installed_toolsObject



217
218
219
220
221
222
# File 'lib/fastlane/setup/setup_ios.rb', line 217

def detect_installed_tools
  self.tools = {}
  self.tools[:snapshot] = File.exist?(File.join(folder, 'Snapfile'))
  self.tools[:cocoapods] = File.exist?(File.join(File.expand_path('..', folder), 'Podfile'))
  self.tools[:carthage] = File.exist?(File.join(File.expand_path('..', folder), 'Cartfile'))
end

#enable_deliverObject



224
225
226
227
228
229
230
231
# File 'lib/fastlane/setup/setup_ios.rb', line 224

def enable_deliver
  UI.message("Loading up 'deliver', this might take a few seconds")
  require 'deliver'
  require 'deliver/setup'
  options = FastlaneCore::Configuration.create(Deliver::Options.available_options, {})
  Deliver::Runner.new(options) # to login...
  Deliver::Setup.new.run(options)
end

#files_to_copyObject



135
136
137
# File 'lib/fastlane/setup/setup_ios.rb', line 135

def files_to_copy
  ['Deliverfile', 'deliver', 'screenshots', 'metadata']
end

#folderObject



260
261
262
# File 'lib/fastlane/setup/setup_ios.rb', line 260

def folder
  FastlaneFolder.path
end

#generate_appfile(manually: false) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/fastlane/setup/setup_ios.rb', line 158

def generate_appfile(manually: false)
  template = File.read("#{Helper.gem_path('fastlane')}/lib/assets/AppfileTemplate")
  if manually
    ask_for_app_identifier
    ask_for_apple_id
  end

  template.gsub!('[[DEV_PORTAL_TEAM_ID]]', self.dev_portal_team) if self.dev_portal_team

  itc_team = self.itc_team ? "itc_team_id \"#{self.itc_team}\" # iTunes Connect Team ID\n" : ""
  template.gsub!('[[ITC_TEAM]]', itc_team)

  template.gsub!('[[APP_IDENTIFIER]]', self.app_identifier)
  template.gsub!('[[APPLE_ID]]', self.apple_id)

  path = File.join(folder, 'Appfile')
  File.write(path, template)
  UI.success("Created new file '#{path}'. Edit it to manage your preferred app metadata information.")
end

#generate_fastfile(manually: false) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/fastlane/setup/setup_ios.rb', line 233

def generate_fastfile(manually: false)
  scheme = self.project.schemes.first unless manually

  template = File.read("#{Helper.gem_path('fastlane')}/lib/assets/DefaultFastfileTemplate")

  scheme = ask("Optional: The scheme name of your app (If you don't need one, just hit Enter): ").to_s.strip unless scheme
  if scheme.length > 0
    template.gsub!('[[SCHEME]]', "(scheme: \"#{scheme}\")")
  else
    template.gsub!('[[SCHEME]]', "")
  end

  template.gsub!('snapshot', '# snapshot') unless self.tools[:snapshot]
  template.gsub!('cocoapods', '') unless self.tools[:cocoapods]
  template.gsub!('carthage', '') unless self.tools[:carthage]
  template.gsub!('[[FASTLANE_VERSION]]', Fastlane::VERSION)

  self.tools.each do |key, value|
    UI.message("'#{key}' enabled.".magenta) if value
    UI.important("'#{key}' not enabled.") unless value
  end

  path = File.join(folder, 'Fastfile')
  File.write(path, template)
  UI.success("Created new file '#{path}'. Edit it to manage your own deployment lanes.")
end

#handle_exception(exception: nil) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/fastlane/setup/setup_ios.rb', line 52

def handle_exception(exception: nil)
  # Something went wrong with the setup, clear the folder again
  # and restore previous files
  UI.error('Error occurred with the setup program! Reverting changes now!')
  restore_previous_state
  raise exception
end

#manual_setupObject



78
79
80
81
82
83
84
85
# File 'lib/fastlane/setup/setup_ios.rb', line 78

def manual_setup
  copy_existing_files
  generate_appfile(manually: true)
  detect_installed_tools # after copying the existing files
  ask_to_enable_other_tools
  generate_fastfile(manually: true)
  show_analytics
end


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/fastlane/setup/setup_ios.rb', line 109

def print_config_table
  rows = []
  rows << ["Apple ID", self.apple_id]
  rows << ["App Name", self.app_name]
  rows << ["App Identifier", self.app_identifier]
  rows << [(self.project.is_workspace ? "Workspace" : "Project"), self.project.path]
  require 'terminal-table'
  puts ""
  puts Terminal::Table.new(rows: rows, title: "Detected Values")
  puts ""

  unless self.itc_ref
    UI.important "This app identifier doesn't exist on iTunes Connect yet, it will be created for you"
  end

  unless self.portal_ref
    UI.important "This app identifier doesn't exist on the Apple Developer Portal yet, it will be created for you"
  end
end

#restore_previous_stateObject



264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/fastlane/setup/setup_ios.rb', line 264

def restore_previous_state
  # Move all moved files back
  files_to_copy.each do |current|
    from_path = File.join(folder, current)
    to_path = File.basename(current)
    if File.exist?(from_path)
      UI.important("Moving '#{from_path}' to '#{to_path}'")
      FileUtils.mv(from_path, to_path)
    end
  end

  UI.important("Deleting the 'fastlane' folder")
  FileUtils.rm_rf(folder)
end

#runObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/fastlane/setup/setup_ios.rb', line 17

def run
  if FastlaneFolder.setup? and !Helper.is_test?
    UI.important("Fastlane already set up at path #{folder}")
    return
  end

  show_infos

  FastlaneFolder.create_folder! unless Helper.is_test?
  is_manual_setup = false

  begin
    setup_project
    ask_for_apple_id
    detect_if_app_is_available
    print_config_table
    if UI.confirm("Please confirm the above values")
      default_setup
    else
      is_manual_setup = true
      manual_setup
    end
    UI.success('Successfully finished setting up fastlane')
  rescue => ex # this will also be caused by Ctrl + C
    if is_manual_setup
      handle_exception(exception: ex)
    else
      UI.error(ex.to_s)
      UI.error('An error occured during the setup process. Falling back to manual setup!')
      try_manual_setup
    end
  end
  # rubocop:enable Lint/RescueException
end

#setup_projectObject



101
102
103
104
105
106
107
# File 'lib/fastlane/setup/setup_ios.rb', line 101

def setup_project
  config = {}
  FastlaneCore::Project.detect_projects(config)
  self.project = FastlaneCore::Project.new(config)
  self.app_identifier = self.project.default_app_identifier # These two vars need to be accessed in order to be set
  self.app_name = self.project.default_app_name # They are set as a side effect, this could/should be changed down the road
end

#show_infosObject



129
130
131
132
133
# File 'lib/fastlane/setup/setup_ios.rb', line 129

def show_infos
  UI.success('This setup will help you get up and running in no time.')
  UI.success("fastlane will check what tools you're already using and set up")
  UI.success('the tool automatically for you. Have fun! ')
end

#try_manual_setupObject



60
61
62
63
64
# File 'lib/fastlane/setup/setup_ios.rb', line 60

def try_manual_setup
  manual_setup
rescue => ex
  handle_exception(exception: ex)
end