Swift is a fast, safe, modern programming language with an open governance model and a vibrant community. There’s no reason that it should be limited to just making apps. And indeed, many smart people are working hard to bring Swift to new platforms and evolve its capabilities for web development and machine learning.
Scripting is another point of interest for the Swift community, but the amount of setup required to incorporate 3rd-party dependencies has long been seen as a non-starter.
…that is, until now.
On Friday, Max Howellannounced a new project called swift-sh. The project provides a shim around the Swift compiler that creates a package for your script and uses it to automatically add and manage external dependencies imported with a special trailing comment.
Although in its initial stages of development, it’s already seemingly managed to solve the biggest obstacle to Swift from becoming a productive scripting language.
This week, let’s take a very early look at this promising new library and learn how to start using it today to write Swift scripts.
The original Swift Package Manager examples provided a shuffleableDeck of PlayingCard values. So it feels appropriate to revisit them here. For this example, let’s build a Swift script to deal and print out a formatted representation of a hand of Bridge. The final result should look something like this:
The comment after the import declaration for DeckOfPlayingCards
tells swift-sh to look for the package
on GitHub in a repository by the same name
under the NSHipster username.
The tilde-greater-than operator in ~> 4.0.0
is shorthand for specifying a version
“equal to or greater than in the least significant digit”
according to Semantic Versioning conventions.
In this case, Swift Package Manager will use the latest release
whose major is equal to 4 and minor release is equal 0
(that is, it will use 4.0.1 or4.0.0, but not 4.1.0 or 5.0.0).
For the PlayingCard module,
we don’t have to add an import specification for
because it’s already included as a dependency of DeckOfPlayingCards.
Finally, the Cycle module is an external package and includes an external import specification that tells swift-sh how to add it as a dependency. The notable difference here is that the == operator is used to specify a revision rather than a tagged release.
In an extension, conform Player to CustomStringConvertible
and implement the description property,
taking advantage of the convenient
added to Swift 4.
a player’s hand is grouped by suit and ordered by rank.
On Unix systems, a shebang (#!) indicates how a script should be interpreted. In our case, the shebang line at the top of bridge.swift tells the system to run the file using the sh subcommand of the swift command (/usr/bin/swift):
Doing so allows you to take the extra step to make a Swift script look and act just like a binary executable. Use mv to strip the .swift extension and chmod to add executable (+x) permissions.
swift-sh requires module names to match the name of their repository. In many cases, this isn’t a problem because projects typically have descriptive names. However, in our example, the DeckOfPlayingCards module
was provided by the repository
As described in last week’s article, Swift provides special syntax for importing individual declarations from external modules.
In our example, we import the Cycle package to access its cycle() function, which is used to iterate over the players during the initial deal repeatedly.
In a conventional Swift package setup, we could import that function only. However, that syntax isn’t yet supported by swift-sh.
// 🔮 Possible Future SyntaximportfuncCycle.cycle()// @NSHipster/Cycle
Until full support for import declaration syntax is added, you’ll only be able to import external modules in their entirety.
Given the importance of this functionality, we think swift-sh is destined to become part of the language. As momentum and excitement build around this project, keep an eye out in the Swift forums for proposals to incorporate this as a language feature in a future release.