#!/bin/bash

# created by lichanghong ; mail: lichanghong@soyoung.com

 

# XXX.sh  AFNetworking 3.0.0

 

set -e

 

. ./pod_function.sh

 

read_pod_file_at_current_path

 

 

git_path_dir=$HOME"/.changhong_bin"

framework_dir=$git_path_dir"/frameworks/"

open $framework_dir

 

 

 

#!/bin/bash

set -e

 

function read_lib_line_name_version(){

    line=$1

    #clip for name

    first=${line#*\'}

    name=${first%%\'*}

    #clip for version

    second=${line%\'*}

    pre_version=${second##*\'}

    version=${pre_version/"~> "/""}

 

    #判断带版本号的和不带版本号的

    first_char=${version:0:1}

    case $first_char in

        [1-9])

            echo "带版本..."$line

            create_lib $name $version

        ;;

        [a-z]|[A-Z])

            echo "不带版本号:"$line

            create_lib $name ""

        ;;

    esac

}

 

function create_lib(){

    pod_get_gitpath $1

 

    git_path_dir=$HOME"/.changhong_bin"

    framework_dir=$git_path_dir"/frameworks/"

 

    create_changhong_bin_dir $git_path_dir $framework_dir

    cd $git_path_dir

 

    clone_git_source_or_pull $1 $2

 

    package_podspec_to_framework $1 $2 $framework_dir

 

}

 

function read_pod_file_at_current_path(){

    while read line

    do

        if [[ ${line:0:1} != "#" && $line =~ "pod" ]];then

            read_lib_line_name_version "$line"

        fi

    done < `pwd`"/Podfile"

}

 

 

function pod_get_gitpath(){

    echo "pod search $1"

    search_result=$(pod search $1)

    #- Source: https://github.com/AFNetworking/AFNetworking.git - Versions

    search_result_path=${search_result%%" - Versions"*}

    git_path=${search_result_path##*Source:}

    echo "the git path is "$git_path

}

 

function create_changhong_bin_dir(){

    git_path_dir=$1

    framework_dir=$2

    if [ ! -d "$git_path_dir" ]; then

        echo "create dir $git_path_dir"

        mkdir $git_path_dir

    fi

    if [ ! -d "$git_path_dir/frameworks" ]; then

        echo "create dir $framework_dir"

        mkdir $framework_dir

    fi

}

 

 

function clone_git_source_or_pull(){

    # clone git

    lib_dir="$git_path_dir/"$1

    lib_dir2=${lib_dir/+/-}  #mac 会把带有+的文件名改为-

    if [ ! -d $lib_dir2 ]; then

        git clone $git_path -b $2 || git clone $git_path -b "v"$2

        cd $lib_dir2

        git checkout -b $2

    else

        rm -rf $lib_dir2

    fi

}

 

function package_podspec_to_framework(){

    framework_dir=$3

    #find podspec file & package framework path

    podspec_file=`find  .  -type f -regex  ".*\.podspec"`

    build_dir=`pwd`"/$1-$2/ios/"

 

    echo -e "\033[32m  $build_framework_ios  \033[0m"

    # package file to framework

    if pod package $podspec_file; then

        framework_name=`ls  $build_dir  |  grep ".*\.framework"`

        build_dir_ios=$build_dir"/"$framework_name

        echo "...........success package cp $build_dir_ios to $framework_dir.........."

        rm -Rf $framework_dir"/"$framework_name && echo "$framework_name already exists,update ..."

        cp -Rf $build_dir_ios $framework_dir

 

        echo -e "\033[32m .....$1-$2 success packaged by changhong.... \033[0m" #绿色字

        echo "$1-$2 success..."`date "+%Y-%m-%d %H:%M:%S" ` >> ../changhong_build.log

    else

        echo "failure to create framework for $1"

        exit 1

    fi

 

}