• Stars
    star
    1,732
  • Rank 26,900 (Top 0.6 %)
  • Language
    Swift
  • License
    The Unlicense
  • Created almost 6 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Easily script with third-party Swift dependencies.

swift sh badge-platforms badge-languages Build Status

Writing Swift scripts is easy:

$ cat <<EOF > script
#!/usr/bin/swift
print("Hi!")
EOF
$ chmod u+x script
$ ./script
Hi!

Sadly, to use third-party dependencies we have to migrate our script to a swift package and use swift build, a relatively heavy solution when all we wanted was to whip up a quick script. swift-sh gives us the best of both worlds:

$ cat <<EOF > script
#!/usr/bin/swift sh
import PromiseKit  // @mxcl ~> 6.5
print(Promise.value("Hi!"))
EOF
$ chmod u+x script
$ ./script
Promise("Hi!")

In case it’s not clear, swift-sh reads the comment after the import and uses this information to fetch your dependencies.


Let’s work through an example: if you had a single file called foo.swift and you wanted to import mxcl/PromiseKit:

#!/usr/bin/swift sh

import Foundation
import PromiseKit  // @mxcl ~> 6.5

firstly {
    after(.seconds(2))
}.then {
    after(.milliseconds(500))
}.done {
    print("notice: two and a half seconds elapsed")
    exit(0)
}

RunLoop.main.run()

You could run it with:

$ swift sh foo.swift

Or to make it more “scripty”, first make it executable:

$ chmod u+x foo.swift
$ mv foo.swift foo    # optional step!

And then run it directly:

$ ./foo

Sponsorship

If your company depends on swift-sh please consider sponsoring the project. Otherwise it is hard for me to justify maintaining it.

Installation

brew install swift-sh

Or you can build manually using swift build.

Installation results in a single executable called swift-sh, the swift executable will call this (provided it is in your PATH) when you type: swift sh.

We actively support both Linux and Mac and will support Windows as soon as it is possible to do so.

Usage

Add the shebang as the first line in your script: #!/usr/bin/swift sh.

Your dependencies are determined via your import lines:

#!/usr/bin/swift sh
import AppUpdater    // @mxcl
// ^^ https://github.com/mxcl/AppUpdater, latest version

import PromiseKit    // @mxcl ~> 6.5
// ^^ mxcl/PromiseKit, version 6.5.0 or higher up to but not including 7.0.0 or higher

import Chalk         // @mxcl == 0.3.1
// ^^ mxcl/Chalk, only version 0.3.1

import LegibleError  // @mxcl == b4de8c12
// ^^ mxcl/LegibleError, the precise commit `b4de8c12`

import Path          // mxcl/Path.swift ~> 0.16
// ^^ for when the module-name and repo-name are not identical

import BumbleButt    // https://example.com/bb.git ~> 9
// ^^ non-GitHub URLs are fine

import CommonTaDa    // [email protected]:mxcl/tada.git ~> 1
// ^^ ssh URLs are fine

import TaDa          // ssh://[email protected]:mxcl/tada.git ~> 1
// ^^ this style of ssh URL is also fine

import Foo  // ./my/project
import Bar  // ../my/other/project
import Baz  // ~/my/other/other/project
import Fuz  // /I/have/many/projects
// ^^ local dependencies must expose library products in their `Package.swift`
// careful: `foo/bar` will be treated as a GitHub dependency; prefix with `./`
// local dependencies do *not* need to be versioned


import Floibles  // @mxcl ~> 1.0.0-alpha.1
import Bloibles  // @mxcl == 1.0.0-alpha.1
// ^^ alphas/betas will only be fetched if you specify them explicitly like so
// this is per Semantic Versioning guidelines

swift-sh reads the comments after your imports and fetches the requested SwiftPM dependencies.

It is not necessary to add a comment specification for transitive dependencies.

Editing in Xcode

The following will generate an Xcode project (not in the working directory, we keep it out the way in our cache directory) and open it, edits are saved to your script file.

$ swift sh edit ./myScript

Examples

Converting your script to a package

Simple scripts can quickly become bigger projects that would benefit from being packages that you build with SwiftPM. To help you migrate your project we provide swift sh eject, for example:

$ swift sh eject foo.swift

creates a Swift package in ./Foo, from now on use swift build in the Foo directory. Your script is now ./Foo/Sources/main.swift.

Use in CI

If you want to make scripts available to people using CI; use stdin:

