iOS 使用脚本自动化复制target
有些项目成熟以后,就会有需求自动化配置生成一个全新的项目,不需要再让开发人员手动修改工程文件,将配置化工作直接移交给运维或者配置团队去做
其实按照普通的做法,无非就是在xcode里将目标target duplicate一下,然后修改相关的项目名称、target名称、bundleid等等,这些内容其实在xcodeproj文件中都有对应的配置信息,所以我们可以通过直接修改Xcodeproj直接文件的方式实现
首先感谢虾神 提供了详细的原理解说和工具介绍,在实现时少走了很多弯路,有兴趣的同学可以前往 虾神 的文章学习,这里我把最终实现贴出来供大家参考~~
希望进一步学习ios工程配置和脚本相关的同学建议去这里:
#!/usr/bin/env ruby
require 'rubygems'
require 'xcodeproj'
require 'fileutils' #----------------------------------- 目标项目配置内容----------------------------#
name = "newyorktoon"
displayname = "纽约通"
target_toonType = 10001
target_pushType = "hello"
target_channel = "hello"
target_mapKey = "hello"
target_schemeType = "hello"
#----------------------------------- 目标项目配置内容----------------------------# # 模板项目
# srcname = "tzhqtoon"
# srcdisplayname = "后勤通" #project
project_path = "Hello.xcodeproj"
# 复制资源文件,注意:
# 1. 复制资源文件时需要排除源资源文件
# 2. 在此文件的最后面将复制出来的资源文件添加到目标target
targetdir = "TNTarget/#{name}"
srcroot = "TNTarget/#{srcname}" # 复制资源文件夹,将源target下的图片资源文件夹复制到目标target目录
if !Dir.exists?(targetdir)
Dir.mkdir(targetdir)
end
codeDirs = [
"#{srcroot}/Resources",
"#{srcroot}/NetWork",
"#{srcroot}/TabbarSetDataSource",
"#{srcroot}/TNHQHome"
]
#复制源target目录下的定制化代码目录到目标target目录
hasAllListFiles = false
codeDirs.each do |d|
hasAllListFiles = Dir.exists?(d)#-> 此处假设所有的code file为一个整体,一有具有
if hasAllListFiles
FileUtils.cp_r d, targetdir
end
end # 寻找模板target
proj = Xcodeproj::Project.open(project_path)
src_target = proj.targets.find { |item| item.to_s == srcname }
# 创建目标target
target = proj.new_target(src_target.symbol_type, name, src_target.platform_name, src_target.deployment_target)
target.product_name = name # create scheme
scheme = Xcodeproj::XCScheme.new
scheme.add_build_target(target)
scheme.set_launch_target(target)
scheme.save_as(project_path, name) # build_configurations
target.build_configurations.map do |item| #设置target相关配置
item.build_settings.update(src_target.build_settings(item.name))
# puts "-"*30 + "#{item.build_settings}" +"_"*30
item.build_settings["PRODUCT_BUNDLE_IDENTIFIER"] = "com.abc.aa.#{name}"
item.build_settings["PRODUCT_NAME"] =displayname targetInfoPlist = item.build_settings["INFOPLIST_FILE"]
item.build_settings["INFOPLIST_FILE"] = targetInfoPlist.sub(srcname, name) puts "-"*30 + "#{item.build_settings['PRODUCT_BUNDLE_IDENTIFIER']}" +"_"*30
puts "-"*30 + "#{item.build_settings['PRODUCT_NAME']}" +"_"*30
end # build_phases
phases = src_target.build_phases.reject { |x| x.instance_of? Xcodeproj::Project::Object::PBXShellScriptBuildPhase }.collect(&:class) #复制源target引用的source和resource文件引用
phases.each do |klass|
puts "||---------------------> copy phases #{klass}--------------------||"
src = src_target.build_phases.find { |x| x.instance_of? klass }
dst = target.build_phases.find { |x| x.instance_of? klass } unless dst
dst ||= proj.new(klass)
target.build_phases << dst
end
dst.files.map { |x| x.remove_from_project } idx = 1
src.files.each do |f|
# 排除文件,将源target中的文件排除,不引用该文件
if f.file_ref and f.file_ref.hierarchy_path.index(srcroot) != nil
puts "\n................... ignore file: #{f.file_ref}, #{f.file_ref.hierarchy_path}...................\n"
next
end file_ref = proj.new(Xcodeproj::Project::Object::PBXFileReference)
if f.settings
puts ">>file.settings: #{idx} > file: " + f.file_ref.to_s + " settings: " + f.settings.to_s
end idx = idx+1
if f.file_ref
if f.file_ref.name
puts ">> file_ref name: #{f.file_ref.name} path: #{f.file_ref.path} source_tree: #{f.file_ref.source_tree}"
end
# puts ">> file path: #{f.file_ref.hierarchy_path}-- #{f.file_ref.real_path}" file_ref.name = f.file_ref.name
file_ref.path = f.file_ref.path
file_ref.source_tree = f.file_ref.source_tree
file_ref.last_known_file_type = f.file_ref.last_known_file_type
# file_ref.fileEncoding = f.file_ref.fileEncoding
begin
file_ref.move(f.file_ref.parent)
rescue
end end build_file = proj.new(Xcodeproj::Project::Object::PBXBuildFile)
build_file.file_ref = f.file_ref
# 文件属性配置,如no-arc
if f.settings
build_file.settings = f.settings
end
dst.files << build_file
end
end #设置目标target文件组
projTargetGroup = proj.main_group.groups.find { |x| x.path == 'TNTarget' }
targetGroup = projTargetGroup.new_group(name, name)
# resource
resourceGroup = targetGroup.new_group("Resources", "./Resources")
supportingGroup=resourceGroup.new_group("Supporting Files") # 添加资源文件引用,注意和代码文件引用方式不同
target.add_resources(
[
resourceGroup.new_reference("areaCode.plist"),
resourceGroup.new_reference("login_toon_bg@2x.png"),
resourceGroup.new_reference("login_toon_bg@3x.png"),
resourceGroup.new_reference("tab_item_home_highlight@2x.png"),
resourceGroup.new_reference("tab_item_home_highlight@3x.png"),
resourceGroup.new_reference("tab_item_home_normal@2x.png"),
resourceGroup.new_reference("tab_item_home_normal@3x.png"),
resourceGroup.new_reference("Toon_logo@2x.png"),
resourceGroup.new_reference("Toon_logo@3x.png"),
resourceGroup.new_reference("toon_serviceProtocol.html"),
resourceGroup.new_reference("user_protocol.html"),
resourceGroup.new_reference("NewFunction.html"), supportingGroup.new_reference("Supporting Files/configuration.plist"),
supportingGroup.new_reference("Supporting Files/Info.plist"),
supportingGroup.new_reference("Supporting Files/Images.xcassets"),
supportingGroup.new_reference("Supporting Files/InfoPlist.strings"),
supportingGroup.new_reference("Supporting Files/Localizable.strings")
]) if hasAllListFiles
# 添加代码文件组
code1 = targetGroup.new_group("NetWork", "./NetWork")
code2 = targetGroup.new_group("TabbarSetDataSource", "./TabbarSetDataSource")
code3 = targetGroup.new_group("TNHQHome", "./TNHQHome") # 添加代码文件引用
target.add_file_references(
[
code1.new_reference("NetworkRequestURL.h"),
code1.new_reference("NetworkRequestURL.m"), code2.new_reference("TNTabSettingDataSource.h"),
code2.new_reference("TNTabSettingDataSource.m"), code3.new_reference("TNHomeViewController.m")
])
end # 修改文件通用内容
infoplistfile = "#{targetdir}/Resources/Supporting Files/Info.plist"
files = [
"#{targetdir}/Resources/areaCode.plist",
"#{targetdir}/Resources/toon_serviceProtocol.html",
"#{targetdir}/Resources/user_protocol.html",
"#{targetdir}/Resources/NewFunction.html",
infoplistfile,
"#{targetdir}/Resources/Supporting Files/InfoPlist.strings",
"#{targetdir}/Resources/Supporting Files/Localizable.strings" ]
if hasAllListFiles
files << "#{targetdir}/TabbarSetDataSource/TNTabSettingDataSource.m"
end
files.each do |f1|
File.open(f1) do |fr|
buffer = fr.read.gsub(srcdisplayname, displayname)
buffer= buffer.gsub("项目名", displayname)
buffer= buffer.gsub("大同", displayname)
File.open(f1, "w") { |fw| fw.write(buffer) }
end
end # 修改info.plist
File.open(infoplistfile) do |fr|
if hasAllListFiles
puts "*************************** 1"
buffer = fr.read.gsub("<string>10024</string>", "<string>#{target_pushType}</string>")
buffer= buffer.gsub("<integer>124</integer>", "<integer>#{target_toonType}</integer>")
buffer= buffer.gsub("<string>1241002</string>", "<string>#{target_channel}</string>")
buffer= buffer.gsub("<string>8058bda8c0ad5a7cfb8742cfbac4ecb8</string>", "<string>#{target_mapKey}</string>")
buffer= buffer.gsub("<string>toon124</string>", "<string>#{target_schemeType}</string>")
else
puts "*************************** 2"
buffer = fr.read.gsub("<string>10016</string>", "<string>#{target_pushType}</string>")
buffer= buffer.gsub("<integer>116</integer>", "<integer>#{target_toonType}</integer>")
buffer= buffer.gsub("<string>10035</string>", "<string>#{target_channel}</string>")
buffer= buffer.gsub("<string>e851d7df83d59f143bff1ad5a3a8e554</string>", "<string>#{target_mapKey}</string>")
buffer= buffer.gsub("<string>toon116</string>", "<string>#{target_schemeType}</string>")
end
puts "*************************** updating InfoPlist" File.open(infoplistfile, "w") { |fw| fw.write(buffer) } end
proj.save # 修改Podfile
puts ">> prepare loading pods ..."
podTarget = "target '#{name}' do shared_pods end"
File.open("Podfile") do |file|
if file.read().index(podTarget) ==nil
File.open(infoplistfile, "w") { |fw| fw.puts podTarget }
puts ">> add pod item"
else
puts ">> pod has been added"
end end # file.close # 更新pod依赖
exec 'pod install'
iOS 使用脚本自动化复制target的更多相关文章
- shell脚本自动化部署服务
shell脚本自动化部署 !/bin/bash #export PATH=$PATH:/export/maven/bin run_flag_dir="/data0/shell/deploy_ ...
- Cocos2d-x3.0 iOS 一键编译多个target并打包ipa。
1.编写app打包为ipa的 shell脚本.将以下代码保存为app2ipa.sh. #!/bin/sh m_appPath="" m_ipaPath="" m ...
- fdisk分区硬盘并shell脚本自动化
最近工作需要用到对硬盘进行shell脚本自动化分区和mount的操作,google了一些资料,下面做个总结. 如果硬盘没有进行分区(逻辑分区或者扩展分区,关于两者概念,自行google),我们将无法将 ...
- Modelsim调用用do脚本自动化仿真
前言 EDA发展的趋势是自动化,使用脚本自动化仿真可以减少不必要的时间浪费. 流程 在windows下新建批处理脚本bat文件(linux下可用shell脚本或者其他,注意给脚本运行权限即可:chmo ...
- MySQL数据库主从切换脚本自动化
MySQL数据库主从切换脚本自动化 本文转载自:https://blog.csdn.net/weixin_36135773/article/details/79514507 在一些实际环境中,如何实现 ...
- iOS开发系列-自动化分发测试打包
概述 项目在测试阶段需要频繁打包给测试人员,对于这些固定化的操作我们可以使用自动化的手段去解决,将时间放在有意义的事情上. xcodebuild 是苹果发布自动构建的工具. Shell脚本打包 xcr ...
- iOS使用fastlane自动化打包到fir(最全最详细流程)
# iOS使用fastlane自动化打包到fir(最全最详细流程)1. **首先确认是否安装了ruby,终端查看下ruby版本**> ruby -v终端输出:ruby 2.4.1p111 (20 ...
- shell脚本自动化部署
由于公司技术部团队较小,没有专门的运维团队,所以运维工作技术部承包了. 一.纯人工部署是这样的: 1. 本地打包:一般 maven clean package 2. 借助xftp上传到服务器对应目录 ...
- appium ios真机自动化环境搭建&运行(送源码)
appium ios真机自动化环境搭建&运行(送源码) 原创: f i n 测试开发社区 6天前 Appium测试环境的搭建相对比较烦琐,不少初学者在此走过不少弯路 首先是熟悉Mac的使用 ...
随机推荐
- graphviz.js的图形及属性简单用法
digraph A { graph[bgcolor="cadetblue" label="图的标题" fontsize=48 fontcolor="g ...
- PowerShell攻防进阶篇:nishang工具用法详解
PowerShell攻防进阶篇:nishang工具用法详解 导语:nishang,PowerShell下并肩Empire,Powersploit的神器. 开始之前,先放出个下载地址! 下载地址:htt ...
- 软件开发 —— 重构(refactor)
0. 代码坏味道 Large Class,过大的类:Large method,过长的(成员)函数: 1. 基本内涵 在不改变代码外在行为的前提下对代码做出修改,以改进代码的内部结构的过程. -- &l ...
- 杂项-JAVA:MVP
ylbtech-杂项-JAVA:MVP 简称:MVP 全称:Model-View-Presenter :MVP 是从经典的模式MVC演变而来,它们的基本思想有相通的地方:Controller/Pres ...
- mysql5.7下面windows平台大小写敏感
转自:https://blog.csdn.net/lrl219/article/details/52889582 根据网上的信息在my.ini下面的mysqld的配置下面添加lower_case_ta ...
- python 3.7 replace函数的坑
使用replace时必须用 str=str.replace(old,new) 如果用 str.replace(old,new)会不起作用. 注意:若str中没有old变量,也不会报错 应用: 练习题 ...
- .net中的TreeView的数据绑定与EasyUi_tree的数据绑定
昨天看到了.net中的TreeView,学习了一波TreeView的数据绑定,联想到EasyUi中的Tree的数据,觉得里面的逻辑差不多,就总结了一下两者的数据绑定. 前端页面和必要的JS如下 < ...
- 文档控件NTKO OFFICE 详细使用说明之预览PDF文件(禁止打印、下载、另存为、防抓包下载)
1.在线预览PDF文件(禁止打印.下载.复制.另存为) (1) 运行环境 ① 浏览器:支持IE7-IE11(平台版本还支持Chrome和Firefox) ② IE工具栏-Internet 选项:将ww ...
- JavaScript的面向对象
JavaScript的对象 对象是JavaScript的一种数据类型.对象可以看成是属性的无序集合,每个属性都是一个键值对,属性名是字符串,因此可以把对象看成是从字符串到值的映射.这种数据结构在其他语 ...
- 第一课 导入库 - 创建数据集 - CSV读取 - 导出 - 查找最大值 - 绘制数据
第1课 创建数据 - 我们从创建自己的数据集开始分析.这可以防止阅读本教程的最终用户为得到下面的结果而不得不下载许多文件.我们将把这个数据集导出到一个文本文件中,这样您就可以获得从文本文件中一些拉取数 ...