Class: ReactNativeUtil::Project

Inherits:
Xcodeproj::Project show all
Includes:
Util
Defined in:
lib/react_native_util/project.rb

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
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#boolean_env_var?, #elapsed_from, #execute, #float_env_var, #have_command?, #log, #mac?, #platform, #run_command_with_spinner!, #validate_commands!

Instance Attribute Details

#app_nameObject

Returns the value of attribute app_name.



30
31
32
# File 'lib/react_native_util/project.rb', line 30

def app_name
  @app_name
end

Instance Method Details

#add_packager_script_from(react_project) ⇒ 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 the relevant build phase is not found, a warning is logged, and this step is skipped.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/react_native_util/project.rb', line 132

def add_packager_script_from(react_project)
  old_packager_phase = react_project.packager_phase
  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

  # Move packager script to first position. This is independent of the
  # entire Xcode build process. As an optimization, the packager can
  # load its dependencies in parallel. This is the way it is on the
  # original React.xcodeproj.
  app_target.build_phases.delete phase
  app_target.build_phases.insert 0, phase
end

#app_targetObject



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

def app_target
  targets.find { |t| t.platform_name == :ios && t.product_type == 'com.apple.product-type.application' }
end

#dependenciesArray<String>

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

Returns:

  • (Array<String>)

    a list of NPM package names



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
# File 'lib/react_native_util/project.rb', line 85

def dependencies
  return [] if libraries_group.nil?

  dependency_paths.map do |path|
    # Map each path to a Pathname, expanding $(SRCROOT) or ${SRCROOT}
    # SRCROOT = location of the app project: ./ios
    Pathname.new path.gsub(/\$(\(SRCROOT\)|{SRCROOT})/, 'ios')
  end.select do |pathname|
    # Valid if any path component named node_modules
    pathname.each_filename do |path_component|
      break true if path_component == 'node_modules'

      false
    end
  end.map do |pathname|
    # Map each selected Pathname to the root immediately under node_modules
    node_modules_found = false
    pathname.each_filename do |path_component|
      break path_component if node_modules_found

      node_modules_found = path_component == 'node_modules'
      '' # In case node_modules is the last component
      # TODO: Then what? Shouldn't happen, of course....
    end
  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



114
115
116
117
118
119
# File 'lib/react_native_util/project.rb', line 114

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

#libraries_groupObject

A representation of the Libraries group (if any) from the Xcode project.

Returns:

  • the Libraries group



48
49
50
# File 'lib/react_native_util/project.rb', line 48

def libraries_group
  self['Libraries']
end

#library_rootsObject



121
122
123
124
125
# File 'lib/react_native_util/project.rb', line 121

def library_roots
  libraries_group.children.map do |library|
    File.basename(library.path).sub(/\.xcodeproj$/, '')
  end
end

#packager_phaseObject

Returns the original Start Packager build phase (from the React.xcodeproj under node_modules). This contains the original script.

Returns:

  • the packager build phase if found

  • nil if not found



163
164
165
# File 'lib/react_native_util/project.rb', line 163

def packager_phase
  targets.first.build_phases.find { |p| p.name =~ /packager/i }
end

#remove_libraries_from_target(target) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/react_native_util/project.rb', line 68

def remove_libraries_from_target(target)
  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

  log "Removing Libraries from #{target.name}" unless to_remove.empty?
  to_remove.each do |f|
    log " Removing #{f.file_ref.pretty_print}"
    target.frameworks_build_phase.remove_build_file f
  end
end

#remove_libraries_groupObject

Remove the Libraries group from the xcodeproj in memory.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/react_native_util/project.rb', line 53

def remove_libraries_group
  # Remove links against these static libraries
  targets.select { |t| t.platform_name == :ios }.each do |t|
    remove_libraries_from_target t
  end

  unless (library_roots - DEFAULT_DEPENDENCIES).empty?
    log 'Libraries group not empty. Not removing.'
    return
  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



155
156
157
# File 'lib/react_native_util/project.rb', line 155

def static_libs
  library_roots.map { |root| "lib#{root}.a" }
end

#test_targetObject



36
37
38
# File 'lib/react_native_util/project.rb', line 36

def test_target
  targets.select(&:test_target_type?).select { |t| t.platform_name == :ios }.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.



42
43
44
# File 'lib/react_native_util/project.rb', line 42

def validate_app_target!
  raise ConversionError, "Unable to find application target in #{path}." if app_target.nil?
end