Golang enables iOS to automatically pack and upload fir

Source: Internet
Author: User
Tags readfile

Objective

As the iOS client development of the small buddies, must have a day to testers, product group packaging n times experience. If each time the manual packaging, not only wasted the developer's time, let the test of the little sister suffering from the waiting is also an unkind thing.
So automated packaging is especially important at this point. With the automated packaging tools, the development of small brother only need to run the packaging tools, the rest of the matter to the automated packaging tools just fine. The tool will automatically pack and upload the IPA package to the FIR, and if necessary, let the tool automatically send a message to inform the test that the little sister can test. O (∩_∩) o hahaha ~

Basic commands

This time you will use xcodebuild : command, Generate Archive , export ipa .
PS: It is said that other commands can be implemented, let other small partners to introduce it. )
xcodebuildThere are two steps to using a command:
First step: archive : Compile packaged into Archive and Xcode operations "product-archive" consistent
xcodebuild archive -workspace xxx.xcworkspace -scheme xxx -configuration Release -archivePath ${ARCHIVE_PATH} CONFIGURATION_BUILD_DIR=${BUILD_DIR}

Parameter description
-workspace: Specify workspace file Xxx.xcworkspace
-scheme: Specify the construction project name
-configuartion: [Debug/release] optional, choose Debug or Release Build
-archivePath: Save build. xcarchive Package Path
CONFIGURATION_BUILD_DIR: The file path at build time (mainly contains. A files, xxx.app files, and XXX.app.dSYM files) if you do not need to use the binaries in the XXX.app.dSYM file (some third-party crash statistics need to be used for this file), you can not add this.

Step two: export : Export the generated xxx.xcarchive file to Xxx.ipa. export"consistent with Xcode operations"organizer-Archives
xcodebuild -exportArchive -archivePath ${ARCHIVE_PATH} -exportOptionsPlist ${EXPORT_OPTIONS_PLIST_PATH} -exportPath ${EXPORT_DIR}

Parameter description
-archivePath: The path to the Xxx.xcarchive file generated in the first step
-exportOptionsPlist: The configuration file path required during export
-exportPath: Export the Save directory of IPA

Special Instructions
第二步中的配置文件特别重要,是你打包为测试包或者发布版本的关键。稳妥起见,请手动打包一次,从生成的目录中获取

The above two steps are the core of automated packaging, if the above command is not clear or wrong place, you can view the details through the command xcodebuild -usage .

How to use

Because we are currently learning Golang, we use Golang to complete the development of automated packaging tools, the goal of this package is a beta version, and a small partner who needs a release version can perform the modification according to the principle.

1. Create a project

First create a Golang project, add the Main.go file, because the project is simple, so all the code is written in the Main.go file.

2. Configuration files

In order to be able to package different projects later, so that the project needs to use the directory and so write as a configuration file, easy to modify.
Create config.json a file, the profile name can be named casually.

{    "scheme":"构建的工程名称", // eg.我的项目叫TShop    "path":"工程根目录路径", // eg. "~/Desktop/TShop_SVN"    "workspace":"工作空间文件名", // 即项目中.xcworkspace后缀名文件的名称    "archPath":"保存Archive文件的路径+Archive文件名",  // eg. "~/Desktop/autoArchive/TShop"    "exportPath":"导出ipa文件的路径",    "config": "第二步中需要用到的配置文件的路径",    "apiToken":"你注册的fir的Api Token"}

3. Parsing the configuration JSON file in Main.go

Create a type in Main.go

type buildInfo struct {    Path       string `json:"path"`    Workspace  string `json:"workspace"`    Scheme     string `json:"scheme"`    ArchPath   string `json:"archPath"`    ExportPath string `json:"exportPath"`    Config     string `json:"config"`    ApiToken   string `json:"apiToken"`}

Parsing JSON in the main function

func main() {    file := "config.json" // 应改为你的配置文件的路径    content, err := ioutil.ReadFile(file)    if err != nil {        panic(err.Error())    }    var info buildInfo    err = json.Unmarshal(content, &info)    if err != nil {        panic(err.Error())    }}

4, Archive

Create a archive function to perform the first step of the command

func Archive(info buildInfo) {    err := os.Chdir(info.Path)    if err != nil {        panic(err.Error())    }    workspace := info.Workspace + ".xcworkspace"    archCommand := exec.Command("xcodebuild", "archive", "-workspace", workspace, "-scheme", info.Scheme, "-archivePath", info.ArchPath)    archCommand.Stdout = os.Stdout    err = archCommand.Run()    if err != nil {        panic(err.Error())    }}

5. Export

func Export(info buildInfo) {    arch := info.ArchPath + ".xcarchive"    export := exec.Command("xcodebuild", "-exportArchive", "-archivePath", arch, "-exportOptionsPlist", info.Config, "-exportPath", info.ExportPath)    export.Stdout = os.Stdout    err := export.Run()    if err != nil {        panic(err.Error())    }}

Call Archive (info) and export (info) at the end of the main function to automatically package and generate the IPA file.

6. Upload to Fir

To upload an IPA using FIR-CLI, you need to download and install FIR-CLI, check git yourself and follow the instructions to install it.
Upload commandfir publish APP_FILE_PATH -T API_TOKEN

Parameter description
APP_FILE_PATH: The path to the generated. IPA file
-T: API Token for FIR account
(See document For additional parameters of publish)

Creating the Upload function in Main.go

func Upload(info buildInfo) {    ipaPath := info.ExportPath + info.Scheme + ".ipa"    upload := exec.Command("fir", "publish", ipaPath, "-T", info.ApiToken)    upload.Stdout = os.Stdout    err := upload.Run()    if err != nil {        panic(err.Error())    }}

You can then upload the generated IPA to the FIR by calling upload (info) at the end of the main function.
The last main function

func main() {    file := "config.json"    content, err := ioutil.ReadFile(file)    if err != nil {        panic(err.Error())    }    var info buildInfo    err = json.Unmarshal(content, &info)    if err != nil {        panic(err.Error())    }    Archive(info)    Export(info)    Upload(info)}

If configured correctly, you go run main.go can complete the process of automatically packaging and uploading the FIR by typing in the command-line tool.
You can also use the GO command to make the tool an executable file. When you need to hit the test package, it is convenient to double-click the Automated Packaging tool to complete all the processes.

O (∩_∩) o
Finish

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.