react_native_util 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6db3f0bd398429fdfe2b21315c6fbf4c0e269d1c2194d3b4c0db07339ae03921
4
- data.tar.gz: '08a734aa93a6bb080eb7fd63f932165a5fd3a4652f847cf2b5d74c03ee557df5'
3
+ metadata.gz: ec7faa11d4e1299c289b1fb7f155068749610da6c8c82c38a832bf71931a1c06
4
+ data.tar.gz: 4e84dd8004655ba30919db3fcf9af3fdb06b7e15bacd0bf77b9169abdc6afcd6
5
5
  SHA512:
6
- metadata.gz: c20af6ddf47bf1f33d6a2239b1495744aa51b494b7c0e17ac0d02f0bf5972a45f527f81d20e230b539206cb4e1cf913fcd66f4eecdc3d3271eabc0f21ec3a2b2
7
- data.tar.gz: 8052d6b6998484502602087f15dc62d1b8eda7ad6f74e6f8868ea7490be2cd4e4dcfa6e793bc81be62d47fd3784a9e06c472096337652f5330b102ccc81959e6
6
+ metadata.gz: b7da0570c0270088031c9666b1ef8b2b34e41bb08df9b3f8b7442612ddea5e450832d04380d2a739e77263b450782a263561b20fb90b50459b075be8a902bf8c
7
+ data.tar.gz: 8ee1632015a0c50e1e1722772c0bb67239d030f8cd0300decd14ec56162c5571adfe67fa3f0a26d753e4efb27a97ce11cf312445edbf0070498418463f39c29c
data/README.md CHANGED
@@ -72,6 +72,35 @@ rn -h
72
72
  rn react_pod -h
73
73
  ```
74
74
 
75
+ ## react_pod command
76
+
77
+ Converts a React Native Xcode project to use the React pod from node_modules
78
+ instead of the projects in the Libraries group. This makes it easier to manage
79
+ native dependencies while preserving compatibility with `react-native link`.
80
+ The command looks for your app's package.json in the current directory and
81
+ expects your Xcode project to be located under the ios subdirectory and have
82
+ the name specified for your app in package.json. If a Podfile is found in the
83
+ ios subdirectory, the conversion will fail.
84
+
85
+ The React.xcodeproj in the Libraries group of a project created by
86
+ `react-native init` automatically starts the Metro packager via a Run Script
87
+ build phase. When the react_pod command removes the Libraries group from your
88
+ app project, it adds an equivalent build phase to your app project so that the
89
+ packager will automatically be started when necessary by Xcode.
90
+
91
+ Use the `-u` or `--update` option to update the packager script after
92
+ updating React Native, in case the packager script on the React.xcodeproj changes
93
+ after it's removed from your project.
94
+
95
+ ### Options
96
+
97
+ |option|description|env. var.|
98
+ |------|-----------|---------|
99
+ |-h, --help|Print command help||
100
+ |-t, --trace|Print a stack trace in case of error||
101
+ |-u, --update|Update a previously converted project||
102
+ |--[no-]repo-update|Don't update the local podspec repo|REACT_NATIVE_UTIL_REPO_UPDATE|
103
+
75
104
  ## Try it out
76
105
 
77
106
  Convert examples/TestApp.
@@ -99,7 +128,7 @@ git status
99
128
 
100
129
  _Typical command output:_
101
130
  ```
102
- 2019-05-14T12:30:50-07:00 react_native_util react_pod v0.4.0
131
+ 2019-05-14T12:30:50-07:00 react_native_util react_pod v0.5.0
103
132
  2019-05-14T12:30:52-07:00 Darwin 18.5.0 x86_64
104
133
  2019-05-14T12:30:52-07:00 Ruby 2.3.7: ~/.rvm/rubies/ruby-2.3.7/bin/ruby
105
134
  2019-05-14T12:30:52-07:00 RubyGems 3.0.3: ~/.rvm/rubies/ruby-2.3.7/bin/gem
@@ -151,6 +180,7 @@ require 'react_native_util/rake'
151
180
  ReactNativeUtil::Rake::ReactPodTask.new(
152
181
  :react_pod, # task name
153
182
  'Convert project to use React pod', # description
183
+ 'Update project', # description for :update task
154
184
  chdir: '/path/to/rn/project', # path to project package.json
155
185
  repo_update: true # optionally disable pod repo update
156
186
  )
@@ -161,6 +191,16 @@ Override `chdir` at the command line:
161
191
  rake react_pod[/path/to/another/rn/project]
162
192
  ```
163
193
 
194
+ Convert project:
195
+ ```bash
196
+ rake react_pod
197
+ ```
198
+
199
+ Update converted project:
200
+ ```bash
201
+ rake react_pod:update
202
+ ```
203
+
164
204
  ## Ruby script
165
205
 
166
206
  ```Ruby
@@ -168,7 +208,13 @@ require 'react_native_util/converter'
168
208
 
169
209
  Dir.chdir '/path/to/rn/project' do
170
210
  begin
171
- ReactNativeUtil::Converter.new(repo_update: true).convert_to_react_pod!
211
+ converter = ReactNativeUtil::Converter.new(repo_update: true)
212
+
213
+ # Convert a project to use the React pod
214
+ converter.convert_to_react_pod!
215
+
216
+ # Update a converted project
217
+ converter.update_project!
172
218
  rescue ReactNativeUtil::BaseException => e
173
219
  puts "Error from #convert_to_react_pod!: #{e.message}"
174
220
  end
@@ -17,22 +17,45 @@ for more information.
17
17
  DESC
18
18
 
19
19
  command :react_pod do |c|
20
- c.syntax = "#{NAME} react_pod [OPTIONS]"
20
+ c.syntax = "#{NAME} react_pod [OPTIONS]\n rn react_pod [OPTIONS]"
21
21
  c.summary = 'Convert a React Native app to use the React pod from node_modules.'
22
22
  c.description = <<DESC
23
- [Work in progress] Converts a project created with <%= color 'react-native init', BOLD %> to use
24
- CocoaPods with the React pod from node_modules. This preserves compatibility
25
- with <%= color 'react-native link', BOLD %>. A converted project will still start the Metro packager
26
- automatically via a Run Script build phase in the Xcode project. This is an
27
- alternative to performing manual surgery on a project in Xcode.
23
+ Converts a React Native Xcode project to use the React pod from node_modules
24
+ instead of the projects in the Libraries group. This makes it easier to manage
25
+ native dependencies while preserving compatibility with <%= color 'react-native link', BOLD %>.
26
+ The command looks for your app's package.json in the current directory and
27
+ expects your Xcode project to be located under the ios subdirectory and have
28
+ the name specified for your app in package.json. If a Podfile is found in the
29
+ ios subdirectory, the conversion will fail.
30
+
31
+ The React.xcodeproj in the Libraries group of a project created by
32
+ <%= color 'react-native init', BOLD %> automatically starts the Metro packager via a Run Script
33
+ build phase. When the react_pod command removes the Libraries group from your
34
+ app project, it adds an equivalent build phase to your app project so that the
35
+ packager will automatically be started when necessary by Xcode.
36
+
37
+ Use the <%= color '-u', BOLD %> or <%= color '--update', BOLD %> option to update the packager script after
38
+ updating React Native, in case the packager script on the React.xcodeproj changes
39
+ after it's removed from your project.
28
40
  DESC
29
41
 
42
+ c.option '-u', '--update', 'Update a previously converted project (default: convert)'
30
43
  c.option '--[no-]repo-update', 'Update the local podspec repo (default: update; env. var. REACT_NATIVE_UTIL_REPO_UPDATE)'
31
44
 
45
+ c.examples = {
46
+ 'Convert an app project' => 'rn react_pod',
47
+ 'Convert an app project without updating the podspec repo' => 'rn react_pod --no-repo-update',
48
+ 'Update a converted project' => 'rn react_pod -u'
49
+ }
50
+
32
51
  c.action do |_args, opts|
33
52
  begin
34
53
  converter = Converter.new repo_update: opts.repo_update
35
- converter.convert_to_react_pod!
54
+ if opts.update
55
+ converter.update_project!
56
+ else
57
+ converter.convert_to_react_pod!
58
+ end
36
59
  exit 0
37
60
  rescue ExecutionError => e
38
61
  # Generic command failure.
@@ -55,27 +55,11 @@ module ReactNativeUtil
55
55
  # @raise ExecutionError on generic command failure
56
56
  # @raise Errno::ENOENT if a required command is not present
57
57
  def convert_to_react_pod!
58
- validate_commands! REQUIRED_COMMANDS
59
-
60
- # Make sure no uncommitted changes
61
- check_repo_status!
62
-
63
- report_configuration!
64
-
65
- raise ConversionError, "macOS required." unless mac?
66
-
67
- load_package_json!
68
- log 'package.json:'
69
- log " app name: #{app_name.inspect}"
70
-
71
- # 1. Detect project. TODO: Add an option to override.
72
- @xcodeproj_path = File.expand_path "ios/#{app_name}.xcodeproj"
73
- load_xcodeproj!
74
- log "Found Xcode project at #{xcodeproj_path}"
58
+ startup!
75
59
 
