Skip to content

Instantly share code, notes, and snippets.

@naithar
Last active July 23, 2017 12:57
Show Gist options
  • Save naithar/75a7dc70be0256aead0561f5d4cabe5e to your computer and use it in GitHub Desktop.
Save naithar/75a7dc70be0256aead0561f5d4cabe5e to your computer and use it in GitHub Desktop.
# coding=utf-8
import sys
import os
from datetime import datetime, date, time
def main(arguments):
if len(arguments) == 0: return
name = arguments[0][:1].upper() + arguments[0][1:]
createDirectory = False
if len(arguments) > 1 and arguments[1] == '--create_directory': createDirectory = True
print 'Creating VIPER modules for name `{}`'.format(name)
additionalPath = ''
if createDirectory:
additionalPath = name + '/'
if not os.path.exists(additionalPath):
os.makedirs(additionalPath)
print 'Creating and using `{}` directory'.format(additionalPath)
date = datetime.now()
terms = ('//' +
'\n// {}' +
'\n//' +
'\n//' +
'\n// Created on {}.'.format(date.strftime("%d.%m.%y")) +
'\n// Copyright © 2016. All rights reserved.' +
'\n//\n')
fileCreation = ('Creating `{}` file')
routerName = '{}Router.swift'.format(name)
print fileCreation.format(routerName)
with open((additionalPath + routerName), 'w') as router:
router.write(terms.format(routerName) +
('\n\n' +
'import UIKit\n\n' +
'protocol {0}RouterProtocol {{\n\n' +
'\t\n' +
'\n}}\n\n' +
'struct {0}Router: {0}RouterProtocol {{\n\n' +
'\tprivate weak var view: {0}View?\n' +
'\n\n' +
'\tinit(view: {0}View) {{\n' +
'\t\tself.view = view' +
'\t}}' +
'\n}}').format(name))
wireframeName = '{}Wireframe.swift'.format(name)
print fileCreation.format(wireframeName)
with open((additionalPath + wireframeName), 'w') as wireframe:
wireframe.write(terms.format(wireframeName) +
('\n\n' +
'import UIKit\n\n' +
'enum {0}Wireframe {{\n\n' +
'\tstatic func module() -> {0}View {{\n' +
'\t\tlet view = {0}View()\n' +
'\t\tlet interactor = {0}Interactor()\n' +
'\t\tlet router = {0}Router(view: view)\n' +
'\t\t_ = {0}Presenter(view: view, interactor: interactor, router: router)\n\n' +
'\t\treturn view\n' +
'\t}}' +
'\n}}\n').format(name))
viewName = '{}View.swift'.format(name)
print fileCreation.format(viewName)
with open((additionalPath + viewName), 'w') as view:
view.write(terms.format(viewName) +
('\n\n' +
'import UIKit\n\n' +
'protocol {0}ViewPresenter {{\n\n' +
'}}\n\n'+
'class {0}View: UIViewController {{\n\n' +
'\tvar presenter: {0}ViewPresenter?\n' +
'\n' +
'\tvar layout: {0}Layout {{\n' +
'\t\treturn self.view as! {0}Layout\n' +
'\t}}\n\n' +
'\toverride func loadView() {{\n' +
'\t\tself.view = {0}Layout()\n' +
'\t}}\n' +
'\n' +
'\toverride func viewDidLoad() {{\n' +
'\t\tsuper.viewDidLoad()\n' +
'\t\tself.layout.setup()\n' +
'\t}}' +
'\n\n}}\n\n' +
'extension {0}View: {0}PresenterView {{\n' +
'}}\n').format(name))
viewName = '{}Layout.swift'.format(name)
print fileCreation.format(viewName)
with open((additionalPath + viewName), 'w') as view:
view.write(terms.format(viewName) +
('\n\n' +
'import UIKit\n\n' +
'class {0}Layout: UIView {{\n\n' +
'\tfunc setup() {{\n' +
'\t}}' +
'}}\n').format(name))
presenterName = '{}Presenter.swift'.format(name)
print fileCreation.format(presenterName)
with open((additionalPath + presenterName), 'w') as presenter:
presenter.write(terms.format(presenterName) +
('\n\n' +
'import UIKit\n\n' +
'protocol {0}PresenterView: class {{\n\n' +
'\tvar presenter: {0}ViewPresenter? {{ get set }}\n' +
'}}\n\n'+
'protocol {0}PresenterInteractor {{\n\n' +
'\tweak var presenter: {0}InteractorPresenter? {{ get set }}\n' +
'}}\n\n'+
'class {0}Presenter {{\n\n' +
'\tfileprivate weak var view: {0}PresenterView?\n' +
'\tfileprivate var interactor: {0}PresenterInteractor?\n' +
'\tfileprivate var router: {0}RouterProtocol\n' +
'\n\tinit(view: {0}PresenterView, interactor: {0}PresenterInteractor, router: {0}RouterProtocol) {{\n' +
'\t\tself.view = view\n' +
'\t\tself.interactor = interactor\n' +
'\t\tself.router = router\n\n' +
'\t\tself.view?.presenter = self\n' +
'\t\tself.interactor?.presenter = self\n'
'\t}}' +
'\n}}\n\n' +
'extension {0}Presenter: {0}ViewPresenter {{\n' +
'\n}}\n\n' +
'extension {0}Presenter: {0}InteractorPresenter {{\n' +
'\n}}\n').format(name))
interactorName = '{}Interactor.swift'.format(name)
print fileCreation.format(interactorName)
with open((additionalPath + interactorName), 'w') as interactor:
interactor.write(terms.format(interactorName) +
('\n\n' +
'import Foundation\n\n' +
'protocol {0}InteractorPresenter: class {{\n\n' +
'\n}}\n\n'+
'class {0}Interactor {{\n\n' +
'\tweak var presenter: {0}InteractorPresenter?\n' +
'\n}}\n\n' +
'extension {0}Interactor: {0}PresenterInteractor {{\n' +
'\n}}\n').format(name))
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment