Class: GoNative::Plugins::IOS::EmbedExtensions

Inherits:
Object
  • Object
show all
Extended by:
DSL::Serviceable
Defined in:
lib/gonative/plugins/ios/embed_extensions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#extensionsObject (readonly)

Returns the value of attribute extensions.



13
14
15
# File 'lib/gonative/plugins/ios/embed_extensions.rb', line 13

def extensions
  @extensions
end

Instance Method Details

#callObject



15
16
17
18
19
# File 'lib/gonative/plugins/ios/embed_extensions.rb', line 15

def call
  prepare_extensions
  embed_in_project
  modify_podfile
end

#embed_in_projectObject



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
# File 'lib/gonative/plugins/ios/embed_extensions.rb', line 33

def embed_in_project
  proj = Xcodeproj::Project.open('MedianIOS.xcodeproj')
  app_target = proj.native_targets.first
  extensions.each do |extension|
    extension_target = proj.new_target(:app_extension,
                                       extension['name'],
                                       :ios,
                                       '13.0')
    extension_group = proj.new_group(extension['name'], extension['name'])

    extension['headers'].each { |path| extension_group.new_file(path) }
    extension_target.add_file_references(extension['source_files'].map do |path|
                                           extension_group.new_file(path)
                                         end)
    extension_target.add_resources(extension['resources'].map { |path| extension_group.new_file(path) })
    extension_target.add_system_frameworks(extension['system_frameworks'])
    extension_group.new_file(extension['info'])
    extension_group.new_file(extension['entitlements']) if extension['entitlements']
    extension_target.build_configurations.each do |config|
      config.build_settings['PRODUCT_NAME'] = '$(TARGET_NAME)'
      config.build_settings['INFOPLIST_FILE'] = "#{extension["name"]}/#{extension["info"]}"
      if extension['entitlements']
        config.build_settings['CODE_SIGN_ENTITLEMENTS'] =
          "#{extension["name"]}/#{extension["entitlements"]}"
      end
    end
    app_target.add_dependency(extension_target)
    extension_file = proj.files.find { |f| f.path == "#{extension["name"]}.appex" }
    embed_phase = app_target.build_phases.find do |phase|
      phase.class.method_defined?(:name) && phase.name == 'Embed Foundation Extensions'
    end
    embed_phase.add_file_reference(extension_file, true)
    app_config = proj.files.find { |f| f.path == 'appConfig.json' }
    extension_target.resources_build_phase.add_file_reference(app_config, true)
  end

  proj.save
end

#modify_podfileObject



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/gonative/plugins/ios/embed_extensions.rb', line 72

def modify_podfile
  extensions.each do |extension|
    open('Podfile', 'a') do |f|
      f.puts "target '#{extension["name"]}' do"
      f.puts "\tuse_frameworks!"
      extension['dependencies'].each do |dep|
        f.puts "\tpod '#{dep["name"]}', '#{dep["requirement"]}'"
      end
      f.puts 'end'
    end
  end
end

#prepare_extensionsObject



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/gonative/plugins/ios/embed_extensions.rb', line 21

def prepare_extensions
  extension_paths = `find ./Pods -iname Extensions -not -empty -type d`.split("\n")
  extension_paths
    .map { |path| Pathname(path).children.select(&:directory?) }
    .flatten
    .each { |path| FileUtils.cp_r(path, '.') }
  @extensions = extension_paths
                .map { |path| Pathname(path).children.select { |p| p.extname == '.json' } }
                .flatten
                .map { |p| JSON.parse(File.read(p)) }
end