Last active
December 8, 2016 20:18
-
-
Save dbonates/79d769046041e2fe6c1e3926c74ceb29 to your computer and use it in GitHub Desktop.
Swift code that writes an new playground file. Inspired on PlayNow (https://github.com/marcboquet/PlayNow) but with more options.
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
// | |
// PlayAlways | |
// Generates Playground with options and open it on Xcode | |
// | |
// Created by Daniel Bonates on Dez/8/16. | |
// | |
// Usage PlayAlways [-mac] [playground_name] [destination] | |
// | |
// -mac is used to generate MacOS version (iOS is default) | |
// | |
import Foundation | |
struct PlayAlways { | |
let forMac: Bool | |
var dateString: String { | |
let date = Date() | |
let dateFormat = DateFormatter() | |
dateFormat.dateFormat = "Ymd_HMS" | |
return dateFormat.string(from: date) | |
} | |
var currentDir: String { | |
return FileManager.default.currentDirectoryPath | |
} | |
var importHeader: String { | |
return "//: Playground - noun: a place where people can play\n\nimport \(forMac ? "Cocoa" : "UIKit")\n\nvar str = \"Hello, playground\"" | |
} | |
var contentHeader: String { | |
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" | |
} | |
func createPlaygroundFolder(_ path: String) -> Bool { | |
let fileManager = FileManager.default | |
do { | |
try fileManager.createDirectory(atPath: path, withIntermediateDirectories: true, attributes: nil) | |
return true | |
} | |
catch let error as NSError { | |
print(error) | |
} | |
return false | |
} | |
func writeFile(_ filename: String, at: String, content: String) -> Bool { | |
let destinationPath = URL(fileURLWithPath: at).appendingPathComponent(filename) | |
do { | |
try content.write(to: destinationPath, atomically: true, encoding: String.Encoding.utf8) | |
return true | |
} | |
catch let error as NSError { | |
print(error) | |
} | |
return false | |
} | |
func createPlayground(fileName: String? = nil, atDestination: String? = nil) { | |
// essencial Playground structure: | |
// |- folder with name.playground | |
// |-- contents.xcplayground | |
// |-- Contents.swift | |
let choosedFileName = fileName ?? dateString | |
let destinationDir = atDestination ?? currentDir | |
let playgroundDir = URL(fileURLWithPath: destinationDir).appendingPathComponent(choosedFileName + ".playground") | |
if createPlaygroundFolder(playgroundDir.path) && | |
writeFile("contents.xcplayground", at: playgroundDir.path, content: contentHeader) && | |
writeFile("Contents.swift", at: playgroundDir.path, content: importHeader) { | |
print("\n\t\u{001B}[0;32mplayground criado com sucesso. Abrindo...\n") | |
let task = Process() | |
task.launchPath = "/bin/sh" | |
task.arguments = ["-c", "open \(playgroundDir.path)"] | |
task.launch() | |
task.waitUntilExit() | |
return | |
} | |
print("\t\u{001B}[0;31mnão foi possível criar o playground com os parametros passados.") | |
} | |
} | |
let forMac = CommandLine.arguments.filter { $0.hasPrefix("-")}.contains("-mac") | |
var fileParameters = forMac ? Array(CommandLine.arguments[2..<CommandLine.arguments.count]) : Array(CommandLine.arguments.dropFirst()) | |
let pg = PlayAlways(forMac: forMac) | |
switch fileParameters.count { | |
case 2: | |
// create playground com nome e dir | |
let playgroundName = fileParameters[0] | |
let playgroundDestination = fileParameters[1] | |
pg.createPlayground(fileName: playgroundName, atDestination: playgroundDestination) | |
case 1: | |
// only name | |
let playgroundName = fileParameters[0] | |
pg.createPlayground(fileName: playgroundName) | |
default: | |
// default name on current folder | |
print("\n\tCriando um playground no diretório atual...") | |
pg.createPlayground() | |
print("\t\u{001B}[0;33mComo usar:\n\t\u{001B}[0;37PlayAlways [-mac] [nome_do_playground] [destino]") | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment