Class: Motion::Project::CocoaPods

Inherits:
Object
  • Object
show all
Defined in:
lib/motion/project/cocoapods.rb,
lib/motion/project/version.rb

Overview

—————————————————————————#

Constant Summary collapse

PODS_ROOT =
'vendor/Pods'
TARGET_NAME =
'RubyMotion'
SUPPORT_FILES =
File.join(PODS_ROOT, "Target Support Files/Pods-#{TARGET_NAME}")
PUBLIC_HEADERS_ROOT =
File.join(PODS_ROOT, 'Headers/Public')
VERSION =
'1.8.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, vendor_options) ⇒ CocoaPods

Returns a new instance of CocoaPods.



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
# File 'lib/motion/project/cocoapods.rb', line 70

def initialize(config, vendor_options)
  @config = config
  @vendor_options = vendor_options

  case @config.deploy_platform
  when 'MacOSX'
    platform = :osx
  when 'iPhoneOS'
    platform = :ios
  when 'AppleTVOS'
    platform = :tvos
  when 'WatchOS'
    platform = :watchos
  else
    App.fail "Unknown CocoaPods platform: #{@config.deploy_platform}"
  end

  @podfile = Pod::Podfile.new(Pathname.new(Rake.original_dir) + 'Rakefile') {}
  @podfile.platform(platform, config.deployment_target)
  @podfile.target(TARGET_NAME)
  cp_config.podfile = @podfile
  cp_config.installation_root = Pathname.new(File.expand_path(config.project_dir)) + 'vendor'

  if cp_config.verbose = !!ENV['COCOAPODS_VERBOSE']
    require 'claide'
  end

  configure_project
end

Instance Attribute Details

#podfileObject

Returns the value of attribute podfile.



68
69
70
# File 'lib/motion/project/cocoapods.rb', line 68

def podfile
  @podfile
end

Instance Method Details

#analyzerObject



328
329
330
331
# File 'lib/motion/project/cocoapods.rb', line 328

def analyzer
  cp_config = Pod::Config.instance
  Pod::Installer::Analyzer.new(cp_config.sandbox, @podfile, cp_config.lockfile)
end

#configure_projectObject

Adds the Pods project to the RubyMotion config as a vendored project and



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
139
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
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
232
233
234
235
236
# File 'lib/motion/project/cocoapods.rb', line 102

def configure_project
  @config.resources_dirs << resources_dir.to_s

  # TODO replace this all once Xcodeproj has the proper xcconfig parser.
  if (xcconfig = self.pods_xcconfig_hash) && ldflags = xcconfig['OTHER_LDFLAGS']
    lib_search_path_flags = xcconfig['LIBRARY_SEARCH_PATHS'] || ""
    lib_search_paths = []
    lib_search_path_flags = lib_search_path_flags.split(/\s/).map do |path|
      if path =~ /(\$\(inherited\))|(\$\{inherited\})|(\$CONFIGURATION_BUILD_DIR)/
        nil
      else
        path = path.gsub(/(\$\(PODS_ROOT\))|(\$\{PODS_ROOT\})/, File.join(@config.project_dir, PODS_ROOT))
        lib_search_paths << path.gsub('"', '')
        '-L ' << path
      end
    end.compact.join(' ')

    # Get the name of all static libraries that come pre-built with pods
    pre_built_static_libs = lib_search_paths.map do |path|
      Dir[File.join(path, '**/*.a')].map { |f| File.basename(f) }
    end.flatten

    # Collect the Pod products
    pods_libs = []

    @config.libs.concat(ldflags.scan(/-l"?([^\s"]+)"?/).map { |m|
      lib_name = m[0]
      next if lib_name.nil?
      if lib_name.start_with?('Pods-')
        # For CocoaPods 0.37.x or below. This block is marked as deprecated.
        pods_libs << lib_name
        nil
      elsif pre_built_static_libs.include?("lib#{lib_name}.a")
        "#{lib_search_path_flags} -ObjC -l#{lib_name}"
      elsif File.exist?("/usr/lib/lib#{lib_name}.dylib")
        "/usr/lib/lib#{lib_name}.dylib"
      else
        pods_libs << lib_name
        nil
      end
    }.compact)
    @config.libs.uniq!

    framework_search_paths = []
    if search_paths = xcconfig['FRAMEWORK_SEARCH_PATHS']
      search_paths = search_paths.strip
      unless search_paths.empty?
        search_paths.scan(/"([^"]+)"/) do |search_path|
          path = search_path.first.gsub!(/(\$\(PODS_ROOT\))|(\$\{PODS_ROOT\})/, "#{@config.project_dir}/#{PODS_ROOT}")
          framework_search_paths << path if path
        end
        # If we couldn't parse any search paths, then presumably nothing was properly quoted, so
        # fallback to just assuming the whole value is one path.
        if framework_search_paths.empty?
          path = search_paths.gsub!(/(\$\(PODS_ROOT\))|(\$\{PODS_ROOT\})/, "#{@config.project_dir}/#{PODS_ROOT}")
          framework_search_paths << path if path
        end
      end
    end

    header_dirs = ['Headers/Public']

    # We want to set the proper header search paths otherwise we might generate incorrect bridgesupport
    header_search_paths = []
    if search_paths = xcconfig['HEADER_SEARCH_PATHS']
      search_paths = search_paths.strip
      unless search_paths.empty?
        search_paths.scan(/"([^"]+)"/) do |search_path|
          path = search_path.first.gsub!(/(\$\(PODS_ROOT\))|(\$\{PODS_ROOT\})/, "#{@config.project_dir}/#{PODS_ROOT}")
          header_search_paths << File.expand_path(path) if path
        end
        # If we couldn't parse any search paths, then presumably nothing was properly quoted, so
        # fallback to just assuming the whole value is one path.
        if header_search_paths.empty?
          path = search_paths.gsub!(/(\$\(PODS_ROOT\))|(\$\{PODS_ROOT\})/, "#{@config.project_dir}/#{PODS_ROOT}")
          header_search_paths << File.expand_path(path) if path
        end
      end
    end
    header_search_paths = header_search_paths.map { |p| "-I'#{p}'" }.join(' ')
    # Initialize ':bridgesupport_cflags', in case the use
    @vendor_options[:bridgesupport_cflags] ||= ''
    @vendor_options[:bridgesupport_cflags] << " #{header_search_paths}"

    frameworks = ldflags.scan(/-framework\s+"?([^\s"]+)"?/).map { |m| m[0] }

    case @config.deploy_platform
    when 'MacOSX'
      @config.framework_search_paths.concat(framework_search_paths)
      @config.framework_search_paths.uniq!
      framework_search_paths.each do |framework_search_path|
        frameworks.reject! do |framework|
          path = File.join(framework_search_path, "#{framework}.framework")
          if File.exist?(path)
            @config.embedded_frameworks << path
            true
          else
            false
          end
        end
      end
    when 'iPhoneOS'
      pods_root = cp_config.installation_root + 'Pods'
      # If we would really specify these as ‘frameworks’ then the linker
      # would not link the archive into the application, because it does not
      # see any references to any of the symbols in the archive. Treating it
      # as a static library (which it is) with `-ObjC` fixes this.
      #
      framework_search_paths.each do |framework_search_path|
        frameworks.reject! do |framework|
          path = File.join(framework_search_path, "#{framework}.framework")
          if File.exist?(path)
            @config.libs << "-ObjC '#{File.join(path, framework)}'"
            true
          else
            false
          end
        end
      end
    end

    @config.frameworks.concat(frameworks)
    @config.frameworks.uniq!

    @config.weak_frameworks.concat(ldflags.scan(/-weak_framework\s+([^\s]+)/).map { |m| m[0] })
    @config.weak_frameworks.uniq!

    @config.vendor_project(PODS_ROOT, :xcode, {
      :target => "Pods-#{TARGET_NAME}",
      :headers_dir => "{#{header_dirs.join(',')}}",
      :products => pods_libs.map { |lib_name| "lib#{lib_name}.a" },
      :allow_empty_products => (pods_libs.empty? ? true : false),
    }.merge(@vendor_options))
  end
end

#copy_cocoapods_env_and_prefix_headersObject



299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/motion/project/cocoapods.rb', line 299

def copy_cocoapods_env_and_prefix_headers
  headers = Dir.glob(["#{PODS_ROOT}/*.h", "#{PODS_ROOT}/*.pch", "#{PODS_ROOT}/Target Support Files/**/*.h", "#{PODS_ROOT}/Target Support Files/**/*.pch"])
  headers.each do |header|
    src = File.basename(header)
    dst = src.sub(/\.pch$/, '.h')
    dst_path = File.join(PUBLIC_HEADERS_ROOT, "____#{dst}")
    unless File.exist?(dst_path)
      FileUtils.mkdir_p(PUBLIC_HEADERS_ROOT)
      FileUtils.cp(header, dst_path)
    end
  end
