Running the Swift 3.0 migrator on a standalone Swift file

The Swift 3.0 migrator works great on Xcode projects and Playgrounds. However, when I opened up a Swift script, which is a standalone Swift file, the Convert to Current Swift Syntax was not available in Xcode. 🐼

One workaround was to copy all of the script code into a Playground and do the migration there, but things got really annoying when I tried to import CommonCrypto (long-ish story for another time), and it also felt weird to have a Playground doing file I/O.

After going to the Swift lab at WWDC, some really helpful Swift engineers helped me out with a workaround solution. (Thank you!!)

All you have to do is run the follow line from the command line.

$ xcrun swift-update -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk -target arm64-apple-ios9 the-file-you-want-to-migrate.swift > the-output-file-name.swift

You might get a lot of output about what changed, but don’t mistake them for errors! Also make sure your Xcode beta is actually in Applications and not in Downloads or something because I had that issue/

If you are like me and are using CommonCrypto and have defined a module map for it to get it to work in a script, we can just ad the -I CommonCrypto option to get it to work property.

$ xcrun swift-update -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk -target arm64-apple-ios9 -I CommonCrypto the-file-you-want-to-migrate.swift > the-output-file-name.swift

Let me know if you have any questions, comments, or ideas. Happy Swift scripting! 🐥