【问题标题】:How do I automate the installation of Xcode?如何自动安装 Xcode?
【发布时间】:2011-12-27 22:20:12
【问题描述】:

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

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

我这样做是为了:

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

【问题讨论】:

标签: xcode build-automation


【解决方案1】:

全自动 XCode 安装

首先,登录Apple Developer Downloads Site 并获取适用于您的 OSX 版本的 XCode.dmg 版本。您可能想要获取几个版本来支持不同的 OSX 平台(例如:10.10 Yosemite、10.9 Mavericks 等)。将 dmg 上传到您可以访问的文件主机(例如:Amazon S3、Dropbox、您选择的共享主机提供商等...)

在以下脚本中更改DOWNLOAD_BASE_URL,并使用版本和内部版本号适当地重命名 dmg,或将其添加到脚本中 下面:

#!/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】:

好吧,对于访问此页面的人,Fastlane 的一位作者构建了一个 gem 来自动执行此过程。

https://github.com/KrauseFx/xcode-install

【讨论】:

    【解决方案3】:

    从 Xcode 4.3 开始,它作为 app bundle 提供。首次启动应用程序时,它将继续安装。由于它是一个测试版,它仍然没有修复,但目前发布后的安装包位于名为 Contents/Resources/Packages 的目录中的应用程序包中。

    因此,根据您的环境,您的安装脚本只需将 Xcode.app 包复制到应用程序目录中,您的用户就可以在他们第一次启动它时完成安装。或者,您可以复制捆绑包,然后使用installer(8) 实用程序手动安装其他包。有关使用安装程序的更多信息,请参阅man pages

    【讨论】:

      【解决方案4】:

      很多用于自动下载和安装的东西都已经过时了。我一直在为另一个项目工作,并认为我有一个很好的解决方案:

      https://github.com/rockholla/macosa/blob/master/bin/macosa_xcode

      这是下载部分:

      https://github.com/rockholla/macosa/blob/master/bin/macosa_xcode_download

      【讨论】:

        【解决方案5】:
        # Install Command-line tools as dependency for Homebrew
        xcode-select --install # Sets the development directory path to /Library/Developer/CommandLineTools
        
        # Install Homebrew
        /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
        # Install Mas (command-line interface for Mac App Store)
        brew install mas
        
        # Search for Xcode showing only the first 5 results
        mas search xcode | head -5
        # Install Xcode using App ID
        mas install 497799835 # The appid for Xcode shown when doing search
        
        sudo xcode-select -r  # Reset the development directory path to put to Xcode /Applications/Xcode.app/Contents/Developer
        
        #sudo xcodebuild -license
        
        # Updaate all Apple software and auto agree to any licenses and restart if necessary
        sudo softwareupdate --install --agree-to-license -aR
        

        【讨论】:

          猜你喜欢
          • 2015-12-16
          • 2013-09-28
          • 2016-11-04
          • 2011-10-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多