美文网首页
Flutter-远程Pod集成Flutter脚本优化

Flutter-远程Pod集成Flutter脚本优化

作者: yuyangkk | 来源:发表于2021-06-07 19:47 被阅读0次

    相关链接,如有需要请移步:
    Flutter-已有iOS工程中加入Flutter之Cocoapods+Flutter环境方式集成
    Flutter-已有iOS工程中加入Flutter之远程Pod产物方式集成
    Flutter-远程Pod集成Flutter脚本优化

    前段时间写了个pod远程仓库打包的脚本,感觉不太完善,有些缺点,最近抽空研究了下,做了些优化。

    1. 脚本优化

    截止到现在,打包并上传到远程仓库的脚本build_ios.sh长这样:

    echo "进入到Flutter项目所在目录"
    projectURL=$(cd $(dirname $0); pwd)
    
    #echo $projectURL
    
    cd ${projectURL}
    
    #获取Flutter项目远程更新
    git fetch
    #拉取Flutter项目代码
    git pull
    echo "拉取代码结束"
    
    # 读取控制台输入要打包的环境
    read -p "请输入打包环境:1==Release, 0==Debug?  " configuration
    
    echo "获取的输入结果: $configuration"
    
    isRelease=true
    
    if [ "0" == $configuration ]; then
        isRelease=false
    fi
    
    echo "对比结果是 $isRelease"
    # 相应的环境
    if [ $isRelease == true ]; then
        echo "选择了Release"
        #路径换成自己的生产pod对应的路径
        out='../iOSFlutterSDK/iOSFlutterSDK/ios_frameworks'
    else
        echo "选择了Debug"
        #路径换成自己的测试pod对应的路径
        out='../iOSFlutterDevSDK/iOSFlutterDevSDK/ios_frameworks'
    fi
    #if [ -z $out ]; then
    #    out='../iOSFlutterSDK/iOSFlutterSDK/ios_frameworks'
    #fi
    
    buildxc='buildxc'
    
    echo "准备输出所有文件到目录: $out"
    
    echo "清除所有已编译文件"
    #flutter clean
    rm -rf $out
    
    # 拉取flutter依赖库
    #flutter packages get
    
    echo "编译flutter"
    #flutter build ios --release --no-codesign
    if [ $isRelease == true ]; then
        flutter build ios-framework --xcframework --no-debug --no-profile --no-universal --output=$buildxc
    else
        //使用profile包,因为debug的包,在某些机型上不可用
        flutter build ios-framework --xcframework --no-release --no-debug --no-universal --output=$buildxc
    fi
    
    echo "编译flutter完成,编译所有文件到目录: $buildxc"
    
    # 创建导出包的文件夹
    mkdir $out
    
    if [ $isRelease == true ]; then
        # 拷贝Release编译产物到指定目录
        cp -r $buildxc/Release/*.xcframework $out
    else
        # 拷贝Debug编译产物到指定目录
        cp -r $buildxc/Profile/*.xcframework $out
    fi
    
    echo "删除编译完成的临时文件夹"
    rm -rf $buildxc
    
    read -p "是否需要推送到原生Pod仓库 yes/no? " shouldPush
    read -p "请输入版本说明: " versionMsg
    
    # 推送到远程,并打上tag
    pushSpec(){
        if [ $isRelease == true ]; then
            # 拷贝Release编译产物到指定目录
            cd ../iOSFlutterSDK
        else
            # 拷贝Debug编译产物到指定目录
            cd ../iOSFlutterDevSDK
        fi
        
        echo "拉取远程Pod仓库代码"
        git fetch
        git pull
    
        #修改iOSFlutterSDK.podspec中s.version          = '0.1.4'
    
        # 读取iOSFlutterSDK.podspec的第11行
        # a=`cat iOSFlutterSDK.podspec|head -ne12`
        # echo $a
        
        if [ $isRelease == true ]; then
            a=`sed -n 11p iOSFlutterSDK.podspec`
        else
            a=`sed -n 11p iOSFlutterDevSDK.podspec`
        fi
        
        # echo $a #s.version = '0.1.4'
        # echo "删除最右边.开始向及其左边的数据"
        vs=${a##*.}
        # echo $vs
        oldNum=${vs%%\'*}
        # echo '当前版本:'$oldNum
        newNum=$((oldNum+1))
        # echo '新版本:'$newNum
    
        # echo "替换版本号"
        v=0.1.$newNum
        version="  s.version          = '$v'"
        echo $version
        
        #区分Release和Debug两种环境
        if [ $isRelease == true ]; then
            sed -i '' "s/$a/$version/" iOSFlutterSDK.podspec
        else
            sed -i '' "s/$a/$version/" iOSFlutterDevSDK.podspec
        fi
    
        # echo "add 并且commit"
    
        # git status
        # git rm --cached
        git add .
        #添加描述
        git commit -m "版本号:$v $versionMsg"
    
        # echo "添加tag"
        git tag -a $v -m "$v $versionMsg"
    
        # echo "连同tag推送到远程"
        git push
        git push --tags
    }
    
    
    if [ $shouldPush == "yes" ]; then
        echo "正在向远程仓库推送"
        pushSpec
    else
        echo "不向远程仓库推送"
    fi
    
    # 以前的编译导出的包
    #cp -r build/ios/Release-iphoneos/*/*.framework $out
    #cp -r .ios/Flutter/App.framework $out
    #cp -r .ios/Flutter/engine/Flutter.framework $out
    
    
    效果图

    2. 使用

    build_ios.sh放到flutter项目的路径下,打包时,将脚本文件直接拖入到终端,直接回车即可,按照控制台的提示,输入相应的信息,就可以将pod仓库推送到远程了。

    3. 修改iOS原生项目中的Profile文件

    Profile文件本身也是脚本文件,凭借我临时抱佛脚的脚本功底,通过入参的形式,将Profile中的三方依赖,设置为可以根据环境(Debug或者Release)更替为相应的SDK,同时支持三种环境集成:

    1. 集成Debug的SDK,pod install命令行:
    env PODFILE_TYPE='development_sdk' pod install
    #env PODFILE_TYPE='development_sdk' pod update
    
    1. 集成Release的SDK,pod install命令行为
    env PODFILE_TYPE= 'release_sdk' pod install
    #env PODFILE_TYPE='release_sdk' pod update
    
    1. 集成本地flutter环境的SDK,pod install命令行为
    pod install
    

    具体Profile中的代码修改为下面这样:

    source 'https://github.com/CocoaPods/Specs.git'
    source 'https://github.com/aliyun/aliyun-specs.git'
    
    platform :ios, '9.0'
    
    #环境变量
    #development_sdk 使用远程pod集成,flutter环境为Debug
    #release_sdk 使用远程pod集成,flutter环境为Release
    #local_flutter 使用本地Flutter环境集成,Debug、Release 开发者在scheme中自己选择
    case ENV['PODFILE_TYPE']
      when 'development_sdk'
      when 'release_sdk'
      else
      flutter_application_path = '这里是你的flutter_module对应的路径'
      load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
    end
    
    
    use_frameworks!
    #swift_version = '4.1'
    target "QianGunGun" do
      pod 'AFNetworking'
      case ENV['PODFILE_TYPE']
        when 'development_sdk'
        # 测试环境
        pod 'iOSFlutterDevSDK', :git => 'https://codeup.aliyun.com/xxxx/iOSSDK/iOSFlutterDevSDK.git'
        when 'release_sdk'
        # 生产环境 Flutter SDK, 因为我们自己的gitlab限制大文件传输,所以使用了其他
        pod 'iOSFlutterSDK', :git => 'https://codeup.aliyun.com/xxxx/iOSSDK/iOSFlutterSDK.git'
        else
        ## ==============Flutter ==============_
        ## Flutter 模块的路径 pod update --verbose --no-repo-update_
        install_all_flutter_pods(flutter_application_path)
        ## ==============Flutter ==============_
      end
      
      
      post_install do |installer|
        installer.pods_project.targets.each do |target|
          target.build_configurations.each do |config|
            config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '9.0'
            config.build_settings['ENABLE_BITCODE'] = 'NO'
          end
        end
      end
      
    end
    

    4. Jenkins中配置

    在Jenkins项目->General->参数化构建中添加外化参数分别为:
    use_remote_sdk:布尔值参数,是否使用远程SDK集成
    env_archive:选项参数,Build环境,使用Release还是Debug(如果是使用本地flutter集成环境,则必须为Release)
    flutter_branch:选项参数,flutter项目其Git分支

    在Jenkins项目->Build->添加一个执行脚本,如下:

    if  [ $use_remote_sdk == true ]; then
            echo "===Use remote sdk to integrate flutter==="
            if [ $env_archive == "Release" ]; then
                echo "====Release Flutter"
                env PODFILE_TYPE=release_sdk pod install
            else
                echo "====Debug Flutter"
                env PODFILE_TYPE=development_sdk pod install
            fi
    else
            echo "===Use local environment to integrate flutter==="
            projectURL=`pwd`
            echo "current dictionary:$projectURL"
            cd ../app-flutter #进入到flutter项目路径
            # 此处应该先校验一下分支名是否存在,但是我还没找到合适的方法
            # 目前是每次有新分支了,先在打包机器上Flutter项目中,手动切出一个本地分支
            git pull origin $flutter_branch
            
            flutter pub get
            cd $projectURL
            pod install
    fi
    

    至此,脚本基本完成了,可以尝试使用了,如有问题,欢迎批评指正!

    相关文章

      网友评论

          本文标题:Flutter-远程Pod集成Flutter脚本优化

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