Skip to content

Instantly share code, notes, and snippets.

@gotev
Created July 10, 2017 08:13
Show Gist options
  • Save gotev/b4413b64034fac553366ff41b74134c6 to your computer and use it in GitHub Desktop.
Save gotev/b4413b64034fac553366ff41b74134c6 to your computer and use it in GitHub Desktop.
Auto increment podspec build

The problem

Auto increment podspec version. I have a MyPodName.podspec file like this:

Pod::Spec.new do |s|
  s.name         = "MyPodName"
  s.version      = "1.0.32"
  s.summary       = "My pod summary"
  s.description   = <<-DESC
My Pod complete description
                    DESC
  s.homepage      = "https://my.homepage.com"
  s.license       = { :type => 'Apache License, Version 2.0' }
  s.author        = { "Alex Gotev" => "[email protected]" }
  s.ios.deployment_target = '9.0'
  s.requires_arc  = true
  s.source        = { :git => "[email protected]:project/group/mypodrepo.git" }
  s.source_files  = "generated/*.swift", "generated/runtime/*.swift", "OtherFile.swift"
  s.dependency 'CocoaMQTT'
end

and I'm tired of editing it manually each time to publish a new pod version.

The solution

Create a new file called MyPodName.podtemplate and copy all the contents of MyPodName.podspec in it. Then replace 1.0.32 which is the version, with [[VERSION]]. You will have a file like this:

Pod::Spec.new do |s|
  s.name         = "MyPodName"
  s.version      = "[[VERSION]]"
  s.summary       = "My pod summary"
  s.description   = <<-DESC
My Pod complete description
                    DESC
  s.homepage      = "https://my.homepage.com"
  s.license       = { :type => 'Apache License, Version 2.0' }
  s.author        = { "Alex Gotev" => "[email protected]" }
  s.ios.deployment_target = '9.0'
  s.requires_arc  = true
  s.source        = { :git => "[email protected]:project/group/mypodrepo.git" }
  s.source_files  = "generated/*.swift", "generated/runtime/*.swift", "OtherFile.swift"
  s.dependency 'CocoaMQTT'
end

From now on, you will edit only the MyPodName.podtemplate file!

Then create a file called build.properties and add the following:

BASE_VERSION=1.0
BUILD_VERSION=32

Create a file called increment_build and add the following in it:

#!/bin/bash

TEMPLATE_FILE_NAME="MyPodName.podtemplate"
OUT_FILE_NAME="MyPodName.podspec"

# Increment the build version and save it into build.properties
. build.properties

BUILD_VERSION=$((BUILD_VERSION+1))

echo "BASE_VERSION=${BASE_VERSION}" > build.properties
echo "BUILD_VERSION=${BUILD_VERSION}" >> build.properties

# Load the template podspec and replace version
TEMPLATE=$(cat "$TEMPLATE_FILE_NAME" | sed "s/\[\[VERSION\]\]/${BASE_VERSION}\.${BUILD_VERSION}/g")

echo "$TEMPLATE" > "$OUT_FILE_NAME"

replace TEMPLATE_FILE_NAME and OUT_FILE_NAME accordingly to your pod and then make increment_build executable with sudo chmod +x increment_build.

Now, to increase the build of your podspec, you can simply execute:

./increment_build

Et voilà!

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