Class: PodBuilder::Podfile

Inherits:
Object
  • Object
show all
Defined in:
lib/pod_builder/podfile.rb,
lib/pod_builder/podfile/post_actions.rb

Constant Summary collapse

PODBUILDER_LOCK_ACTION =
["raise \"\\n🚨  Do not launch 'pod install' manually, use `pod_builder` instead!\\n\" if !File.exist?('pod_builder.lock')"].freeze
POST_INSTALL_ACTIONS =
["require 'pod_builder/podfile/post_actions'", "PodBuilder::Podfile::remove_target_support_duplicate_entries", "PodBuilder::Podfile::check_target_support_resource_collisions"].freeze

Class Method Summary collapse

Class Method Details

.add_install_block(podfile_path) ⇒ Object



191
192
193
# File 'lib/pod_builder/podfile.rb', line 191

def self.add_install_block(podfile_path)
  add(PODBUILDER_LOCK_ACTION, "pre_install", podfile_path)
end

.check_target_support_resource_collisionsObject



16
17
18
19
20
21
22
23
24
# File 'lib/pod_builder/podfile/post_actions.rb', line 16

def self.check_target_support_resource_collisions
  puts "[PodBuilder] Checking target support resource collisions".yellow

  targets = find_xcodeproj_targets

  targets.map(&:name).each do |target|
    check_for_colliding_resources("Pods/Target Support Files/Pods-#{target}/Pods-#{target}-resources.sh", target, targets)
  end
end

.from_podfile_items(items, analyzer) ⇒ Object



9
10
11
12
13
14
15
16
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
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
# File 'lib/pod_builder/podfile.rb', line 9

def self.from_podfile_items(items, analyzer)
  raise "no items" unless items.count > 0

  sources = analyzer.sources
  
  cwd = File.dirname(File.expand_path(__FILE__))
  podfile = File.read("#{cwd}/templates/build_podfile.template")

  platform = analyzer.instance_variable_get("@result").targets.first.platform
  podfile.sub!("%%%platform_name%%%", platform.name.to_s)
  podfile.sub!("%%%deployment_version%%%", platform.deployment_target.version)

  podfile.sub!("%%%sources%%%", sources.map { |x| "source '#{x.url}'" }.join("\n"))

  build_configurations = items.map(&:build_configuration).uniq
  raise "Found different build configurations in #{items}" if build_configurations.count != 1
  podfile.sub!("%%%build_configuration%%%", build_configurations.first.capitalize)

  podfile_build_settings = ""
  
  pod_dependencies = {}

  items.each do |item|
    build_settings = Configuration.build_settings.dup

    item_build_settings = Configuration.build_settings_overrides[item.name] || {}
    
    # These settings need to be set as is to properly build frameworks
    build_settings['SWIFT_COMPILATION_MODE'] = 'wholemodule'
    build_settings['CLANG_ENABLE_MODULE_DEBUGGING'] = 'NO'
    build_settings['ONLY_ACTIVE_ARCH'] = 'NO'

    build_settings['SWIFT_VERSION'] = item_build_settings["SWIFT_VERSION"] || item.swift_version || project_swift_version(analyzer)
    if item.is_static
      # https://forums.developer.apple.com/thread/17921
      build_settings['CLANG_ENABLE_MODULE_DEBUGGING'] = "NO"
    end

    item_build_settings.each do |k, v|
      build_settings[k] = v
    end

    podfile_build_settings += "set_build_settings(\"#{item.root_name}\", #{build_settings.to_s}, installer)\n  "

    dependency_names = item.dependency_names.map { |x|
      if x.split("/").first == item.root_name
        next nil # remove dependency to parent spec
      end
      if overridded_module_name = Configuration.spec_overrides.fetch(x, {})["module_name"]
        next overridded_module_name
      end
    }.compact
  
    if dependency_names.count > 0
      pod_dependencies[item.root_name] = dependency_names
    end
  end

  podfile.sub!("%%%build_settings%%%", podfile_build_settings)

  podfile.sub!("%%%build_system%%%", Configuration.build_system)

  podfile.sub!("%%%pods%%%", "\"#{items.map(&:name).join('", "')}\"")
  
  podfile.sub!("%%%pods_dependencies%%%", pod_dependencies.to_s)
  
  podfile.sub!("%%%targets%%%", items.map(&:entry).join("\n  "))

  return podfile
