Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. //
  2. // PlayAlways
  3. // Generates Playground with options and open it on Xcode
  4. //
  5. // Created by Daniel Bonates on Dez/8/16.
  6. //
  7. // Usage PlayAlways [-mac] [playground_name] [destination]
  8. //
  9. // -mac is used to generate MacOS version (iOS is default)
  10. //
  11.  
  12. import Foundation
  13.  
  14. struct PlayAlways {
  15.  
  16. let forMac: Bool
  17.  
  18. var dateString: String {
  19. let date = Date()
  20. let dateFormat = DateFormatter()
  21. dateFormat.dateFormat = "Ymd_HMS"
  22. return dateFormat.string(from: date)
  23. }
  24.  
  25. var currentDir: String {
  26. return FileManager.default.currentDirectoryPath
  27. }
  28.  
  29. var importHeader: String {
  30. return "//: Playground - noun: a place where people can play\n\nimport \(forMac ? "Cocoa" : "UIKit")\n\nvar str = \"Hello, playground\""
  31. }
  32.  
  33. var contentHeader: String {
  34. return "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<playground version='5.0' target-platform='\(forMac ? "macos" : "ios")'>\n\t<timeline fileName='timeline.xctimeline'/>\n</playground>\n"
  35. }
  36.  
  37. func createPlaygroundFolder(_ path: String) -> Bool {
  38.  
  39. let fileManager = FileManager.default
  40.  
  41. do {
  42. try fileManager.createDirectory(atPath: path, withIntermediateDirectories: true, attributes: nil)
  43.  
  44. return true
  45. }
  46. catch let error as NSError {
  47. print(error)
  48. }
  49.  
  50. return false
  51. }
  52.  
  53. func writeFile(_ filename: String, at: String, content: String) -> Bool {
  54.  
  55. let destinationPath = URL(fileURLWithPath: at).appendingPathComponent(filename)
  56.  
  57. do {
  58. try content.write(to: destinationPath, atomically: true, encoding: String.Encoding.utf8)
  59. return true
  60. }
  61. catch let error as NSError {
  62. print(error)
  63. }
  64.  
  65. return false
  66. }
  67.  
  68. func createPlayground(fileName: String? = nil, atDestination: String? = nil) {
  69.  
  70. // essencial Playground structure:
  71. // |- folder with name.playground
  72. // |-- contents.xcplayground
  73. // |-- Contents.swift
  74.  
  75. let choosedFileName = fileName ?? dateString
  76. let destinationDir = atDestination ?? currentDir
  77.  
  78. let playgroundDir = URL(fileURLWithPath: destinationDir).appendingPathComponent(choosedFileName + ".playground")
  79.  
  80. if createPlaygroundFolder(playgroundDir.path) &&
  81. writeFile("contents.xcplayground", at: playgroundDir.path, content: contentHeader) &&
  82. writeFile("Contents.swift", at: playgroundDir.path, content: importHeader) {
  83. print("\n\t\u{001B}[0;32mplayground criado com sucesso. Abrindo...\n")
  84.  
  85. let task = Process()
  86. task.launchPath = "/bin/sh"
  87. task.arguments = ["-c", "open \(playgroundDir.path)"]
  88. task.launch()
  89. task.waitUntilExit()
  90.  
  91. return
  92. }
  93.  
  94. print("\t\u{001B}[0;31mnão foi possível criar o playground com os parametros passados.")
  95. }
  96. }
  97.  
  98. let forMac = CommandLine.arguments.filter { $0.hasPrefix("-")}.contains("-mac")
  99.  
  100. var fileParameters = forMac ? Array(CommandLine.arguments[2..<CommandLine.arguments.count]) : Array(CommandLine.arguments.dropFirst())
  101.  
  102. let pg = PlayAlways(forMac: forMac)
  103.  
  104. switch fileParameters.count {
  105. case 2:
  106. // create playground com nome e dir
  107. let playgroundName = fileParameters[0]
  108. let playgroundDestination = fileParameters[1]
  109. pg.createPlayground(fileName: playgroundName, atDestination: playgroundDestination)
  110. case 1:
  111. // only name
  112. let playgroundName = fileParameters[0]
  113. pg.createPlayground(fileName: playgroundName)
  114. default:
  115. // default name on current folder
  116. print("\n\tCriando um playground no diretório atual...")
  117. pg.createPlayground()
  118. print("\t\u{001B}[0;33mComo usar:\n\t\u{001B}[0;37PlayAlways [-mac] [nome_do_playground] [destino]")
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement