Skip to content

Commit

Permalink
Add Find It (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmartha committed May 2, 2016
1 parent 72a7bcb commit 789e62a
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 5 deletions.
70 changes: 70 additions & 0 deletions Documentation/FindIt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Find It 🔍

### A Work In Progress 👷

For now, support `$ tryswiftdev -f -name <value>` only. 🙏

<br />

<img src="./Images/FindIt.png">

<br />

## Example

### Building your project _Pokemon_ with Xcode

```
ld: framework not found sourcekitd
clang: error: linker command failed with exit code 1 (use -v to see invocation)
```

### 🤔

<br />

### Running your project _Pokemon_ with Xcode

```
dyld: Library not loaded: @rpath/sourcekitd.framework/Versions/A/sourcekitd
Referenced from: /usr/local/bin/pokemon
Reason: image not found
```

### 🙄

<br />

### Finding it with _tryswiftdev_

```bash
$ tryswiftdev -f -name sourcekitd.framework
```

```
Searching... /Applications/Xcode.app/Contents
Searching... /Library/Developer
Searching... /Library/Frameworks
Found it!
- /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/sourcekitd.framework
- /Library/Developer/Toolchains/swift-2.2-SNAPSHOT-2016-03-01-a.xctoolchain/usr/lib/sourcekitd.framework
- /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2016-04-25-a.xctoolchain/usr/lib/sourcekitd.framework
(Check your `Runpath Search Paths`, `Framework Search Paths`, etc.)
```

### 😇

<br />

## ToDo

- [ ] Error Handling
- [ ] Tests

etc.
Binary file added Documentation/Images/FindIt.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Documentation/Images/tryswiftdev.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,12 @@ $ tryswiftdev
Do not overwrite an existing file.
-r [--replace-readme] Replace strings in a README.md
located in the current directory.
-f [--find] Search for specified file. ✨
-h [--help] Display available options.
```

For more information about the `-f` option, please see [Find It](./Documentation/FindIt.md).

<br />

## Example
Expand Down Expand Up @@ -94,9 +97,19 @@ HomeDirectory

<br />

### Finding It

```bash
$ tryswiftdev -f -name sourcekitd.framework
```

For more information, please see [Find It](./Documentation/FindIt.md).

<br />

## Availability

Recommend Swift 3.0-dev.
Swift 3.0-dev

<br />

Expand Down
11 changes: 7 additions & 4 deletions Sources/Command.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@ func executeCommand(argments args: [String]) -> [String]? {

close(pipe[1])

let N = 4096
var buf = [Int8](repeating: 0, count: N + 1)
let bufferSize = 4096
var buffer = [Int8](repeating: 0, count: bufferSize + 1)

var n: Int
var outputStrings = [String]()

// FIXME: Return value is not correct value when executing `$ xcodebuild -showBuildSettings`.
repeat {
n = read(pipe[0], &buf, N)
if let output = String(validatingUTF8: buf) {
n = read(pipe[0], &buffer, bufferSize)
if let output = String(validatingUTF8: buffer) {
outputStrings.append(output)
}
buffer = [Int8](repeating: 0, count: bufferSize)
} while n > 0

close(pipe[0])
Expand Down
3 changes: 3 additions & 0 deletions Sources/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@

enum Error: ErrorProtocol {
case InvalidValue
case UnsupportedOption
}

extension Error: CustomStringConvertible {
var description: String {
switch self {
case .InvalidValue:
return "Please input [option <value> ...]."
case .UnsupportedOption:
return "Please input `tryswift -f -name <value>`."
}
}
}
29 changes: 29 additions & 0 deletions Sources/FindIt.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
let maybeHere = [
"/Applications/Xcode.app/Contents",
"/Library/Developer",
"/Library/Frameworks",
]

func findFile(targetOption option: String, targetName name: String) {
print("")
var existingFilePaths = [String]()
maybeHere.forEach {
print("Searching... \($0)")
guard let outputStrings = executeCommand(argments: ["find", $0, option, name]) else { return }
existingFilePaths.append(contentsOf: outputStrings)
}
displayResult(sources: parseToArray(source: existingFilePaths.reduce("", combine: { $0 + $1 })))
}

func displayResult(sources: [String]) {
print("")
guard !sources.isEmpty else {
print("Not found.")
return
}
print("Found it!")
print("")
sources.forEach { print(" - \($0)\n") }
print("")
print("(Check your `Runpath Search Paths`, `Framework Search Paths`, etc.)")
}
5 changes: 5 additions & 0 deletions Sources/Option.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
enum Options: String, CustomStringConvertible {
case DuplicateReadme
case ReplaceStringsInReadme
case FindIt
case Usage

init?(argment: String) {
Expand All @@ -9,6 +10,8 @@ enum Options: String, CustomStringConvertible {
self = .DuplicateReadme
case "-r", "--replace-readme":
self = .ReplaceStringsInReadme
case "-f", "--find":
self = .FindIt
case "-h", "--help":
self = .Usage
default:
Expand All @@ -24,6 +27,8 @@ enum Options: String, CustomStringConvertible {
case .ReplaceStringsInReadme:
return "Replace strings in a README.md"
+ "\n located in the current directory."
case .FindIt:
return "Search for specified file."
case .Usage:
return "Display available options."
}
Expand Down
3 changes: 3 additions & 0 deletions Sources/Parse.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
func parseToArray(source: String) -> [String] {
return source.characters.split(separator: "\n").map { String($0) }
}
2 changes: 2 additions & 0 deletions Sources/Usage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ func usage() {
print("OPTIONS:")
print(" -d [--duplicate-readme] \(Options.DuplicateReadme.description)")
print(" -r [--replace-readme] \(Options.ReplaceStringsInReadme.description)")
print(" -f [--find] \(Options.FindIt.description)")
print(" -h [--help] \(Options.Usage.description)")
print("")
print("USAGE:")
Expand All @@ -16,6 +17,7 @@ func usage() {
print("EXAMPLE:")
print(" $ tryswiftdev -d ./Pokemon ./DragonBall")
print(" $ tryswiftdev -r Pokemon DragonBall")
print(" $ tryswiftdev -f -name sourcekitd.framework")
print("")
print("_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/")
print("")
Expand Down
7 changes: 7 additions & 0 deletions Sources/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@ do {
duplicateExistingReadme(existingReadmeDirctoryPath: value1, newReadmeDirectoryPath: value2)
case .ReplaceStringsInReadme:
replaceStringsInReadme(source: value1, target: value2)
case .FindIt:
guard value1 == "-name" else {
// TODO: For now, support `-name` only.
throw Error.UnsupportedOption
}
findFile(targetOption: "-name", targetName: value2)
case .Usage:
usage()
}
} catch let error as Error {
print("")
print(error.description)
} catch {
// TODO: Error Handling
Expand Down

0 comments on commit 789e62a

Please sign in to comment.