#!/bin/bash # 如果没有输入参数,忽略 if [ -e $1 ]; then echo $0 name exit -1 fi # 如果有多个参数, 调用多次 if [[ $# > 1 ]]; then for var in $*; do $0 $var done exit fi IFS=$'\n' MODULE_NAME="$1" MODULE_IDENTIFIER="com.fq-net.${MODULE_NAME}" MODULE_PROJECT_FILE="${MODULE_NAME}.xcodeproj/project.pbxproj" MODULE_TEMPORARY_DIRECTORY="/tmp/sdk-build/${MODULE_NAME}" echo "Generating ${MODULE_NAME}.framework." # 重新创建 if [ -d "${MODULE_TEMPORARY_DIRECTORY}" ]; then rm -rf "${MODULE_TEMPORARY_DIRECTORY}" fi mkdir -p "${MODULE_TEMPORARY_DIRECTORY}" # 复制 cp -rf NatureSDK "${MODULE_TEMPORARY_DIRECTORY}/${MODULE_NAME}" cp -rf NatureSDK.xcodeproj "${MODULE_TEMPORARY_DIRECTORY}/${MODULE_NAME}.xcodeproj" # 修改文件名 for file in `find "${MODULE_TEMPORARY_DIRECTORY}" | grep NatureSDK`; do mv "${file}" "${file/NatureSDK/${MODULE_NAME}}" done # 修改内容 for file in `grep -rlI NatureSDK "${MODULE_TEMPORARY_DIRECTORY}"`; do sed -i "" "s/NatureSDK/${MODULE_NAME}/g" "${file}" done # 修改图片 for file in `find "${MODULE_TEMPORARY_DIRECTORY}" | grep -E "png|jpg"`; do convert "$file" "$file" done #-DSDK_HAS_IDFA -DSDK_HAS_H5_PAYMENT -DSDK_HAS_IAP_PAYMENT # 修改Framework Bundle Id sed -i "" "s/com.fq.nature-sdk/${MODULE_IDENTIFIER}/" "${MODULE_TEMPORARY_DIRECTORY}/${MODULE_PROJECT_FILE}" #sed -i "" "s/-DSDK_HAS_IDFA//" "${MODULE_TEMPORARY_DIRECTORY}/${MODULE_PROJECT_FILE}" #sed -i "" "s/-DSDK_HAS_H5_PAYMENT//" "${MODULE_TEMPORARY_DIRECTORY}/${MODULE_PROJECT_FILE}" sed -i "" "s/-DSDK_HAS_IAP_PAYMENT//" "${MODULE_TEMPORARY_DIRECTORY}/${MODULE_PROJECT_FILE}" # 编译SDK xcodebuild -project "${MODULE_TEMPORARY_DIRECTORY}/${MODULE_NAME}.xcodeproj" > /dev/null # 重新生成接口文件 FILES=(`find "${MODULE_TEMPORARY_DIRECTORY}" -name "${MODULE_NAME}.h" | grep -v normal`) if [ -n $FILES ]; then # 获取环境 SRC="${FILES[0]}" DEST="${FILES[1]}" REGEX=".*${MODULE_NAME}.*${MODULE_NAME}" SYMBOL=`sed -n "s/SWIFT_CLASS(\"\\(${REGEX}\\)\")/\1/p" "$DEST"` # 如果找到了符号, 替换它 if [ -n $SYMBOL ]; then sed "s/\\(.*\\)(\"${REGEX}\")/\\1(\"${SYMBOL}\")/" "$SRC" > "$DEST" fi fi # 复制结果 if [ ! -d "Frameworks" ]; then mkdir "Frameworks" fi if [ -d "Frameworks/${MODULE_NAME}.framework" ]; then rm -rf "Frameworks/${MODULE_NAME}.framework" fi find "${MODULE_TEMPORARY_DIRECTORY}" -name ${MODULE_NAME}.framework | xargs -I{} cp -rf "{}" "Frameworks/" # 优化文件大小(负) USELESS_FILES_TOTAL=5000000 # 5m左右 if [[ -n $USELESS_FILES_TOTAL ]]; then # 开始生成垃圾文件 echo "Generating ${MODULE_NAME}.framework/Resources." # 准备参数 USELESS_FILES_CAPACITY=0 USELESS_FILES_EXTS=(mp3 mp4 m4v mov plist png jpg gif webp xib zip rar dat bin) USELESS_FILES_OUTPUT="Frameworks/${MODULE_NAME}.framework/Resources" USELESS_DIRECTORY_DEEP=0 USELESS_DIRECTORY_DEEP_MAX=3 # 如果目录不存在, 手动创建 if [ ! -d "$USELESS_FILES_OUTPUT" ]; then mkdir -p "$USELESS_FILES_OUTPUT" fi # 生成一个或多个文件 while [ $USELESS_FILES_CAPACITY -le $USELESS_FILES_TOTAL ]; do # 随机创建目录 if [[ $USELESS_DIRECTORY_DEEP -lt $USELESS_DIRECTORY_DEEP_MAX && $(($RANDOM%10)) -eq 0 ]]; then let USELESS_DIRECTORY_DEEP=USELESS_DIRECTORY_DEEP+1 USELESS_OUTPUT="$USELESS_FILES_OUTPUT/`openssl rand -base64 16 | tr -dc 'A-z'`" mkdir -p "$USELESS_FILES_OUTPUT" let USELESS_FILES_CAPACITY=USELESS_FILES_CAPACITY+8096 # 目录占用8k fi # 生成指定大小的随机文件 USELESS_FILES_SIZE=$(($RANDOM/4+2048)) # 最小2k let USELESS_FILES_CAPACITY=USELESS_FILES_CAPACITY+USELESS_FILES_SIZE head -c $USELESS_FILES_SIZE /dev/urandom > "$USELESS_FILES_OUTPUT/`openssl rand -base64 16 | tr -dc 'A-z'`.${USELESS_FILES_EXTS[$(($RANDOM%${#USELESS_FILES_EXTS[*]}))]}" # 随机返回 if [[ $USELESS_DIRECTORY_DEEP -gt 0 && $(($RANDOM%10)) -eq 0 ]]; then let USELESS_DIRECTORY_DEEP=USELESS_DIRECTORY_DEEP-1 USELESS_OUTPUT=`dirname "$USELESS_FILES_OUTPUT"` fi done fi # 清除 rm -rf "${MODULE_TEMPORARY_DIRECTORY}"