如果存在框架的本地副本,如何在Podfile中使用?

11

我有一个框架和一个项目,我正在其中使用我的框架。我尝试在开发过程中使用本地构建的框架。是否有一种方法可以这样做:

如果my-local-library-path存在,则执行此操作:

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

否则,执行此操作:

pod 'MyLibraryName', git: 'https://github.com/mygithuburl', tag: '0.0.1'
2个回答

16

因为 Podfile 实际上是 Ruby 代码,所以让我们检查该文件是否存在:

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

1
我的库文件在根目录中,所以我不得不使用File.exist?(File.expand_path("~/path-to-file")),但它起作用了。谢谢! - iamkaan

1
我可以提供我长期使用的解决方案。在cocoapods命令周围有一个脚本包装器,可以简化本地文件夹或远程存储库之间选择安装源的过程。这是 在Github上。这里复制了它,但新版本可能在存储库中可用。
#!/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

使用步骤:

  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.

运行 podrun -h 命令获取帮助。

虽然这个链接可能回答了问题,但最好在这里包含答案的关键部分,并提供链接作为参考。仅有链接的答案如果链接页面发生变化,就会失效。- 来自审查 - LordWilmore
1
@LordWilmore,更新了一个答案。感谢您的评论。 - Accid Bright

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接