GithubHelp home page GithubHelp logo

kevinenax / swiftshell Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kareman/swiftshell

0.0 1.0 0.0 1.08 MB

A Swift framework for shell scripting.

License: MIT License

Swift 85.63% Shell 14.37%

swiftshell's Introduction

Carthage compatible

Works with Swift 2.0 (Xcode 7). Use v0.2 for Swift 1.2.

For the future of SwiftShell, check out SwiftShell 2.0 (work in progress).

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 from functional programming.

Usage

Put this at the beginning of each script file:

#!/usr/bin/env swiftshell

import SwiftShell

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.

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++): ", appendNewline: false)
	print(line)
}

or

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

Launch with e.g. ls | print_linenumbers.swift

Examples

Documentation

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 Misc/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 $SWIFTSHELL_FRAMEWORK_PATH environment variable then you need to add your folder to $SWIFTSHELL_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

swiftshell's People

Contributors

kareman avatar beltex avatar kevinenax avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.