Last active
August 4, 2018 06:18
-
-
Save anirudhamahale/0797b5e4923433e7aed4a4e28f870b5b to your computer and use it in GitHub Desktop.
Viper Files Creator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // DetailEventsBuilder.swift | |
| // DetailEventsBuilder | |
| // | |
| // Created by Tibor Bödecs | |
| // Copyright © 2018. Tibor Bödecs. All rights reserved. | |
| // | |
| // Modified by Anirudha Mahale | |
| // Copyright © 2018. Anirudha Mahale. All rights reserved. | |
| // | |
| #!/usr/bin/env swift | |
| import Foundation | |
| guard CommandLine.arguments.count > 1 else { | |
| print("You have to to provide a module name as the first argument.") | |
| exit(-1) | |
| } | |
| func getUserName(_ args: String...) -> String { | |
| let task = Process() | |
| let pipe = Pipe() | |
| task.standardOutput = pipe | |
| task.standardError = pipe | |
| task.launchPath = "/usr/bin/env" | |
| task.arguments = ["git", "config", "--global", "user.name"] | |
| task.launch() | |
| let data = pipe.fileHandleForReading.readDataToEndOfFile() | |
| let output = String(data: data, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines) ?? "VIPERA" | |
| task.waitUntilExit() | |
| return output | |
| } | |
| let userName = getUserName() | |
| let module = CommandLine.arguments[1] | |
| let fileManager = FileManager.default | |
| let workUrl = URL(fileURLWithPath: fileManager.currentDirectoryPath, isDirectory: true) | |
| let moduleUrl = workUrl.appendingPathComponent(module) | |
| let interfacesUrl = moduleUrl.appendingPathComponent("Protocols") | |
| let implmentationsUrl = moduleUrl.appendingPathComponent("Classes") | |
| let interfaceRouterUrl = interfacesUrl.appendingPathComponent(module+"RouterProtocol").appendingPathExtension("swift") | |
| let interfacePresenterUrl = interfacesUrl.appendingPathComponent(module+"PresenterProtocol").appendingPathExtension("swift") | |
| let interfaceInteractorUrl = interfacesUrl.appendingPathComponent(module+"InteractorProtocol").appendingPathExtension("swift") | |
| let interfaceViewControllerUrl = interfacesUrl.appendingPathComponent(module+"ViewProtocol").appendingPathExtension("swift") | |
| let builderUrl = implmentationsUrl.appendingPathComponent(module+"Builder").appendingPathExtension("swift") | |
| let routerUrl = implmentationsUrl.appendingPathComponent(module+"Router").appendingPathExtension("swift") | |
| let presenterUrl = implmentationsUrl.appendingPathComponent(module+"Presenter").appendingPathExtension("swift") | |
| let interactorUrl = implmentationsUrl.appendingPathComponent(module+"Interactor").appendingPathExtension("swift") | |
| let viewControllerUrl = implmentationsUrl.appendingPathComponent(module+"ViewController").appendingPathExtension("swift") | |
| func fileComment(for module: String, type: String) -> String { | |
| let today = Date() | |
| let calendar = Calendar(identifier: .gregorian) | |
| let year = String(calendar.component(.year, from: today)) | |
| let month = String(format: "%02d", calendar.component(.month, from: today)) | |
| let day = String(format: "%02d", calendar.component(.day, from: today)) | |
| return """ | |
| // | |
| // \(module)\(type).swift | |
| // \(module)\(type) | |
| // | |
| // Created by \(userName) on \(year). \(month). \(day).. | |
| // Copyright © \(year). \(userName). All rights reserved. | |
| // | |
| """ | |
| } | |
| let interfaceRouter = """ | |
| \(fileComment(for: module, type: "Router")) | |
| import Foundation | |
| protocol \(module)RouterProtocol { | |
| var presenter: \(module)PresenterProtocol? { get set } | |
| } | |
| """ | |
| let interfacePresenter = """ | |
| \(fileComment(for: module, type: "Presenter")) | |
| import Foundation | |
| protocol \(module)PresenterProtocol: class { | |
| var router: \(module)RouterProtocol? { get set } | |
| var interactor: \(module)InteractorProtocol? { get set } | |
| var view: \(module)ViewProtocol? { get set } | |
| } | |
| """ | |
| let interfaceInteractor = """ | |
| \(fileComment(for: module, type: "Interactor")) | |
| import Foundation | |
| protocol \(module)InteractorProtocol { | |
| var presenter: \(module)PresenterProtocol? { get set } | |
| } | |
| """ | |
| let interfaceViewController = """ | |
| \(fileComment(for: module, type: "ViewController")) | |
| import Foundation | |
| protocol \(module)ViewProtocol: class { | |
| var presenter: \(module)PresenterProtocol? { get set } | |
| } | |
| """ | |
| let defaultBuilder = """ | |
| \(fileComment(for: module, type: "Builder")) | |
| import Foundation | |
| import UIKit | |
| class \(module)Builder { | |
| func main() -> UIViewController { | |
| let view = \(module)ViewController() | |
| let interactor = \(module)Interactor() | |
| let presenter = \(module)Presenter() | |
| let router = \(module)Router() | |
| let controller = UINavigationController(rootViewController: view) | |
| view.presenter = presenter | |
| presenter.interactor = interactor | |
| presenter.view = view | |
| presenter.router = router | |
| router.presenter = presenter | |
| router.viewController = view | |
| return controller | |
| } | |
| } | |
| """ | |
| let defaultRouter = """ | |
| \(fileComment(for: module, type: "Router")) | |
| import Foundation | |
| import UIKit | |
| class \(module)Router: \(module)RouterProtocol { | |
| weak var presenter: \(module)PresenterProtocol? | |
| weak var viewController: UIViewController? | |
| } | |
| """ | |
| let defaultPresenter = """ | |
| \(fileComment(for: module, type: "Presenter")) | |
| import Foundation | |
| class \(module)Presenter: \(module)PresenterProtocol { | |
| var router: \(module)RouterProtocol? | |
| var interactor: \(module)InteractorProtocol? | |
| weak var view: \(module)ViewProtocol? | |
| } | |
| """ | |
| let defaultInteractor = """ | |
| \(fileComment(for: module, type: "Interactor")) | |
| import Foundation | |
| import UIKit | |
| class \(module)Interactor: \(module)InteractorProtocol { | |
| weak var presenter: \(module)PresenterProtocol? | |
| } | |
| """ | |
| let defaultViewController = """ | |
| \(fileComment(for: module, type: "ViewController")) | |
| import Foundation | |
| import UIKit | |
| class \(module)ViewController: UIViewController, \(module)ViewProtocol { | |
| var presenter: \(module)PresenterProtocol? | |
| } | |
| """ | |
| do { | |
| try [moduleUrl, interfacesUrl, implmentationsUrl].forEach { | |
| try fileManager.createDirectory(at: $0, withIntermediateDirectories: true, attributes: nil) | |
| } | |
| try interfaceRouter.write(to: interfaceRouterUrl, atomically: true, encoding: .utf8) | |
| try interfacePresenter.write(to: interfacePresenterUrl, atomically: true, encoding: .utf8) | |
| try interfaceInteractor.write(to: interfaceInteractorUrl, atomically: true, encoding: .utf8) | |
| try interfaceViewController.write(to: interfaceViewControllerUrl, atomically: true, encoding: .utf8) | |
| try defaultBuilder.write(to: builderUrl, atomically: true, encoding: .utf8) | |
| try defaultRouter.write(to: routerUrl, atomically: true, encoding: .utf8) | |
| try defaultPresenter.write(to: presenterUrl, atomically: true, encoding: .utf8) | |
| try defaultInteractor.write(to: interactorUrl, atomically: true, encoding: .utf8) | |
| try defaultViewController.write(to: viewControllerUrl, atomically: true, encoding: .utf8) | |
| } | |
| catch { | |
| print(error.localizedDescription) | |
| } | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In terminal hit
./vipera.swift <#ModuleName#>Click here to open original link of this repo & click here to read article about this file.