Blended Cocoa

Adventures in Objective-C, Swift and Cocoa

Supporting Swift in CodeRunner

I like to use CodeRunner to test out quick snippets of code. I have added Swift as a supported language. I did this by adapting the Objective-C compile.sh script that is used by CodeRunner to compile Objective-C with clang. Hopefully the next release of CodeRunner will support Swift out of the box with a better compiler script and syntax highlighting.

In the CodeRunner Preferences add a new language, check the Language uses compilation script checkbox and edit the script to contain the following:

compile.shView Gist
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/bin/bash
# This is a CodeRunner compilation script. Compilation scripts are used to
# compile code before being run using the run command specified in CodeRunner
# preferences. This script should have the following properties:
#
# Launch directory ($PWD):      Will be the same as the file being run
#
# Exit status:                  Should be 0 on success (will cause CodeRunner
#                               to continue and execute the run command)
#
# Output (stdout):              On success, one line of text which can be accessed
#                               using the $compiler variable in the run command
#
# Output (stderr):              Anything outputted here will be displayed in
#                               the CodeRunner console
#
# Arguments:                    $1      Filename of the source file
#                               $2      Encoding of the source file
#                               $3      Compilation flags set in CodeRunner preferences
#                               $4      Path of a temporary directory (without / suffix)
#
# The encoding argument may be used to specify to the compiler which encoding
# the source file uses. It will be one of the integers in the following array:

enc[4]=""       # UTF-8
enc[10]=""      # UTF-16
enc[5]=""       # ISO Latin 1
enc[9]=""       # ISO Latin 2
enc[30]=""      # Mac OS Roman
enc[12]=""      # Windows Latin 1
enc[3]=""       # Japanese (EUC)
enc[8]=""       # Japanese (Shift JIS)
enc[1]=""       # ASCII

compname=`echo "$1" | sed 's/\(.*\)\..*/\1/'`

xcrun swift -sdk $(xcrun --show-sdk-path --sdk macosx) "$1" -o "$compname" $3

status=$?
if [ $status -eq 0 ]
then
echo $compname
exit 0
elif [ $status -eq 127 ]
then
echo -e "\nTo run code in this language, you need to have compilers installed. These are bundled with Xcode 6 or later which can be downloaded through the Mac App Store or Apple's developer site."
fi
exit $status

The Run Command needs to be set to ./$compiler.

I set my Code Template to the following to include all the Cocoa goodness and set the file extension to swift:

Template
1
2
3
import Cocoa

%@

Finally, you’ll probably want to change New Language to Swift by right-clicking in the Language list.

Here is a screenshot of the settings:

In order to allow the xcrun command in the compilation script to find swift you need to ensure that xcode-select is pointing at a version of Xcode that includes the swift compiler, e.g.

1
sudo xcode-select -s /Applications/Xcode6-Beta.app/Contents/Developer

Comments