76
60
  if project.libraries_group.nil?
77
61
  log "Libraries group not found in #{xcodeproj_path}. No conversion necessary."
78
- exit 0
62
+ return
79
63
  end
80
64
 
81
65
  if File.exist? podfile_path
@@ -85,19 +69,17 @@ module ReactNativeUtil
85
69
  exit 1
86
70
  end
87
71
 
88
- project.validate_app_target!
89
-
90
72
  # Not used at the moment
91
73
  # load_react_podspec!
92
74
 
93
- # 2. Detect native dependencies in Libraries group.
75
+ # 1. Detect native dependencies in Libraries group.
94
76
  log 'Dependencies:'
95
77
  project.dependencies.each { |d| log " #{d}" }
96
78
 
97
79
  # Save for after Libraries removed.
98
80
  deps_to_add = project.dependencies
99
81
 
100
- # 3. Run react-native unlink for each one.
82
+ # 2. Run react-native unlink for each one.
101
83
  log 'Unlinking dependencies'
102
84
  project.dependencies.each do |dep|
103
85
  run_command_with_spinner! 'react-native', 'unlink', dep, log: File.join(Dir.tmpdir, "react-native-unlink-#{dep}.log")
@@ -107,23 +89,23 @@ module ReactNativeUtil
107
89
  load_xcodeproj!
108
90
  load_react_project!
109
91
 
110
- # 4. Add Start Packager script
92
+ # 3. Add Start Packager script
111
93
  project.add_packager_script_from react_project
112
94
 
113
- # 5. Generate boilerplate Podfile.
95
+ # 4. Generate boilerplate Podfile.
114
96
  generate_podfile!
115
97
 
116
- # 6. Remove Libraries group from Xcode project.
98
+ # 5. Remove Libraries group from Xcode project.
117
99
  project.remove_libraries_group
118
100
  project.save
119
101
 
120
- # 7. Run react-native link for each dependency (adds to Podfile).
102
+ # 6. Run react-native link for each dependency (adds to Podfile).
121
103
  log 'Linking dependencies'
122
104
  deps_to_add.each do |dep|
123
105
  run_command_with_spinner! 'react-native', 'link', dep, log: File.join(Dir.tmpdir, "react-native-link-#{dep}.log")
124
106
  end
125
107
 
126
- # 8. pod install
108
+ # 7. pod install
127
109
  log "Generating Pods project and ios/#{app_name}.xcworkspace"
128
110
  command = %w[pod install]
129
111
  command << '--repo-update' if options[:repo_update]
@@ -131,10 +113,76 @@ module ReactNativeUtil
131
113
 
132
114
  log 'Conversion complete ✅'
133
115
 
134
- # 9. Open workspace/build
116
+ # 8. Open workspace/build
135
117
  execute 'open', File.join('ios', "#{app_name}.xcworkspace")
136
118
 
137
- # 10. TODO: SCM/git (add, commit - optional)
119
+ # 9. TODO: SCM/git (add, commit - optional)
120
+ end
121
+
122
+ def update_project!
123
+ startup!
124
+
125
+ unless project.libraries_group.nil?
126
+ raise ConversionError, "Libraries group present in #{xcodeproj_path}. Conversion necessary. Run rn react_pod without -u."
127
+ end
128
+
129
+ unless File.exist? podfile_path
130
+ raise ConversionError, "#{podfile_path} not found. Conversion necessary. Run rn react_pod without -u."
131
+ end
132
+
133
+ log "Updating project at #{xcodeproj_path}"
134
+
135
+ # Check/update the contents of the packager script in React.xcodeproj
136
+ load_react_project!
137
+
138
+ current_script_phase = project.packager_phase
139
+
140
+ # Not an error. User may have removed it.
141
+ log "Packager build phase not found in #{xcodeproj_path}. Not updating.".yellow and return if current_script_phase.nil?
142
+
143
+ new_script_phase = react_project.packager_phase
144
+ # Totally unexpected. Exception. TODO: This is not treated as an error on conversion. Probably should be.
145
+ raise ConversionError, "Packager build phase not found in #{react_project.path}." if new_script_phase.nil?
146
+
147
+ new_script = new_script_phase.shell_script.gsub %r{../scripts}, '../node_modules/react-native/scripts'
148
+
149
+ if new_script == current_script_phase.shell_script && new_script_phase.name == current_script_phase.name
150
+ log "#{current_script_phase.name} build phase up to date. ✅"
151
+ return
152
+ end
153
+
154
+ log 'Updating packager phase.'
155
+ log " Current name: #{current_script_phase.name}"
156
+ log " New name : #{new_script_phase.name}"
157
+
158
+ current_script_phase.name = new_script_phase.name
159
+ current_script_phase.shell_script = new_script
160
+
161
+ project.save
162
+
163
+ log "Updated #{xcodeproj_path} ✅"
164
+ end
165
+
166
+ def startup!
167
+ validate_commands! REQUIRED_COMMANDS
168
+
169
+ # Make sure no uncommitted changes
170
+ check_repo_status!
171
+
172
+ report_configuration!
173
+
174
+ raise ConversionError, "macOS required." unless mac?
175
+
176
+ load_package_json!
177
+ log 'package.json:'
178
+ log " app name: #{app_name.inspect}"
179
+
180
+ # Detect project. TODO: Add an option to override.
181
+ @xcodeproj_path = File.expand_path "ios/#{app_name}.xcodeproj"
182
+ load_xcodeproj!
183
+ log "Found Xcode project at #{xcodeproj_path}"
184
+
185
+ project.validate_app_target!
138
186
  end
