require 'fileutils' require 'json' module EMMFiles def self.create_podfile(configs, output_dir, proj_name) # create_podfile : 通过 json 配置文件,创建一个 podfile # configs : 项目配置 # output_dir : Podfile 文件导出目录 podfile = File.new(output_dir + "/Podfile", "w+") podfile.syswrite("source 'https://github.com/CocoaPods/Specs.git'\n") podfile.syswrite("source '" + configs["private_repo"] + "'\n") podfile.syswrite("platform :ios, ‘" + configs["deployment_target"] + "’\n") podfile.syswrite("target '" + proj_name + "' do\n") for pod in configs["pods"] podfile.syswrite(pod + "\n") end podfile.syswrite("end") podfile.close end def self.copy_xcconfig(source_path, export_path) # 从 pod 的 xcconfig 中提取出所需内容,生成项目所需的 xcconfig # sourcePath : Cocoapods 生成的 xcconfig 文件 # outputPath : 导出的 xcconfig 文件 output_file = File.new(export_path, "w+") IO.foreach(source_path) do |line| if line.start_with?("OTHER_LDFLAGS") output_file.syswrite(line) end end output_file.syswrite("HEADER_SEARCH_PATHS = $(inherited) ${SRCROOT}/EMM_Pods/Headers/**\n") output_file.syswrite("LIBRARY_SEARCH_PATHS = $(inherited) ${SRCROOT}/EMM_Pods/Libraries") output_file.close end def self.copy_resources(pods_dir, pods_proj_name, export_dir) # 从 pod 的 XXX_Proj-resources.sh 脚本文件中提取出资源文件的路径,并将资源文件拷贝到导出目录下 # source_path: Pods 文件夹路径 # export_dir: 资源文件导出目录 start_string = 'if [[ "$CONFIGURATION" == "Release" ]]; then' end_string = 'fi' writing = false # 读取 Cocoapods 提供的 copy resources 的脚本文件 IO.foreach(pods_dir + "/Target Support Files/Pods-" + pods_proj_name + "/Pods-" + pods_proj_name + "-resources.sh") do |line| if line.start_with?(start_string) writing = true next end if writing if line.start_with?(end_string) break end line = line.match("\".+\"")[0] line = line.gsub("\"", "") src = pods_dir + "/" + line dst = export_dir + "/" + line FileUtils.mkdir_p(File.dirname(dst)) if File.directory?(src) FileUtils.cp_r(src, File.dirname(dst)) else FileUtils.cp(src, dst) end end end end end