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>
-
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. '../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.
-
#xcodeproj ⇒ Object
readonly
- Xcodeproj::Project
-
Contents of the project at xcodproj_path.
-
#xcodeproj_path ⇒ Object
readonly
- String
-
Full path to Xcode project.
Attributes included from Util
Instance Method Summary collapse
- #add_packager_script ⇒ Object
-
#app_name ⇒ String
The name of the app as specified in package.json.
- #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.
-
#load_package_json! ⇒ Object
Read the contents of ./package.json into @package_json.
-
#load_xcodeproj! ⇒ Object
Load the project at @xcodeproj_path into @xcodeproj.
- #packager_script ⇒ Object
- #react_project! ⇒ Object
- #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?
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_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.
40 41 42 |
# File 'lib/react_native_util/converter.rb', line 40 def @options end |
#package_json ⇒ Object (readonly)
- Hash
-
Contents of ./package.json
32 33 34 |
# File 'lib/react_native_util/converter.rb', line 32 def package_json @package_json end |
#xcodeproj ⇒ Object (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_path ⇒ Object (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_script ⇒ Object
245 246 247 248 249 250 251 252 253 254 |
# File 'lib/react_native_util/converter.rb', line 245 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 = script # TODO: Move this build phase to the top of the list before the pod install. end |
#app_name ⇒ String
The name of the app as specified in package.json
203 204 205 |
# File 'lib/react_native_util/converter.rb', line 203 def app_name @app_name ||= package_json['name'] end |
#check_repo_status! ⇒ Object
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/react_native_util/converter.rb', line 227 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
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 114 115 116 117 118 |
# 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. "ios/#{package_json['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 # 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 from contents of Libraries group 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 [: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 |
#dependencies ⇒ Array<String>
A list of external dependencies from NPM requiring react-native link.
173 174 175 176 177 178 179 180 181 |
# File 'lib/react_native_util/converter.rb', line 173 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.
185 186 187 188 189 190 |
# File 'lib/react_native_util/converter.rb', line 185 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.
220 221 222 223 224 225 |
# File 'lib/react_native_util/converter.rb', line 220 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_group ⇒ Object
A representation of the Libraries group (if any) from the Xcode project.
143 144 145 |
# File 'lib/react_native_util/converter.rb', line 143 def libraries_group xcodeproj['Libraries'] end |
#load_package_json! ⇒ Object
Read the contents of ./package.json into @package_json
122 123 124 125 126 127 128 |
# File 'lib/react_native_util/converter.rb', line 122 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_xcodeproj! ⇒ Object
Load the project at @xcodeproj_path into @xcodeproj
132 133 134 135 136 137 138 139 |
# File 'lib/react_native_util/converter.rb', line 132 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_script ⇒ Object
263 264 265 266 267 268 269 270 271 |
# File 'lib/react_native_util/converter.rb', line 263 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. File not found.' nil rescue Xcodeproj::PlainInformative => e log "Could not open React.xcodeproj. #{e.}" nil end |
#react_project! ⇒ Object
256 257 258 259 260 261 |
# File 'lib/react_native_util/converter.rb', line 256 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
159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/react_native_util/converter.rb', line 159 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.
149 150 151 152 153 154 155 156 157 |
# File 'lib/react_native_util/converter.rb', line 149 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
194 195 196 197 198 199 |
# File 'lib/react_native_util/converter.rb', line 194 def static_libs libraries_group.children.map do |library| root = File.basename(library.path).sub(/\.xcodeproj$/, '') "lib#{root}.a" end end |
#test_target ⇒ Object
207 208 209 |
# File 'lib/react_native_util/converter.rb', line 207 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.
213 214 215 216 217 |
# File 'lib/react_native_util/converter.rb', line 213 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 |