Kare Morstol 11a028d4ff Fix #2: "ReadableStreamType.readSome crashes with error "6555 Illegal instruction: 4" when using a Release build of SwiftShell."
Also fixes #1: "SwiftShell_Speed_Tests unit tests never finish.".
It turns out NSString(data data: NSData, encoding encoding: UInt) returns an optional String, something Xcode/Swift neglected to tell me. Which is strange as they are normally very strict about these things. But what really solved these 2 issues (which are really the same issue) was removing the unnecessary cast "as String". For more on this really bizarre bug in the Swift compiler, see https://devforums.apple.com/message/1084852#1084852 .
2014-12-18 01:47:33 +01:00
2014-12-09 18:07:04 +01:00
2014-09-09 19:39:24 +02:00
2014-11-14 19:19:59 +01:00

SwiftShell

An OS X Framework for command line scripting in Swift. It supports joining together shell commands and Swift functions, like the pipe in shell commands and the pipe forward operator in F#. As Swift itself it supports both object-oriented and functional programming.

Usage

Commands

Shell commands return readable streams, which can be read all at once with "read()" or read lazily (as in piece by piece) with "readSome()". The latter is useful for long texts.

#!/usr/bin/env swiftshell

import SwiftShell

let result = run("some shell command").read()

Commands can be piped together:

run("echo piped to the next command") |> run("wc -w") |>> standardoutput

Use any sequence as parameters for a command:

run( "chmod +x" + parameters(files) )

For in-line commands, use $("command"):

print( "The time and date is " + $("date -u") )

Files

Files are streams too. They can be read line by line:

for line in open("file1.txt").lines() {
	// Do something with each line
}

Or written to:

let file2 = open(forWriting: tempdirectory / "newfile.txt" )
run("echo line 1") |>> file2
file2.writeln("line 2")

And there's easy access to NSFileManager:

if File.fileExistsAtPath("fileiwant.txt") {...}
if File.isExecutableFileAtPath("program") {...}
...

Standard input

is also a stream:

var i = 1
for line in standardinput.lines() {
	print("line \(i++): ")
	println(line)
}

or

var i = 1
standardinput.lines() |> map {line in "line \(i++): \(line)\n"} |>> standardoutput

Launch with e.g. ls | print_linenumbers.swift

Examples

Installation

  • In the Terminal, go to where you want to download SwiftShell.

  • Run

      git clone https://github.com/kareman/SwiftShell.git 
      cd SwiftShell
    
  • Copy/link Div/swiftshell to your bin folder or anywhere in your PATH.

  • To install the framework itself, either:

    • run xcodebuild install from the project's root folder. This will install the SwiftShell framework in ~/Library/Frameworks.
    • or run xcodebuild and copy the resulting framework from the build folder to your library folder of choice. If that is not "~/Library/Frameworks", "/Library/Frameworks" or a folder mentioned in the $DYLD_FRAMEWORK_PATH environment variable then you need to add your folder to $DYLD_FRAMEWORK_PATH.

License

Released under the MIT License (MIT), http://opensource.org/licenses/MIT

Some files are covered by other licences, see included works.

Kåre Morstøl, NotTooBad Software

S
Description
A Swift framework for shell scripting.
Readme 3.5 MiB
Languages
Swift 96.1%
Shell 2.9%
Ruby 1%