LoginSignup
15
9

More than 5 years have passed since last update.

R.swiftをどうしてもCarthageで使いたいあなたへ

Posted at

R.swift

https://github.com/mac-cain13/R.swift

R.swift 便利ですよね。

これが

let icon = UIImage(named: "settings-icon")
let font = UIFont(name: "San Francisco", size: 42)
let color = UIColor(named: "indictator highlight")
let viewController = CustomViewController(nibName: "CustomView", bundle: nil)
let string = String(format: NSLocalizedString("welcome.withName", comment: ""), locale: NSLocale.current, "Arthur Dent")

こうなるやつです。

let icon = R.image.settingsIcon()
let font = R.font.sanFrancisco(size: 42)
let color = R.color.indicatorHighlight()
let viewController = CustomViewController(nib: R.nib.customView)
let string = R.string.localizable.welcomeWithName("Arthur Dent")

Carthageだと使えない?

CocoaPods is the recommended way of installation, as this avoids including any binary files into your project.

Note on Carthage: R.swift is a tool used in a build step, it is not a dynamic library. Therefore it is not possible to install it with Carthage.

https://github.com/mac-cain13/R.swift#installation

R.swiftはCocoaPodsでのインストールがオススメされています。
R.swiftはビルド時に実行されるツールなので、Carthageで単純にインストールできるようなものではないからですね。

それでもできればCocoaPodsではなくCarthageを使いたい

そういう時、あるかもしれません。
そんな時はこれを使いましょう。

RswiftDownloader.swift - Download R.swift binary for your Cartfile.resolved · GitHub

使い方はかんたん。

  1. CarthageでR.swift.Libraryをインストール
  2. RswiftDownloaderを実行
  3. 公式インストール手順の2から続ける

もう少し詳しく見ていきます。

R.swift.Libraryをインストール

Cartfileを用意して、

github "mac-cain13/R.swift.Library"

インストールします。

$ carthage update

ビルドされたFrameworkはXcodeプロジェクトに入れておきましょう。

詳しくは公式ドキュメントを読んで下さい。
https://github.com/Carthage/Carthage#if-youre-building-for-ios-tvos-or-watchos

RswiftDownloaderの使い方

RswiftDownloader.swift - Download R.swift binary for your Cartfile.resolved · GitHub
ここからダウンロードして、プロジェクト内に置きましょう。(場所はどこでもOK)

Cartfile.resolvedのパスを指定してRswiftDownloaderを実行します。

$ swift PATH/TO/RswiftDownloader.swift PATH/TO/Cartfile.resolved

Cartfile.resolvedからインストールされているR.swift.Libraryのバージョンを特定できると、
対応するR.swiftのビルド済みバイナリを https://github.com/mac-cain13/R.swift/releases からダウンロードします。
Cartfile.resolvedの置かれているディレクトリにrswiftディレクトリがつくられ、その中にrswiftバイナリが置かれます。

YourApp/
  ├ RswiftDownloader.swift
  ├ Cartfile.resolved
  └ rswift/
    └ rswift

Xcodeプロジェクトの設定をする

ここまでで 公式インストール手順 の1までは終わっています。
以降はCocoaPodsやマニュアルインストールと同じ手順と変わりません。
強いていうと、Run Script で指定するrswiftのパスを間違えないようにして下さい。

こんな感じですね。

"$SRCROOT/rswift/rswift" generate "$SRCROOT/R.generated.swift"

番外編 fastlaneで使う

たとえばこんな感じ。

lane :rswift do
  swift_file = Dir.pwd + "/../RswiftDownloader.swift"
  cartfile_resolved_path = Dir.pwd + "/../Cartfile.resolved"
  command = "/usr/bin/swift #{swift_file} #{cartfile_resolved_path}"
  system(command)
end

まとめ

結局のところ、R.swiftをCocoaPodsでインストールする時にやっているのは、
指定されたバージョンのR.swift.Libraryをインストールし、
該当バージョンのバイナリをダウンロードしてきてプロジェクト内に設置しているだけです。

なので、バイナリダウンロードの部分さえお手軽にできて、
バージョンの整合性が取れればCarthageでも特に困りません。アップデートもお手軽ですね。
コマンドを何度も叩くのが面倒なら、fastlaneでlaneを組み合わせれば解決できますね。

もしどうしてもCocoaPodsを使いたくないとか、使いたくない事情がある方がいたら、ぜひ🙂

参考

15
9
0

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
15
9