brew install mxcl/made/swift-sh
swift sh <(curl http://example.com/yourscript) arg1 arg2

Internal Details

swift sh creates a Swift Package.swift package manager project with dependencies in a directory below the swift-sh cache directory †, builds the executable, and then executes it via swift run.
The script is (only) rebuilt when the script file is newer than the executable.

† Specify the cache parent directory using the (FreeDesktop) environment variable XDG_CACHE_HOME. If unspecified, on macOS swif-sh uses $HOME/Library/Developer/swift-sh.cache, and otherwise it uses $HOME/.cache/swift-sh.

Swift Versions

swfit-sh v2 requires Swift 5.1. We had to drop support for Swift v4.2 because maintenance was just too tricky.

swift-sh uses the active tools version, (ie: xcode-select) or whichever Swift is first in the PATH on Linux. It writes a manifest for the package it will swift build with that tools-version. Thus Xcode 11.0 builds with Swift 5.1. Dependencies build with the Swift versions they declare support for, provided the active toolchain can do that (eg. Xcode 11.0 supports Swift 4.2 and above)

To declare a support for specific Swift versions in your script itself, use #if swift or #if compiler directives.

Alternatives


Troubleshooting

error: unable to invoke subcommand: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-sh

If you got here via Google, you have a script that uses this tool, if you now install swift-sh, you will be able to run your script:

brew install mxcl/made/swift-sh

Or see the above installation instructions.

More Repositories

1

PromiseKit

Promises for Swift & ObjC.
Swift
14,218
star
2

Path.swift

Delightful, robust, cross-platform and chainable file-pathing functions.
Swift
929
star
3

Workbench

Seamless, automatic, “dotfile” sync to iCloud.
Swift
668
star
4

YOLOKit

Getting square objects down round holes
Objective-C
663
star
5

Cake

A delicious, quality‑of‑life supplement for your app‑development toolbox.
Swift
537
star
6

OMGHTTPURLRQ

Vital extensions to NSURLRequest that Apple left out for some reason.
Objective-C
339
star
7

Version

semver (Semantic Version) Swift µFramework.
Swift
293
star
8

xcodebuild

A continuously resilient `xcodebuild` “GitHub Action”. Also it’s the best.
TypeScript
285
star
9

AppUpdater

Automatically update open source macOS apps from GitHub releases.
Swift
245
star
10

Chalk

Terminal colors using Swift 5’s string interpolation extensions.
Swift
215
star
11

LegibleError

Beating `Error.localizedDescription` at its own game.
Swift
180
star
12

Audioscrobbler.app

Minimal, robust iTunes scrobbling
Objective-C
140
star
13

UIImageWithColor

[UIImage imageWithColor]
Objective-C
80
star
14

yosemite-menu-inverter

Invert your Yosemite icons
Ruby
71
star
15

MBWebSocketServer

An objc draft 10 websocket implementation
Objective-C
69
star
16

AmA

Ask mxcl anything.
50
star
17

ChuzzleKit

A chuzzled object is nil if it is falsy, otherwise it has its falsy parts removed.
Objective-C
48
star
18

UIImageAverageColor

[UIImage averageColor]
Objective-C
32
star
19

playdar.prefpane

All in one Mac OS X Preference Pane for the Playdar daemon
Objective-C
28
star
20

initWith...FuckIt

Constructors for pithy, potty-mouths.
Objective-C
26
star
21

MBAppStoreRater

Non-intrusive invitation to rate your iOS app
Objective-C
22
star
22

bs

Write HTML inline with your JS
Ruby
22
star
23

UIColorPerceivedLuminance

[UIColor perceivedLuminance]
Objective-C
18
star
24

StreamReader

Swift
17
star
25

SuperGoodDeleteWiggle

Wiggle animation (almost) as good as the Springboard delete wiggle animation
Objective-C
13
star
26

mxcl.github.io

HTML
12
star
27

scrobsub

Portable scrobbling solution in plain c
C
9
star
28

generate-podspec

Generate a podspec for your SwiftPM project based on tagged releases.
TypeScript
9
star
29

Kissogram

Swift
8
star
30

UIInvertedImage

[UIImage invertedImage]
Objective-C
8
star
31

homebrew-made

Ruby
7
star
32

Flickr-Friends.wdgt

A recent photo slideshow of your Flickr Friends for the OS X Dashboard
JavaScript
7
star
33

roofparty

A Last.fm radio player written in D
5
star
34

.github

5
star
35

DraftLottery

Example Swift app
Swift
4
star
36

get-swift-version

JavaScript
4
star
37

Roole.tmBundle

TextMate Syntax Highlighting Bundle for Roole
4
star
38

mxcl

3
star
39

SelfieAssist

Objective-C
3
star
40

MXCLPlaceholderTextView

3
star
41

sym0

Cocoa utility functions with zero symbol pollution
Objective-C
2
star