#!/bin/bash set -e IFS=$'\n' # 生成比较正经一点的名字, 但需要字典支持 if [ -f "$DIR/dictionary.txt" ]; then WORDs=(`cat "$DIR/dictionary.txt" | cut -d ' ' -f1`) fi # 配置参数 SDK_HAS_IDFA=1 SDK_HAS_H5_PAYMENT=1 SDK_HAS_IAP_PAYMENT=1 SDK_HAS_PROTECT_NETWORK_ENCRYPT=1 SDK_HAS_PROTECT_NETWORK_CAPTURE=0 #SDK_USELESS_FILES_TOTAL=50000000 # 50m左右 #SDK_USELESS_FILES_TOTAL= # 不生成 # 生成随机名 # 生成随机名 function RANDOM_NAME() { if [ -n $WORDs ]; then local name= while [ ${#name} -lt 16 ]; do local index=$(($RANDOM%${#WORDs[@]})) name="$name-${WORDs[${index}]}" done # 删除中划线并且将首字母大写 echo ${name:1} | awk -F[-] '{for(i=1;i<=NF;i++) printf toupper(substr($i,1,1)) substr($i,2); printf "\n";}' else # 生成uuid类型的名字 echo `openssl rand -hex 8` fi }; # 编译工具 function BUILD_TOOL() { cat > "$1.m" << EOF #import FOUNDATION_STATIC_INLINE id fqse(id s, uint32_t o) { NSUInteger len = [s length]; if (len == 0) { return s; } char offset = o % 256; // 0-255 char* source = (void*)[s UTF8String]; // utf8-ptr char* destination = malloc(len + 1 + 4); // output memcpy(destination, &o, 4); for (int i = 0; i < len; ++i) { destination[4 + ((i + o) % len)] = source[i] - offset--; } id result = [[NSData dataWithBytes:destination length:len + 1 + 4] base64EncodedStringWithOptions: 0]; free(destination); return result; } FOUNDATION_STATIC_INLINE id fqsd(id s) { NSData* d = [[NSData alloc] initWithBase64EncodedString:s options:0]; NSUInteger len = d.length - 1 - 4; if (len == 0) { return s; } uint32_t o = 0; char* source = (void*)d.bytes; // utf8-ptr char* destination = malloc(len + 1); // output memcpy(&o, source, 4); char offset = o % 256; // 0-255 for (int i = 0; i < len; ++i) { destination[i] = source[4 + (i + o) % len] + offset--; } destination[len] = 0; id result = [NSString stringWithUTF8String:destination]; free(destination); return result; } int main(int argc, char* argv[]) { if (argc != 3) { return -1; } NSString* ptr = fqse([NSString stringWithUTF8String: argv[1]], atoi(argv[2])); printf("%s\n", ptr.UTF8String); return 0; } EOF cc "$1.m" -o "$1" -framework Foundation } # 如果没有输入参数,忽略 if [ -e $1 ]; then echo "$0 name" echo "$0 -r|-s" exit -1 fi # 如果第一个参数是-, 那么就是工具要求 if [[ $1 == -* ]]; then case $1 in "-r") echo "$(RANDOM_NAME)" ;; "-s") tool="/tmp/sdk-build/tool/encode" if [[ ! -f "$tool" ]]; then if [[ ! -d `dirname "$tool"` ]]; then mkdir -p "`dirname "$tool"`" fi BUILD_TOOL "$tool" fi if [[ -z $2 ]]; then cat "${tool}.m" exit 0 fi echo "`$tool "$2" "$RANDOM"`" ;; esac exit 0 fi # 如果有多个参数, 调用多次 if [[ $# > 1 ]]; then for var in $*; do $0 $var done exit fi 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}" # 删除旧SDK if [ -d "Frameworks/${MODULE_NAME}.framework" ]; then rm -rf "Frameworks/${MODULE_NAME}.framework" fi # 复制 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 # 修改内容2 for file in `grep -rlI sdk_ "${MODULE_TEMPORARY_DIRECTORY}"`; do sed -i "" "s/sdk_/${MODULE_NAME}_/g" "${file}" done # 修改图片 for file in `find "${MODULE_TEMPORARY_DIRECTORY}" | grep -E "png|jpg"`; do # 需要ImageMagick支持 convert "$file" "$file" done # 修改Framework Bundle Id sed -i "" "s/com.fq.nature-sdk/${MODULE_IDENTIFIER}/" "${MODULE_TEMPORARY_DIRECTORY}/${MODULE_PROJECT_FILE}" # 是否是需要关闭IDFA if [ ! $SDK_HAS_IDFA -eq 1 ]; then # off sed -i "" "s/\\b-DSDK_HAS_IDFA\\b//" "${MODULE_TEMPORARY_DIRECTORY}/${MODULE_PROJECT_FILE}" fi # 是否是需要关闭H5支付 if [ ! $SDK_HAS_H5_PAYMENT -eq 1 ]; then # off sed -i "" "s/\\b-DSDK_HAS_H5_PAYMENT\\b//" "${MODULE_TEMPORARY_DIRECTORY}/${MODULE_PROJECT_FILE}" fi # 是否是需要关闭iAP支付 if [ ! $SDK_HAS_IAP_PAYMENT -eq 1 ]; then # off sed -i "" "s/\\b-DSDK_HAS_IAP_PAYMENT\\b//" "${MODULE_TEMPORARY_DIRECTORY}/${MODULE_PROJECT_FILE}" fi if [ ! $SDK_HAS_PROTECT_NETWORK_CAPTURE -eq 1 ]; then # off sed -i "" "s/\\b-DSDK_HAS_PROTECT_NETWORK_CAPTURE\\b//" "${MODULE_TEMPORARY_DIRECTORY}/${MODULE_PROJECT_FILE}" fi if [ ! $SDK_HAS_PROTECT_NETWORK_ENCRYPT -eq 1 ]; then # off sed -i "" "s/\\b-DSDK_HAS_PROTECT_NETWORK_ENCRYPT\\b//" "${MODULE_TEMPORARY_DIRECTORY}/${MODULE_PROJECT_FILE}" fi # 编译SDK xcodebuild -project "${MODULE_TEMPORARY_DIRECTORY}/${MODULE_NAME}.xcodeproj" > /dev/null # 重新生成接口文件 OUTPUT=(`find "${MODULE_TEMPORARY_DIRECTORY}" -name "${MODULE_NAME}.framework"`) if [[ -n "$OUTPUT" ]]; then # 获取环境 SRC="$OUTPUT/Headers/${MODULE_NAME}.h" DEST="$OUTPUT/Headers/${MODULE_NAME}-Export.h" REGEX=".*${MODULE_NAME}.*${MODULE_NAME}" SYMBOL=`sed -n "s/SWIFT_CLASS(\"\\(${REGEX}\\)\")/\1/p" "$SRC"` SYMBOL=`echo $SYMBOL | cut -d ' ' -f1` # 如果找到了符号, 替换它 if [[ -z $SYMBOL ]]; then echo "SRC: $SRC" echo "DEST: $DEST" echo "SYMBOL: $SYMBOL" echo "Unable to find symbol." exit -1 fi sed -e "s/\\(.*\\)(\"${REGEX}\")/\\1(\"${SYMBOL}\")/" "$DEST" > "$SRC" rm "$DEST" fi # 复制结果 if [ ! -d "Frameworks" ]; then mkdir "Frameworks" fi find "${MODULE_TEMPORARY_DIRECTORY}" -name ${MODULE_NAME}.framework | xargs -I{} cp -rf "{}" "Frameworks/" # 优化文件大小(负) USELESS_FILES_TOTAL=$SDK_USELESS_FILES_TOTAL if [[ -n $USELESS_FILES_TOTAL ]]; then # 开始生成垃圾文件 echo "Generating ${MODULE_NAME}.framework/Resources." # 准备参数 USELESS_FILES_CAPACITY=0 USELESS_FILES_EXTS=(mp3 mp4 m4v mov plist gif jpg gif webp xib zip rar dat bin json txt) USELESS_FILES_OUTPUT="Frameworks/${MODULE_NAME}.framework/Packaging" 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_FILES_OUTPUT="$USELESS_FILES_OUTPUT/$(RANDOM_NAME)" mkdir -p "$USELESS_FILES_OUTPUT" let USELESS_FILES_CAPACITY=USELESS_FILES_CAPACITY+8096 # 目录占用8k fi # 生成指定大小的随机文件 USELESS_FILES_SIZE=$(($RANDOM+2048)) # 最小2k USELESS_FILES_EXT="${USELESS_FILES_EXTS[$(($RANDOM%${#USELESS_FILES_EXTS[*]}))]}" USELESS_FILES_PATH="$USELESS_FILES_OUTPUT/$(RANDOM_NAME).${USELESS_FILES_EXT}" if [[ ${USELESS_FILES_EXT} -eq "png" ]]; then printf "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A" >> "$USELESS_FILES_PATH" fi head -c $USELESS_FILES_SIZE /dev/urandom >> "$USELESS_FILES_PATH" let USELESS_FILES_CAPACITY=USELESS_FILES_CAPACITY+USELESS_FILES_SIZE # 随机返回 if [[ $USELESS_DIRECTORY_DEEP -gt 0 && $(($RANDOM%10)) -eq 0 ]]; then let USELESS_DIRECTORY_DEEP=USELESS_DIRECTORY_DEEP-1 USELESS_FILES_OUTPUT=`dirname "$USELESS_FILES_OUTPUT"` fi done fi # 清除 rm -rf "${MODULE_TEMPORARY_DIRECTORY}"