Randall
Randall

Reputation: 14849

Add custom build rule with Podfile post_install hook

I have a Cocoapod that requires some custom build rules to work correctly.

It doesn't appear to me that there would be a way to add any custom build rules in the podspec, but maybe there is a way with a post_install hook in the Podfile?

Upvotes: 1

Views: 764

Answers (1)

Randall
Randall

Reputation: 14849

I was eventually able to get this working. I wrote a ruby function that uses the Xcodeproj gem (which is available to use in a Podfile.)

def add_build_rule(target_name, project)
  new_rule = project.new(Xcodeproj::Project::Object::PBXBuildRule)

  project.targets.each do |target|
    if target.name == target_name
      if target.build_rules.count != 0
        puts "#{target.name} already has a build rule."
        return
      end
      puts "Updating #{target.name} rules"

      new_rule.name = 'My Custom Rule'
      new_rule.compiler_spec = 'com.apple.compilers.proxy.script'
      new_rule.file_patterns = 'myFile.whatever'
      new_rule.file_type = 'pattern.proxy'
      new_rule.is_editable = '1'
      new_rule.output_files = []
      new_rule.input_files = []
      new_rule.output_files_compiler_flags = []
      new_rule.script = "echo Hello World"
      target.build_rules.append(new_rule)
    end
  end

  project.objects_by_uuid[new_rule.uuid] = new_rule
  project.save()
end

Then in my podfile I added this post_install hook.

post_install do |installer|
    add_build_rule("MyTarget", installer.pods_project)
end

Upvotes: 1

Related Questions