LoginSignup
9
10

More than 5 years have passed since last update.

【Xcode】ファイル自動生成ツール「Generamba」の使い方

Posted at

はじめに

Generambaとは、
テンプレートに則ってファイルの生成やXcodeへの参照の追加をしてくれる便利な子です。
主にVIPERやCleanArchitecture等の1モジュールに対するファイル数が多いアーキテクチャで役立ちます。
もちろんテンプレートは自作することも出来ます。

インストール

gem install generambaでインストール出来ます。
ただし、そのままではXcode10環境ではファイル生成時に失敗してしまいます。
developブランチのものであればXcode10対応もしてあるようなので、そちらを利用しましょう。(https://github.com/strongself/Generamba/issues/225)
bundlerを利用してdevelopブランチのものをインストールします。
bundlerの詳細は省きますが、Gemfileにgem 'generamba', github: 'strongself/Generamba', :branch => "develop"と記述して実行してください。
※以下のコマンドの記述では、bundlerに関するものを省きます。bundler経由でインストールした場合はそれぞれのコマンドの頭にbundle execを追加してください。
ちなみにもっと便利にしてバージョン2.0としてリリースするために対応中?のようです。(https://github.com/strongself/Generamba/blob/develop/docs/2.x/roadmap.md)

準備

セットアップ

コマンドラインツールで対象プロジェクトのルートに移動して、generamba setupを実行してください。
すると、プロジェクトの構成等について色々と質問されるので答えていきます。

Console
The company name which will be used in the headers: sample # 企業名を入力してください。
The name of your project is Hoge. Do you want to use it? (yes/no) no # プロジェクト名がHogeで合っているか?です。
The project name: Sample # プロジェクト名を入力します。
The project prefix (if any):  # ある場合は、Prefixを入力します。
The path to a .xcodeproj file of the project is 'Sample.xcodeproj'. Do you want to use it? (yes/no) yes # プロジェクトファイルがSample.xcodeprojで合っているか?です。
Select the appropriate target for adding your MODULES (type the index): # 以下の三択からモジュールを追加するターゲットの番号を入力します。
0. Sample
1. SampleTests
2. SampleUITests
 0
Are you using unit-tests in this project? (yes/no) yes # UnitTestを利用するか?です。
Select the appropriate target for adding your TESTS (type the index): # 以下の三択からテストを追加するターゲットの番号を入力します。
0. Sample
1. SampleTests
2. SampleUITests
 1
Do you want to add all your modules by one path? (yes/no) yes # 全てのモジュールを一つのパスに追加するか?だと思います。yesにしました。
Do you want to use the same paths for your files both in Xcode and the filesystem? (yes/no) yes # Xcode上の参照とファイルシステム両方にファイルを追加するか?だと思います。yesにしました。
The default path for creating new modules: Sample/Class/Module # モジュールまでのパスを入力します。
The default path for creating tests: SampleTests/Module # テストまでのパスを入力します。
Are you using Cocoapods? (yes/no) yes # Cocoapodsを使うか?です。
The path to a Podfile is 'Podfile'. Do you want to use it? (yes/no) yes # 利用するPodfileはPodfileで合っているか?です。
Are you using Carthage? (yes/no) yes # Carthageを使うか?です。
The path to a Cartfile is 'Cartfile'. Do you want to use it? (yes/no) yes # 利用するCartfileはCartfileで合っているか?です。
Do you want to add some well known templates to the Rambafile? (yes/no) yes # Rambafileにテンプレートを追加しますか?です。

+--------------------+----------------------------------------------------------------------------------+
|                                      Summary for generamba setup                                      |
+--------------------+----------------------------------------------------------------------------------+
| company            | sample                                                                           |
| project_name       | Sample                                                                           |
| prefix             |                                                                                  |
| xcodeproj_path     | Sample.xcodeproj                                                                 |
| podfile_path       | Podfile                                                                          |
| cartfile_path      | Cartfile                                                                         |
| templates          | ["{name: rviper_controller}", "{name: mvvm_controller}", "{name: swifty_viper}"] |
| project_target     | Sample                                                                           |
| project_file_path  | Sample/Class/Module                                                              |
| project_group_path | Sample/Class/Module                                                              |
| test_target        | SampleTests                                                                      |
| test_file_path     | SampleTests/Module                                                               |
| test_group_path    | SampleTests/Module                                                               |
+--------------------+----------------------------------------------------------------------------------+

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

すると、Rambafileが生成されるかと思います。
(https://github.com/strongself/Generamba/wiki/Rambafile-Structure)

テンプレート作成

おそらくGenerambaを利用する人は大体テンプレートを自作したいと思います。
generamba template create [テンプレート名]を実行すると、質問されるので答えていきます。

Console
The brief description of your new template: This template is original VIPER template. # このテンプレートの簡単な説明を入力します。
Who is the author of this template: Auther # Autherを入力します。
What license will be used (e.g. MIT): MIT # ライセンスを入力します。
Will your template contain any third-party dependencies (available via Cocoapods or Carthage)? (yes/no) no # このテンプレートはサードパーティ製のものに依存するか?です。

+--------------+-------------------------------------------+
|               Summary for template create                |
+--------------+-------------------------------------------+
| name         | [テンプレート名]                            |
| summary      | This template is original VIPER template. |
| author       | Author                                    |
| license      | MIT                                       |
+--------------+-------------------------------------------+

The template [テンプレート名] is successfully generated! Now add some file templates into it.

すると、入力したテンプレート名のフォルダが出来ているかと思います。

[テンプレート名].rambaspec
# Template information section
name: [テンプレート名]
summary: This template is original VIPER template.
author: Author
version: 1.0.0
license: MIT

# The declarations for code files
code_files:
- {name: Service/Service.h, path: Code/Service/service.h.liquid}
- {name: Service/Service.m, path: Code/Service/service.m.liquid}

# The declarations for test files
test_files:
- {name: Service/ServiceTests.m, path: Tests/Service/service_tests.m.liquid}

# Template dependencies
dependencies:

デフォルトのコメントにある通り、code_filesやtest_filesの箇所に生成したいファイルを追加します。

実際に追加されるファイルが上記のCode/Service/service.h.liquidにあたります。
liquidファイルの中身はほぼそのまま実装されるファイルです。
変数等は公式のWikiを参照ください。

service.h.liquid
//
//  {{ prefix }}{{ module_info.name }}{{ module_info.file_name }}
//  {{ module_info.project_name }}
//
//  Created by {{ developer.name }} on {{ date }}.
//  Copyright {{ year }} {{ developer.company }}. All rights reserved.
//

@interface {{ prefix }}{{ module_info.name }}Service : NSObject

@end

テンプレート導入

テンプレートが出来たら、Generambaと紐付けます。
まずはRambafileに作成したテンプレートへの参照を追記します。
ファイル内の一番下のTenplateの箇所です。

Rambafile
### Templates
templates:
- {name: rviper_controller}
- {name: mvvm_controller}
- {name: swifty_viper}
- {name: [テンプレート名], local:'[テンプレートまでのパス]'}

テンプレート名とパスを追記します。
ここまでプロジェクトのルートで作業している前提ですので、そのままであればlocalの中身はテンプレート名(フォルダ名)のみになるかなと思います。

追記して保存したら、generamba template installを実行します。
すると、Templatesというフォルダに自作のテンプレートが追加されているかと思います。

ファイル追加

ファイル追加時は、generamba gen [モジュール名] [テンプレート名]を実行します。

Console
+----------------+----------------------------+
|           Summary for gen Hoge              |
+----------------+----------------------------+
| Targets        | Sample                     |
| Module path    | Sample/Class/Module/Hoge   |
| Test targets   | SampleTests                |
| Test file path | SampleTests/Module/Hoge    |
| Template       | swifty_viper_hoge          |
+----------------+----------------------------+

Creating code files...
Creating test files...
Module successfully created!
Name: Hoge
Project file path: Sample/Class/Module/Hoge
Project group path: Sample/Class/Module/Hoge
Test file path: SampleTests/Module/Hoge
Test group path: SampleTests/Module/Hoge

ファイルが追加されました!
Xcodeを開いていただくとXcode上のファイル参照もされているかと思います。

おわりに

導入自体もそこまで難しくなく、作業短縮には大いに貢献してくれるためおすすめです!
チーム内での認識の共有にもなるかと思います。
コマンドリストは以下になります。
https://github.com/strongself/Generamba/wiki/Available-Commands

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