Class: ReactNativeUtil::Converter

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/react_native_util/converter.rb

Overview

Class to perform conversion operations.

Constant Summary collapse

DEFAULT_DEPENDENCIES =
Array<String>

Default contents of Libraries group

%w[
  RCTAnimation
  React
  RCTActionSheet
  RCTBlob
  RCTGeolocation
  RCTImage
  RCTLinking
  RCTNetwork
  RCTSettings
  RCTText
  RCTVibration
  RCTWebSocket
]
PODFILE_TEMPLATE_PATH =
String

Path to the Podfile template

File.expand_path '../assets/templates/Podfile.erb', __dir__

Instance Attribute Summary collapse

Attributes included from Util

#platform

Instance Method Summary collapse

Methods included from Util

#boolean_env_var?, #execute, #float_env_var, #log, #mac?

Constructor Details

#initialize(repo_update: nil) ⇒ Converter

Returns a new instance of Converter.



42
43
44
45
46
47
48
49
# File 'lib/react_native_util/converter.rb', line 42

def initialize(repo_update: nil)
  @options = {}
  if repo_update.nil?
    @options[:repo_update] = boolean_env_var?(:REACT_NATIVE_CONVERT_REPO_UPDATE, default_value: true)
  else
    @options[:repo_update] = repo_update
  end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



40
41
42
# File 'lib/react_native_util/converter.rb', line 40

def options
  @options
end

#package_jsonObject (readonly)

Hash

Contents of ./package.json



32
33
34
# File 'lib/react_native_util/converter.rb', line 32

def package_json
  @package_json
end

#xcodeprojObject (readonly)

Xcodeproj::Project

Contents of the project at xcodproj_path



38
39
40
# File 'lib/react_native_util/converter.rb', line 38

def xcodeproj
  @xcodeproj
end

#xcodeproj_pathObject (readonly)

String

Full path to Xcode project



35
36
37
# File 'lib/react_native_util/converter.rb', line 35

def xcodeproj_path
  @xcodeproj_path
end

Instance Method Details

#add_packager_scriptObject



241
242
243
244
245
246
247
248
249
250
# File 'lib/react_native_util/converter.rb', line 241

def add_packager_script
  script = packager_script
  return unless script

  target = xcodeproj.targets.find { |t| t.name == app_name }
  phase = target.new_shell_script_build_phase 'Start Packager'
  phase.shell_script = packager_script

  # TODO: Move this build phase to the top of the list before the pod install.
end

#app_nameString

The name of the app as specified in package.json

Returns:



199
200
201
# File 'lib/react_native_util/converter.rb', line 199

def app_name
  @app_name ||= package_json['name']
end

#check_repo_status!Object

Raises:



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/react_native_util/converter.rb', line 223

def check_repo_status!
  # If the git command is not installed, there's not much we can do.
  # Don't want to use verify_git here, which will insist on installing
  # the command. The logic of that method could change.
  return if `which git`.empty?

  unless Dir.exist? ".git"
    `git rev-parse --git-dir > /dev/null 2>&1`
    # Not a git repo
    return unless $?.success?
  end

  `git diff-index --quiet HEAD --`
  return if $?.success?

  raise ConversionError, 'Uncommitted changes in repo. Please commit or stash before continuing.'
end

#convert_to_react_pod!Object

Convert project to use React pod

Raises:

  • ConversionError on failure



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
79
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
# File 'lib/react_native_util/converter.rb', line 53

def convert_to_react_pod!
  raise ConversionError, "macOS required." unless mac?

  # Make sure no uncommitted changes
  check_repo_status!

  load_package_json!
  log 'package.json:'
  log " app name: #{app_name.inspect}"

  log 'Installing NPM dependencies with yarn'
  execute 'yarn'

  # 1. Detect project. TODO: Add an option to override.
  @xcodeproj_path = File.expand_path "ios/#{package_json['name']}.xcodeproj"
  load_xcodeproj!
  log "Found Xcode project at #{xcodeproj_path}"

  # 2. Detect native dependencies in Libraries group.
  log 'Dependencies:'
  dependencies.each { |d| log " #{d}" }

  # Save for after Libraries removed.
  deps_to_add = dependencies

  # 3. Run react-native unlink for each one.
  dependencies.each do |dep|
    execute 'react-native', 'unlink', dep
  end

  # reload after react-native unlink
  load_xcodeproj!

  # 4a. TODO: Add Start Packager script
  validate_app_target!
  add_packager_script

  # 4b. Remove Libraries group from Xcode project.
  remove_libraries_group_from_project!

  xcodeproj.save

  # 5. Generate boilerplate Podfile.
  # TODO: Determine appropriate subspecs
  generate_podfile!

  # 6. Run react-native link for each dependency.
  deps_to_add.each do |dep|
    execute 'react-native', 'link', dep
  end

  # 7. pod install
  command = %w[pod install --silent]
  command << '--repo-update' if options[:repo_update]
  execute(*command, chdir: 'ios')

  # 8. SCM/git (add, commit - optional)

  # 9. Open workspace/build
  execute 'open', File.join('ios', "#{app_name}.xcworkspace")
end

#dependenciesArray<String>

A list of external dependencies from NPM requiring react-native link.

Returns:

  • (Array<String>)

    a list of NPM package names



169
170
171
172
173
174
175
176
177
# File 'lib/react_native_util/converter.rb', line 169

