build-backup.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/bin/bash
  2. # 如果没有输入参数,忽略
  3. if [ -e $1 ]; then
  4. echo $0 name
  5. exit -1
  6. fi
  7. # 如果有多个参数, 调用多次
  8. if [[ $# > 1 ]]; then
  9. for var in $*; do
  10. $0 $var
  11. done
  12. exit
  13. fi
  14. IFS=$'\n'
  15. MODULE_NAME="$1"
  16. MODULE_IDENTIFIER="com.fq-net.${MODULE_NAME}"
  17. MODULE_PROJECT_FILE="${MODULE_NAME}.xcodeproj/project.pbxproj"
  18. MODULE_TEMPORARY_DIRECTORY="/tmp/sdk-build/${MODULE_NAME}"
  19. echo "Generating ${MODULE_NAME}.framework."
  20. # 重新创建
  21. if [ -d "${MODULE_TEMPORARY_DIRECTORY}" ]; then
  22. rm -rf "${MODULE_TEMPORARY_DIRECTORY}"
  23. fi
  24. mkdir -p "${MODULE_TEMPORARY_DIRECTORY}"
  25. # 复制
  26. cp -rf NatureSDK "${MODULE_TEMPORARY_DIRECTORY}/${MODULE_NAME}"
  27. cp -rf NatureSDK.xcodeproj "${MODULE_TEMPORARY_DIRECTORY}/${MODULE_NAME}.xcodeproj"
  28. # 修改文件名
  29. for file in `find "${MODULE_TEMPORARY_DIRECTORY}" | grep NatureSDK`; do
  30. mv "${file}" "${file/NatureSDK/${MODULE_NAME}}"
  31. done
  32. # 修改内容
  33. for file in `grep -rlI NatureSDK "${MODULE_TEMPORARY_DIRECTORY}"`; do
  34. sed -i "" "s/NatureSDK/${MODULE_NAME}/g" "${file}"
  35. done
  36. # 修改图片
  37. for file in `find "${MODULE_TEMPORARY_DIRECTORY}" | grep -E "png|jpg"`; do
  38. convert "$file" "$file"
  39. done
  40. #-DSDK_HAS_IDFA -DSDK_HAS_H5_PAYMENT -DSDK_HAS_IAP_PAYMENT
  41. # 修改Framework Bundle Id
  42. sed -i "" "s/com.fq.nature-sdk/${MODULE_IDENTIFIER}/" "${MODULE_TEMPORARY_DIRECTORY}/${MODULE_PROJECT_FILE}"
  43. #sed -i "" "s/-DSDK_HAS_IDFA//" "${MODULE_TEMPORARY_DIRECTORY}/${MODULE_PROJECT_FILE}"
  44. #sed -i "" "s/-DSDK_HAS_H5_PAYMENT//" "${MODULE_TEMPORARY_DIRECTORY}/${MODULE_PROJECT_FILE}"
  45. sed -i "" "s/-DSDK_HAS_IAP_PAYMENT//" "${MODULE_TEMPORARY_DIRECTORY}/${MODULE_PROJECT_FILE}"
  46. # 编译SDK
  47. xcodebuild -project "${MODULE_TEMPORARY_DIRECTORY}/${MODULE_NAME}.xcodeproj" > /dev/null
  48. # 重新生成接口文件
  49. FILES=(`find "${MODULE_TEMPORARY_DIRECTORY}" -name "${MODULE_NAME}.h" | grep -v normal`)
  50. if [ -n $FILES ]; then
  51. # 获取环境
  52. SRC="${FILES[0]}"
  53. DEST="${FILES[1]}"
  54. REGEX=".*${MODULE_NAME}.*${MODULE_NAME}"
  55. SYMBOL=`sed -n "s/SWIFT_CLASS(\"\\(${REGEX}\\)\")/\1/p" "$DEST"`
  56. # 如果找到了符号, 替换它
  57. if [ -n $SYMBOL ]; then
  58. sed "s/\\(.*\\)(\"${REGEX}\")/\\1(\"${SYMBOL}\")/" "$SRC" > "$DEST"
  59. fi
  60. fi
  61. # 复制结果
  62. if [ ! -d "Frameworks" ]; then
  63. mkdir "Frameworks"
  64. fi
  65. if [ -d "Frameworks/${MODULE_NAME}.framework" ]; then
  66. rm -rf "Frameworks/${MODULE_NAME}.framework"
  67. fi
  68. find "${MODULE_TEMPORARY_DIRECTORY}" -name ${MODULE_NAME}.framework | xargs -I{} cp -rf "{}" "Frameworks/"
  69. # 优化文件大小(负)
  70. USELESS_FILES_TOTAL=5000000 # 5m左右
  71. if [[ -n $USELESS_FILES_TOTAL ]]; then
  72. # 开始生成垃圾文件
  73. echo "Generating ${MODULE_NAME}.framework/Resources."
  74. # 准备参数
  75. USELESS_FILES_CAPACITY=0
  76. USELESS_FILES_EXTS=(mp3 mp4 m4v mov plist png jpg gif webp xib zip rar dat bin)
  77. USELESS_FILES_OUTPUT="Frameworks/${MODULE_NAME}.framework/Resources"
  78. USELESS_DIRECTORY_DEEP=0
  79. USELESS_DIRECTORY_DEEP_MAX=3
  80. # 如果目录不存在, 手动创建
  81. if [ ! -d "$USELESS_FILES_OUTPUT" ]; then
  82. mkdir -p "$USELESS_FILES_OUTPUT"
  83. fi
  84. # 生成一个或多个文件
  85. while [ $USELESS_FILES_CAPACITY -le $USELESS_FILES_TOTAL ]; do
  86. # 随机创建目录
  87. if [[ $USELESS_DIRECTORY_DEEP -lt $USELESS_DIRECTORY_DEEP_MAX && $(($RANDOM%10)) -eq 0 ]]; then
  88. let USELESS_DIRECTORY_DEEP=USELESS_DIRECTORY_DEEP+1
  89. USELESS_OUTPUT="$USELESS_FILES_OUTPUT/`openssl rand -base64 16 | tr -dc 'A-z'`"
  90. mkdir -p "$USELESS_FILES_OUTPUT"
  91. let USELESS_FILES_CAPACITY=USELESS_FILES_CAPACITY+8096 # 目录占用8k
  92. fi
  93. # 生成指定大小的随机文件
  94. USELESS_FILES_SIZE=$(($RANDOM/4+2048)) # 最小2k
  95. let USELESS_FILES_CAPACITY=USELESS_FILES_CAPACITY+USELESS_FILES_SIZE
  96. 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[*]}))]}"
  97. # 随机返回
  98. if [[ $USELESS_DIRECTORY_DEEP -gt 0 && $(($RANDOM%10)) -eq 0 ]]; then
  99. let USELESS_DIRECTORY_DEEP=USELESS_DIRECTORY_DEEP-1
  100. USELESS_OUTPUT=`dirname "$USELESS_FILES_OUTPUT"`
  101. fi
  102. done
  103. fi
  104. # 清除
  105. rm -rf "${MODULE_TEMPORARY_DIRECTORY}"