美文网首页
将RN作为Framework引入需要的Scripts

将RN作为Framework引入需要的Scripts

作者: 西西的一天 | 来源:发表于2019-10-22 21:12 被阅读0次

在上一篇Tip中聊到了如何将RN作为Framework引入项目,功能是搞定了,但若想把这个流程在pipeline上自动化的运行起来,还需要一些shell script来支持。

  1. build react native jsbundle
#!/bin/bash -e
build_folder="build"
if [[ -d ${build_folder} ]]; then
  rm -rf ${build_folder};
fi
mkdir -p ${build_folder}/android/res/raw
mkdir -p ${build_folder}/android/assets
mkdir -p ${build_folder}/ios

# bundle.js for ios
react-native bundle --platform='ios' --entry-file='index.js' --dev=false --minify=true --bundle-output="./${build_folder}/ios/main.jsbundle" --assets-dest="./${build_folder}/ios"
# bundle.js for android
react-native bundle --platform='android' --entry-file='index.js' --dev=false --minify=true --bundle-output="./${build_folder}/android/assets/index.bundle" --assets-dest="./${build_folder}/android/res"

# copy package.json to build_folder
cp package.json "./${build_folder}"

build_artifact="rn-artifact.zip"

zip -r "${build_artifact}" "${build_folder}"
  1. 将build的生成的artifact copy到RNContainer中,commit后打上tag。此处用了一个名为0.0.0-snapshot的tag做为最新的tag,供开发时使用。
build_artifact="rn-artifact.zip" # in pipeline work directory

echo "unzip artifact"
unzip -o "path/to/${build_artifact}"

echo "cp jsbundle and assets to rn container"
rm -rf ./RNContainer/assets
rm -rf ./RNContainer/main.jsbundle
cp ./build/ios/main.jsbundle ./RNContainer/
cp -r ./build/ios/assets ./RNContainer/

git diff --quiet HEAD -- || DIFF="true"

if [[ ${DIFF} == "true" ]]; then
    echo "git push to origin"
    git add RNContainer/*
    git commit -m "[Pipeline] Update RNContainer JS bundle and Assets"

    TAG_NAME="0.0.0-snapshot"
    git push --delete origin "refs/tags/${TAG_NAME}"
    git tag --delete "${TAG_NAME}" || true
    git tag "${TAG_NAME}"
    git push origin master --tags
else
    echo "No pod changes to be committed."
fi
  1. 在release时,就需要一个真实的tag了,真实tag的计算方式是最低位加一,这一步在pipeline上通常是手动触发的,这样做可以保证不会有很多无用版本号。

在这里有一点需要注意的是,此处更新没有使用pod的命令行,还是手动的更新了私有Cocoapods,原因在于,使用pod命令行效率很低,并且build经常失败。

CUR_DIR=$(dirname $0)
cd "${CUR_DIR}" && CUR_DIR="${PWD}"

IOS_PODS_REPO="ssh://git@your.priviate.cocoapods.git"
IOS_PODS_DIR=${IOS_PODS_REPO##*/}
IOS_PODS_DIR=${IOS_PODS_DIR%.git}

POD="RNContainer"

find . -type d -name "${IOS_PODS_DIR}" -maxdepth 1 -exec rm -rf "{}" \;

git clone "${IOS_PODS_REPO}"

# --------- Generate new realese tag & update Podsepc file ---------
sorted_tags=$(git ls-remote --tags origin | awk '{gsub("refs/tags/", "", $2); print $2}' | awk '$1~/^[0-9]+\.[0-9]+\.[0-9]+$/' | sort --version-sort)
latest_tag=$(echo "${sorted_tags}" | awk 'END{print}')
new_tag="0.0.1"

[[ ! -z $latest_tag ]] && new_tag=$(echo "${latest_tag}" | perl -pe 's/^((\d+\.)*)(\d+)(.*)$/$1.($3+1).$4/e') # increment least significant version number by 1

find . -type f \( -name "${POD.podspec"\) -maxdepth 3 | xargs sed -i '' "s/^version[[:space:]]*=.*/version = \"${new_tag}\"/g"

# --------- Copy updated Podspec files to the private podspec repo ---------
impl_dir="${CUR_DIR}/${IOS_PODS_DIR}/${POD}/${new_tag}"

[[ -d "${impl_dir}" ]] && echo "Folder ${new_tag} already exists!" && exit 1

mkdir "${impl_dir}"

find "${POD}" -type f -maxdepth 1 -name "${POD}.podspec" -exec cp {} "${impl_dir}" \;

# --------- Git tag & push ---------
cd "${CUR_DIR}"
build_artifact="rn-artifact.zip"
echo "unzip artifact"
unzip -o "../../${build_artifact}"
echo "cp jsbundle and assets to RNContainer"
rm -rf ./RNContainer/assets
rm -rf ./RNContainer/main.jsbundle
cp ./build/ios/main.jsbundle ./RNContainer/
cp -r ./build/ios/assets ./RNContainer/

git add "${POD}/*"
git commit -m "[Pipeline] Added new release tag ${new_tag}."
git tag "${new_tag}"
git push origin master
git push origin "${new_tag}"

cd "${CUR_DIR}/${IOS_PODS_DIR}"
git add "${impl_dir}"
git commit -m "[Pipeline] Added new release tag ${new_tag} for RNContainer."
git push origin master

# --------- Clean up ---------
cd "${CUR_DIR}"
find . -type d -name "${IOS_PODS_DIR}" -maxdepth 1 -exec rm -rf "{}" \;

相关文章

网友评论

      本文标题:将RN作为Framework引入需要的Scripts

      本文链接:https://www.haomeiwen.com/subject/biycvctx.html