This repository has been archived by the owner on Dec 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored the code to be ready for the 0.1.0 release.
- Loading branch information
Showing
7 changed files
with
359 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015 Kiad Software, LLC. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,65 @@ | ||
# apous | ||
Let's make Swift scripting a reality. | ||
_______ | ||
| _ .-----.-----.--.--.-----. | ||
|. 1 | _ | _ | | |__ --| | ||
|. _ | __|_____|_____|_____| | ||
|: | |__| | ||
|::.|:. | | ||
`--- ---' | ||
|
||
Apous is a simple tool that allows for easier authoring of Swift scripts. | ||
|
||
# Workflow | ||
Primary features: | ||
|
||
> apous init | ||
1. Allow the breaking up of scripts into multiple files. | ||
2. Dependency management through [Carthage](https://github.com/Carthage/Carthage). | ||
|
||
# How it Works | ||
|
||
Apous works by first checking for a `Cartfile` in your script's directory. If one is | ||
present, then `carthage update` will be run. | ||
|
||
Next, all of your Swift files are combined into a single `.apous.swift` file that can | ||
then be run by the `swift` REPL. | ||
|
||
It's really that simple. | ||
|
||
# Getting Started | ||
|
||
First, you need to install the latest build of Apous. | ||
|
||
TODO(owensd): Finish the install steps. | ||
|
||
|
||
# Creating Your First Script | ||
|
||
1. Create a new directory for your scripts, say `mkdir demo` | ||
2. Change to that directory: `cd demo` | ||
3. Create a new script file: `touch demo.swift` | ||
4. Change the contents of the file to: | ||
|
||
```swift | ||
import Foundation | ||
|
||
print("Welcome to Apous!") | ||
``` | ||
|
||
5. Run the script: `apous demo.swift` | ||
|
||
This will output: | ||
|
||
Welcome to Apous! | ||
|
||
You can see some other samples here: [Samples](https://github.com/owensd/apous/tree/master/samples). | ||
|
||
|
||
# Known Issues | ||
|
||
Currently there are some design limitations: | ||
|
||
* [Issue #1](https://github.com/owensd/apous/issues/1) - Support for nested directories. | ||
* [Issue #2](https://github.com/owensd/apous/issues/2) - Support for folder structure packages. | ||
|
||
# FAQ | ||
|
||
Q: What is Apous mean? | ||
A: It's from the ancient Greek απους, meaning "without feet". |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// | ||
// ErrorCodes.swift | ||
// apous | ||
// | ||
// Created by David Owens on 7/5/15. | ||
// Copyright © 2015 owensd.io. All rights reserved. | ||
// | ||
|
||
enum ErrorCode: Int, ErrorType { | ||
case InvalidUsage = 1 | ||
case PathNotFound | ||
case CarthageNotInstalled | ||
case SwiftNotInstalled | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
// | ||
// Tools.swift | ||
// apous | ||
// | ||
// Created by David Owens on 7/5/15. | ||
// Copyright © 2015 owensd.io. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/// The base abstraction for all command-line utilities. | ||
protocol Tool { | ||
|
||
/// The name of the tool to be run. | ||
var launchPath: String { get } | ||
|
||
/// `true` when the output of the tool should be printed to `stdout`. | ||
var printOutput: Bool { get } | ||
|
||
/// Runs the tool and returns result of the execution. | ||
func run(args: String...) -> (out: String, err: String, code: Int32) | ||
} | ||
|
||
extension Tool { | ||
var printOutput: Bool { return true } | ||
|
||
func run(args: String...) -> (out: String, err: String, code: Int32) { | ||
let output = NSPipe() | ||
let error = NSPipe() | ||
|
||
// NOTE(owensd): This merges stdout and stderr for now... | ||
func stream(handle: NSFileHandle) -> String { | ||
let data = handle.availableData | ||
let str = NSString(data: data, encoding: NSUTF8StringEncoding) ?? "" | ||
|
||
if self.printOutput { | ||
print("\(str)", appendNewline: false) | ||
} | ||
|
||
return str as String | ||
} | ||
|
||
var out = "" | ||
var err = "" | ||
|
||
output.fileHandleForReading.readabilityHandler = { out += stream($0) } | ||
error.fileHandleForReading.readabilityHandler = { err += stream($0) } | ||
|
||
let task = NSTask() | ||
task.launchPath = self.launchPath | ||
task.arguments = args | ||
task.standardOutput = output | ||
task.standardError = error | ||
task.terminationHandler = { | ||
($0.standardOutput as? NSFileHandle)?.readabilityHandler = nil | ||
($0.standardError as? NSFileHandle)?.readabilityHandler = nil | ||
} | ||
|
||
task.launch() | ||
task.waitUntilExit() | ||
|
||
return ( | ||
out: out.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()), | ||
err: err.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()), | ||
code: task.terminationStatus) | ||
} | ||
|
||
} | ||
|
||
|
||
struct WhichTool : Tool { | ||
let printOutput = false | ||
let launchPath = "/usr/bin/which" | ||
} | ||
|
||
struct CarthageTool : Tool { | ||
let launchPath: String | ||
|
||
init?() { | ||
let which = WhichTool() | ||
let result = which.run("carthage") | ||
if result.out.characters.count == 0 { return nil } | ||
self.launchPath = result.out | ||
} | ||
|
||
// HACK(owensd): I cannot figure out why this tool will not flush our to stdout in real-time, | ||
// so forcing it to write to stdout for now. | ||
func run(args: String...) -> (out: String, err: String, code: Int32) { | ||
let output = NSFileHandle.fileHandleWithStandardOutput() | ||
let error = NSFileHandle.fileHandleWithStandardError() | ||
|
||
var out = "" | ||
var err = "" | ||
|
||
func stream(handle: NSFileHandle) -> String { | ||
let data = handle.availableData | ||
let str = NSString(data: data, encoding: NSUTF8StringEncoding) ?? "" | ||
return str as String | ||
} | ||
|
||
// NOTE(owensd): These don't work for stdout and stderr... | ||
output.readabilityHandler = { out += stream($0) } | ||
error.readabilityHandler = { err += stream($0) } | ||
|
||
let task = NSTask() | ||
task.launchPath = self.launchPath | ||
task.arguments = args | ||
task.standardOutput = output | ||
task.standardError = error | ||
task.terminationHandler = { | ||
($0.standardOutput as? NSFileHandle)?.readabilityHandler = nil | ||
($0.standardError as? NSFileHandle)?.readabilityHandler = nil | ||
} | ||
|
||
task.launch() | ||
task.waitUntilExit() | ||
|
||
return ( | ||
out: out.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()), | ||
err: err.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()), | ||
code: task.terminationStatus) | ||
} | ||
} | ||
|
||
struct SwiftTool : Tool { | ||
let launchPath: String | ||
|
||
init?() { | ||
let which = WhichTool() | ||
let result = which.run("swift") | ||
if result.out.characters.count == 0 { return nil } | ||
self.launchPath = result.out | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// | ||
// Utils.swift | ||
// apous | ||
// | ||
// Created by David Owens on 7/5/15. | ||
// Copyright © 2015 owensd.io. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Returns the root path that contains the script(s). | ||
/// This is | ||
func canonicalPath(item: String) throws -> String { | ||
func extract() -> String { | ||
if item.pathExtension == "swift" { | ||
let path = item.stringByDeletingLastPathComponent | ||
if path == "" { | ||
return NSFileManager.defaultManager().currentDirectoryPath | ||
} | ||
|
||
return path | ||
} | ||
else { | ||
return item | ||
} | ||
} | ||
|
||
let path = extract().stringByStandardizingPath | ||
|
||
var isDirectory: ObjCBool = false | ||
if NSFileManager.defaultManager().fileExistsAtPath(path, isDirectory: &isDirectory) { | ||
return path | ||
} | ||
|
||
throw ErrorCode.PathNotFound | ||
} | ||
|
||
/// Exit the process error with the given `ErrorCode`. | ||
@noreturn func exit(code: ErrorCode) { | ||
exit(Int32(code.rawValue)) | ||
} | ||
|
||
/// Returns the full path of the valid script files at the given `path`. | ||
func filesAtPath(path: String) -> [String] { | ||
if DebugOutputEnabled { | ||
print("[debug] Finding script files at path: \(path)") | ||
} | ||
|
||
let items: [String] = { | ||
do { | ||
return try NSFileManager.defaultManager().contentsOfDirectoryAtPath(path) | ||
} | ||
catch { | ||
return [] | ||
} | ||
}() | ||
|
||
return items | ||
.filter() { $0 != ".apous.swift" && $0.pathExtension == "swift" } | ||
.map() { path.stringByAppendingPathComponent($0) } | ||
} | ||
|
||
|
Oops, something went wrong.