-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
30 additions
and
5 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 |
---|---|---|
@@ -1,11 +1,38 @@ | ||
import Darwin | ||
|
||
func excuteCommand(argments args: [String]) { | ||
func executeCommand(argments args: [String]) -> [String]? { | ||
var pipe: [Int32] = [0, 0] | ||
Darwin.pipe(&pipe) | ||
|
||
var fileActions: posix_spawn_file_actions_t? = nil | ||
posix_spawn_file_actions_init(&fileActions) | ||
|
||
posix_spawn_file_actions_addopen(&fileActions, 0, "/dev/null", O_RDONLY, 0) | ||
posix_spawn_file_actions_adddup2(&fileActions, pipe[1], 1) | ||
|
||
posix_spawn_file_actions_addclose(&fileActions, pipe[0]) | ||
posix_spawn_file_actions_addclose(&fileActions, pipe[1]) | ||
|
||
let argv: [UnsafeMutablePointer<CChar>?] = args.map{ $0.withCString(strdup) } | ||
var pid = pid_t() | ||
posix_spawnp(&pid, argv[0], &fileActions, nil, argv + [nil], nil) | ||
posix_spawn_file_actions_destroy(&fileActions) | ||
|
||
close(pipe[1]) | ||
|
||
let N = 4096 | ||
var buf = [Int8](repeating: 0, count: N + 1) | ||
|
||
var n: Int | ||
var outputStrings = [String]() | ||
repeat { | ||
n = read(pipe[0], &buf, N) | ||
if let output = String(validatingUTF8: buf) { | ||
outputStrings.append(output) | ||
} | ||
} while n > 0 | ||
|
||
close(pipe[0]) | ||
|
||
return outputStrings | ||
} |
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,12 +1,10 @@ | ||
import Darwin | ||
|
||
let readmeFileName = "README.md" | ||
|
||
/// Do not overwrite an existing file. | ||
func duplicateExistingReadme(existingReadmeDirctoryPath sourcePath: String, newReadmeDirectoryPath targetPath: String) { | ||
excuteCommand(argments: ["cp", "-n", "\(sourcePath)/\(readmeFileName)", targetPath]) | ||
executeCommand(argments: ["cp", "-nv", "\(sourcePath)/\(readmeFileName)", targetPath]) | ||
} | ||
|
||
func replaceStringsInReadme(source: String, target: String) { | ||
excuteCommand(argments:["sed", "-i", "", "-e", "s/\(source)/\(target)/g", "./\(readmeFileName)"]) | ||
executeCommand(argments:["sed", "-i", "", "-e", "s/\(source)/\(target)/g", "./\(readmeFileName)"]) | ||
} |