Gabriel
Gabriel

Reputation: 561

Making a CocoaPod from an existing Xcode project

I've been trying to crack this with no avail so far. I have my Xcode Swift project with its github repository for source control. I would like to create a new CocoaPod with this project.

Ive gone through the CocoaPods documentation but nothing has worked as of yet and Im really confused by now, could someone please write the steps in order I need to do for making that new pod from my existing Xcode Project which already has a Github repository linked to it.

Thanks in advance!

Upvotes: 26

Views: 8645

Answers (3)

apollov
apollov

Reputation: 147

You can use https://github.com/CocoaPods/Xcodeproj (which is shipped with Cocoapods, so if you use the latter, you already have the former) to parse you XCode project/workspace and, for example

  • get needed target from your project
  • get all the files included in that target compilation
  • print it or write it into a file (or even directly into a Podspec)
  • update your Podspec, so spec.source_files would contain all those files

And since Podspec is a Ruby script, you can even integrate this process into the Podspec itself to maintain only single XCode project target source files and have Podspec update itself from it. Something like

require 'xcodeproj'
project = Xcodeproj::Project.open('MySuperProject.xcodeproj')
target = project.targets.select { |target| target.name == 'MyTarget1' }.first

def self.relative_file_paths(build_phase)
  folder_path_length = __dir__.length + 1  # add '/' to the end
  paths = build_phase.files_references.map{ |f| f.real_path.to_s[folder_path_length..-1] }
  puts build_phase, paths
  return paths
end

Pod::Spec.new do |s|
  s.name = 'MySuperProject'
  s.version = '1.0'
  s.authors = 'My team'
  s.license = 'My License'
  s.homepage = 'github.com:me/MySuperProject.git'
  s.source = { :git => 'github.me/MySuperProject.git' }
  s.summary = 'My Super Project'

  headers = (self.relative_file_paths target.headers_build_phase) + ['include/SomeHeader.h']
  s.source_files = (self.relative_file_paths target.source_build_phase) + headers
  s.public_header_files = headers
end

Upvotes: 0

Mohammad Kamran Usmani
Mohammad Kamran Usmani

Reputation: 878

Easy steps to create Cocoapod from existing xcode project

  • Create a repository on your git account (Repo name, check README, choose MIT under license).
  • Copy the url of your repository.Open terminal and run following command.

    git clone copied your repository url

  • Now copy your Xcode project inside the cloned repository folder on your Mac. Now run following commands

    git add -u to add all files (if not added use: git add filepath/folder)

    git commit -m "your custom message"

    git push origin master

  • Create a new release to go to your git repository or run following commands

    git tag 1.0.0

    git push --tags

  • First, we need to make sure that you have CocoaPods installed and ready to use in your Terminal. run the following command:

    sudo gem install cocoapods --pre

Creating a Podspec 

  • All Pods have a podspec file. A podspec, as its name suggests, defines the specifications of the Pod! Now let’s make one, run following command on terminal

    touch PodName.podspec

  • After adding and modifying your .podspec file. Validate your .podspec file by hitting following command on terminal

    pod lib lint

  • Once you validate it successfully without errors run following command to register you and build cocoapod respectively

    pod trunk register  

    pod trunk push PodName.podspec

If all goes well, you will get this on terminal

🚀 PodName (1.0.0) successfully published

📅 February 5th, 02:32

🌎 https://cocoapods.org/pods/PodName

👍 Tell your friends!

Yeah!!!!! congrats you have got your pod link. Use wherever you want to use it.

Upvotes: 7

mipmip
mipmip

Reputation: 1160

Taken from the Cocoapods documentation:

$ pod spec create YourProject
$ edit YourProject.podspec

Then in editor replace s.source_files = "Classes", "Classes/**/*.{h,m}" with

s.source_files = "Classes/**/*.{swift}"

Then run until you pass all tests..

$ pod spec lint YourProject.podspec

Thats basically all.

You can test this Podspec in real life by creating a new demo app and follow the 9 steps from Anbu.Karthik.

In the created podfile add something like:

pod 'YourProject', :path => '/Users/you/work/YourProject'

The path should point to the dir containing the podspec file. Run pod install again.

Upvotes: 18

Related Questions