Skip to content

Commit

Permalink
Read Podfile dependencies from third-party-podspec (#6086)
Browse files Browse the repository at this point in the history
  • Loading branch information
mokagio authored Sep 18, 2023
2 parents 16c1ed9 + 37058de commit 2e52634
Show file tree
Hide file tree
Showing 41 changed files with 50 additions and 6,217 deletions.
93 changes: 15 additions & 78 deletions bin/generate-podspecs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ function warn_missing_tag_commit() {
fi
}

function add_codegen_warning_to_podspec() {
jq '. + {"__WARNING!__": "This file is autogenerated by generate-podspecs.sh script. Do not modify manually. Re-run the script if necessary."}' "$DEST/$pod.podspec.json" > "$TMP_SPEC"
}

# Change to the expected directory.
pushd "$( dirname "$0" )" > /dev/null
popd > /dev/null
WD=$(pwd)
echo "Working directory: $WD"

# Check for cocoapods & jq
# Check for CocoaPods and jq
command -v pod > /dev/null || ( echo Cocoapods is required to generate podspecs; exit 1 )
command -v jq > /dev/null || ( echo jq is required to generate podspecs; exit 1 )

Expand Down Expand Up @@ -50,8 +54,7 @@ mkdir "$DEST"
NODE_MODULES_DIR="gutenberg/node_modules"

# Generate the external (non-RN podspecs)
EXTERNAL_PODSPECS=$(find "$NODE_MODULES_DIR/react-native/third-party-podspecs" \
"$NODE_MODULES_DIR/@react-native-community/blur" \
EXTERNAL_PODSPECS=$(find "$NODE_MODULES_DIR/@react-native-community/blur" \
"$NODE_MODULES_DIR/@react-native-masked-view/masked-view" \
"$NODE_MODULES_DIR/@react-native-community/slider" \
"$NODE_MODULES_DIR/@react-native-clipboard/clipboard" \
Expand Down Expand Up @@ -87,7 +90,7 @@ do

# Add warning to bottom
TMP_SPEC=$(mktemp)
jq '. + {"__WARNING!__": "This file is autogenerated by generate-podspecs.sh script. Do not modify manually. Re-run the script if necessary."}' "$DEST/$pod.podspec.json" > "$TMP_SPEC"
add_codegen_warning_to_podspec
mv "$TMP_SPEC" "$DEST/$pod.podspec.json"

# As a last step check if podspec has a "tag" or "commit" field in "source"
Expand All @@ -98,14 +101,12 @@ done
# Change to the React Native directory to get relative paths for the RN podspecs
pushd "$NODE_MODULES_DIR/react-native" > /dev/null

RN_DIR="./"
SCRIPTS_PATH="./scripts/"
CODEGEN_REPO_PATH="../packages/react-native-codegen"
CODEGEN_NPM_PATH="../react-native-codegen"
PACKAGE_JSON_PATH="./package.json"
SRCS_DIR=${SRCS_DIR:-$(cd "./Libraries" && pwd)}
RN_VERSION=$(cat ./package.json | grep -m 1 version | sed 's/[^0-9.]//g')
HERMES_ENABLED="true"

RN_PODSPECS=$(find * -type f -name "*.podspec" -not -name "React-rncore.podspec" -not -path "third-party-podspecs/*" -not -path "*Fabric*" -print)
TMP_DEST=$(mktemp -d)
Expand All @@ -115,7 +116,14 @@ do
pod=$(basename "$podspec" .podspec)
path=$(dirname "$podspec")

# The only one we need is React-jsc, all the others can be skipped.
if [[ $pod != 'React-jsc' ]]; then
echo "Skipping copying React Native $pod podspec..."
continue
fi

echo "Generating podspec for $pod with path $path"

pod ipc spec "$podspec" > "$TMP_DEST/$pod.podspec.json"
# Removes message [Codegen] Found at the beginning of the file
sed -i '' -e '/\[Codegen\] Found/d' "$TMP_DEST/$pod.podspec.json"
Expand All @@ -128,82 +136,11 @@ do

# Add warning to bottom
TMP_SPEC=$(mktemp)
jq '. + {"__WARNING!__": "This file is autogenerated by generate-podspecs.sh script. Do not modify manually. Re-run the script if necessary."}' "$DEST/$pod.podspec.json" > "$TMP_SPEC"
add_codegen_warning_to_podspec
mv "$TMP_SPEC" "$DEST/$pod.podspec.json"

# As a last step check if podspec has a "tag" or "commit" field in "source"
warn_missing_tag_commit

# FBReactNativeSpec needs special treatment because of react-native-codegen code generation
if [[ "$pod" == "FBReactNativeSpec" ]]; then
echo " ==> Patching $pod podspec"
# First move it to its own folder
mkdir -p "$DEST/FBReactNativeSpec"
mv "$DEST/FBReactNativeSpec.podspec.json" "$DEST/FBReactNativeSpec"

# Then we generate FBReactNativeSpec-generated.mm and FBReactNativeSpec.h files.
# They are normally generated during compile time using a Script Phase in FBReactNativeSpec added via the `use_react_native_codegen` function.
# This script is inside node_modules/react-native/scripts folder. Since we don't have the node_modules when compiling WPiOS,
# we're calling the script here manually to generate these files ahead of time.
SCHEMA_FILE="$TMP_DEST/schema.json"
NODE_BINARY="${NODE_BINARY:-$(command -v node || true)}"

if [ -d "$CODEGEN_REPO_PATH" ]; then
CODEGEN_PATH=$(cd "$CODEGEN_REPO_PATH" && pwd)
elif [ -d "$CODEGEN_NPM_PATH" ]; then
CODEGEN_PATH=$(cd "$CODEGEN_NPM_PATH" && pwd)
else
echo "Error: Could not determine react-native-codegen location. Try running 'yarn install' or 'npm install' in your project root." 1>&2
exit 1
fi

if [ ! -d "$CODEGEN_PATH/lib" ]; then
describe "Building react-native-codegen package"
bash "$CODEGEN_PATH/scripts/oss/build.sh"
fi

# Generate React-Codegen
# A copy of codegen_utils.rb is done to modify the content within get_react_codegen_spec
# this enables getting the schema for React-Codegen in runtime by printing the content.
echo "Generating React-Codegen"
REACT_NATIVE_CODEGEN_UTILS_PATH="$SCRIPTS_PATH/cocoapods/codegen_utils.rb"
REACT_NATIVE_CODEGEN_UTILS_MODIFIED_PATH="$SCRIPTS_PATH/cocoapods/codegen_utils_modified.rb"
# Making a temp copy of codegen_utils.rb
cp $REACT_NATIVE_CODEGEN_UTILS_PATH $REACT_NATIVE_CODEGEN_UTILS_MODIFIED_PATH
# Manually add the min_ios_version_supported variable to the CodegenUtils class
# The modified script won't be able to detect min_ios_version_supported otherwise
echo "
def min_ios_version_supported
'12.4'
end
" >> "$REACT_NATIVE_CODEGEN_UTILS_MODIFIED_PATH"
# Modify the get_react_codegen_spec method to return the result using print and JSON.pretty
sed -i '' -e "s/:git => ''/:git => 'https:\/\/github.com\/facebook\/react-native.git', :tag => 'v$RN_VERSION'/" "$REACT_NATIVE_CODEGEN_UTILS_MODIFIED_PATH"
sed -i '' -e 's/return spec/print JSON.pretty_generate(spec)/' "$REACT_NATIVE_CODEGEN_UTILS_MODIFIED_PATH"
# Run get_react_codegen_spec and generate React-Codegen.podspec.json
ruby -r "./scripts/cocoapods/codegen_utils_modified.rb" -e "CodegenUtils.new.get_react_codegen_spec('$PACKAGE_JSON_PATH', hermes_enabled:$HERMES_ENABLED)" > "$DEST/React-Codegen.podspec.json"
TMP_ReactCodeGenSpec=$(mktemp)
jq '.source_files = "third-party-podspecs/FBReactNativeSpec/**/*.{c,h,m,mm,cpp}"' "$DEST/React-Codegen.podspec.json" > "$TMP_ReactCodeGenSpec"
mv "$TMP_ReactCodeGenSpec" "$DEST/React-Codegen.podspec.json"
# Remove temp copy of codegen_utils.rb
rm $REACT_NATIVE_CODEGEN_UTILS_MODIFIED_PATH

echo "Generating schema from Flow types"
"$NODE_BINARY" "$CODEGEN_PATH/lib/cli/combine/combine-js-to-schema-cli.js" "$SCHEMA_FILE" "$SRCS_DIR"

echo "Generating native code from schema (iOS)"
"$NODE_BINARY" "./scripts/generate-specs-cli.js" -p "ios" -s "$SCHEMA_FILE" -o "$DEST/FBReactNativeSpec"

# Removing unneeded files
find "$DEST/FBReactNativeSpec" -type f -not -name "FBReactNativeSpec.podspec.json" -not -name "FBReactNativeSpec-generated.mm" -not -name "FBReactNativeSpec.h" -not -name "FBReactNativeSpec.h" -delete

# Removing 'script_phases' that shouldn't be needed anymore.
# Removing 'prepare_command' that includes additional steps to create intermediate folders to keep generated files which won't be needed.
# Removing 'source.tag' as we'll use a commit hash from gutenberg-mobile instead.
TMP_FBReactNativeSpec=$(mktemp)
jq --arg COMMIT_HASH "$COMMIT_HASH" 'del(.script_phases) | del(.prepare_command) | del(.source.tag) | .source.git = "https://github.com/wordpress-mobile/gutenberg-mobile.git" | .source.commit = $COMMIT_HASH | .source.submodules = "true" | .source_files = "third-party-podspecs/FBReactNativeSpec/**/*.{c,h,m,mm,cpp}"' "$DEST/FBReactNativeSpec/FBReactNativeSpec.podspec.json" > "$TMP_FBReactNativeSpec"
mv "$TMP_FBReactNativeSpec" "$DEST/FBReactNativeSpec/FBReactNativeSpec.podspec.json"
fi
done
popd > /dev/null

Expand Down
75 changes: 34 additions & 41 deletions ios-xcframework/Podfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require 'json'
require 'xcodeproj'

GUTENBERG_PATH = '../gutenberg'
Expand All @@ -10,57 +11,49 @@ HERMES_ENABLED = ENV.fetch('HERMES_ENABLED', true)

puts "[Gutenberg] Installing pods with Hermes #{HERMES_ENABLED ? 'enabled' : 'disabled'}"

# Note that the pods in this array might seem unused if you look for
# `import` statements in this codebase. However, make sure to also check
# whether they are used in the gutenberg-mobile and Gutenberg projects.
#
# Also notice that these are not all the dependencies the project uses.
# Later in the config, we call use_react_native! which fetches more.
#
# See https://github.com/wordpress-mobile/gutenberg-mobile/issues/5025
DEPENDENCIES = %w[
react-native-safe-area
react-native-safe-area-context
react-native-video
react-native-webview
RNSVG
react-native-slider
BVLinearGradient
react-native-get-random-values
react-native-blur
RNScreens
RNReanimated
RNGestureHandler
RNCMaskedView
RNCClipboard
RNFastImage
React-jsc
].freeze

def gutenberg_dependencies
podspec_prefix = '..'

computed_dependencies = DEPENDENCIES.dup

# Use a custom RNReanimated version while we coordinate a fix upstream
computed_dependencies.delete('RNReanimated')

computed_dependencies.delete('React-jsc') unless HERMES_ENABLED

computed_dependencies.each do |pod_name|
pod pod_name, podspec: "#{podspec_prefix}/third-party-podspecs/#{pod_name}.podspec.json"
end

def setup_rnreanimated_pod_from_fork
# This is required to workaround an issue with RNReanimated after upgrading to version 2.17.0
rn_node_modules = File.join(Dir.pwd, '..', 'gutenberg', 'node_modules')
raise "Could not find node modules at given path #{rn_node_modules}" unless File.exist? rn_node_modules

ENV['REACT_NATIVE_NODE_MODULES_DIR'] = rn_node_modules
puts "[Gutenberg] Set REACT_NATIVE_NODE_MODULES_DIR env var for RNReanimated to #{rn_node_modules}"

# Use a custom RNReanimated version while we coordinate a fix upstream
pod 'RNReanimated', git: 'https://github.com/wordpress-mobile/react-native-reanimated', branch: 'wp-fork-2.17.0'
end

def gutenberg_dependencies
Dir['../third-party-podspecs/*.podspec.json'].each do |podspec_path|
pod_name = JSON.parse(File.read(podspec_path))['name']

raise "Could not find pod name in JSON spec at #{podspec_path}" unless pod_name

# Skip the standard RNReanimated podspec because we use a custom version
# while working on an upstream fix
#
# https://github.com/software-mansion/react-native-reanimated/pull/4684
if pod_name == 'RNReanimated'
setup_rnreanimated_pod_from_fork
next
end

# Skip React-jsc when using Hermes
#
# Context: React native supports different JavaScript engines.
# Hermes is the recommended engine, but we always have the option to switch back to JavaScriptCore (JSC).
# Of course, if we use one we don't need to add the other.
#
# See also:
#
# - https://github.com/facebook/react-native/tree/598b7ed690d908c408adea970d26382c834f5ead/packages/react-native/ReactCommon/jsc
# - https://reactnative.dev/docs/hermes
next if pod_name == 'React-jsc' && HERMES_ENABLED == true

pod pod_name, podspec: podspec_path
end
end

VERSION_XCCONFIG_PATH = File.join(File.expand_path(__dir__), 'Config', 'Gutenberg-Shared.xcconfig')
IOS_VERSION_KEY = 'IPHONEOS_DEPLOYMENT_TARGET'
APP_IOS_DEPLOYMENT_TARGET = Gem::Version.new(Xcodeproj::Config.new(VERSION_XCCONFIG_PATH).to_hash[IOS_VERSION_KEY])
Expand Down
11 changes: 1 addition & 10 deletions ios-xcframework/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,6 @@ PODS:
- React-jsiexecutor (= 0.71.11)
- React-jsinspector (= 0.71.11)
- React-perflogger (= 0.71.11)
- React-jsc (0.71.11):
- React-jsc/Fabric (= 0.71.11)
- React-jsi (= 0.71.11)
- React-jsc/Fabric (0.71.11):
- React-jsi (= 0.71.11)
- React-jsi (0.71.11):
- boost (= 1.76.0)
- DoubleConversion
Expand Down Expand Up @@ -461,7 +456,6 @@ DEPENDENCIES:
- React-CoreModules (from `../gutenberg/node_modules/react-native/React/CoreModules`)
- React-cxxreact (from `../gutenberg/node_modules/react-native/ReactCommon/cxxreact`)
- React-hermes (from `../gutenberg/node_modules/react-native/ReactCommon/hermes`)
- React-jsc (from `../third-party-podspecs/React-jsc.podspec.json`)
- React-jsi (from `../gutenberg/node_modules/react-native/ReactCommon/jsi`)
- React-jsiexecutor (from `../gutenberg/node_modules/react-native/ReactCommon/jsiexecutor`)
- React-jsinspector (from `../gutenberg/node_modules/react-native/ReactCommon/jsinspector`)
Expand Down Expand Up @@ -540,8 +534,6 @@ EXTERNAL SOURCES:
:path: "../gutenberg/node_modules/react-native/ReactCommon/cxxreact"
React-hermes:
:path: "../gutenberg/node_modules/react-native/ReactCommon/hermes"
React-jsc:
:podspec: "../third-party-podspecs/React-jsc.podspec.json"
React-jsi:
:path: "../gutenberg/node_modules/react-native/ReactCommon/jsi"
React-jsiexecutor:
Expand Down Expand Up @@ -636,7 +628,6 @@ SPEC CHECKSUMS:
React-CoreModules: ffd19b082fc36b9b463fedf30955138b5426c053
React-cxxreact: 8b3dd87e3b8ea96dd4ad5c7bac8f31f1cc3da97f
React-hermes: be95942c3f47fc032da1387360413f00dae0ea68
React-jsc: 711d76870c4cdb8a0dc2dbaaf6d5898f278f8a6b
React-jsi: 9978e2a64c2a4371b40e109f4ef30a33deaa9bcb
React-jsiexecutor: 18b5b33c5f2687a784a61bc8176611b73524ae77
React-jsinspector: b6ed4cb3ffa27a041cd440300503dc512b761450
Expand Down Expand Up @@ -674,6 +665,6 @@ SPEC CHECKSUMS:
WordPress-Aztec-iOS: fbebd569c61baa252b3f5058c0a2a9a6ada686bb
Yoga: f7decafdc5e8c125e6fa0da38a687e35238420fa

PODFILE CHECKSUM: 2523902ce6070e39dd8ec3dee9afd7d928bf4b66
PODFILE CHECKSUM: e9562063718a2c990efeba80fc1f8ab6b8e008ac

COCOAPODS: 1.12.1
26 changes: 0 additions & 26 deletions third-party-podspecs/DoubleConversion.podspec.json

This file was deleted.

19 changes: 0 additions & 19 deletions third-party-podspecs/FBLazyVector.podspec.json

This file was deleted.

This file was deleted.

Loading

0 comments on commit 2e52634

Please sign in to comment.