- 项目目录:
- protocol_def、userPreference_def 是protobuf协议文件
- start可执行文件源码:
#!/bin/sh
#
# An example hook script to prepare a packed repository for use over
# dumb transports.
#
# To enable this hook, rename this file to "post-update".protoPPath=`dirname $0`
echo ${protoPPath}
cd ${protoPPath}
ruby protocol.rb
- protocol.rb 源码:
require 'xcodeproj'
system 'echo $(pwd)'
project_path = File.join(File.dirname(__FILE__), "../KLProtobufs.xcodeproj")
project = Xcodeproj::Project.open(project_path)
target = project.targets.first
group = project.main_group.find_subpath(File.join('KLProtobufs', 'protoBufs'), true)
group.set_source_tree('<group>')
group.set_path('protoBufs')def removeBuildPhaseFilesRecursively(aTarget, aGroup)
aGroup.files.each do |file|
if file.real_path.to_s.end_with?(".m", ".mm", ".cpp") then
aTarget.source_build_phase.remove_file_reference(file)
elsif file.real_path.to_s.end_with?(".plist") then
aTarget.resources_build_phase.remove_file_reference(file)
end
end
aGroup.groups.each do |group|
removeBuildPhaseFilesRecursively(aTarget, group)
end
endif !group.empty? then
removeBuildPhaseFilesRecursively(target, group)
group.clear()
group.remove_from_project
endgroup = project.main_group.find_subpath(File.join('KLProtobufs', 'protoBufs'), true)
group.set_source_tree('<group>')
group.set_path('protoBufs')def createProtoFile(project)
system 'sh protocol.sh'
endcreateProtoFile(project)
def addFilesToGroup(project, aTarget, aGroup)
Dir.foreach(aGroup.real_path) do |entry|
filePath = File.join(aGroup.real_path, entry)
# 过滤目录和.DS_Store文件
if !File.directory?(filePath) && entry != ".DS_Store" && entry != ".md" && filePath.to_s.end_with?(".m", ".mm", ".h") then
# 特殊逻辑
# 向group中增加文件引用
fileReference = aGroup.new_reference(filePath)
# 如果不是头文件则继续增加到Build Phase中,PB文件需要加编译标志
if filePath.to_s.end_with?("pbobjc.m", "pbobjc.mm") then
aTarget.source_build_phase.add_file_reference(fileReference, true)
elsif filePath.to_s.end_with?(".m", ".mm", ".cpp") then
aTarget.source_build_phase.add_file_reference(fileReference, true)
elsif filePath.to_s.end_with?(".plist") then
aTarget.resources_build_phase.add_file_reference(fileReference, true)
end
# 目录情况下, 递归添加
elsif File.directory?(filePath) && entry != '.' && entry != '..' then
hierarchy_path = aGroup.hierarchy_path[1, aGroup.hierarchy_path.length]
subGroup = project.main_group.find_subpath(hierarchy_path + '/' + entry, true)
subGroup.set_source_tree(aGroup.source_tree)
subGroup.set_path(aGroup.real_path + entry)
addFilesToGroup(project, aTarget, subGroup)
end
end
endaddFilesToGroup(project, target, group)
project.save
- protocol.sh 源码:
#!/bin/sh
protoFPath="$(pwd)/protoBufs"
rm -rf protoBufs
mkdir protoBufscd protocol_def/
protoc *.proto protocols/shared/*.proto --proto_path="." --objc_out="../protoBufs"
function scandir() {
local cur_dir parent_dir workdir
workdir=$1
cd ${workdir}
if [ ${workdir} = "/" ]
then
cur_dir=""
else
cur_dir=$(pwd)
fi
for dirlist in $(ls ${cur_dir})
do
if test -d ${dirlist};then
cd ${dirlist}
sed -i '' -e 's/protocols\/shared\///g' *
mv ${cur_dir}/${dirlist}/*.m *.h ${protoFPath}
scandir ${cur_dir}/${dirlist}
cd ..
elif test -f ${dirlist}; then
sed -i '' -e 's/protocols\/shared\///g' ${dirlist}
fi
done
}
for dirlist in ${protoFPath}; do
if test -d ${dirlist};then
scandir ${dirlist}
fi
donecd ../userPreference_def/
protoc *.proto share/*.proto --proto_path="." --objc_out="../protoBufs"
function scandir1() {
local cur_dir parent_dir workdir
workdir=$1
cd ${workdir}
if [ ${workdir} = "/" ]
then
cur_dir=""
else
cur_dir=$(pwd)
fi
for dirlist in $(ls ${cur_dir})
do
if test -d ${dirlist};then
cd ${dirlist}
sed -i '' -e 's/share\///g' *
mv ${cur_dir}/${dirlist}/*.m *.h ${protoFPath}
scandir1 ${cur_dir}/${dirlist}
cd ..
elif test -f ${dirlist}; then
sed -i '' -e 's/share\///g' ${dirlist}
fi
done
}
for dirlist in ${protoFPath}; do
if test -d ${dirlist};then
scandir1 ${dirlist}
fi
done
protoBufs目录就是执行start之后生成的,里面存着变异好的oc文件(就是在项目中可以实际用到的)
好了代码都贴出来了,及时看不懂的复制粘贴也应该知道有些地方是需要替换成自己项目中的目录和路径的吧~