iOS 开发学习成长之路

xcode自动打包

2017-06-22  本文已影响829人  smile_青儿

最近在研究Xcode自动打包上传到fir或App Store,这里主要介绍一些步骤。


一、Xcodebuild介绍:(终端进入项目根目录下)

1.xcodebuild –help/–h         查看具体的选项
2.xcodebuild –version         显示xcodebuild version
3.xcodebuild –showsdks        显示当前系统安装的sdk
4.xcodebuild –list            显示当前目录下project Information
5.xcodebuild -workspace       build工程命令,其参数稍后附上
xcodebuild -help(大概展示一下)
     xcodebuild [-project projectname] [-target targetname ...] [-configuration configurationname]
                [-sdk [sdkfullpath | sdkname]] [buildaction ...] [setting=value ...]
                [-userdefault=value ...]
     xcodebuild [-project projectname] -scheme schemename [-destination destinationspecifier]
                [-destination-timeout value] [-configuration configurationname]
                [-sdk [sdkfullpath | sdkname]] [buildaction ...] [setting=value ...]
                [-userdefault=value ...]
     xcodebuild -workspace workspacename -scheme schemename [-destination destinationspecifier]
                [-destination-timeout value] [-configuration configurationname]
                [-sdk [sdkfullpath | sdkname]] [buildaction ...] [setting=value ...]
                [-userdefault=value ...]
     xcodebuild -version [-sdk [sdkfullpath | sdkname]] [infoitem]
     xcodebuild -showsdks
     xcodebuild -list [-project projectname | -workspace workspacename]
     xcodebuild -exportArchive -exportFormat format -archivePath xcarchivepath -exportPath destinationpath
                [-exportProvisioningProfile profilename] [-exportSigningIdentity identityname]
                [-exportInstallerIdentity identityname]
xcodebuild –version
Xcode 8.3.2
Build version 8E2002
xcodebuild –list
Information about project "XXX":
    Targets:
        XXX
        XXX Tests
        XXX UITests

    Build Configurations:
        Debug
        Release

    If no build configuration is specified and -scheme is not passed then "Release" is used.

    Schemes:
        XXX
xcodebuild -workspace
从help中可得信息
xcodebuild -workspace workspacename -scheme schemename [-destination destinationspecifier]
                [-destination-timeout value] [-configuration configurationname]
                [-sdk [sdkfullpath | sdkname]] [buildaction ...] [setting=value ...]
                [-userdefault=value ...]
其中scheme,configuration即list里面参数,workspace必须和scheme一起使用,构建该workspace下的一个scheme。若当根目录下有多个Project的时候,必须使用“-project”指定project。target:构建某个Target。

1.新建文件

首先在工程目录下建一个XXX.sh文件(自动打包上传脚本),两个plist文件:XXXAdHoc.plist,XXXAppStore.plist(导出IPA使用)

2.plist文件(Plist文件用于导出不同ipa包时使用。)
XXXAdHoc.plist

method=ad-hoc,compileBitcode=NO

XXXAppStore.plist

method=app-store,uploadBitcode=YES,uploadSymbols=YES

3.XXX.sh文件

#!/bin/sh(建立.sh时自动生成)
1).基本配置信息
# 脚本配置
# 项目
target_name="需要打包的target名字"
scheme_name="$target_name"
configurationType="Release" #所选要与证书匹配

# 证书(与环境相匹配的证书)、描述文件(DVTPlugInCompatibilityUUID,可以通过$ xcodebuild -list查看,也可以前往/Applications/Xcode.app/Contents,查看info.plist文件)
CODE_SIGN_IDENTITY="iPhone Distribution: XX  XX(XXXXXXXXXX)"
PROVISIONING_PROFILE="XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"

# 目录(生成的xcarchive会在archive文件下,ipa会在archive/${target_name}.ipa文件下)
archive_path="archive/${target_name}.xcarchive"
ipa_path="archive/${target_name}.ipa"

# fir配置
commit_msg="$1"
fir_Token="XXXXXXXXXXXXXXXXXXXXX"
2).打包前清理一些旧文件
#  删除旧文件
rm -rf "archive/${target_name}.xcarchive"
rm -rf "archive/${target_name}.ipa"

# 清理旧项目
xcodebuild clean -configuration "$configurationType" -alltargets

