3 ms·
Either as shebang/interpreted script, or just compile to a binary for great startup speed. I actually only recently found out you could use swift for scripting.
by jbverschoor 2y ago
Either as shebang/interpreted script, or just compile to a binary for great startup speed. I actually only recently found out you could use swift for scripting. Turns out, you can also access all the other Apple APIs :-)
#!/usr/bin/env swift
I made this small "shebang thing runner script" that either runs your script interpreted, or compiles and executes it. Depending on if you're on a TTY or not. I'm also changing it to simply check for the number of executions since last modified, and then compile. In some cases the extra time is well appreciated. You still get the convenience of interpreting, but also the performance of native.
It's basically a drop-in replacement:
#/path/to/autocompile_or_interpret_swift
Very noticeable difference.
real 0m0.212s user 0m0.159s sys 0m0.045s
vs
real 0m0.014s user 0m0.004s sys 0m0.008s
- turnsout 2y agoHa, nice! I've used the Swift shebang before, but honestly once you have something that works as a "script," it's so easy to convert it to a real command line utility via Swift Argument Parser [0] that I usually spend the 5 minutes to wrap it up. [0]: https://github.com/apple/swift-argument-parser
- jbverschoor 2y agoThat's pretty sweet the argparser It's kind of nice to be able to access all native libraries. I usually use ruby, but having something compiled and strict is super nice. Shellscripting it one big minefield. Compiling is a simple step ofc. But it's nice to only have the scriptfile and it's nice to not have your directory cluttered with binaries. I'll see if I can post it tomorrow. It's very small anyway
- saagarjha 2y agoYes, but then you have to compile it to use it, since scripts can’t use dependencies. Personally I just use getopt which sounds insane but it’s part of the standard library (imported from C).