end

.installObject



178
179
180
181
182
183
184
# File 'lib/pod_builder/podfile.rb', line 178

def self.install
  puts "Running pod install".yellow

  Dir.chdir(PodBuilder::project_path) do
    system("pod install;")
  end
end

.pod_definition_in(line, include_commented) ⇒ Object



195
196
197
198
199
200
201
202
203
204
# File 'lib/pod_builder/podfile.rb', line 195

def self.pod_definition_in(line, include_commented)
  stripped_line = strip_line(line)
  matches = stripped_line.match(/(^pod')(.*?)(')/)
  
  if matches&.size == 4 && (include_commented || !stripped_line.start_with?("#"))
    return matches[2]
  else
    return nil
  end
end

.remove_target_support_duplicate_entriesObject



7
8
9
10
11
12
13
14
# File 'lib/pod_builder/podfile/post_actions.rb', line 7

def self.remove_target_support_duplicate_entries
  puts "[PodBuilder] Removing target support duplicated entries".yellow
  # Frameworks and resources
  find_xcodeproj_targets.map(&:name).each do |target|
    remove_duplicate_entries("Pods/Target Support Files/Pods-#{target}/Pods-#{target}-frameworks.sh")
    remove_duplicate_entries("Pods/Target Support Files/Pods-#{target}/Pods-#{target}-resources.sh")
  end
end

.restore_file_sanity_checkObject



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
259
260
261
262
# File 'lib/pod_builder/podfile.rb', line 233

def self.restore_file_sanity_check
  unless Configuration.restore_enabled
    return nil
  end

  podfile_restore_path = PodBuilder::basepath("Podfile.restore")
  unless File.exist?(podfile_restore_path)
    return
  end

  error = nil

  begin
    File.rename(PodBuilder::basepath("Podfile"), PodBuilder::basepath("Podfile.tmp2"))
    File.rename(podfile_restore_path, PodBuilder::basepath("Podfile"))

    Analyze.installer_at(PodBuilder::basepath, false)
  rescue Exception => e
    error = e.to_s
  ensure
    File.rename(PodBuilder::basepath("Podfile"), podfile_restore_path)
    File.rename(PodBuilder::basepath("Podfile.tmp2"), PodBuilder::basepath("Podfile"))
  end

  if !error.nil?
    FileUtils.rm(podfile_restore_path)
  end

  return error
end

.restore_podfile_clean(pod_items) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/pod_builder/podfile.rb', line 206

def self.restore_podfile_clean(pod_items)
  unless Configuration.restore_enabled
    return
  end

  # remove pods that are no longer listed in pod_items
  podfile_restore_path = PodBuilder::basepath("Podfile.restore")
  unless File.exist?(podfile_restore_path)
    return
  end

  restore_content = File.read(podfile_restore_path)

  cleaned_lines = []
  restore_content.each_line do |line|
    if pod_name = pod_definition_in(line, false)
      if pod_items.map(&:name).include?(pod_name)
        cleaned_lines.push(line)      
      end
    else
      cleaned_lines.push(line)
    end
  end

  File.write(podfile_restore_path, cleaned_lines.join)
end

.sanity_checkObject



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/pod_builder/podfile.rb', line 264

def self.sanity_check
  podfile_path = PodBuilder::basepath("Podfile")
  unless File.exist?(podfile_path)
    return
  end

  File.read(podfile_path).each_line do |line|
    stripped_line = strip_line(line)
    unless !stripped_line.start_with?("#")
      next
    end

    if stripped_line.match(/(pod')(.*?)(')/) != nil
      starting_def_found = stripped_line.start_with?("def") && (line.match("\s*def\s") != nil)
      raise "Unsupported single line def/pod. `def` and `pod` shouldn't be on the same line, please modify the following line:\n#{line}" if starting_def_found
    end
  end
end

.strip_line(line) ⇒ Object



186
187
188
189
# File 'lib/pod_builder/podfile.rb', line 186

