2011-12-27 70 views
4

我想寫一個shell腳本,將安裝所有的我們的開發工具&依賴關係到一個乾淨的OSX機器。如何自動安裝XCode?

有誰知道自動化的XCode的安裝最好的辦法?

我這樣做是爲了:

  1. 文件的開發環境。
  2. 加快新開發人員的入職流程。
  3. 遵循automate-everything原則。
+0

這可能是一個更好的問題爲http://serverfault.com。 – 2011-12-27 22:24:47

回答

5

全自動Xcode的安裝

首先,登錄Apple Developer Downloads Site並獲得XCode.dmg的版本爲您的OSX版本的作品。您可能需要購買幾個版本來支持不同的OSX平臺(例如:10.10優勝美地,10.9小牛等)。上傳傷害到你可以訪問文件的主機(如:亞馬遜S3,Dropbox的,您選擇的共享主機提供商,等...)

更改DOWNLOAD_BASE_URL在下面的腳本,並重新命名 DMGS適當地版本&版本號或其添加到下面的腳本

#!/bin/bash 
DOWNLOAD_BASE_URL=http://example.com/path/to/xcode/dmgs/ 

## Figure out OSX version (source: https://www.opscode.com/chef/install.sh) 
function detect_platform_version() { 
    # Matching the tab-space with sed is error-prone 
    platform_version=$(sw_vers | awk '/^ProductVersion:/ { print $2 }') 

    major_version=$(echo $platform_version | cut -d. -f1,2) 

    # x86_64 Apple hardware often runs 32-bit kernels (see OHAI-63) 
    x86_64=$(sysctl -n hw.optional.x86_64) 
    if [ $x86_64 -eq 1 ]; then 
    machine="x86_64" 
    fi 
} 

detect_platform_version 

# Determine which XCode version to use based on platform version 
case $platform_version in 
    "10.10") XCODE_DMG='XCode-6.1.1-6A2008a.dmg' ;; 
    "10.9") XCODE_DMG='XCode-5.0.2-5A3005.dmg' ;; 
    *)  XCODE_DMG='XCode-5.0.1-5A2053.dmg' ;; 
esac 

# Bootstrap XCode from dmg 
if [ ! -d "/Applications/Xcode.app" ]; then 
    echo "INFO: XCode.app not found. Installing XCode..." 
    if [ ! -e "$XCODE_DMG" ]; then 
    curl -L -O "${DOWNLOAD_BASE_URL}/${XCODE_DMG}" 
    fi 

    hdiutil attach "$XCODE_DMG" 
    export __CFPREFERENCES_AVOID_DAEMON=1 
    sudo installer -pkg '/Volumes/XCode/XCode.pkg' -target/
    hdiutil detach '/Volumes/XCode' 
fi 

您可能會感興趣安裝XCode的命令行工具,並繞過了XCode許可協議也:

curl -Ls https://gist.github.com/trinitronx/6217746/raw/58456d6675e437cebbf771c60b6005b4491a0980/xcode-cli-tools.sh | sudo bash 

# We need to accept the xcodebuild license agreement before building anything works 
# Silly Apple... 
if [ -x "$(which expect)" ]; then 
    echo "INFO: GNU expect found! By using this script, you automatically accept the XCode License agreement found here: http://www.apple.com/legal/sla/docs/xcode.pdf" 
    expect ./bootstrap-scripts/accept-xcodebuild-license.exp 
else 
    echo -e "\x1b[31;1mERROR:\x1b[0m Could not find expect utility (is '$(which expect)' executable?)" 
    echo -e "\x1b[31;1mWarning:\x1b[0m You have not agreed to the Xcode license.\nBuilds will fail! Agree to the license by opening Xcode.app or running:\n 
    xcodebuild -license\n\nOR for system-wide acceptance\n 
    sudo xcodebuild -license" 
    exit 1 
fi 

備選方法

一種替代方法是使用this applescript I created

要使用:

git clone https://gist.github.com/6237049.git 
cd 6237049/ 
# Edit the script with AppleScript Editor to replace your APPLE ID and PASSWORD 
osascript Install_XCode.applescript 

您可能也有興趣在我的答案在這裏:How download and Install Command Line Tools for Xcode

我會建議在AppleScript的方法的另一種方法。 applescript取決於UI元素層次結構,OSX上未來版本的App Store應用程序可能並不總是相同的。這對我來說似乎很脆弱。通過dmg通過命令行安裝XCode可能是最可靠的方法。

2

和Xcode 4.3開始,它的交付作爲一個應用程序包。第一次啓動應用程序時,它將繼續安裝。因爲它是一個測試版,它依然不動,但目前啓動後的安裝包是在一個叫做內容/資源/包目錄內的應用程序包。

因此,根據你的環境,你的安裝腳本可以只複製Xcode.app捆綁到應用程序目錄,用戶可以完成安裝,他們推出它的第一次。或者,您可以複製軟件包,然後使用installer(8)實用程序手動安裝其他軟件包。有關使用安裝程序的更多信息,請參閱man pages