- Xcodeproj Ruby works on Github
- Mod-pbxproj Python operations on Github
This article focuses on xcodeproj
Load XXX.The project configuration
project_path = 'xxx.xcodeproj'
project = Xcodeproj::Project.open(project_path)
Copy the code
2. Modify the build setting of a Target of the project
project.targets.each do |target| #your target name if target.name == 'target1' target.build_configurations.each do | config # | Debug/Release if the config name = = 'Release' # for the build Settings build_settings = config. Build_settings # build_settings is a hash, there is one configured build_settings. Each do | key, value | print key, "= =", value, "\ n" # Settings such as certificate here for operation, the commonly used are as follows: # build_settings[key] = "com.test.demo" # build_settings["CODE_SIGN_ENTITLEMENTS"] = "Xxx. entitlements" # set signature iPhone Distribution/iPhone Developer build_settings["PROVISIONING_PROFILE_SPECIFIER"] = "PROFILENAME" build_settings["PRODUCT_BUNDLE_IDENTIFIER"] = "your BUNDLEID" build_settings["DEVELOPMENT_TEAM"] = "your TEAMID" # ..... Otherwise, look up the API as requiredCopy the code
3. Rely on and unrely on Extension
def updateDependecies(mode, project, app_target_name, ext) require 'xcodeproj' # Find project and app target puts mode xc = project app_target = xc.targets.find { |t| t.name == app_target_name } # Find app extensions' target objects target = xc.targets.find { |t| t.name == ext } return "Couldn't find a '#{ext}' target in '#{app_target_name}'" if target.nil? return "'#{ext}' doesn't seem to be an application extension target" unless target.product_type.include? 'app-extension' target # Find .appex product files appex = target.product_reference # Add or remove dependency on extension targets dependency = app_target.dependency_for_target(target) puts app_target.dependencies if mode == 'add' if dependency puts "[WARN] App already has dependency on #{target.name}" else app_target.add_dependency(target) end else if dependency app_target.dependencies.delete(dependency) else puts "[WARN] Couldn't find dependency on #{target.name}" end end # Add or remove .appex copy jobs embed_extensions_phase = app_target.copy_files_build_phases.find do |copy_phase| copy_phase.symbol_dst_subfolder_spec == :plug_ins end return "Couldn't find 'Embed App Extensions' phase" if embed_extensions_phase.nil? appex_included = embed_extensions_phase.files_references.include? appex if mode == 'add' if appex_included puts "[WARN] App already embeds #{appex.display_name}" else build_file = embed_extensions_phase.add_file_reference(appex) build_file.settings = { "ATTRIBUTES" => ['RemoveHeadersOnCopy'] } end else if appex_included embed_extensions_phase.remove_file_reference(appex) else puts "[WARN] App doesn't seem to embed #{appex.display_name}" end end return "update dependency '#{ext}' target in '#{app_target_name}'" endCopy the code
Add/remove framework to Target
Related knowledge reference links
Link binary with libraries system libraries, link them.
Embed Frameworks 3rd party libraries, embed them.
- Add or remove a system framework
def exsit_framework? (build_phase,name) # next ! if build_phase.nil? Build_phase. Files_references. Each do | ref. | if ref name = = "# {name}" puts "# {name}" existing return true end end puts "does not exist #{name}" return false end def updateSystemFramework(project,target,name, command) require 'xcodeproj' puts "#{command} #{name} to #{target}" build_phase = target.frameworks_build_phase framework_group = project.frameworks_group if command == :add if self.exsit_framework?(build_phase,name) return end systempath = "System/Library/Frameworks/" path = "#{systempath}#{name}" file_ref = framework_group.new_reference(path) file_ref.name = "#{name}" file_ref.source_tree = 'SDKROOT' build_file = build_phase.add_file_reference(file_ref) else Build_phase. Files_references. Each do | ref. | if ref name = = "# {name}" puts "delete # {name}" build_phase.remove_file_reference(ref) framework_group.children.delete(ref) break end end end endCopy the code
- Add or remove custom framework and lib
def updateCustomFramework(project,target, path,name,command) # Get useful variables frameworks_group = project.groups.find { |group| group.display_name == 'Frameworks' } frameworks_build_phase = target.build_phases.find { |build_phase| build_phase.to_s == 'FrameworksBuildPhase' } embed_frameworks_build_phase = nil target.build_phases.each do |phase| if phase.display_name == 'Embed Frameworks' embed_frameworks_build_phase = phase end # puts phase end if command == :add # Add new "Embed Frameworks" build phase to target if embed_frameworks_build_phase.nil? embed_frameworks_build_phase = project.new(Xcodeproj::Project::Object::PBXCopyFilesBuildPhase) embed_frameworks_build_phase.name = 'Embed Frameworks' embed_frameworks_build_phase.symbol_dst_subfolder_spec = :frameworks target.build_phases << embed_frameworks_build_phase end # Add framework search path to target ['Debug', 'Release'].each do |config| paths = ['$(inherited)', Path] orgpath = target.build_settings(config)['FRAMEWORK_SEARCH_PATHS'] if orgpath.nil? Puts "Path is empty ----------" Target. Build_settings (config) [' FRAMEWORK_SEARCH_PATHS] = paths else puts "path not empty -- -- -- -- -- -- -- -- -- --" paths. Each do | | if p! orgpath.include? (p) orgpath << p end end end end # Add framework to target as "Embedded Frameworks" framework_ref = frameworks_group.new_file("#{path}#{name}") exsit = false embed_frameworks_build_phase.files_references.each do |ref| if ref.name = name exsit = true end end if ! exsit build_file = embed_frameworks_build_phase.add_file_reference(framework_ref) frameworks_build_phase.add_file_reference(framework_ref) build_file.settings = { 'ATTRIBUTES' => ['CodeSignOnCopy'] } End the else frameworks_build_phase. Files_references. Each do | ref. | if ref name = = "# {name}" puts "delete # {name}" frameworks_build_phase.remove_file_reference(ref) embed_frameworks_build_phase.remove_file_reference(ref) break end end end endCopy the code
Enable SystemCapabilities
For example: AccessWiFi
Note: Functions like group need to be modified XXX. Entitlements
# introduced from outside the parameters of the wifiFlag = ARGV [0] project. The objects. Each do | obj | if obj. Isa = = "PBXProject systemCapabilities =" obj.attributes["TargetAttributes"][ta.uuid]["SystemCapabilities"] puts systemCapabilities systemCapabilities["com.apple.AccessWiFi"] = {"enabled":"#{wifiFlag}"} end endCopy the code
6. Modify PLIST directly
# encoding: utf-8 #! /usr/bin/ruby # XXXX changes its project name to puts "and enters" require 'xcodeProj' project_path = './ ios. xcodeproj' project = Xcodeproj:: project.open (project_path) # Open the Project file plist_path = './IOS/Supporting Files/ info.plist 'plistHash = Xcodeproj:: plist. read_from_path(plist_path) # Reads the project Plist configuration file puts(" before modification imappmios-info.plist =",plistHash) # passes the argument ARGV if ARGV.size ! = 4 puts "failed to modify" else NAMES = array. new names = plistHash['CFBundleURLTypes'] names.delete_at(1) names.delete_at(1) plistHashitems = names.at(3) weixinArror = plistHashitems['CFBundleURLSchemes'] weixinArror.pop weixinArror.push('wxf8aece62e88666c8') plistHash['CFBundleURLTypes'] = names # end # if ARGV[1] ! = "appBundleStr" # plistHash['CFBundleIdentifier'] = ARGV[1] #bundle id # end # if ARGV[2] ! = "ShortVersionString" # plistHash['CFBundleShortVersionString'] = ARGV[2] #bundle version # end # if ARGV[3] ! = "appVersionStr" # plistHash['CFBundleVersion'] = ARGV[3] # end # plistHash['CFBundleDisplayName'] = ARGV[0] # plistHash['CFBundleIdentifier'] = ARGV[1] #bundle ID # plistHash['CFBundleVersion'] = ARGV[2] #bundle version Puts (" modified imappmios-info.plist =",plistHash) Xcodeproj:: plist. write_to_path(plistHash, The plist configuration file puts "modified successfully" endCopy the code
From juejin. Cn/post / 684490…
Thanks to asML, author of the original article
See article for more
The following several cases of the original text have not been encountered.