Import swift file in script

Would love to hear if there are any more announcements in this area that I might have missed!

https://github.com/swift-nest/clutch

silently synchronizes scripts to regular executable's in a package, organized by a common library module (a "clutch" of related scripts in the "nest" library package).

You get the ergonomics of scripting without losing other Swift features (libraries, debugging, build configurations...).

The cost? You have to build, trust, and use the (open-source) launcher.

Hello!

Clutch seems really cool, especially if you're doing heavy scripting.

If people are looking for a simple "just make it work" solution, I played around a bit with the compiler and a helper script to allow me to write my not-so-complex ci scripts on Xcode Cloud in Swift comfortably, see my post here. Perhaps it can help somebody else!

It would be great to have this support when creating scripts. Do we have any updates on this?

Not really. It doesn’t look like much activity has happened here of the last few years. But it is something I’ve given thought to.

Ideally we can do something without changing the language. The import statement today is for modules, so would prefer to leave it at that.

The other thing I’d like to do with scripts is to be able to import modules from packages. Maybe once we have a mechanism for that we can solve scripts as multiple files in the same way. Either something in comments at the top of the file, or parameters to the swift command in the #!.

The main problem there is today, SwiftPM isn’t involved with running scripts. The swift driver sends them off to the swift-frontend to interpret them. If, instead, it sent them to SwiftPM and it assembled the dependencies, we could then get swift-frontend all the information it needs about those dependencies to interpret the script.

It’s a fascinating extension to Swift scripts. I’ve tried writing a script when I did the prebuilts work to build the prebuilts and just ended up creating an executableTarget in the SwiftPM package instead. I’d much rather it just be a standalone script that CI could run that would specify its own dependencies. It would be great to have something there.

4 Likes

People have talked about embed-like support; if we get that, then this falls out naturally, no?