LoginSignup
11
11

More than 5 years have passed since last update.

rbenvを使ってる開発者が新規iOSプロジェクトのためにBundlerとcocoapodsを入れる手順

Last updated at Posted at 2018-05-07

rbenv を普段使ってる開発者がiOSのプロジェクトを新規で始める時の手順を書いておきます。

要点としては

  • 新規でiOSのプロジェクト(.xcodeproj)を作成するところから始める
  • rbenvを使っている
  • bundlerを新規でインストールする
    • システムのgemsを利用したくない(root権限を変えたりしたくない)
  • CocoaPodsをインストールしてGemfile.lockでCocoaPods自体のバージョンを固定したい

具体例の手順

具体例として新規プロジェクトでfooを作り、そのディレクトリで作業していきます

$ cd foo

まずrbenvでruby2.5.0をローカルで利用

$ rbenv local 2.5.0
$ rbenv rehash

補足、ここではruby 2.5.0を利用しましたが、rubyのバージョンが特定のバージョンである必要性が出てくるのはiOSアプリ開発ではレアケースなので何でも良いと思います。ただし、rbenv local system としてしまうと、システムのgemsにbundlerをインストールしようとして権限の問題でエラーになります。

ローカルで利用するバージョンの確認

$ rbenv versions
  system
  2.2.2
  2.2.4
  2.4.1
* 2.5.0 (set by /Users/yimajo/dev/foo/.ruby-version)

bundlerをインストールします

$ gem install bundler

rbenvがインストールされていてパスが設定されていればrbenv execは必要はなく、gem install bundler で充分です1

bundlerがインストールされたかの確認

$ which bundle
/Users/yimajo/.rbenv/shims/bundle

次にbundleを初期化する。これでGemfileの雛形も用意される

$ bundle init

rbenv exec bundle init としない理由は後述)

Gemfileの雛形ができてるので確認

$ vim Gemfile
# frozen_string_literal: true

source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

# gem "rails"

cocoapodsを入れるためgem "cocoapods"を追記

# frozen_string_literal: true

source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

# gem "rails"
gem "cocoapods"

次にcocoapodsをインストール

$ bundle install --path vendor/bundle

(もしくは次のようにconfigすると、configファイルをカレントディレクトリに作りインストールの設定がそこを参照できるようになります)
$ bundle config --local path vendor/bundle
$ bundle install

cocoapodsを初期化してPodfileが欲しい

$ bundle exec pod init

Podfileの雛形がカレントディレクトリにできるので確認

# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

target 'foo' do
  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!

  # Pods for foo

  target "fooTests' do
    inherit! :search_paths
    # Pods for testing
  end

end

あとは自分の使いたいpodを記載するだけ。例えば pod 'APIKit'pod 'HydraAsync'なら下記のようにして

# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

target 'foo' do
  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!

  # Pods for foo
  pod 'APIKit'
  pod 'HydraAsync'

  target "foo' do
    inherit! :search_paths
    # Pods for testing
  end

end

podインストールする

$ bundle exec pod install

補足

rbenv exec bundle initのように rbenv exec付けないのはなぜか

理由は2つ。

1つはそもそもrbenvを使っている際に使っているシェルにパスを設定していれば必要ないから。

もう1つは、bundle のsh自体で rbenv execされているためです。

#!/usr/bin/env bash
set -e
[ -n "$RBENV_DEBUG" ] && set -x

program="${0##*/}"
if [ "$program" = "ruby" ]; then
  for arg; do
    case "$arg" in
    -e* | -- ) break ;;
    */* )
      if [ -f "$arg" ]; then
        export RBENV_DIR="${arg%/*}"
        break
      fi
      ;;
    esac
  done
fi

export RBENV_ROOT="/Users/yimajo/.rbenv"
exec "/usr/local/Cellar/rbenv/1.1.1/libexec/rbenv" exec "$program" "$@"

必要のないファイルのgit管理について

この手順を行うと下記のファイル/ディレクトリが作成されますが、基本的に他の開発者が必要としないものなので、私は .gitignoreに追加してgit管理しないようにしています。

  • .ruby-version
    • rbenv local バージョン で作成されたファイル
    • 他の開発者のrubyバージョンを固定する必要性がなければgit管理しない
  • vendor/bundle
    • bundle install --path vendor/bundle で作成されたディレクトリ
    • 他の開発者のgemのファイルパスを固定する必要性がなければgit管理しない

たいてい必要なのはCocoaPods自体のバージョンであって、それはGemfile.lockをgitで管理するようにしています。


  1. パスは例えばfishなら ~/.config/fish/config.fishrbenv init - | sourceを追記します 

11
11
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
11