天天看點

Xcodebuild指令使用

Xcodebuild簡介

Xcodebuild是指令行工具包的其中一項。

指令行工具包(Command Line Tools)是一個輕量的、可以與XCode分開的、在Mac上單獨下載下傳的指令行工具包。

它有兩部分組成:Mac OS SDK和使用者系統庫目錄/usr/bin下的諸多指令工具。例如:gcc/g++編譯器,make、git、nase、xcodebuild、xcrun等等。

指令行工具包(Command Line Tools)的安裝

Xcode-select指令

xcode-select是Mac系統自帶的指令行工具,屬于使用者系統内/usr/bin。當電腦上安裝多個Xcode時,xcode-select用來選擇指令行工具為哪一個版本的Xcode服務。

指令行工具安裝指令:xcode-select --install

選擇指定的Xcode路徑:xcode-select --switch <path>

常見指令

man指令

man可以進行指令用法的線上文檔查詢,包括使用例子。比如:man xcodebuild。

xcpretty指令

指令行輸出美化小工具,可以對錯誤,警告給予高亮顯示。使用方式:xcodebuild | xcpretty

xcrun指令

xcrun用于調用其他指令執行,如:xcrun xcodebuild。

xcrun的調用是基于xcode-select選擇的工具鍊,當電腦中存在多個版本的xcode時,使用xcrun調用可以保證指令的環境一緻性。

xcodebuild指令

下面重點介紹xcodebuild如何使用。

在使用xcodebuild時,從終端進入到projectname .xcodeproj 目錄下。

有workspace時,參數中要帶-workspace和-scheme。

隻有project時,則參數中要帶 -project和-scheme選項。

xcodebuild在Xcode中存在的預設配置在路徑project/info頁面中

Xcodebuild指令使用

xcodebuild的常見使用場景

簡單指令行build

xcodebuild

單寫一個xcodebuild,工程編譯使用預設的scheme和編譯配置。
scheme、targets、配置、目标裝置、SDK和導出資料位置可以在後面自定義配置      

 archive打包操作

xcodebuild archive  -workspace PROJECT_NAME.xcworkspace   
-scheme SCHEME_NAME -configuration release -archivePath  
EXPORT_ARCHIVE_PATH 

-archivePath:設定項目的歸檔路徑      

 導出ipa檔案

xcodebuild -exportArchive -archivePath EXPORT_ARCHIVE_PATH    
-exportPath EXPORT_IPA_PATH -exportOptionsPlist ExportOptionsPlistPath
-allowProvisioningUpdates

-exportArchive:導出ipa
-exportPath:導出ipa檔案的路徑
-exportOptionsPlist:檔案導出時的配置資訊
-allowProvisioningUpdates:允許xcodebuild與蘋果網站通訊,進行自動簽名,證書自動更新,生成。      

 單元測試

xcodebuild test -project PROJECT_NAME.xcodeproj -scheme SCHEME_NAME 
-destination \'platform=iOS Simulator,name=iPhone 6s,OS=11.2\' -
configuration Debug -derivedDataPath output

-derivedDataPath:産生的緩存檔案放在./output目錄下
 configuration:編譯環境,選擇Debug/Release
 -destination :選擇test時的目标裝置和系統版本号      

 UI測試/單元測試,針對某個方法進行測試

xcodebuild test -project PROJECT_NAME.xcodeproj -scheme SCHEME_NAME 
-destination \'platform=iOS Simulator,name=iPhone 6s,OS=11.2\' 
-only-testing:TARGET_NAME/CLASS_NAME/FUNC_NAME -quiet

-only-testing: 隻測試某一個方法,target名/類名/方法名
-quiet : 除了錯誤和警告不列印任何資訊      

使用上次編譯成功的測試用例進行測試

注意:app建立時需要指定app的bundle名

self.app = [[XCUIApplication alloc] initWithBundleIdentifier:@"com.xxx.id"];
[self.app launch];      

1.UI測試/單元測試,不進行代碼編譯,利用上次編譯的緩存(包括工程編譯+測試用例編譯),進行重新跑測試。

xcodebuild test-without-building -workspace PROJECT_NAME.xcworkspace 
-scheme doctor -destination \'platform=iOS Simulator,name=iPhone 6s,OS=12.0\' 
-only-testing:TARGET_NAME/CLASS_NAME/FUNC_NAME      

2.UI測試,使用選項-xctestrun生産測試檔案,進行測試調試

//1.産生xctestrun檔案
xcodebuild build-for-testing -project PROJECT_NAME.xcodeproj -scheme SCHEME_NAME 
-destination \'platform=iOS Simulator,name=iPhone 6s,OS=11.2\' -
configuration Debug -derivedDataPath output

-derivedDataPath: derivedDataPath/Build/Products目錄下生成一個.xctestrun檔案,包含測試資訊


//2.使用xctestrun檔案(不帶-workspace/-project/-scheme參數)
xcodebuild test-without-building -destination \'platform=iOS Simulator,name=iPhone 6s,OS=12.0\' 
-xctestrun DerivedDataPath.xctestrun -only-testing:TARGET_NAME/CLASS_NAME/FUNC_NAME

-xctestrun:有這個選項就從指定的路徑下尋找bundle,沒有這個選項在derivedDataPath下尋找bundle
-only-testing:TARGET_NAME/CLASS_NAME/FUNC_NAME      

 xcodebuild常見action

Xcodebuild指令使用

另外一些常見的指令

genstrings 指令

本地化指令,根據指定的C/Object-C源檔案生成.strings檔案。

genstrings -a /path/to/source/files/*.m      

ibtool 指令

本地化指令,作用于xib檔案。

ibtool --generate-strings-file Localizable.strings en.lpoj/Interface.xib      

文章參考:

https://www.hualong.me/2018/03/14/Xcodebuild/

https://www.jianshu.com/p/2a351587f0ef