如何自动安装Xcode?

Wil*_*yne 5 build-automation xcode

我正在尝试编写一个shell脚本,将所有开发工具和依赖项安装到干净的OSX机器上.

有没有人知道自动安装Xcode的最佳方法?

我这样做是为了:

  1. 记录开发环境.
  2. 加快新开发人员的入职流程.
  3. 遵循自动化 - 一切原则.

Tri*_*onX 6

全自动XCode安装

首先,登录苹果开发者下载网站,并得到XCode.dmg的版本为您的OSX版本的作品.你可能想要获得几个版本来支持不同的OSX平台(例如:10.10 Yosemite,10.9 Mavericks等......).将dmg上传到您可以访问的文件主机(例如:Amazon 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
Run Code Online (Sandbox Code Playgroud)

您可能有兴趣安装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
Run Code Online (Sandbox Code Playgroud)

替代方法

另一种方法是使用我创建的这个AppleScript.

使用:

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
Run Code Online (Sandbox Code Playgroud)

您可能也对我的答案感兴趣:如何下载和安装Xcode的命令行工具.

我会推荐另一种方法而不是AppleScript方法.applescript依赖于UI元素层次结构,对于OSX上的App Store应用程序的未来版本,它可能并不总是保持不变.这对我来说似乎很脆弱.通过命令行通过dmg安装XCode可能是最可靠的方法.