Skip to content

Estremadoyro/ViperBoiler

Repository files navigation

VIPER Boiler

Python script designed to create/customize boilerplate code used in the VIPER architecture.

Requirements

Python 3.8+

VIPER-M

Architecture variation that will be generated by the project.

Architecture

  • This variation includes Module class in charge of assembling the module.
  • There is no Entity (Model) addressed. As this approach considers it in a different level beyond VIPER.
  • The Screen is an abstraction of a UIViewController.
  • The Module object assembles the module by executing the start() method creating the Screen. It must only exist during creation and be disposed afterwards. Otherwise it may casuse a leak in memory (2 strong references to presenter alive).
  • The Screen will hold the only strong reference to the Presenter. If it's disposed so will the rest of the objects (Presenter, Interactor, Router).

Files organization

Folders

  • default_files .*
    • Used to create stock files, of the same kind, when needed.
  • template_files .txt
    • Templates, with placeholders, that define the structure of the output files.
  • configuration_files .py
    • Python files used to configure the templates (Inject variables).
  • output_processed_files .swift
    • Contains the output final files after being configured. These are ment the copy/pasted in the target Xcode project.

Files

  • main.py
    • Main script which holds most of the logic.
  • content_factory.py
    • Factory in charge of assigning a template_file to a configuration_file

Usage

The script requires only 2 user-input-variables

  • PROJECT_NAME
    • Name of the project the files will go to. Used for the files' headers.
    • i.e. NotesApp
  • MODULE_NAME
    • Name of the VIPER module. Used for naming the other modules and their protocols.
    • i.e NewNote

Example:

python3 main.py PROJECT_NAME MODULE_NAME

Run:

python3 main.py NotesApp NewNote

If there are no errors, then the terminal-logs should look as the following.

terminal_output

Finally, your files will be located @ output_processed_files

output_files

🎉🎉🎉

Customization

Editing templates

Editing the templates with your own approach can be done as following:

1. Open the file (/template_files) you want to customize.

Edit the file, add new variables/functions/protocols as you need.

2. Adding placeholders

In case you need to inject the name of the module/project/etc into the file or other new external variable, simply add the %s identifier where needed.

Don't forget to save (write) the changes.

template_interactor.txt configuration_interactor.py
template_edit_example configuration_edit_example

The newly generated CastInteractor.swift should look something like this. swift_output_example

🎉🎉🎉

Adding a new template (New File)

The script is not limited to VIPER files, as you could have already guessed, so creating different .swift files for different purposes such as factories, protocols, enums, extensions, etc can be achieved with relative ease.

This example will cover the addition of a CastNavigationBar file.

1. Create a new template_*.txt file

First, a new template file will be created named template_CastNavigationBar.txt. Then you can edit the file following the steps in the Editing templates section. The case of the letters will determine the output file names.

navbar_new_file_example

navbar_new_file_edited_example

2. Execute the script main.py

Re-executing the script will generate /configuration_files/configuration_navigationbar.py & output_processed_files/CastNavigationBar.swift files. The former, will be used for injecting outside variables into the template.

python3 main.py AnimeCalendar Cast

If you look closely you will notice NavigationBar has been added a file identifier.

navbar_new_log

3. Editing the configuration_*.py file

This file contains a default implementation that will need to be edited. navbar_new_configuration_file

Since we have added 2 external placeholders into the template_navigationbar.txt we now need to inject them. Just as done @ Editing templates navbar_new_configuration_file_injected

4. Making main.py aware of the new file

Finally, in order to apply the configuration from configuration_navigationbar.py we need to make small changes in content_factory.py.

content_factory

  • Import the just edited configuration_navigationbar.py file so it can be accessed.
    • import configuration_navigationbar
  • Add the condition for it IN LOWERCASE, as string comparison is case sensitive
  • Then aggregate the content += (Just as shown for the other files) with the configuration_navigationbar.configure() method.
  • Save (write) the file.

5. Re-run the script for one last time

python3 main.py AnimeCalendar Cast

If everything went as planned CastNavigationBar.swift should've been updated with the injected variables.

navbar_new_swift_file

🎉🎉🎉

License

MIT

If it was useful, a 🌟 would be highly appreciated!

Releases

No releases published

Packages

No packages published