LoginSignup
61
39

More than 5 years have passed since last update.

iOS開発で VIPER / Clean Architecture を使うなら、ファイル自動生成の Generamba もどうぞ

Last updated at Posted at 2017-01-17

iOS開発で、architectureはVIPERを採用したいけどファイル追加とか面倒?
なら、Generamba(※)を使お(ง ˙ω˙) というお話です。
※ 僕はGenerambaを「ジェネラムバァ」って呼んでます。同僚のロシア人がそう言ってるように聞こえるからです。ギミランバーかもしれません。

Generambaとは?

GenerambaはXcodeのために作られたコードジェネレーターです。VIPER Moduleのためにデザインされていますが、カスタマイズが容易で色んなクラスがObjective-CやSwiftで作れます。(Generamba Overviewより)

インストール

Ruby2.2以上が動く環境で、gem install generamba を実行するだけ。

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

generambageneramba versionコマンドを実行して以下のように出力されていればOKだと思います。

➜  ~ generamba
Commands:
  generamba gen [MODULE_NAME] [TEMPLATE_NAME]  # Creates a new VIPER module with a given name from a s...
  generamba help [COMMAND]                     # Describe available commands or one specific command
  generamba setup                              # Creates a Rambafile with a config for a given project
  generamba template <command>                 # Provides a set of commands for working with templates
  generamba version                            # Prints out Generamba current version

➜  ~ generamba version
Version: 1.3.0
Release date: 05.10.2016
Change notes: https://github.com/rambler-digital-solutions/Generamba/releases/tag/1.3.0

簡単な How to

準備 / 環境

  • プロジェクト作成 xcode.png
  • (任意) Podfile
platform :ios, :deployment_target => "9.0"
use_frameworks!

def pods
    pod 'Alamofire', '~> 4.0.1'
    pod 'SwiftyJSON'
end

target 'sample-viper3' do
    pods
end

post_install do |installer|
    installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
            config.build_settings['SWIFT_VERSION'] = '3.0'
        end
    end
end

generamba setup / generamba template install / generamba gen

generambaのsetupからModuleの作成までやってしまう。

1. generamba setup

質問に答えていけば良い。

➜  sample-viper4 git:(master) ✗ generamba setup
The company name which will be used in the headers: ykeisuke
The name of your project is sample-viper4. Do you want to use it? (yes/no) yes
The project prefix (if any):
The path to a .xcodeproj file of the project is 'sample-viper4.xcodeproj'. Do you want to use it? (yes/no) yes
Select the appropriate target for adding your MODULES (type the index):
0. sample-viper4
1. sample-viper4Tests
2. sample-viper4UITests
 0 👈僕は0って回答した
Are you using unit-tests in this project? (yes/no) yes
Select the appropriate target for adding your TESTS (type the index):
0. sample-viper4
1. sample-viper4Tests
2. sample-viper4UITests
 1 👈僕は1って回答した
Do you want to add all your modules by one path? (yes/no) yes
Do you want to use the same paths for your files both in Xcode and the filesystem? (yes/no) yes
The default path for creating new modules: /sample-viper4/modules
The default path for creating tests: /sample-viper4/tests
Are you using Cocoapods? (yes/no) yes
The path to a Podfile is 'Podfile'. Do you want to use it? (yes/no) yes
Are you using Carthage? (yes/no) no
Do you want to add some well known templates to the Rambafile? (yes/no) yes 👈yesって答えること。

+--------------------+----------------------------------------------------------------------------------+
|                                      Summary for generamba setup                                      |
+--------------------+----------------------------------------------------------------------------------+
| company            | ykeisuke                                                                         |
| project_name       | sample-viper4                                                                    |
| prefix             |                                                                                  |
| xcodeproj_path     | sample-viper4.xcodeproj                                                          |
| podfile_path       | Podfile                                                                          |
| templates          | ["{name: rviper_controller}", "{name: mvvm_controller}", "{name: swifty_viper}"] |
| project_target     | sample-viper4                                                                    |
| project_file_path  | /sample-viper4/modules                                                           |
| project_group_path | /sample-viper4/modules                                                           |
| test_target        | sample-viper4Tests                                                               |
| test_file_path     | /sample-viper4/tests                                                             |
| test_group_path    | /sample-viper4/tests                                                             |
+--------------------+----------------------------------------------------------------------------------+

Rambafile successfully created! Now run `generamba template install`.

2. Templateをダウンロードする。

☝︎の最後に言われたとおり、generamba template install を実行すれば良い

➜  sample-viper4 git:(master) ✗ generamba template install
Updating shared generamba-catalog specs...
Installing rviper_controller...
Installing mvvm_controller...
Installing swifty_viper...

3. コードのジェネレート

topViewController等を作成すると仮定し、実行した。
swifty_viper2.でダウンロード済み。
ちなみに、Swift2.3のがダウンロードされているのでビルド前に少し治してあげる必要がある。

➜  sample-viper4 git:(master) ✗ generamba gen top swifty_viper

+----------------+---------------------------+
|            Summary for gen top             |
+----------------+---------------------------+
| Targets        | sample-viper4             |
| Module path    | sample-viper4/modules/top |
| Test targets   | sample-viper4Tests        |
| Test file path | sample-viper4/tests/top   |
| Template       | swifty_viper              |
+----------------+---------------------------+

Creating code files...
Creating test files...
Module successfully created!
Name: top
Project file path: sample-viper4/modules/top
Project group path: sample-viper4/modules/top
Test file path: sample-viper4/tests/top
Test group path: sample-viper4/tests/top

generate した後: Main.Storyboradを修正する

  1. StoryBorad追加
    Viewフォルダに自分は配置している。

  2. Cusstom ClasstopViewController を設定

3.png

  1. Top View Controller SceneObject を追加して、Cusstom ClasstopModuleInitializer を設定

4.png

備考

筆者環境

  • Swift3.0
  • Xcode8.1
  • Zsh, ruby2.3, macOS Sierra, iTerm2
  • Generamba Version: 1.3.0 / Release date: 05.10.2016

VIPERに関して参考になる文章

VIPERのサンプルプロジェクト

最後に

二ヶ月くらい前に書いて、「Qiitaに投稿」押したと思ったら下書き保存されてるだけでした。
別の記事をみて公開したようなーあれー?ってなって投稿、そして今に至りました。

Templateを用意すればいいので(☝︎のではswifty_viper)、結構応用が利くきがします。

追記

Generamba以外の選択肢として、これをみつけました。
https://github.com/infinum/iOS-VIPER-Xcode-Templates

アイコンが可愛いので、人によってはこっちつかってもいいかもしれないです。僕はまだ使ってないです。

61
39
1

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
61
39