end

#cp_configObject



324
325
326
# File 'lib/motion/project/cocoapods.rb', line 324

def cp_config
  Pod::Config.instance
end

#dependency(*name_and_version_requirements, &block) ⇒ Object

Deprecated.



250
251
252
# File 'lib/motion/project/cocoapods.rb', line 250

def dependency(*name_and_version_requirements, &block)
  @podfile.dependency(*name_and_version_requirements, &block)
end

#inspectObject

This is the output that gets shown in ‘rake config`, so it should be short and sweet.



318
319
320
321
322
# File 'lib/motion/project/cocoapods.rb', line 318

def inspect
  cp_config.lockfile.to_hash['PODS'].map do |pod|
    pod.is_a?(Hash) ? pod.keys.first : pod
  end.inspect
end

#install!(update) ⇒ Object

Performs a CocoaPods Installation.

For now we only support one Pods target, this will have to be expanded once we work on more spec support.

Let RubyMotion re-generate the BridgeSupport file whenever the list of installed pods changes.



273
274
275
276
277
278
279
# File 'lib/motion/project/cocoapods.rb', line 273

def install!(update)
  pods_installer.update = update
  pods_installer.installation_options.integrate_targets = false
  pods_installer.install!
  install_resources
  copy_cocoapods_env_and_prefix_headers
end

#install_resourcesObject

TODO this probably breaks in cases like resource bundles etc, need to test.



283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/motion/project/cocoapods.rb', line 283

def install_resources
  FileUtils.rm_rf(resources_dir)
  FileUtils.mkdir_p(resources_dir)
  resources.each do |file|
    begin
      FileUtils.cp_r(file, resources_dir) if file.exist?
    rescue ArgumentError => exc
      unless exc.message =~ /same file/
        raise
      end
    end
  end
end

#pod(*name_and_version_requirements, &block) ⇒ Object



245
246
247
# File 'lib/motion/project/cocoapods.rb', line 245

def pod(*name_and_version_requirements, &block)
  @podfile.pod(*name_and_version_requirements, &block)
end

#pods_installerObject

Installation ————————————————————————-#



261
262
263
# File 'lib/motion/project/cocoapods.rb', line 261

def pods_installer
  @installer ||= Pod::Installer.new(cp_config.sandbox, @podfile, cp_config.lockfile)
end

#pods_xcconfigObject



333
334
335
336
# File 'lib/motion/project/cocoapods.rb', line 333

def pods_xcconfig
  path = Pathname.new(@config.project_dir) + SUPPORT_FILES + "Pods-#{TARGET_NAME}.release.xcconfig"
  Xcodeproj::Config.new(path) if path.exist?
end

#pods_xcconfig_hashObject



338
339
340
341
342
# File 'lib/motion/project/cocoapods.rb', line 338

def pods_xcconfig_hash
  if xcconfig = pods_xcconfig
    xcconfig.to_hash
  end
end

#post_install(&block) ⇒ Object



254
255
256
# File 'lib/motion/project/cocoapods.rb', line 254

def post_install(&block)
  @podfile.post_install(&block)
end

#resourcesObject

Do not copy ‘.framework` bundles, these should be handled through RM’s ‘embedded_frameworks` config attribute.



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/motion/project/cocoapods.rb', line 347

def resources
  resources = []
  File.open(Pathname.new(@config.project_dir) + SUPPORT_FILES + "Pods-#{TARGET_NAME}-resources.sh") { |f|
    f.each_line do |line|
      if matched = line.match(/install_resource\s+(.*)/)
        path = (matched[1].strip)[1..-2]
        path.sub!("${BUILD_DIR}/${CONFIGURATION}${EFFECTIVE_PLATFORM_NAME}", ".build")
        unless File.extname(path) == '.framework'
          resources << Pathname.new(@config.project_dir) + PODS_ROOT + path
        end
      end
    end
  }
  resources.uniq
end

#resources_dirObject



363
364
365
# File 'lib/motion/project/cocoapods.rb', line 363

def resources_dir
  Pathname.new(@config.project_dir) + PODS_ROOT + 'Resources'
end

#source(source) ⇒ Object

DSL ————————————————————————-#



241
242
243
# File 'lib/motion/project/cocoapods.rb', line 241

def source(source)
  @podfile.source(source)
end