Skip to content

Commit

Permalink
Install and cache Pods
Browse files Browse the repository at this point in the history
  • Loading branch information
jdee committed Jun 11, 2021
1 parent 6116e74 commit f6b2e9c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 6 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ jobs:
with:
path: |
vendor
key: ${{ runner.os }}-ruby2.7-${{ hashFiles('Gemfile.lock') }}
Branch-TestBed/Pods
key: ${{ runner.os }}-${{ hashFiles('Gemfile.lock','Branch-TestBed/Podfile.lock') }}
restore-keys: |
${{ runner.os }}-ruby2.7-
${{ runner.os }}-
- name: Install Ruby dependencies
run: |
bundle config set --local path vendor
bundle check || bundle install
- name: Test Fastlane
run: bundle exec fastlane hello
- name: Install CocoaPods dependencies
run: bundle exec fastlane prepare_pods
7 changes: 5 additions & 2 deletions fastlane/Fastfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
require_relative 'lib/helper/update_helper'

fastlane_version "2.69.0"

lane :hello do
UI.message "Hello"
lane :prepare_pods do
# This helps optimize CI with caching by testing whether a pod install is necessary
pod_install_if_required '../Branch-TestBed', verbose: true
end
62 changes: 62 additions & 0 deletions fastlane/lib/helper/update_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require 'cocoapods'
require 'pathname'

module UpdateHelper
UI = FastlaneCore::UI

def pod_install_required?(podfile_folder)
podfile_folder = File.expand_path podfile_folder
podfile_path = File.join podfile_folder, 'Podfile'
raise ArgumentError, "No Podfile at #{podfile_folder}" unless File.readable?(podfile_path)

# Podfile must be evalled in its current directory in order to resolve
# the require_relative at the top.
podfile = Dir.chdir(podfile_folder) { Pod::Podfile.from_file podfile_path }

# From here on we expect pod install to succeed. We just check whether it's
# necessary. The Podfile.from_file call above can raise if the Podfile
# contains errors. In that case, pod install will also fail, so we allow
# the exception to be raised instead of returning true.

lockfile_path = File.join podfile_folder, 'Podfile.lock'
manifest_path = File.join podfile_folder, 'Pods', 'Manifest.lock'

return true unless File.readable?(lockfile_path) && File.readable?(manifest_path)

begin
# This validates the Podfile.lock for yaml formatting at least and makes
# the lockfile hash available to check the Podfile checksum later.
lockfile = Pod::Lockfile.from_file Pathname.new lockfile_path

# diff the contents of Podfile.lock and Pods/Manifest.lock
# This is just what is done in the "[CP] Check Pods Manifest.lock" script
# build phase in a project using CocoaPods. This is a stricter requirement
# than semantic comparison of the two lockfile hashes.
return true unless File.read(lockfile_path) == File.read(manifest_path)

# compare checksum of Podfile with checksum in Podfile.lock in case Podfile
# updated since last pod install/update.
lockfile.to_hash["PODFILE CHECKSUM"] != podfile.checksum
rescue StandardError, Pod::PlainInformative => e
# Any error from Pod::Lockfile.from_file or File.read after verifying a
# file exists and is readable. pod install will regenerate these files.
UI.error e.message
true
end
end

def pod_install_if_required(podfile_folder, verbose: false, repo_update: true)
podfile_folder = File.expand_path podfile_folder
install_required = pod_install_required? podfile_folder
UI.message "pod install #{install_required ? '' : 'not '}required in #{podfile_folder}"
return unless install_required

command = %w[pod install]
command << '--silent' unless verbose
command << '--repo-update' if repo_update

Dir.chdir(podfile_folder) { Fastlane::Action.sh(*command) }
end
end

include UpdateHelper

0 comments on commit f6b2e9c

Please sign in to comment.