2011-12-27 64 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