美文网首页
iOS自动化打包--基于NSTask--脚本打包

iOS自动化打包--基于NSTask--脚本打包

作者: 阳光下de奔跑 | 来源:发表于2018-12-09 23:14 被阅读0次

    在日常开发中,尤其是在项目开发完成时,需要分包给测试去测试,由于改动比较频繁,导致发包也比较频繁,这在开发过程中是一件很简单而枯燥的事情,所以就决定自己撸一个自动打包的工具。

    一、熟悉NSTask的使用

    在看了不少的文档后大部分都是通过python自动化打包,但是偶然间发现NSTask这个类,NSTask是可以在APP里调用终端命令的累,它可以在你的设备上执行另一个程序,于是就猜想,是不是可以通过NSTask调用xcode打包的命令呢?

    小实验

    假如在Documents中存在一个名为tst.txt的文件,我们可以使用NSTask通过命令将其删除。

    NSString *pt =[NSString stringWithFormat:@"%@/Documents",NSHomeDirectory()];
    
    [self configText];
    
    NSTask *task =[[NSTask alloc]init];
    
    NSString *tr =[NSString stringWithFormat:@"cd %@;rm tst.txt",pt];
    
    [task setLaunchPath:@"/bin/sh"];
    
    NSMutableArray *arg =[NSMutableArray new];
    
    [arg addObject:@"-c"];
    
    [arg addObject:tr];
    
    task.arguments = arg;
    
    [task launch];
    
    [task waitUntilExit];
    

    运行后tst.txt可看到tst.txt已经被移除了

    二、通过NSTask调用脚本文件

    在当前项目中创建一个upload.command的脚本文件,输入xcode的编译及打包命令

    xcodebuild -workspace "${1}" -sdk iphoneos -scheme "${4}" -configuration "${6}" CONFIGURATION_BUILD_DIR="${3}"
    /usr/bin/xcrun -sdk iphoneos PackageApplication -v "${3}/${4}.app" -o "${5}/${4}.ipa"
    ${1}、${2}、${3}、${4}、${5}、${6} 是NSTask传入的参数

    NSString *arcPath    = [panArcField.cell.title stringByRemovingPercentEncoding];
    NSString *savPath    = [panSaveField.cell.title stringByRemovingPercentEncoding];
    NSString *scheme     = [schemeField.cell.title stringByRemovingPercentEncoding];
    NSString *debug      = [debugField.cell.title uppercaseString];
    NSString *scriptPath = [[NSBundle mainBundle] pathForResource:@"FSUpload" ofType:@"command"];
    finaPath = [NSString stringWithFormat:@"%@%@.ipa",savPath,scheme];
    task = [[NSTask alloc] init];
    [task setLaunchPath:scriptPath];
    [task setTerminationHandler:^(NSTask *ts) {
        [ts terminate];
        [self upload_pgy];
    }];
    NSMutableArray *arg = [NSMutableArray new];
    [arg addObject:[NSString stringWithFormat:@"%@%@.xcworkspace",arcPath,scheme]];
    [arg addObject:scheme];
    [arg addObject:[NSString stringWithFormat:@"%@build",savPath]];
    [arg addObject:scheme];
    [arg addObject:savPath];
    [arg addObject:debug];
    task.arguments = arg;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [task launch];
        [task waitUntilExit];
    });
    

    arcPath是要打包工程所在的路径,savPath是编译后储存的路径,scheme一般为工程名可通过xcodebuild -list查看。debug是打包的版本release或debug版本,scriptPath为当前upload.command所在的路径。一次添加这些到arg数组里,并赋值到task.arguments里。结合上面的命令可以看出${1}就是.xcworkspace的所在路径,${2}就是scheme名,${3}就是build的路径,以此类推。
    然后到upload.command所在的目录,执行命令
    chmod +x upload.command
    给于执行的权限,否则会报“launch path not accessible”的错误。
    注意:xcode9及以上版本打包时可能会报
    xcrun: error: unable to find utility "PackageApplication", not a developer tool or in PATH错误,原因是/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/该目录没有PackageApplication命令文件,下载PackageApplication文件然后复制到该目录,再执行下面的命令即可
    sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer/
    chmod +x /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/PackageApplication
    最后执行不出意外的话可以在保存的路径中看见ipa的包了。
    最后要做的就是上传到服务器获取下载地址,demo中是将其上传到蒲公英上去,这样每次需要重新发包就不用那么麻烦了。最后附上Demo,欢迎大家给个小星星.

    相关文章

      网友评论

          本文标题:iOS自动化打包--基于NSTask--脚本打包

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