def dependencies
  return [] if libraries_group.nil?

  dependency_paths.map do |path|
    # Find the root above the ios/*.xcodeproj under node_modules
    root = File.expand_path '../..', path
    File.basename root
  end
end

#dependency_pathsArray<String>

Paths to Xcode projects in the Libraries group from external deps.

Returns:

  • (Array<String>)

    a list of absolute paths to Xcode projects



181
182
183
184
185
186
# File 'lib/react_native_util/converter.rb', line 181

def dependency_paths
  return [] if libraries_group.nil?

  paths = libraries_group.children.reject { |c| DEFAULT_DEPENDENCIES.include?(c.name.sub(/\.xcodeproj$/, '')) }.map(&:path)
  paths.map { |p| File.expand_path p, File.join(Dir.pwd, 'ios') }
end

#generate_podfile!Object

Generate a Podfile from a template.



216
217
218
219
220
221
# File 'lib/react_native_util/converter.rb', line 216

def generate_podfile!
  podfile_contents = ERB.new(File.read(PODFILE_TEMPLATE_PATH)).result binding
  File.open 'ios/Podfile', 'w' do |file|
    file.write podfile_contents
  end
end

#libraries_groupArray

A list, usually of PBXFileReferences, of children of the Libraries group from the xcodeproj.

Returns:

  • (Array)

    an array of child references



139
140
141
# File 'lib/react_native_util/converter.rb', line 139

def libraries_group
  xcodeproj['Libraries']
end

#load_package_json!Object

Read the contents of ./package.json into @package_json

Raises:

  • ConversionError on failure



117
118
119
120
121
122
123
# File 'lib/react_native_util/converter.rb', line 117

def load_package_json!
  @package_json = File.open('package.json') { |f| JSON.parse f.read }
rescue Errno::ENOENT
  raise ConversionError, 'Failed to load package.json. File not found. Please run from the project root.'
rescue JSON::ParserError => e
  raise ConversionError, "Failed to parse package.json: #{e.message}"
end

#load_xcodeproj!Object

Load the project at @xcodeproj_path into @xcodeproj

Raises:

  • ConversionError on failure



127
128
129
130
131
132
133
134
# File 'lib/react_native_util/converter.rb', line 127

def load_xcodeproj!
  @xcodeproj = nil # in case of exception on reopen
  @xcodeproj = Xcodeproj::Project.open xcodeproj_path
rescue Errno::ENOENT
  raise ConversionError, "Failed to open #{xcodeproj_path}. File not found."
rescue Xcodeproj::PlainInformative => e
  raise ConversionError, "Failed to load #{xcodeproj_path}: #{e.message}"
end

#packager_scriptObject



259
260
261
262
263
264
# File 'lib/react_native_util/converter.rb', line 259

def packager_script
  react_project!.targets.first.build_phases.find { |p| p.name =~ /packager/i }.shell_script.gsub(%r{../scripts}, '../node_modules/react-native/scripts')
rescue Errno::ENOENT
  log 'Could not open React.xcodeproj'
  nil
end

#react_project!Object



252
253
254
255
256
257
# File 'lib/react_native_util/converter.rb', line 252

def react_project!
  return @react_project if @react_project

  path = libraries_group.children.find { |c| c.path =~ /React.xcodeproj/ }.real_path
  @react_project = Xcodeproj::Project.open path
end

#remove_libraries_from_target(target) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
# File 'lib/react_native_util/converter.rb', line 155

def remove_libraries_from_target(target)
  log "Removing Libraries from #{target.name}"
  to_remove = target.frameworks_build_phase.files.select do |file|
    path = file.file_ref.pretty_print
    next false unless /^lib(.+)\.a$/.match?(path)

    static_libs.include?(path)
  end

  to_remove.each { |f| target.frameworks_build_phase.remove_build_file f }
end

#remove_libraries_group_from_project!Object

Remove the Libraries group from the xcodeproj in memory. Resets @libraries_group to nil as well.



145
146
147
148
149
150
151
152
153
# File 'lib/react_native_util/converter.rb', line 145

def remove_libraries_group_from_project!
  # Remove links against these static libraries
  xcodeproj.targets.reject { |t| t.name =~ /-tvOS/ }.each do |t|
    remove_libraries_from_target t
  end

  log 'Removing Libraries group'
  libraries_group.remove_from_project
end

#static_libsArray<String>

All static libraries from the Libraries group

Returns:

  • (Array<String>)

    an array of filenames



190
191
192
193
194
195
# File 'lib/react_native_util/converter.rb', line 190

def static_libs
  libraries_group.children.map do |library|
    root = File.basename(library.path).sub(/\.xcodeproj$/, '')
    "lib#{root}.a"
  end
end

#test_targetObject



203
204
205
# File 'lib/react_native_util/converter.rb', line 203

def test_target
  xcodeproj.targets.select(&:test_target_type?).reject { |t| t.name =~ /tvOS/ }.first
end

#validate_app_target!Object

Validate an assumption about the project. TODO: Provide override option.

Raises:

  • ConversionError if an application target is not found with the same name as the project.



209
210
211
212
213
# File 'lib/react_native_util/converter.rb', line 209

def validate_app_target!
  app_target = xcodeproj.targets.find { |t| t.name = app_name }
  raise ConversionError, "Unable to find target #{app_name} in #{xcodeproj_path}." if app_target.nil?
  raise ConversionError, "Target #{app_name} is not an application target." unless app_target.product_type == 'com.apple.product-type.application'
end