How to 'import' 3rd-party frameworks and files when using the /usr/bin/swift REPL


(has) #1

Hi folks,

I'm working on a new "AppleScript" (Apple event) bridge, SwiftAutomation <https://bitbucket.org/hhas/swiftae>, and while the code's done bar cleaning and testing, I've gotten stuck on how to write a step-by-step tutorial for it.

As I understand it, the recommended way to provide user tutorials is as one or more Playgrounds embedded within the Xcode project itself, as that gives them automatic access to the project's framework products, Source files, etc, and the tutorial text itself included as `//!` comments.

However, Playgrounds have one giant limitation: each time the user adds or changes ONE line of code, ALL OF THE CODE in the playground is automatically re-executed. That's not a problem when the code doesn't have any external side-effects, but it's a deal killer here: repeatedly sending a `make` command will spawn multiple extra TextEdit documents; repeatedly sending a `delete` command could wipe too many files in Finder, and so on. Switching the playground to manual execution doesn't really help either, since the point of a step-by-step tutorial is to enter one line at a time and see what it does before moving on to the next step. So Playgrounds are a non-starter for this kind of use - unless I've missed something?

Luckily, Swift also provides an old-school interactive REPL at `/usr/bin/swift`, which allows Swift code to be entered and executed in traditional single-line mode, while also preserving any variable assignments for reuse in later lines. Thus the user can safely `make` a new TextEdit document on line 1, modify its contents on line 2, and save and close it on line 3, and TextEdit will perform each of these steps immediately and only once. However, I can't figure out how to make the `swift ...` command do the following:

1. Import the `SwiftAutomation.framework` from my Xcode project (since Swift 3 still doesn't allow us to build standalone frameworks for installing into `~/Library`).

2. Read and compile additional Swift source files (`TextEditGlue.swift`, `FinderGlue.swift`, etc) from my Xcode project, so that those files' classes are also available for use when the REPL starts.

Can the `swift` command do both these things (I don't mind if it takes a couple seconds to launch while it compiles the glue files), and if it can what is the magical incantation for doing so?

Many thanks,

has