如何自动安装 Xcode?

发布于 2024-12-23 10:54:09 字数 176 浏览 0 评论 0原文

我正在尝试编写一个 shell 脚本来安装我们所有的开发工具和工具。依赖于干净的 OSX 机器。

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

我这样做是为了:

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

I am trying to write a shell script that will install all of our development tools & dependencies onto a clean OSX machine.

Does anybody know the best approach to automate the installation of Xcode?

I am doing this to:

  1. Document the development environment.
  2. Speed up the on-boarding process for new developers.
  3. Follow the automate-everything principle.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

救星 2024-12-30 10:54:09

全自动 XCode 安装

首先,登录Apple 开发者下载站点< /a> 并获取适用于您的 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

替代方法

另一种方法是使用 我创建的这个 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

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

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

Fully Automated XCode Installation

First, login to the Apple Developer Downloads Site and get the version of XCode.dmg that works for your version of OSX. You might want to grab a couple versions to support the different OSX platforms (e.g.: 10.10 Yosemite, 10.9 Mavericks, etc...). Upload the dmg to a file host you can access (e.g.: Amazon S3, Dropbox, your shared hosting provider of choice, etc...)

Change the DOWNLOAD_BASE_URL in the following script, and rename the dmgs appropriately with the version & build number or add it to the script below:

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

You might be interested in installing the XCode Command Line Tools, and bypassing the XCode License Agreement also:

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

Alternative Method

An alternative is to use this applescript I created.

To use:

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

You might also be interested in my answer here: How download and Install Command Line Tools for Xcode.

I would recommend the other method over the applescript method. The applescript depends on a UI element hierarchy that may not always stay the same for future versions of the App Store app on OSX. It just seems fragile to me. Installing XCode via the command line via a dmg is probably the most reliable way.

霊感 2024-12-30 10:54:09

好吧,对于到达此页面的人来说,Fastlane 的作者之一构建了一个 gem 来自动执行此过程。

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

Well, just for who reach this page, there is a gem build by one of author of Fastlane to automatic this process.

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

夜雨飘雪 2024-12-30 10:54:09

从 Xcode 4.3 开始,它作为应用程序包提供。第一次启动该应用程序时,它将继续安装。由于它是测试版,因此尚未修复,但目前启动后安装包位于应用程序包中名为 Contents/Resources/Packages 的目录中。

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

Starting with Xcode 4.3, it's delivered as an app bundle. The first time you launch the app, it will continue the installation. Since it's a beta, it's still not fixed, but currently the post-launch installation packages are in the app bundle inside a directory called Contents/Resources/Packages.

So, depending on your environment, your install script can just copy the Xcode.app bundle into the Applications directory and your users can complete the installation the first time they launch it. Or, you can copy the bundle and then manually install the additional packages using the installer(8) utility. For more information on using installer, see the man pages.

无敌元气妹 2024-12-30 10:54:09

许多用于自动下载和安装的东西已经过时了。我一直在为另一个项目做一些事情,并且认为我有一个非常好的解决方案:

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

这是下载部分:

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

A lot of things of things out there for automating both downloading and installing are outdated. I've been at work on something for another project and think I have a pretty good solution working:

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

And here's the download part:

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

奶气 2024-12-30 10:54:09
# 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
# 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文