Apple Swift under Linux

Following instructions found here (and here) you can install Swift on your Ubuntu machine (precompiled binaries). If you plan to make Swift projects, I wrote a script to easily make empty projects.

To install Swift:

wget https://swift.org/builds/ubuntu1404/swift-2.2-SNAPSHOT-2015-12-18-a/swift-2.2-SNAPSHOT-2015-12-18-a-ubuntu14.04.tar.gz
sudo aptitude update
sudo apt-get install git cmake ninja-build clang uuid-dev libicu-dev icu-devtools libbsd-dev libedit-dev libxml2-dev libsqlite3-dev swig libpython-dev libncurses5-dev pkg-config
export PATH=/path/to/swift/usr/bin:$PATH

After installing you can invoke swift, type some commands and exit a-la-vim (:q).

swift-128x128Some editors already support Swift syntax, but Atom seems promising as a (temptative) IDE, so after installing Atom (.deb package available) you can type:

apm install swift-debugger language-swift

To create a proper project and setting the debugger there is a nice tutorial on Medium.

To automate the creation of a new project and launching Atom accordingly I wrote a small Perl program that you can find here (and below).

#!/usr/bin/env perl
use strict; 
use Getopt::Long;
use Cwd;

my $currentDir = getcwd;
my $outputDirectory = $currentDir;
my $swiftBin = `which swift`;
   $swiftBin =~s/swift$//;
my $debug = 0;
my $projectName;
my $outputDirectory;
my $noAtom;
chomp($swiftBin);
my $opt = GetOptions(
	'p|project=s'         => \$projectName,
	'o|outputdirectory=s' => \$outputDirectory,
	'n|noatom'            => \$noAtom,
	'd|debug'             => \$debug
);

print STDERR "
  CREATE NEW SWIFT PROJECT
 ------------------------------------------------------------
  -o  DIRNAME   Output Directory (default: $currentDir)
  -p  STRING    Project name  
  -n            Do not attempt to launch Atom
 ------------------------------------------------------------
";


unless ($projectName) {
	if ($ARGV[-1]) {
		$projectName = $ARGV[-1];
		print STDERR " Setting project name to \"$projectName\".\n";
	} else {
		die " FATAL ERROR:\n Missing parameter -p (project name).\n";
	}
}
unless (-d "$outputDirectory") {
	die " FATAL ERROR:\n Output directory does not exists [$outputDirectory].\n";
}

my $projectDir = $outputDirectory . '/' . $projectName;
print STDERR " Project directory: $projectDir\n";
if (-d "$projectDir") {
	die " FATAL ERROR:\n Output directory \"$projectDir\" already exists.\n";
}
run("mkdir \"$projectDir\"");
run("touch \"$projectDir/main.swift\"");
run("touch \"$projectDir/Package.swift\"");

open(O, ">$projectDir/Package.swift") || die " FATAL ERROR:\n Unable to write to \"$projectDir/Package.swift\".\n";
print O "import PackageDescription
let package = Package(
    name: \"$projectName\"
)
";
close O;

open (O, ">$projectDir/main.swift") ||  die " FATAL ERROR:\n Unable to write to \"$projectDir/main.swift\".\n";
print O "// e=$projectName\n";
print O "// p=$swiftBin" if (-e "$swiftBin");
close O;

print STDERR " Launching ATOM IDE ...\n";
exec("atom \"$projectDir\"");

sub run {
	my ($command, $description) = @_;
	print STDERR ">$command\n" if ($debug);
	system($command) == 0 || die " ERROR EXECUTING:\n> $command\n";
}
Advertisements