def self.strip_line(line)
  stripped_line = line.strip
  return stripped_line.gsub("\"", "'").gsub(" ", "").gsub("\t", "").gsub("\n", "")
end

.write_prebuilt(all_buildable_items, analyzer) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/pod_builder/podfile.rb', line 140

def self.write_prebuilt(all_buildable_items, analyzer)  
  puts "Updating Application Podfile".yellow

  podbuilder_podfile_path = PodBuilder::basepath("Podfile")
  rel_path = Pathname.new(podbuilder_podfile_path).relative_path_from(Pathname.new(PodBuilder::project_path)).to_s
    
  podfile_content = File.read(podbuilder_podfile_path)

  exclude_lines = Podfile::PODBUILDER_LOCK_ACTION.map { |x| strip_line(x) }

  prebuilt_lines = ["# Autogenerated by PodBuilder (https://github.com/Subito-it/PodBuilder)\n", "# Any change to this file should be done on #{rel_path}\n", "\n"]
  podfile_content.each_line do |line|
    stripped_line = strip_line(line)

    if pod_name = pod_definition_in(line, true)
      if podfile_item = all_buildable_items.detect { |x| x.name == pod_name }
        if Podspec.include?(podfile_item.name)
          if podfile_item.is_prebuilt == false
            line = "#{line.detect_indentation}#{podfile_item.prebuilt_entry}\n"
          end
        end
      end
    end

    if !exclude_lines.include?(stripped_line)
      prebuilt_lines.push(line)
    end
  end

  project_podfile_path = PodBuilder::project_path("Podfile")
  File.write(project_podfile_path, prebuilt_lines.join)
  Podfile.update_path_entires(project_podfile_path, false)
  Podfile.update_project_entries(project_podfile_path, false)

  add_pre_install_actions(project_podfile_path)
  add_post_install_checks(project_podfile_path)
end

.write_restorable(updated_pods, podfile_items, analyzer) ⇒ Object



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
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/pod_builder/podfile.rb', line 80

def self.write_restorable(updated_pods, podfile_items, analyzer)
  unless Configuration.restore_enabled
    return
  end
  
  puts "Writing Restore Podfile".yellow

  podfile_items = podfile_items.dup
  podfile_restore_path = PodBuilder::basepath("Podfile.restore")
  podfile_path = PodBuilder::basepath("Podfile")

  if File.exist?(podfile_restore_path)
    restore_podfile_items = podfile_items_at(podfile_restore_path, include_prebuilt = true)

    podfile_items.map! { |podfile_item|
      if updated_pod = updated_pods.detect { |x| x.name == podfile_item.name } then
        updated_pod
      elsif updated_pods.any? { |x| podfile_item.root_name == x.root_name } == false && # podfile_item shouldn't be among those being updated (including root specification)
            restored_pod = restore_podfile_items.detect { |x| x.name == podfile_item.name }
        restored_pod
      else
        podfile_item
      end
    }
  end

  result_targets = analyzer.instance_variable_get("@result").targets.map(&:name) 
  podfile_content = ["# Autogenerated by PodBuilder (https://github.com/Subito-it/PodBuilder)", "# Please don't modify this file", "\n"]
  podfile_content += analyzer.podfile.sources.map { |x| "source '#{x}'" }
  podfile_content += ["", "use_frameworks!", ""]

  # multiple platforms not (yet) supported
  # https://github.com/CocoaPods/Rome/issues/37
  platform = analyzer.instance_variable_get("@result").targets.first.platform
  podfile_content += ["platform :#{platform.name}, '#{platform.deployment_target.version}'", ""]

  analyzer.instance_variable_get("@result").specs_by_target.each do |target, specifications|
    target_name = target.name.to_s

    unless result_targets.select { |x| x.end_with?(target_name) }.count > 0
      next
    end

    podfile_content.push("target '#{target_name}' do")

    if project_path = target.user_project_path
      podfile_content.push("\tproject '#{project_path}'")
    end

    specifications.each do |spec|
      item = podfile_items.detect { |x| x.name == spec.name }
      podfile_content.push("\t#{item.entry}")
    end

    podfile_content.push("end\n")
  end

  File.write(podfile_restore_path, podfile_content.join("\n"))
end