3).归档
#  归档(其他参数不指定的话,默认用的是.xcworkspace或.xcodeproj文件里的配置)
archiveRun () {
    #是否是工作空间
    echo "是否是工作空间:(yes or no)"
    read isWorkspaceParameter
    sleep 0.5
    isWorkspace="$isWorkspaceParameter"

    if [ "$isWorkspace"  == "yes" ]
    then
        workspace_name="${target_name}.xcworkspace"

        xcodebuild archive -workspace "$workspace_name" -scheme "$scheme_name" -destination generic/platform=iOS -configuration "$configurationType" -archivePath "$archive_path" CODE_SIGN_IDENTITY="$CODE_SIGN_IDENTITY" PROVISIONING_PROFILE="$PROVISIONING_PROFILE"
    elif [ "$isWorkspace"  == "no" ]
    then
        project_name = "${target_name}.xcodeproj"

        xcodebuild archive -xcodeproj "$project_name" -scheme "$scheme_name" -archivePath "$archive_path" -configuration "$configurationType" CODE_SIGN_IDENTITY="$CODE_SIGN_IDENTITY" PROVISIONING_PROFILE="$PROVISIONING_PROFILE"
    else
        echo "参数无效, 请重新输入"
        archiveRun
        #exit 1
    fi
}

archiveRun

# xcarchive 实际是一个文件夹不是一个文件所以使用 -d 判断
if [ -d "$archive_path" ]
then
echo "构建成功......"
else
echo "构建失败......"
exit 1
fi
4).导出IPA
exportArchiveRun () {
    echo "选择打包方式(1.ad-hoc or 2.AppStore):"
    read methodParameter
    sleep 0.5
    method="$methodParameter"

    if [ -n "$method" ]
    then
        if [ "$method" == "1" ]
        then

            plist_path="XXXAdHoc.plist"
        elif  [ "$method" == "2" ]
        then

            plist_path="XXXAppStore.plist"
        else
            echo "参数无效, 请重新输入"
            exportArchiveRun
            #exit 1
        fi
    else

        plist_path="XXXAdHoc.plist"
    fi

    xcodebuild -exportArchive -archivePath "$archive_path" -exportPath "$ipa_path" -exportOptionsPlist "$plist_path"
}

exportArchiveRun

if [ -f "$ipa_path/${target_name}.ipa" ]
then
    echo "导出ipa成功......"
else
    echo "导出ipa失败......"
    exit 1
fi
5).上传
publishRun () {
    echo "是否上传(yes or no):"
    read isPublishParameter
    sleep 0.5
    isPublish="$isPublishParameter"

    if [ "$isPublish"  == "yes" ]
    then
        if [ "$method" == "2" ]
        then
            #上传App Store
            echo "请输入开发者账号:"
            read usernameParameter
            sleep 0.5
            username="$usernameParameter"

            echo "请输入开发者账号密码:"
            read passwordParameter
            sleep 0.5
            password="$passwordParameter"

            altoolPath="/Applications/Xcode.app/Contents/Applications/Application Loader.app/Contents/Frameworks/ITunesSoftwareService.framework/Versions/A/Support/altool"

            "${altoolPath}" --validate-app -f "$ipa_path/${target_name}.ipa" -u "$username" -p "$password" --output-format xml
            "${altoolPath}" --upload-app -f "$ipa_path/${target_name}.ipa" -u "$username" -p "$password" --output-format xml
        else

            publishFirRun () {
                echo "是否上传fir.im(yes or no):"
                read isPublishFirParameter
                sleep 0.5
                isPublishFir="$isPublishFirParameter"

                if [ "$isPublishFir"  == "yes" ]
                then
                    fir publish "$ipa_path/${target_name}.ipa" -T "$fir_Token" -c "${commit_msg}"
                elif [ "$isPublishFir"  == "no" ]
                then
                    exit 1
                else
                    echo "参数输入无效,请重新输入"
                    publishFirRun
#                    exit 1
                fi
            }
            publishFirRun
        fi
    elif [ "$isPublish"  == "no" ]
    then
        exit 1
    else
        echo "参数输入无效,请重新输入"
        publishRun
#        exit 1
fi
}

publishRun

if [ $? = 0 ]
then
    echo "~~~~~~~~~~~~~~~~上传成功~~~~~~~~~~~~~~~~~~~"
else
    echo "~~~~~~~~~~~~~~~~上传失败~~~~~~~~~~~~~~~~~~~"
fi

参考
https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man1/xcodebuild.1.html
http://www.jianshu.com/p/bd4c22952e01
http://www.jianshu.com/p/e55f76385ed9
fir-cli安装及问题:https://github.com/FIRHQ/fir-cli/blob/master/doc/install.md

上一篇 下一篇

猜你喜欢

热点阅读