Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Swift Scripting

Swift Scripting

Talk given at Swift Summit (London) on 21 March 2015

Usually, when we think of scripting, we think of languages like Ruby, which most of the tools around Cocoa development are written in. But what if we could use Swift for both our apps and the tools that help us with development? We’ll start with the basics of getting started with scripting in Swift, go over some pros and cons, and get into more uncharted territories such as dependency management.

Ayaka Nonaka

March 21, 2015
Tweet

More Decks by Ayaka Nonaka

Other Decks in Programming

Transcript

  1. ! ▸ Designer designs and exports assets ▸ Upload to

    Dropbox or send over Slack ▸ Engineer adds to venmo-ios codebase
  2. !

  3. ! ▸ So many Dropbox links & Slack messages, gets

    confusing ▸ Sometimes assets sizes are wrong
  4. ! ▸ So many Dropbox links & Slack messages, gets

    confusing ▸ Sometimes assets sizes are wrong ▸ No easy way to see all the assets in our app
  5. !

  6. ! ▸ Designer designs and exports assets ▸ Designer makes

    pull-request to venmo-ios-images repo
  7. ! ▸ Designer designs and exports assets ▸ Designer makes

    pull-request to venmo-ios-images repo ▸ Engineer reviews changes and merges
  8. ! ▸ Designer designs and exports assets ▸ Designer makes

    pull-request to venmo-ios-images repo ▸ Engineer reviews changes and merges ▸ Engineer generates UIImage category for the new image(s)
  9. ! ▸ Designer designs and exports assets ▸ Designer makes

    pull-request to venmo-ios-images repo ▸ Engineer reviews changes and merges ▸ Engineer generates UIImage category for the new image(s) ▸ pod update !
  10. JANUARY 22ND, 2015 Ayaka Nonaka [11:07 AM] ok time to

    write..... ruby? or.... swift?! actually not sure if i want to go down that path
  11. JANUARY 22ND, 2015 Ayaka Nonaka [11:07 AM] ok time to

    write..... ruby? or.... swift?! actually not sure if i want to go down that path a 2 day long project is going to turn into like a 7 day long project lol
  12. JANUARY 22ND, 2015 Ayaka Nonaka [11:07 AM] ok time to

    write..... ruby? or.... swift?! actually not sure if i want to go down that path a 2 day long project is going to turn into like a 7 day long project lol i might look into swift though
  13. JANUARY 22ND, 2015 Ayaka Nonaka [11:07 AM] ok time to

    write..... ruby? or.... swift?! actually not sure if i want to go down that path a 2 day long project is going to turn into like a 7 day long project lol i might look into swift though that might be kind of fun
  14. #!/usr/bin/env xcrun swift import Foundation // MARK: - Helpers func

    writeNewline(outputStream: NSOutputStream) { outputStream.write("\n", maxLength: 1) } func writeLine(outputStream: NSOutputStream, string: String) { outputStream.write(string, maxLength: countElements(string)) writeNewline(outputStream) } func methodDeclarationFromFileName(fileName: String) -> String { let noUnderscoresFileName = fileName.stringByReplacingOccurrencesOfString("_", withString: "", options: .LiteralSearch, range: nil) let uncapitalizedFileName = uncapitalized(noUnderscoresFileName) return "+ (UIImage *)ven_\(uncapitalizedFileName)" } func uncapitalized(string: String) -> String { var index = advance(string.startIndex, 1) let first = string.substringToIndex(index) let rest = string.substringFromIndex(index) return "\(first.lowercaseString)\(rest)" } func fileNameWithoutExtension(fileName: String) -> String { let pattern = "(([a-zA-Z]|_)*)(@2x|@3x)?\\.png" let expression = NSRegularExpression(pattern: pattern, options: nil, error: nil) let result = expression!.matchesInString(fileName, options: nil, range: NSRange(location: 0, length: countElements(fileName))) let textCheckingResult = result[0] as NSTextCheckingResult let range = textCheckingResult.rangeAtIndex(1) return (fileName as NSString).substringWithRange(range) } func imageFileNames(path: String) -> [String] { let fileManager = NSFileManager.defaultManager() let enumerator = fileManager.enumeratorAtPath(path) var fileNames = NSMutableOrderedSet() while let fileName = enumerator?.nextObject() as? String { if fileName.hasSuffix("png") { fileNames.addObject(fileNameWithoutExtension(fileName)) } } return fileNames.array as [String] } // MARK: - Script that generates UIImage+VenmoImages category class let fileNames = imageFileNames("generated-assets") let categoryName = "UIImage+VenmoImages" let headerFileName = "\(categoryName).h" // Generate the interface if let outputStream = NSOutputStream(toFileAtPath: headerFileName, append: false) { outputStream.open() writeLine(outputStream, "#import <Foundation/Foundation.h>") writeNewline(outputStream) writeLine(outputStream, "@interface UIImage (VenmoImages)") writeNewline(outputStream) for fileName in fileNames { writeLine(outputStream, "\(methodDeclarationFromFileName(fileName));") } writeNewline(outputStream) writeLine(outputStream, "@end") outputStream.close() } else { println("Unable to open interface file") } // Generate the implementation if let implOutputStream = NSOutputStream(toFileAtPath: "\(categoryName).m", append: false) { implOutputStream.open() writeLine(implOutputStream, "#import \"\(headerFileName)\"") writeNewline(implOutputStream) writeLine(implOutputStream, "@implementation UIImage (VenmoImages)") writeNewline(implOutputStream) for fileName in fileNames { writeLine(implOutputStream, "\(methodDeclarationFromFileName(fileName))") writeLine(implOutputStream, "{") writeLine(implOutputStream, " return [UIImage imageNamed:@\"Venmo.bundle/\(fileName)\"];") writeLine(implOutputStream, "}") writeNewline(implOutputStream) } writeLine(implOutputStream, "@end") implOutputStream.close() } else { println("Unable to open implementation file") }
  15. $ CARTHAGE BOOTSTRAP *** No Cartfile.resolved found, updating dependencies ***

    Fetching PrettyColors *** Fetching Alamofire *** Checking out Alamofire at "1.1.4" *** Checking out PrettyColors at "v1.0.0" *** xcodebuild output can be found in /var/folders/blah *** Building scheme "Alamofire iOS" in Alamofire.xcworkspace *** Building scheme "Alamofire OSX" in Alamofire.xcworkspace *** Building scheme "PrettyColors iOS" in PrettyColors.xcodeproj *** Building scheme "PrettyColors Mac" in PrettyColors.xcodeproj
  16. ➜ swiftsummit git:(master) ✗ swift -help OVERVIEW: Swift compiler USAGE:

    swift [options] <inputs> OPTIONS: ... -F <value> Add directory to framework search path
  17. THE MISSING STUFF ▸ Auto-completion ▸ A better way to

    do dependency management ▸ More tools