139
187
 
140
188
  # Read the contents of ./package.json into @package_json
@@ -1,3 +1,4 @@
1
+ require 'colored'
1
2
  require 'shellwords'
2
3
  require 'time'
3
4
  require_relative 'string'
@@ -1,7 +1,7 @@
1
1
  # Conversion tools for React Native projects
2
2
  module ReactNativeUtil
3
3
  NAME = 'react_native_util'
4
- VERSION = '0.4.0'
4
+ VERSION = '0.5.0'
5
5
  SUMMARY = 'Community utility CLI for React Native projects'
6
6
  DESCRIPTION = 'Converts a project created with react-native init to use CocoaPods with the ' \
7
7
  'React pod from node_modules. This preserves compatibility with ' \
@@ -16,8 +16,9 @@ module ReactNativeUtil
16
16
  def initialize(
17
17
  name = :react_pod,
18
18
  description = 'Convert project to use React pod',
19
+ update_description = 'Update project',
19
20
  chdir: '.',
20
- repo_update: boolean_env_var?(:REACT_NATIVE_UTIL_REPO_UPDATE)
21
+ repo_update: boolean_env_var?(:REACT_NATIVE_UTIL_REPO_UPDATE, default_value: true)
21
22
  )
22
23
  desc description
23
24
  task name, %i[path] do |_task, opts|
@@ -26,6 +27,14 @@ module ReactNativeUtil
26
27
  Converter.new(repo_update: repo_update).convert_to_react_pod!
27
28
  end
28
29
  end
30
+
31
+ desc update_description
32
+ task "#{name}:update", %i[path] do |_task, opts|
33
+ project_dir = opts[:path] || chdir
34
+ Dir.chdir project_dir do
35
+ Converter.new(repo_update: repo_update).update_project!
36
+ end
37
+ end
29
38
  end
30
39
  end
31
40
  end
@@ -1,4 +1,3 @@
1
- require 'colored'
2
1
  require 'tmpdir'
3
2
  require 'tty/platform'
4
3
  require 'tty/spinner'
@@ -18,15 +17,16 @@ module ReactNativeUtil
18
17
  def run_command_with_spinner!(*command, log: nil, chdir: nil)
19
18
  STDOUT.flush
20
19
  STDERR.flush
20
+
21
21
  spinner = TTY::Spinner.new "[:spinner] #{command.shelljoin}", format: :flip
22
22
  spinner.auto_spin
23
+
23
24
  start_time = Time.now
24
25
  execute(*command, log: nil, output: log, chdir: chdir)
25
- elapsed = Time.now - start_time
26
- spinner.success "success in #{format('%.1f', elapsed)} s"
26
+
27
+ spinner.success "success in #{elapsed_from start_time} s"
27
28
  rescue ExecutionError
28
- elapsed = Time.now - start_time
29
- spinner.error "failure in #{format('%.1f', elapsed)} s"
29
+ spinner.error "failure in #{elapsed_from start_time} s"
30
30
  STDOUT.log "See #{log} for details." if log && log.kind_of?(String)
31
31
  raise
32
32
  end
@@ -51,6 +51,10 @@ module ReactNativeUtil
51
51
  nil
52
52
  end
53
53
 
54
+ def elapsed_from(time)
55
+ format('%.1f', Time.now - time)
56
+ end
57
+
54
58
  # Return a Boolean value associated with an environment variable.
55
59
  #
56
60
  # @param var [#to_s] The name of an environment variable
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: react_native_util
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jimmy Dee