Skip to content

Instantly share code, notes, and snippets.

@phatblat
Created October 20, 2014 15:30
Show Gist options
  • Select an option

  • Save phatblat/87b394ea26a84ba977a6 to your computer and use it in GitHub Desktop.

Select an option

Save phatblat/87b394ea26a84ba977a6 to your computer and use it in GitHub Desktop.
POC of a Pod which installs a run script into Xcode (CocoaPods 0.33 or less)
#!/usr/bin/env ruby
require 'pathname'
require 'xcodeproj'
path_to_xcode_build_script = '"${SRCROOT}/Pods/POD_NAME/run_script.sh"'
xcode_build_script_name = 'Unique Run Script Name'
puts Dir.pwd
puts ARGV[0]
path_to_spec = ARGV[0] # Passed from podspec using path variable
if path_to_spec.start_with?('/private/tmp/CocoaPods/Lint')
# CocoaPods Lint
# e.g. /private/tmp/CocoaPods/Lint/Pods/Local Podspecs/POD_NAME.podspec
puts 'CocoaPods linting, bail now before fail'
exit 0
elsif path_to_spec.include?('.cocoapods')
# Pod installed from spec repo
# podspec: /Users/ben/.cocoapods/repos/REPO_NAME/POD_NAME/1.0.0/POD_NAME.podspec
# Dir.pwd: /Users/ben/APP_PATH/Pods/POD_NAME
path_to_project = Dir.glob(Pathname.new(Dir.pwd) + '../../*.xcodeproj')[0]
else
# Pod installed via :path in Podfile
# podspec: /Users/ben/LOCAL_POD_PATH/POD_NAME/POD_NAME.podspec
# Dir.pwd: /Users/ben/LOCAL_POD_PATH/POD_NAME
path_to_project = Dir.glob(Pathname.new(path_to_spec) + '../../../*.xcodeproj')[0]
end
puts path_to_project
project = Xcodeproj::Project.open(path_to_project)
main_target = project.targets.first
script_installed = false
main_target.shell_script_build_phases.each { |run_script|
script_installed = true if run_script.name == xcode_build_script_name
}
if (!script_installed)
puts "Installing run script in Xcode project #{path_to_project}"
phase = main_target.new_shell_script_build_phase(xcode_build_script_name)
phase.shell_script = path_to_xcode_build_script
project.save()
else
puts "Run script already installed"
end
Pod::Spec.new do |s|
s.name = 'POD_NAME'
s.version = '1.0.0'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.summary = 'Example of pod which installs a run script into the Xcode project (first target)'
s.homepage = 'https://github.com/phatblat/POD_NAME'
s.authors = { 'Ben Chatelain' => 'benchatelain@gmail.com' }
s.source = { :git => 'https://github.com/phatblat/POD_NAME.git', :tag => s.version.to_s }
s.ios.deployment_target = '6.0'
s.requires_arc = true
s.source_files = "Classes/**/*.{h,m}"
s.preserve_paths = 'run_script.sh', 'install_run_script.rb'
s.prepare_command = "ruby install_run_script.rb '#{path}'"
end
#!/bin/sh
echo "Every build is awesome!"
@akashhansel
Copy link
Copy Markdown

akashhansel commented Feb 28, 2018

@phatblat can you tell me how to specify the execution position of this "run_script.sh" in xcode build phases. For e.g. something like:
phase = main_target.new_shell_script_build_phase(xcode_build_script_name)
phase.shell_script = path_to_xcode_build_script
phase.position = before_compile ??
or,
phase.position = after_embed_pods_framework ??

@akashhansel
Copy link
Copy Markdown

akashhansel commented Mar 1, 2018

never mind....solved it.
In case someone else comes here looking to play around with build phases using podspecs, you'll find everything here:
(http://www.rubydoc.info/gems/xcodeproj/Xcodeproj)

@aliabbas90
Copy link
Copy Markdown

never mind....solved it.
In case someone else comes here looking to play around with build phases using podspecs, you'll find everything here:
(http://www.rubydoc.info/gems/xcodeproj/Xcodeproj)

Hi, can you share with me the solution to reorder build phases please ?

@castellet88
Copy link
Copy Markdown

From https://blog.cocoapods.org/CocoaPods-1.4.0/

s.script_phase = {
  :name => 'Hello World',
  :script => 'echo "Hello World"',
  :execution_position => :before_compile
}

Hope it helps!

@umangbista2
Copy link
Copy Markdown

Can we define run script position here: s.preserve_paths = 'run_script.sh', 'install_run_script.rb' ?

@phatblat
Copy link
Copy Markdown
Author

This gist is obsolete now.

@umangbistra2, as @castellet88 noted the script_phase attribute is the right way to create these run scripts today. You can even have CocoaPods install a ruby script using :shell_path => '/usr/bin/ruby'.

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