Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save danielpetroianu/ba99d85e399af0d930a056aa9dae551b to your computer and use it in GitHub Desktop.
Save danielpetroianu/ba99d85e399af0d930a056aa9dae551b to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Sets in a pod the given build setting
#
# @param [Xcodeproj::Project] project
# The xcode project instance.
#
# @param [Hash] build_settings
# An hash with the build configurations
#
# @param [Array<String>] build_configurations = nil
# An array with all the build configurations that the `build_setting` will apply to
#
# @param [Array<String>] targets = nil
# An array with all the names of the targets that the `build_setting` will apply to.
#
# @example Enable asserts on all pods and targets:
# set_project_build_settings project, {'ENABLE_NS_ASSERTIONS' => 'YES'}
#
# @example Enable asserts on a specific configuration:
# set_project_build_settings project, {'ENABLE_NS_ASSERTIONS' => 'YES'}, :build_configurations => ['Testing']
#
def set_project_build_settings(project, build_settings, **params)
build_configurations = params.fetch(:build_configurations, nil)
targets = params.fetch(:targets, nil)
raise "Expecting an hash for `build_settings`, got #{build_settings.inspect}" unless build_settings.kind_of?(Hash)
raise "Invalid build_setting key 'build_configurations', got: #{build_settings.inspect}" if build_settings.has_key?(:build_configurations)
raise "Invalid build_setting key 'targets', got: #{build_settings.inspect}" if build_settings.has_key?(:targets)
raise "Expecting nil or an array for `build_configurations`, got: #{build_configurations.inspect}" if !build_configurations.nil? && !build_configurations.kind_of?(Array)
raise "Expecting nil or an array for `targets`, got #{targets.inspect}" if !targets.nil? && !targets.kind_of?(Array)
title = []
title << "Setting '#{build_settings}' on"
title << " - configurations: #{build_configurations || 'all configurations'}"
title << " - pods project targets: #{targets || 'all targets'}"
Pod::UI.title( title.join("\n") )
found_build_configurations = []
found_targets = []
project.targets.each do |target|
next if !targets.nil? && !targets.include?(target.name)
found_targets << target.name
verboseMessage = "`#{target.name}` ( "
target.build_configurations.each do |build_configuration|
next if !build_configurations.nil? && !build_configurations.include?(build_configuration.name)
found_build_configurations << build_configuration.name
verboseMessage += "#{build_configuration.name} "
build_settings.each do |build_setting, value|
build_configuration.build_settings[build_setting.to_s] = value.to_s
end
end
verboseMessage += ")\n"
Pod::UI.message(verboseMessage)
end
notfound_build_configurations = (build_configurations || []) - found_build_configurations
notfound_targets = (targets || []) - found_targets
if notfound_build_configurations.count != 0
Pod::UI.warn("Could not find #{notfound_build_configurations} build configuration(s)")
end
if notfound_targets.count != 0
Pod::UI.warn("Could not find #{notfound_targets} target(s)")
end
end
@danielpetroianu
Copy link
Author

danielpetroianu commented Jan 31, 2019

Example usage in Podfile

# ...

post_install do |pod_installer|
    require_relative "./cocoapods_set_project_build_settings.rb"
    
    pods_project = pod_installer.pods_project
    
    set_project_build_settings( pods_project, {'CLANG_WARN_DOCUMENTATION_COMMENTS' => 'NO', 'CLANG_WARN_STRICT_PROTOTYPES' => 'NO'})
    set_project_build_settings( pods_project, {'GCC_OPTIMIZATION_LEVEL' => '0'}, :build_configurations => ['Debug'] )

    # https://github.com/CocoaPods/CocoaPods/issues/8069
    # https://github.com/CocoaPods/CocoaPods/issues/7314
    deployment_targets = pods_project.build_configurations.map{ |config| config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] }
    minimal_deployment_target = deployment_targets.min_by{ |version| Gem::Version.new(version) }
    set_project_build_settings( pods_project, { 'IPHONEOS_DEPLOYMENT_TARGET' => minimal_deployment_target } )
   
end

and will output

Setting '{"CLANG_WARN_DOCUMENTATION_COMMENTS"=>"NO", "CLANG_WARN_STRICT_PROTOTYPES"=>"NO"}' on
  - configurations: all configurations
  - pods project targets: all targets

Setting '{"GCC_OPTIMIZATION_LEVEL"=>"0"}' on
  - configurations: ["Debug"]
  - pods project targets: all targets

Setting '{"IPHONEOS_DEPLOYMENT_TARGET"=>"9.0"}' on
  - configurations: all configurations
  - pods project targets: all targets

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment