LoginSignup
15
8

More than 5 years have passed since last update.

【iOS】GenerambaのテンプレートエンジンLiquidの書き方

Posted at

GenerambaとLiquid

GenerambaはiOSアプリでVIPERアーキテクチャの実装を行うためのコードジェネレータです。Generamba内ではコードを生成するためのテンプレートエンジンとしてLiquidが採用されています。

QiitaでもGenerambaについてはいくつか記事が上がっていますが、デフォルトで内蔵されているswifty_viperというテンプレートが自分好みでなく、テンプレートを自作する必要がありました。テンプレートを作る際にLiquidを触る必要がありますが、このあたりの情報があまりなかったので本記事を書いてみました。

Generambaの自作テンプレート作成手順

テンプレートの作成は以下の手順で行います。

1 . $ generamba template create [テンプレート名]を実行
2 . ルートディレクトリにテンプレート名のディレクトリとrambaspecファイル、liquidファイルが作成される
3 . 自分が生成したいコードやディレクトリ構成を考えて、rambaspecファイル、liquidファイルを編集する
4 . Rambafile内のtemplatesに自作テンプレートを追記

templates:
- {name: テンプレート名, local: 'テンプレート名'}

5 . $ generamba template installで自作したテンプレートをインストール
6 . $ generamba gen モジュール名 テンプレート名でモジュールのコードを生成

Liquidファイルサンプル

生成するコードのテンプレートとなるLiquidファイルのサンプルを記載します。

sample.swift.liquid

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

import Foundation

protocol {{ module_info.name }}ViewInterface: class {

    var presenter: {{ module_info.name }}PresenterInterface! { get }

    func reload(entity: [{{ module_info.name }}entity])

}

protocol {{ module_info.name }}PresenterInterface: class {

    weak var view: {{ module_info.name }}ViewInterface? { get set }
    var router: {{ module_info.name }}RouterInterface { get }

    init(router: {{ module_info.name }}RouterInterface)

    func viewDidLoad()
    func viewWillAppear()

}

protocol {{ module_info.name }}RouterInterface: class {

    weak var viewController: UIViewController? { get set }

    static func assembleModule() -> UIViewController

    init(viewController: UIViewController)

}

コード生成時に{{ }}で囲った文に変数値が置き換えられます。

module_info.name
 コード生成時に求められるモジュール名が入力されます。
prefix
 generambaのセットアップ時に指定したアプリのprefixが入力されます。
developer.company
 generambaのセットアップ時に指定した会社名が入力されます。

Rambaspecコードサンプル

生成コードのディレクトリ、ファイル名のルールをまとめRambaspecファイルのサンプルを記載します。

sample_viper.rambaspec

# Template information section
name: sample_viper
summary:
author: tmyk110
version: 1.0.0
license: MIT

# The declarations for code files

code_files:
# Contract
- {name: Contract/Contract.swift, path: Code/Contract/contract.swift.liquid}

# View layer
- {name: View/View.swift, path: Code/View/view.swift.liquid}
- {name: View/ViewController.swift, path: Code/View/viewcontroller.swift.liquid}

# Presenter layer
- {name: Presenter/Presenter.swift, path: Code/Presenter/presenter.swift.liquid}

# Router layer
- {name: Router/Router.swift, path: Code/Router/router.swift.liquid}

name:
 コードを出力するディレクトリ、ファイル名を指定します。
path:
 テンプレートとなるliquidファイルのパスを指定します。

おわりに

generambaで自作のテンプレートを作成することにより、様々なアーキテクチャでの開発を効率的に行うことができるようになります。今回のサンプルではMVPの、特にViewとPresenterに着目したテンプレートになりました。これは私が携わっている業務にてこのスタイルが必要だったからです。アーキテクチャ以外でも決まったパターンのコードを大量に作成したい場面はあるかと思いますので、ぜひ一度試してみてください!

参考情報

Generamba - Template Structure
【Swift】VIPERを使ってサンプルを作成する
ぼくのやっているVIPER(のようなもの)
iOS開発で VIPER / Clean Architecture を使うなら、ファイル自動生成の Generamba もどうぞ

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