美文网首页
多Xcode管理

多Xcode管理

作者: maskerII | 来源:发表于2024-07-06 21:26 被阅读0次

    1. 安装多个版本的Xcode

    因为业务需要,我们有时候需要安装多个xcode版本,新的版本,老的版本。新的版本我们以xcode11为例子,老的版本,我们以xcode10为例子,

    下载老版本到: https://developer.apple.com/download/more/ 下载xcode10版本。

    1.下载以后 我们看到是一个xcode10.xip文件,这是我第一次看到xip后缀的文件, 原来双击一下就可以了,会解压出一个xcode.app 的文件夹。

    2.在/Applications目录下新建一个文件夹Xcode10,将刚才第一步中得到的xcode.app移动到/Applications/Xcode10/目录下

    根据Mac系统版本,下载安装兼容的Xcode版本。注意,需要安装对应版本的Command Line Tools
    Xcode下载链接

    2.管理多个Xcode版本

    2.1 查看当前Xcode版本
    2.1.1 打印当前活跃的Xcode文件夹路径
    xcode-select -p
    
    2.1.2 打印Xcode版本号
    xcodebuild -version
    
    2.1.3 从gcc编译器的版本信息中找
    gcc --version
    
    2.2 切换版本
    sudo xcode-select -s /Applications/Xcode10/Xcode.app/Contents/Developer
    
    sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer
    

    主要通过终端的xcode-select命令操作:

    
    Usage: xcode-select [options]
    
    Print or change the path to the active developer directory. This directory controls which tools are used for the Xcode command line tools (for example, xcodebuild) as well as the BSD development commands (such as cc and make).
    
    Options:
    -h, --help                  print this help message and exit
    -p, --print-path            print the path of the active developer directory
    -s <path>, --switch <path>  set the path for the active developer directory
    --install                   open a dialog for installation of the command line developer tools
    -v, --version               print the xcode-select version
    -r, --reset                 reset to the default command line tools path
    
    

    sudo xcode-select --switch <xcode_folder_path>

    sudo模式需要验证用户密码
    确保传入了正确的Xcode路径,例如/Applications/Xcode/15.0_beta3/Xcode.app/Contents/Developer,否则会报路径错误error: invalid developer directory '...'

    2.3 jenkins自动打包时切换Xcode版本

    通过脚本自动检测、切换打包用的Xcode版本:

    change_xcode_version()
    {
        [ "$1" != "11" -a "$1" != "13" ] && echo "input is $1,not 11 or 13" && exit 1
        xcode_v=$1
        echo "xcode version:"
        xcodebuild -version
        [ "$1" == "11" ] && export DEVELOPER_DIR=/Applications/Xcode/11.4/Xcode.app/Contents/Developer
        [ "$1" == "13" ] && export DEVELOPER_DIR=/Applications/Xcode/13.1/Xcode.app/Contents/Developer
        ./expect_xcode $1  # 调用切换Xcode版本的脚本
        sleep 10
        xcodebuild -version
        # 检查DEVELOPER_DIR是否切换成功,否则异常退出
        xcode_ver=`xcodebuild -version |grep Xcode |awk -F " " '{print $2}'`
        [ "$1" == "11" ] && [ "$xcode_ver" != "11.4" ] && echo "xcode version $xcode_ver, not 11.4" && exit 1
        [ "$1" == "13" ] && [ "$xcode_ver" != "13.1" ] && echo "xcode version $xcode_ver, not 13.1" && exit 1
        echo "new xcode version:"
        xcodebuild -version
    }
    
    

    其中,切换Xcode版本的脚本

    #!/usr/bin/expect
    
    set timeout 10
    set version [lindex $argv 0]
    set password "123456"  # 开机密码
    
    if {$version == "13" } {
        spawn sudo xcode-select -s /Applications/Xcode/13.1/Xcode.app/Contents/Developer
    }
    if {$version == "11" } {
        spawn sudo xcode-select -s /Applications/Xcode/11.4/Xcode.app/Contents/Developer
    }
    expect "*assword*" {send "$password\r"}  # 自动输入密码
    interact
    
    

    相关文章

      网友评论

          本文标题:多Xcode管理

          本文链接:https://www.haomeiwen.com/subject/nthncjtx.html