Using local copy of a framework in podfile if exists
Asked Answered
D

2

11

I have a framework and a project where I'm using my framework in. I'm trying to use the framework that is locally built during development. Is there a way to do something like:

if my-local-library-path exists, do this:

pod 'MyLibraryName', :path => "my-local-library-path"

else, do this:

pod 'MyLibraryName', git: 'https://github.com/mygithuburl', tag: '0.0.1'
Divertimento answered 17/10, 2016 at 9:55 Comment(0)
K
16

Since a Podfile is actually Ruby code, let's check if the file exists:

if File.exist?("complete-path-to-a-library-file")
    pod 'MyLibraryName', :path => "my-local-library-path"
else
    pod 'MyLibraryName', git: 'https://github.com/mygithuburl', tag: '0.0.1'
end
Kenay answered 17/10, 2016 at 12:58 Comment(1)
my library file was in my root directory, so I had to use File.exist?(File.expand_path("~/path-to-file")) but it worked. thanks!Divertimento
L
1

I can offer the solution I use for rather long time. There is script wrapper around cocoapods command that simplify choosing installation source between local folder or remote repo. Here it is at Github. Copied here, but newer version may be available in repo

#!/usr/bin/env bash

# Assume default values
# By default install pods from remote
LOCAL=0

# By default don't delete Pods folder and Podfile.lock
DELETE=0 

# By default do pod install with verbose flag and tell it to grab latest specs from the podspec repo (otherwise we can end up with different states on 
# different people's machines)
COMMAND="pod install --verbose"

# By default do not tell cocoapods to grab latest specs from the podspec repo (otherwise we can end up with different states on different people's machines)
# Designed as separate operation and disabled by default because it is rather long operation and cocoapods itself remove this command from 'pod install' by default
REPO_UPDATE=0

# By default do not show help message
SHOW_HELP=0

# Parse parameters 
while [[ $# > 0 ]]
do
key="$1"

case $key in
    -r|--remove)
    DELETE=1
    ;;
    -l|--local)
    LOCAL=1
    ;;
    -c|--command)
    COMMAND="$2"
    shift # past argument
    ;;
    -u|--update)
    REPO_UPDATE=1
    ;;
    -h|--help)
    SHOW_HELP=1
    ;;
    *)
    # unknown option
    ;;
esac
shift # past argument or value
done

if [ $SHOW_HELP != 0 ] ; then
    echo "podrun script designed as a solution to implement easy installation of pods choosing source from remote or local path."
    echo
    echo "podrun script can run pre- and post-run scripts. To run pre-run script create 'pre-run.sh' file at the same directory as podrun script. \ 
To run post-run script create 'post-run.sh' file at the same directory as podrun script. No parameters can be passed to them. \
To pass parameters you can run another script from 'pre-run.sh' and 'post-run.sh' with parameters, described in them."
    echo
    echo "Usage:"
    echo "podrun [-c cocoapods_command|-l|-r|-d|-u|-h]"
    echo
    echo "-с (--command) Specifies command to be evaluated by Cocoapods. If omitted, 'pod install --verbose' command runs by the default."
    echo
    echo "-l (--local) Makes Cocoapods to install pods from local paths. This is done by installing DC_INSTALL_LOCALLY environment variable to 'true' value.\
 To achieve using this check in podfile value of its variable. If omitted, DC_INSTALL_LOCALLY will be set to 'false' value. This means, that pods will be installed from the remote."
    echo
    echo "-r (--remove) Deletes Podfile.lock file and Pods folder from the current path. By default these files won't be deleted (if flag is omitted)."
    echo
    echo "-u (--update) Makes cocoapods to update specs repos before installing pods (this operation may be rather long)."
    echo
    echo "-h (--help) Shows help (this message)."
    exit 0
fi

# Print out parameters for debug case
echo DELETE  = "${DELETE}"
echo LOCAL   = "${LOCAL}"
echo COMMAND = "${COMMAND}"
echo REPO_UPDATE = "${REPO_UPDATE}"

# Check if we have Podfile in our working folder 
# (a kind of protection, that we won't remove unexpected Pods folder)
# If we are in the wrong folder, we'll get error later from Cocoapods nevertheless
# this is preventive
PODFILE="Podfile"
if [ -f "$PODFILE" ] ; then
    echo "$PODFILE found."
else
    echo "$PODFILE not found. It seems, that you're in a wrong directory. Aborting..."
    exit 1
fi

# Set temporary environment variable that allows us to choose podfile variant
if [ $LOCAL != 0 ] ; then
    export DC_INSTALL_LOCALLY=true
else
    export DC_INSTALL_LOCALLY=false
fi

# Delete Pods folder and Podfile.lock file, if necessary
if [ $DELETE != 0 ] ; then
    rm -rf Pods
    rm -f Podfile.lock
fi

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# Run pre-run.sh script
echo "Start pre-run script"
pushd $DIR
sh pre-run.sh
popd

# Run update of repos to grab latest specs from the podspec repo (otherwise we can end up with different states on 
# different people's machines)
if [ $REPO_UPDATE != 0 ] ; then
    pod repo update
fi

# Run command
${COMMAND}
if [ $? -ne 0 ]; then
    exit 1
fi

# Run post-run.sh script
echo "Start post-run script"
pushd $DIR
sh post-run.sh
popd

# Unset temporary environment variable that allows us to choose podfile variant
unset DC_INSTALL_LOCALLY

Steps to use:

  1. Download script or clone repository with it from Github. Add script to the folder with your podfile or place somewhere else and add path to it to your PATH variable.
  2. Edit or create your podfile with the next format:

    if "#{ENV['DC_INSTALL_LOCALLY']}" == "true"
        puts "Using local pods"
        pod "SomePod", :path => "<SomePod_local_path>"
    else
        puts "Using remote pods"
        pod 'SomePod'
    end
    
  3. Open terminal with podfile folder and run podrun -l to install pods locally or podrun to install from remote.

Run podrun -h for help.

Lorianne answered 19/2, 2018 at 20:27 Comment(2)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewMacrogamete
@LordWilmore, updated an answer. Thanks for your remarksLorianne

© 2022 - 2024 — McMap. All rights reserved.