IOS uses scripts to automatically copy target and iostarget

Source: Internet
Author: User

IOS uses scripts to automatically copy target and iostarget

After some projects are mature, automatic configuration is required to generate a brand new project. Developers do not need to manually modify the project file and hand over the configuration and chemical engineering work to the O & M or configuration team.

In fact, according to the common practice, it is nothing more than to set the target duplicate in xcode, and then modify the relevant project name, target name, bundleid, and so on, in fact, these contents have corresponding configuration information in the xcodeproj file, so we can directly modify the Xcodeproj file.

Thank you firstShrimp GodProvides detailed principles and tools, and reduces the number of detours in implementation. If you are interested, you can goShrimp GodHere I will post the final implementation for your reference ~~

If you want to learn more about ios project configurations and scripts, we recommend that you go here:

Ruby doc

Xcodeproj API documentation

Github example

Use scripts to solve Target Management Problems

 

#! /Usr/bin/env rubyrequire 'rubygems 'require 'xcodeproj' require 'fileutils '# configure the configuration content of the target project. region # name = "newyorktoon" displayname = "New York Tong" target_toonType = 10001 = "hello" target_channel = "hello" target_mapKey = "hello" target_schemeType = "hello" # ----------------------------------- target project configuration content -------------------------- # template project # srcname = "Tzhqtoon" # srcdisplayname = "" # projectproject_path = "Hello. xcodeproj "# copy the resource file. Note: #1. exclude source resource files when copying resource files #2. at the end of the file, add the copied resource file to the target targettargetdir = "TNTarget/# {name}" srcroot = "TNTarget/# {srcname}" # copy the resource folder, copy the image resource folder under the source target to the target directory if! Dir. exists? (Targetdir) Dir. mkdir (targetdir) endcodeDirs = ["# {srcroot}/Resources", "# {srcroot}/NetWork", "# {srcroot}/TabbarSetDataSource ", "# {srcroot}/TNHQHome"] # copy the customized code directory under the source target directory to the target directory hasAllListFiles = falsecodeDirs. each do | d | hasAllListFiles = Dir. exists? (D) #-> assume that all the code files are a whole, and one has the if hasAllListFiles FileUtils. cp_r d, targetdir endend # Find the template targetproj = Xcodeproj: Project. open (project_path) src_target = proj.tar gets. find {| item. to_s = srcname} # create target targettarget = proj. new_target (src_target.symbol_type, name, src_target.platform_name, src_target.deployment_target) target. product_name = name # create schemescheme = Xcodeproj: XCScheme. news Cheme. add_build_target (target) scheme. set_launch_target (target) scheme. save_as (project_path, name) # build_configurationstarget.build_configurations.map do | item | # sets the target configuration 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"] = disp Layname 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']} "+" _ "* 30end # build_phasesphases = src_target.build_phases.reject {| x. instance_of? Xcodeproj: Project: Object: PBXShellScriptBuildPhase }. collect (&: class) # copy the source and resource files referenced by the source target to reference phases. each do | klass | puts "| -------------------> copy phases # {klass} -------------------- |" src = src_target.build_phases.find {| x. instance_of? Klass} dst = target. build_phases.find {| x. instance_of? Klass} unless dst | = proj. new (klass) target. build_phases <dst end dst. files. map {| x. remove_from_project} idx = 1 src. files. each do | f | # exclude files from the source 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 build_file = proj. new (Xcodeproj: Project: Object: PBXBuildFile) build_file.file_ref = f. file_ref # File Attribute configuration, such as no-arc if f. settings build_file.settings = f. settings end dst. files <build_file endend # set the target file group projTargetGroup = proj. main_group.groups.find {| x. path = 'tntarget'} targetGroup = projTargetGroup. new_group (name, name) # resourceresourceGroup = targetGroup. new_group ("Resources ",". /Resources ") supportingGroup = resourceGroup. new_group ("Supporting Files") # Add Resource file references. Note that the reference method is different from that of the code file. 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 # Add code file group code1 = targetGroup. new_group ("NetWork ",". /NetWork ") code2 = targetGroup. new_group ("TabbarSetDataSource ",". /TabbarSetDataSource ") code3 = targetGroup. new_group ("TNHQHome ",". /TNHQHome ") # Add a code file reference 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 # modify the file's common content 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 "endfiles. each do | f1 | File. open (f1) do | fr | buffer = fr. read. gsub (srcdisplayname, displayname) buffer = buffer. gsub ("project name", displayname) buffer = buffer. gsub ("Datong", displayname) File. open (f1, "w") {| fw. write (buffer)} endend # modify 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. write (buffer)} endproj. save # modify Podfileputs "> 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. puts podTarget} puts "> add pod item" else puts "> pod has been added" endend # file. close # update pod dependency exec 'pod install'

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.