Class: GoNative::Plugins::IOS::ExtractExtensions

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ ExtractExtensions

Returns a new instance of ExtractExtensions.



16
17
18
# File 'lib/gonative/plugins/ios/extract_extensions.rb', line 16

def initialize(path)
  @proj_path = path
end

Instance Attribute Details

#proj_pathObject (readonly)

Returns the value of attribute proj_path.



14
15
16
# File 'lib/gonative/plugins/ios/extract_extensions.rb', line 14

def proj_path
  @proj_path
end

Instance Method Details

#callObject



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

def call
  original_path = FileUtils.pwd
  FileUtils.cd(File.dirname(proj_path))
  proj = Xcodeproj::Project.open(proj_path)
  extension_targets = proj.targets.select { |target|
    target.product_type == Xcodeproj::Constants::PRODUCT_TYPE_UTI[:app_extension]
  }

  extension_targets.each do |target|
    extension_name = target.name
    extension_path = "Extensions/#{target.name}"

    target_definition = {}
    target_definition[:name] = extension_name

    FileUtils.cd(extension_name)
    target_definition[:info] = `find . -name '*Info.plist'`.split("\n").first
    target_definition[:entitlements] = `find -E . -regex ".*\.(entitlements)"`.split("\n").first
    target_definition[:headers] = `find -E . -regex ".*\.(h)"`.split("\n")
    target_definition[:source_files] = `find -E . -regex ".*\.(m|swift)"`.split("\n")
    target_definition[:resources] = target
                                    .resources_build_phase
                                    .files
                                    .map(&:file_ref)
                                    .map(&:name)
                                    .map { |name| `find . -name #{name}`.strip }

    target_definition[:system_frameworks] = target
                                            .frameworks_build_phase
                                            .files
                                            .map(&:file_ref)
                                            .map(&:path)
                                            .select { |p| p.start_with? 'System' }
                                            .map { |p| File.basename(p) }

    target_definition[:dependencies] = podfile.target_definitions[extension_name].dependencies.map do |dependency|
      {
        name: dependency.name,
        requirement: dependency.requirement.requirements.map { |r| r.map(&:to_s).join(' ') }.join(', ')
      }
    end

    target_definition.slice(:info, :entitlements, :headers, :source_files,
                            :resources).values.compact.flatten.each do |file_path|
      system('ditto', file_path, "#{original_path}/#{extension_path}/#{file_path}")
    end

    File.open("#{original_path}/#{extension_path}.json", "w") do |f|
      f.write(JSON.pretty_generate(target_definition))
    end

    FileUtils.cd('..')
  end
end

#podfileObject



75
76
77
# File 'lib/gonative/plugins/ios/extract_extensions.rb', line 75

def podfile
  @podfile ||= Pod::Podfile.from_file(Pathname.new(File.dirname(proj_path) + '/Podfile'))
end