【问题标题】:Find Xcode installation path by version按版本查找 Xcode 安装路径
【发布时间】:2011-05-08 07:34:09
【问题描述】:

没有标准路径可以在一个系统上保存不同的 XCode(XCode3 和 XCode4)副本。对于我的 Makefile 或其他基于命令行的构建过程,我正在寻找一种方法来确定特定版本的 XCode 的安装路径 ($DEVELOPER_DIR),将此路径导出为 DEVELOPER_DIR 或使用 xcode-select 使其处于活动状态。

到目前为止,我正在尝试查询 system_profiler 的 xml 输出。

还有其他(更方便)的选择吗?

基本上我需要一个脚本来告诉系统使用 Xcode3(或 Xcode4)而不知道它们的安装路径。

【问题讨论】:

  • 好问题。我玩过pkgutil(1),但 Xcode 本身似乎没有包裹收据,尽管一些相关的包裹有。

标签: xcode command-line makefile installation-path


【解决方案1】:

这是我目前的想法:

DEVELOPER_DIR=`system_profiler SPDeveloperToolsDataType -xml |\
xpath "//*[text()='spdevtools_version']/following-sibling::string[starts-with(text(),'3')]/../*[text()='spdevtools_path']/following-sibling::string[1]/text()"`

这会将 DEVELOPER_DIR 设置为 Xcode3 的安装路径。对于 Xcode4,只需将 '3' 替换为 '4'

安装多个版本的 XCode3 时,这将无法正常工作。

【讨论】:

    【解决方案2】:

    您可以在命令行中使用lsregister 命令查询Launch Services 数据库,找到所有已安装Xcode 版本的安装路径。

    要查找所有 Xcode3 安装路径,请运行:

    /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister  -dump | grep --before-context=2 "com.apple.Xcode" | grep --only-matching "/.*\.app"
    

    要查找所有 Xcode4 安装路径,请运行:

    /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister  -dump | grep --before-context=2 "com.apple.dt.Xcode" | grep --only-matching "/.*\.app"
    

    另见question

    【讨论】:

    • 太棒了。一个小故障是,这也会在最近安装的网络卷上找到 XCode 安装。
    • 以下适用于 Xcode 5 /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -dump | grep -i --before-context=3 "com.apple.dt.Xcode" | grep --only-matching "/.*\.app"
    【解决方案3】:

    这将在 Applications 文件夹中找到具有给定版本的 Xcode,假设其名称以 Xcode 开头:

    for xcode in ls /Applications/Xcode*; do
      if grep -q -s -E "<string>$1\.[0-9.]+</string>" "$xcode/Contents/version.plist"; then
        echo "$xcode/Contents/Developer\c"
        break
      fi
    done
    

    将其保存为 findxcode.sh,然后像这样启动:./findxcode.sh 9,它将打印您可以放入 DEVELOPER_DIR 的 Xcode 9 的路径。如果您需要使用其他脚本/快速通道或其他内容捕获输出,则末尾的 \c 可以在末尾删除新行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      相关资源
      最近更新 更多