Yes, swift CLI will compile and run your swift file.<p>But many people also want to use libraries. For Python, they use the system libraries or work within an environment with installed libraries (i.e., the library-install process happens at environment-configuration time).<p>In Swift, the easiest way to consume libraries is using packages, but that requires a Package.swift declaring the project scope for the script file (which must comply with top-level and main-entrypoint code requirements).<p>The easiest way to do that when scripting is a swift tool that manages the process of gathering your library dependencies, auto-generating a project, building the tool, and caching it all so there's no overhead the next time.<p>The best available tool now is <a href="https://github.com/mxcl/swift-sh">https://github.com/mxcl/swift-sh</a>. It reads dependency information off import comments.<p>It can also generate the project for you, if/when you want to build in XCode (e.g., move into a more complex application, perhaps requiring sandbox declarations).<p>Working scripts are not always updated, so any script-build tool has to maintain backwards compatibility, but the swift package manager has changed a lot in recent versions. swift-sh seems to err on the side of backwards compatibility, and does not support e.g., the most recent dependency versioning styles.<p>Swift-forum discussions about better support for scripting haven't resulted in any official tooling.
Is it really scripting or just feeding the code into the swift cli which then compiles it? I recall writing Swift scripts also, and when feeding it into the swift cli, there was some time delay that was comparable to compiling the program. Whereas something like Python or JS is nearly instant when running a script, the swift cli would take some type before the computation would happen.
I’ve been experimenting with swift scripting recently. Whilst in theory it could be a great substitute for Python/Ruby for dev tooling in iOS teams, I’ve yet to find an IDE setup that’s satisfactory - Xcode needs to be tricked to get something working, and has bugs/restrictions that make it a dead end
What's weird, is that if you create a project to make/edit the script with Xcode, it needs to be named "main.swift" in order to execute from Xcode.