Python script designed to create/customize boilerplate code used in the VIPER architecture.
Python 3.8+
Architecture variation that will be generated by the project.
- 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).
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
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.
Finally, your files will be located @ output_processed_files
🎉🎉🎉
Editing the templates with your own approach can be done as following:
Edit the file, add new variables/functions/protocols as you need.
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 |
---|---|
The newly generated CastInteractor.swift should look something like this.
🎉🎉🎉
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.
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.
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.
This file contains a default implementation that will need to be edited.
Since we have added 2 external placeholders into the template_navigationbar.txt
we now need to inject them. Just as done @ Editing templates
Finally, in order to apply the configuration from configuration_navigationbar.py
we need to make small changes in content_factory.py
.
- 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.
python3 main.py AnimeCalendar Cast
If everything went as planned CastNavigationBar.swift
should've been updated with the injected variables.
🎉🎉🎉