Class: ReactNativeUtil::Converter
- Inherits:
-
Object
- Object
- ReactNativeUtil::Converter
- Includes:
- Util
- Defined in:
- lib/react_native_util/converter.rb
Overview
Class to perform conversion operations.
Constant Summary collapse
- DEFAULT_DEPENDENCIES =
- Array<String>
-
Xcode projects from react-native that may be in the Libraries group
%w[ ART React RCTActionSheet RCTAnimation RCTBlob RCTCameraRoll RCTGeolocation RCTImage RCTLinking RCTNetwork RCTPushNotification RCTSettings RCTTest RCTText RCTVibration RCTWebSocket ]
- PODFILE_TEMPLATE_PATH =
- String
-
Path to the Podfile template
File. '../assets/templates/Podfile.erb', __dir__
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#package_json ⇒ Object
readonly
- Hash
-
Contents of ./package.json.
-
#react_podspec ⇒ Object
readonly
Returns the value of attribute react_podspec.
-
#xcodeproj ⇒ Object
readonly
- Xcodeproj::Project
-
Contents of the project at xcodproj_path.
-
#xcodeproj_path ⇒ Object
readonly
- String
-
Full path to Xcode project.
Instance Method Summary collapse
-
#add_packager_script ⇒ Object
Adds the Start Packager script from the React.xcodeproj under node_modules to the main application target before deleting React.xcodeproj from the Libraries group.
-
#app_name ⇒ String
The name of the app as specified in package.json.
- #app_target ⇒ Object
- #check_repo_status! ⇒ Object
-
#convert_to_react_pod! ⇒ Object
Convert project to use React pod.
-
#dependencies ⇒ Array<String>
A list of external dependencies from NPM requiring react-native link.
-
#dependency_paths ⇒ Array<String>
Paths to Xcode projects in the Libraries group from external deps.
-
#generate_podfile! ⇒ Object
Generate a Podfile from a template.
-
#initialize(repo_update: nil) ⇒ Converter
constructor
A new instance of Converter.
-
#libraries_group ⇒ Object
A representation of the Libraries group (if any) from the Xcode project.
- #library_roots ⇒ Object
-
#load_package_json! ⇒ Object
Read the contents of ./package.json into @package_json.
- #load_react_podspec! ⇒ Object
- #load_subspecs_from_libraries ⇒ Object
-
#load_xcodeproj! ⇒ Object
Load the project at @xcodeproj_path into @xcodeproj.
-
#packager_phase_from_react_project ⇒ Object
Returns the original Start Packager build phase from the React.xcodeproj under node_modules.
- #podfile_path ⇒ Object
-
#react_project! ⇒ Xcodeproj::Project
Returns a Project object with the contents of the React.xcodeproj project from node_modules.
- #remove_libraries_from_target(target) ⇒ Object
-
#remove_libraries_group_from_project! ⇒ Object
Remove the Libraries group from the xcodeproj in memory.
-
#static_libs ⇒ Array<String>
All static libraries from the Libraries group.
- #test_target ⇒ Object
-
#validate_app_target! ⇒ Object
Validate an assumption about the project.
Methods included from Util
#boolean_env_var?, #execute, #float_env_var, #log, #mac?, #platform, #run_command_with_spinner!
Constructor Details
#initialize(repo_update: nil) ⇒ Converter
Returns a new instance of Converter.
48 49 50 51 52 53 54 55 |
# File 'lib/react_native_util/converter.rb', line 48 def initialize(repo_update: nil) @options = {} if repo_update.nil? @options[:repo_update] = boolean_env_var?(:REACT_NATIVE_UTIL_REPO_UPDATE, default_value: true) else @options[:repo_update] = repo_update end end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
45 46 47 |
# File 'lib/react_native_util/converter.rb', line 45 def @options end |
#package_json ⇒ Object (readonly)
- Hash
-
Contents of ./package.json
37 38 39 |
# File 'lib/react_native_util/converter.rb', line 37 def package_json @package_json end |
#react_podspec ⇒ Object (readonly)
Returns the value of attribute react_podspec.
46 47 48 |
# File 'lib/react_native_util/converter.rb', line 46 def react_podspec @react_podspec end |
#xcodeproj ⇒ Object (readonly)
- Xcodeproj::Project
-
Contents of the project at xcodproj_path
43 44 45 |
# File 'lib/react_native_util/converter.rb', line 43 def xcodeproj @xcodeproj end |
#xcodeproj_path ⇒ Object (readonly)
- String
-
Full path to Xcode project
40 41 42 |
# File 'lib/react_native_util/converter.rb', line 40 def xcodeproj_path @xcodeproj_path end |
Instance Method Details
#add_packager_script ⇒ Object
Adds the Start Packager script from the React.xcodeproj under node_modules to the main application target before deleting React.xcodeproj from the Libraries group. Adjusts paths in the script to account for the different project location. If React.xcodeproj cannot be opened, or if the relevant build phase is not found, a warning is logged, and this step is skipped.
TODO: The build phase is added after all other build phases. Ideally it can be moved to the beginning. The packager is independent of the Xcode build process. It may be started at any time. Starting it early is an optimization that allows it to load while the build is in progress. Currently it’s possible to simply drag the build phase in Xcode to a higher position after running the react_pod command.
314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/react_native_util/converter.rb', line 314 def add_packager_script old_packager_phase = packager_phase_from_react_project unless old_packager_phase log 'Could not find packager build phase in React.xcodeproj. Skipping.'.yellow return end # location of project is different relative to packager script script = old_packager_phase.shell_script.gsub(%r{../scripts}, '../node_modules/react-native/scripts') phase = app_target.new_shell_script_build_phase old_packager_phase.name phase.shell_script = script end |
#app_name ⇒ String
The name of the app as specified in package.json
252 253 254 |
# File 'lib/react_native_util/converter.rb', line 252 def app_name @app_name ||= package_json['name'] end |
#app_target ⇒ Object
256 257 258 |
# File 'lib/react_native_util/converter.rb', line 256 def app_target xcodeproj.targets.find { |t| t.name == app_name } end |
#check_repo_status! ⇒ Object
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/react_native_util/converter.rb', line 284 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
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 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 |
# File 'lib/react_native_util/converter.rb', line 59 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}" # 1. Detect project. TODO: Add an option to override. @xcodeproj_path = File. "ios/#{app_name}.xcodeproj" load_xcodeproj! log "Found Xcode project at #{xcodeproj_path}" if libraries_group.nil? log "Libraries group not found in #{xcodeproj_path}. No conversion necessary." exit 0 end if File.exist? podfile_path log "Podfile already present at #{File. podfile_path}.".red.bold log "A future release of #{NAME} may support integration with an existing Podfile." log 'This release can only convert apps that do not currently use a Podfile.' exit 1 end # Don't run yarn until we're sure we're proceeding. log 'Installing NPM dependencies with yarn' run_command_with_spinner! 'yarn', log: File.join(Dir.tmpdir, 'yarn.log') load_react_podspec! # 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. log 'Unlinking dependencies' dependencies.each do |dep| run_command_with_spinner! 'react-native', 'unlink', dep, log: File.join(Dir.tmpdir, "react-native-unlink-#{dep}.log") end # reload after react-native unlink load_xcodeproj! # 4a. Add Start Packager script validate_app_target! add_packager_script # Make a note of pod subspecs to replace Libraries group load_subspecs_from_libraries # 4b. Remove Libraries group from Xcode project. remove_libraries_group_from_project! xcodeproj.save # 5. Generate boilerplate Podfile. generate_podfile! # 6. Run react-native link for each dependency. log 'Linking dependencies' deps_to_add.each do |dep| run_command_with_spinner! 'react-native', 'link', dep, log: File.join(Dir.tmpdir, "react-native-link-#{dep}.log") end # 7. pod install log "Generating Pods project and ios/#{app_name}.xcworkspace" command = %w[pod install] command << '--repo-update' if [:repo_update] run_command_with_spinner!(*command, chdir: 'ios', log: File.join(Dir.tmpdir, 'pod-install.log')) log 'Conversion complete ✅' # 8. SCM/git (add, commit - optional) # 9. Open workspace/build execute 'open', File.join('ios', "#{app_name}.xcworkspace") end |
#dependencies ⇒ Array<String>
A list of external dependencies from NPM requiring react-native link.
207 208 209 210 211 212 213 214 215 |
# File 'lib/react_native_util/converter.rb', line 207 def dependencies return [] if libraries_group.nil? dependency_paths.map do |path| # Find the root above the ios/*.xcodeproj under node_modules root = File. '../..', path File.basename root end end |
#dependency_paths ⇒ Array<String>
Paths to Xcode projects in the Libraries group from external deps.
219 220 221 222 223 224 |
# File 'lib/react_native_util/converter.rb', line 219 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. p, File.join(Dir.pwd, 'ios') } end |
#generate_podfile! ⇒ Object
Generate a Podfile from a template.
276 277 278 279 280 281 282 |
# File 'lib/react_native_util/converter.rb', line 276 def generate_podfile! log "Generating #{podfile_path}" podfile_contents = ERB.new(File.read(PODFILE_TEMPLATE_PATH)).result binding File.open podfile_path, 'w' do |file| file.write podfile_contents end end |
#libraries_group ⇒ Object
A representation of the Libraries group (if any) from the Xcode project.
177 178 179 |
# File 'lib/react_native_util/converter.rb', line 177 def libraries_group xcodeproj['Libraries'] end |
#library_roots ⇒ Object
226 227 228 229 230 |
# File 'lib/react_native_util/converter.rb', line 226 def library_roots libraries_group.children.map do |library| File.basename(library.path).sub(/\.xcodeproj$/, '') end end |
#load_package_json! ⇒ Object
Read the contents of ./package.json into @package_json
145 146 147 148 149 150 151 |
# File 'lib/react_native_util/converter.rb', line 145 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.}" end |
#load_react_podspec! ⇒ Object
164 165 166 167 168 169 170 171 172 173 |
# File 'lib/react_native_util/converter.rb', line 164 def load_react_podspec! podspec_dir = 'node_modules/react-native' podspec_contents = File.read "#{podspec_dir}/React.podspec" podspec_contents.gsub!(/__dir__/, podspec_dir.inspect) require 'cocoapods-core' # rubocop: disable Security/Eval @react_podspec = eval(podspec_contents) # rubocop: enable Security/Eval end |
#load_subspecs_from_libraries ⇒ Object
238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/react_native_util/converter.rb', line 238 def load_subspecs_from_libraries roots = library_roots - %w[React] @subspecs_from_libraries = roots.select { |r| DEFAULT_DEPENDENCIES.include?(r) }.map do |root| case root when 'RCTLinking' 'RCTLinkingIOS' else root end end end |
#load_xcodeproj! ⇒ Object
Load the project at @xcodeproj_path into @xcodeproj
155 156 157 158 159 160 161 162 |
# File 'lib/react_native_util/converter.rb', line 155 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.}" end |
#packager_phase_from_react_project ⇒ Object
Returns the original Start Packager build phase from the React.xcodeproj under node_modules. This contains the original script.
343 344 345 346 347 348 349 350 351 |
# File 'lib/react_native_util/converter.rb', line 343 def packager_phase_from_react_project react_project!.targets.first.build_phases.find { |p| p.name =~ /packager/i } rescue Errno::ENOENT log 'Could not open React.xcodeproj. File not found.' nil rescue Xcodeproj::PlainInformative => e log "Could not open React.xcodeproj. #{e.}" nil end |
#podfile_path ⇒ Object
271 272 273 |
# File 'lib/react_native_util/converter.rb', line 271 def podfile_path 'ios/Podfile' end |
#react_project! ⇒ Xcodeproj::Project
Returns a Project object with the contents of the React.xcodeproj project from node_modules.
332 333 334 335 336 337 |
# File 'lib/react_native_util/converter.rb', line 332 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
193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/react_native_util/converter.rb', line 193 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.
183 184 185 186 187 188 189 190 191 |
# File 'lib/react_native_util/converter.rb', line 183 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_libs ⇒ Array<String>
All static libraries from the Libraries group
234 235 236 |
# File 'lib/react_native_util/converter.rb', line 234 def static_libs library_roots.map { |root| "lib#{root}.a" } end |
#test_target ⇒ Object
260 261 262 |
# File 'lib/react_native_util/converter.rb', line 260 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.
266 267 268 269 |
# File 'lib/react_native_util/converter.rb', line 266 def validate_app_target! 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 |