Skip to content

Commit

Permalink
Merge pull request #27 from cldwalker/use-ios-device
Browse files Browse the repository at this point in the history
Add new command use-ios-device to support development on iOS device
  • Loading branch information
drapanjanas committed Mar 6, 2016
2 parents 1ebfff8 + e8c6cf8 commit df1c8fb
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Luckily, this can be improved by compiling with `optimizations :none` and using
Figwheel.

#### Using Figwheel in iOS simulator
Start your app from Xcode, or just run `react-native run-ios`
Start your app from Xcode and pick a simulator target, or just run `react-native run-ios`

Then, to start development mode execute commands:
```
Expand All @@ -96,6 +96,13 @@ $ lein figwheel ios
```
This will generate index.ios.js and index.android.js which works with compiler mode`optimizations :none`.

#### Using Figwheel in real iOS device

Switch to using your iOS device: `re-natal use-ios-device real`.
If this doesn't correctly detect your computer's IP you can pass your IP address explicitly: `re-natal use-ios-device IP`

Then follow the remaining directions above for the iOS simulator except pick your connected device in Xcode

#### Using Figwheel in real Android device
To run figwheel with real Android device please read [Running on Device](https://facebook.github.io/react-native/docs/running-on-device-android.html#content).
To make it work on USB connected device I had also to do the following:
Expand Down Expand Up @@ -137,7 +144,7 @@ Start your simulator and deploy your app:
```
$ react-native run-android
```
#### Swiching between Android devices
#### Switching between Android devices
Run `use-android-device` to configure device type you want to use in development:
```
$ re-natal use-android-device <real|genymotion|avd>
Expand Down
47 changes: 41 additions & 6 deletions re-natal.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ interfaceDepsRx = /\$INTERFACE_DEPS\$/g
platformRx = /\$PLATFORM\$/g
devHostRx = /\$DEV_HOST\$/g
figwheelUrlRx = /ws:\/\/[0-9a-zA-Z\.]*:/g
appDelegateRx = /http:\/\/[^:]+/g
rnVersion = '0.20.0'
rnPackagerPort = 8081
process.title = 're-natal'
Expand Down Expand Up @@ -140,6 +141,7 @@ generateConfig = (interfaceName, projName) ->
name: projName
interface: interfaceName
androidHost: "localhost"
iosHost: "localhost"
modules: []
imageDirs: ["images"]

Expand All @@ -158,7 +160,7 @@ writeConfig = (config) ->
message

verifyConfig = (config) ->
if !config.androidHost? || !config.modules? || !config.imageDirs? || !config.interface?
if !config.androidHost? || !config.modules? || !config.imageDirs? || !config.interface? || !config.iosHost?
throw new Error 're-natal project needs to be upgraded, please run: re-natal upgrade'
config

Expand Down Expand Up @@ -212,6 +214,21 @@ configureDevHostForAndroidDevice = (deviceType) ->
catch {message}
logErr message

configureDevHostForIosDevice = (deviceType) ->
try
devHost = if deviceType == 'simulator'
'localhost'
else if deviceType == 'real'
exec('ipconfig getifaddr en0', true).toString().trim()
else
deviceType

config = readConfig()
config.iosHost = devHost
writeConfig(config)
catch {message}
logErr message

copyDevEnvironmentFiles = (interfaceName, projNameHyph, projName, devHost) ->
fs.mkdirpSync "env/dev/env/ios"
fs.mkdirpSync "env/dev/env/android"
Expand Down Expand Up @@ -411,10 +428,16 @@ generateRequireModulesCode = (modules) ->
jsCode += "modules['#{m}']=require('#{m}');";
jsCode += '\n'

updateFigwheelUrlForAndroid= (devHost) ->
updateFigwheelUrls = (androidHost, iosHost) ->
mainAndroidDevPath = "env/dev/env/android/main.cljs"
edit mainAndroidDevPath, [[figwheelUrlRx, "ws://#{androidHost}:"]]

edit mainAndroidDevPath, [[figwheelUrlRx, "ws://#{devHost}:"]]
mainIosDevPath = "env/dev/env/ios/main.cljs"
edit mainIosDevPath, [[figwheelUrlRx, "ws://#{iosHost}:"]]

updateIosAppDelegate = (projName, iosHost) ->
appDelegatePath = "ios/#{projName}/AppDelegate.m"
edit appDelegatePath, [[appDelegateRx, "http://#{iosHost}"]]

generateDevScripts = () ->
try
Expand All @@ -433,14 +456,18 @@ generateDevScripts = () ->
moduleMap = generateRequireModulesCode modulesAndImages

androidDevHost = config.androidHost
iosDevHost = config.iosHost

fs.writeFileSync 'index.ios.js', "#{moduleMap}require('figwheel-bridge').withModules(modules).start('#{projName}','ios','localhost');"
fs.writeFileSync 'index.ios.js', "#{moduleMap}require('figwheel-bridge').withModules(modules).start('#{projName}','ios','#{iosDevHost}');"
log 'index.ios.js was regenerated'
fs.writeFileSync 'index.android.js', "#{moduleMap}require('figwheel-bridge').withModules(modules).start('#{projName}','android','#{androidDevHost}');"
log 'index.android.js was regenerated'

updateFigwheelUrlForAndroid(androidDevHost)
log 'Dev server host for iOS: localhost'
updateIosAppDelegate(projName, iosDevHost)
log "AppDelegate.m was updated"

updateFigwheelUrls(androidDevHost, iosDevHost)
log 'Dev server host for iOS: ' + iosDevHost
log 'Dev server host for Android: ' + androidDevHost

catch {message}
Expand All @@ -467,6 +494,9 @@ doUpgrade = (config) ->
unless config.androidHost
config.androidHost = "localhost"

unless config.iosHost
config.iosHost = "localhost"

writeConfig(config)
log 'upgraded .re-natal'

Expand Down Expand Up @@ -542,6 +572,11 @@ cli.command 'use-android-device <type>'
.action (type) ->
configureDevHostForAndroidDevice type

cli.command 'use-ios-device <type>'
.description 'sets up the host for ios device type: \'simulator\' - localhost, \'device\' - auto detect IP on eth0, IP'
.action (type) ->
configureDevHostForIosDevice type

cli.command 'use-component <name>'
.description 'configures a custom component to work with figwheel. name is the value you pass to (js/require) function.'
.action (name) ->
Expand Down

0 comments on commit df1c8fb

Please sign in to comment.