Class: GoNative::Plugins::IOS::BuildFramework

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

Constant Summary collapse

BUILD_TEMPLATE_DIRECTORY_PATH =
File.expand_path(File.join(__dir__, '../../../..', 'templates', 'build', 'ios'))

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(archs, persist_build_dir, use_existing_build) ⇒ BuildFramework

Returns a new instance of BuildFramework.

Raises:



19
20
21
22
23
24
25
26
27
# File 'lib/gonative/plugins/ios/build_framework.rb', line 19

def initialize(archs, persist_build_dir, use_existing_build)
  podspec_file = Dir['./*.podspec'].first
  raise Error, 'No podspec exists.' unless podspec_file

  @plugin_name = File.basename(podspec_file, '.podspec')
  @archs = archs.gsub(',', ' ')
  @persist_build_dir = persist_build_dir
  @use_existing_build = use_existing_build
end

Instance Attribute Details

#archsObject (readonly)

Returns the value of attribute archs.



17
18
19
# File 'lib/gonative/plugins/ios/build_framework.rb', line 17

def archs
  @archs
end

#persist_build_dirObject (readonly)

Returns the value of attribute persist_build_dir.



17
18
19
# File 'lib/gonative/plugins/ios/build_framework.rb', line 17

def persist_build_dir
  @persist_build_dir
end

#plugin_nameObject (readonly)

Returns the value of attribute plugin_name.



17
18
19
# File 'lib/gonative/plugins/ios/build_framework.rb', line 17

def plugin_name
  @plugin_name
end

#use_existing_buildObject (readonly)

Returns the value of attribute use_existing_build.



17
18
19
# File 'lib/gonative/plugins/ios/build_framework.rb', line 17

def use_existing_build
  @use_existing_build
end

Instance Method Details

#add_classes(target, group) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/gonative/plugins/ios/build_framework.rb', line 69

def add_classes(target, group)
  classes_group = group.new_group('Classes', 'Classes')
  references = Dir.glob("../#{plugin_name}/Classes/**/*.{m,swift}").map do |file|
    classes_group.new_file("../../#{file}")
  end
  target.add_file_references(references)
end

#add_frameworks(target, group) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/gonative/plugins/ios/build_framework.rb', line 77

def add_frameworks(target, group)
  frameworks_group = group.new_group('Frameworks', 'Frameworks')
  references = Dir.glob("../#{plugin_name}/Frameworks/*.{framework,xcframework}").map do |file|
    frameworks_group.new_file("../../#{file}")
  end
  return unless references.length

  references.each { |ref| target.frameworks_build_phase.add_file_reference(ref) }
end

#add_headers(target, group) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/gonative/plugins/ios/build_framework.rb', line 87

def add_headers(target, group)
  headers_group = group.new_group('Headers', 'Headers')
  references = Dir.glob("../#{plugin_name}/Classes/**/*.h").map do |file|
    headers_group.new_file("../../#{file}")
  end
  header_files = target.add_file_references(references)
  header_files << target.headers_build_phase.add_file_reference(group.new_file("../#{plugin_name}-umbrella.h"))
  header_files.each do |header|
    header.settings = { 'ATTRIBUTES' => ['Public'] }
  end
end

#build_framework!Object

Raises:



120
121
122
123
124
125
# File 'lib/gonative/plugins/ios/build_framework.rb', line 120

def build_framework!
  Utils::UI.info 'Building framework'
  return if system('sh create-framework.sh >/dev/null 2>/dev/null')

  raise Error, 'Error building framework. Please run the create-framework file manually to fix any errors'
end

#callObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/gonative/plugins/ios/build_framework.rb', line 29

def call
  cd_build_dir(!use_existing_build)
  unless use_existing_build
    create_framework_proj
    move_template_files!
    run_pod_install
    chmod_frameworks_script!
  end
  build_framework!
  move_framework_file
ensure
  FileUtils.cd('..')
  FileUtils.rm_rf('build') unless persist_build_dir
end

#cd_build_dir(create) ⇒ Object



44
45
46
47
48
# File 'lib/gonative/plugins/ios/build_framework.rb', line 44

def cd_build_dir(create)
  build_dir = File.join(FileUtils.pwd, 'build')
  FileUtils.mkdir(build_dir) if create
  FileUtils.cd(build_dir)
end

#chmod_frameworks_script!Object



116
117
118
# File 'lib/gonative/plugins/ios/build_framework.rb', line 116

def chmod_frameworks_script!
  FileUtils.chmod 0755, 'create-framework.sh'
end

#create_framework_projObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/gonative/plugins/ios/build_framework.rb', line 50

def create_framework_proj
  proj = Xcodeproj::Project.new(FileUtils.pwd)
  target = proj.new_target(:framework,
                           plugin_name.to_s,
                           :ios,
                           deployment_target)
  main_group = proj.new_group(plugin_name, plugin_name)
  add_classes(target, main_group)
  add_frameworks(target, main_group)
  add_headers(target, main_group)
  target.build_configurations.each do |config|
    config.build_settings['INFOPLIST_FILE'] = 'Info.plist'
    config.build_settings['MARKETING_VERSION'] = spec.version
    config.build_settings['MODULEMAP_FILE'] = "#{plugin_name}.modulemap"
    config.build_settings['PRODUCT_BUNDLE_IDENTIFIER'] = "org.cocoapods.#{plugin_name}"
  end
  proj.save("#{plugin_name}.xcodeproj")
end

#deployment_targetObject



132
133
134
# File 'lib/gonative/plugins/ios/build_framework.rb', line 132

def deployment_target
  spec.deployment_target('ios') || '13.0'
end

#move_framework_fileObject



127
128
129
130
# File 'lib/gonative/plugins/ios/build_framework.rb', line 127

def move_framework_file
  FileUtils.rm_rf('../XCFramework')
  FileUtils.mv('XCFramework', '..', force: true)
end

#move_template_files!Object



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/gonative/plugins/ios/build_framework.rb', line 99

def move_template_files!
  plugin_dependencies = spec.dependencies.map do |d|
    ["pod '#{d.name}'", "'#{d.requirement}'"].compact.join(', ')
  end * "\n\t"
  FileUtils.cp_r("#{BUILD_TEMPLATE_DIRECTORY_PATH}/.", '.')
  headers = Dir.glob("../#{plugin_name}/Classes/**/*.h").map { |f| "#import \"#{File.basename(f)}\"" } * "\n"
  Utils::TemplateInflator.new(plugin_name: plugin_name,
                              plugin_dependencies: plugin_dependencies,
                              plugin_headers: headers,
                              archs: archs,
                              deployment_target: deployment_target).call
end

#run_pod_installObject



112
113
114
# File 'lib/gonative/plugins/ios/build_framework.rb', line 112

def run_pod_install
  system 'pod install'
end

#specObject



136
137
138
# File 'lib/gonative/plugins/ios/build_framework.rb', line 136

def spec
  @spec ||= Pod::Specification.from_file("../#{plugin_name}.podspec")
end