Skip to content

Instantly share code, notes, and snippets.

@SudoPlz

SudoPlz/README.md

Last active Aug 23, 2018
Embed
What would you like to do?
React-native import fixing script

React-native import fixing script

This script fixes imports so that they work both with react-native link React projects and cocoapods React projects.

Usage:

./importFixingUtil.sh . RCTConvert

where param1 is the directory you want it to scan and change, and param2 is the name of the React import you want to change.

Example

ios/ExampleApp/AppDelegate.m:

Before running the script:

...
#import <React/UIView+React.h>
...

After running the script ./importFixingUtil.sh ios/ExampleApp/ UIView+React:

...
#if __has_include(<React/UIView+React.h>)
    #import <React/UIView+React.h>
#else
    #import "UIView+React.h"
#endif
...
  • This script runs as is on Linux shell.
  • For OSX shell I
  1. renamed the find command to gfind and the sed command to gsed and then
  2. installed brew install findutils gnu-sed (Found that here)
  3. commented out the chmod line
#!/bin/bash
set -e
main() {
if [[ "$#" -lt 2 ]]; then
echo "Usage: $0 <dir> <anytext>" 1>&2
exit 1
fi
# reinvoke this script, replacing directory name with '--',
# then perform the invoked seds with some care.
if [[ -d "$1" ]]; then
find "$1" -type f \( -name '*.m' -o -name '*.h' \) -exec "$0" -- "$2" {} +
elif [[ "$1" = '--' ]]; then
shift
do_seds "$@"
fi
}
do_seds() {
iname="$1"
shift
del=$'\a'
nl=$'\\\n'
frm="#import <React/$iname.h>"
to="#if __has_include(<React/$iname.h>)$nl #import <React/$iname.h>$nl#else$nl #import \"$iname.h\"$nl#endif"
for file; do
if sed "s$del^$frm\$$del$to$del" "$file" > "$file".$$; then
if cmp -s "$file" "$file".$$; then
# nothing changed; delete the new file
rm -f "$file".$$
else
# if chmod supports --reference, retain perms that way
chmod --reference="$file" "$file".$$
# leave the original file around, with ~ suffix
mv "$file" "$file"~
mv "$file".$$ "$file"
fi
else
echo "$file: failed to process" 1>&2
rm -f "$file".$$
fi
done
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment