Skip to content

Instantly share code, notes, and snippets.

@GomPam
Created May 15, 2019 08:08
Show Gist options
  • Save GomPam/3986e317774085e3fb25cb5c00be0dd0 to your computer and use it in GitHub Desktop.
Save GomPam/3986e317774085e3fb25cb5c00be0dd0 to your computer and use it in GitHub Desktop.
Shell FileWrite + Execute Script
import Foundation
let gitPath = "/Users/jeongwon/TestGit/fakeGit/build/Products/usr/local/bin/fakeGit"
let gitCommitFormat = "\(gitPath) commit -m \"%@\""
let nfdCommitMsg = "\"유니코드 정규화 확인을 위한 문장. And Check With Alphabet.\""
let escapedCommitMsg = nfdCommitMsg.replacingOccurrences(of: "\"", with: "\\\"")
let commitMsg = escapedCommitMsg.precomposedStringWithCanonicalMapping
let tmpFileURL = FileManager.default.temporaryDirectory.appendingPathComponent("gitShell_\(UUID().hashValue).sh")
let fileText = String(format: gitCommitFormat, commitMsg)
try fileText.write(to: tmpFileURL, atomically: true, encoding: .utf8)
var launchedTask = Process()
var taskPipe = Pipe()
launchedTask.launchPath = "/usr/bin/env"
launchedTask.arguments = ["sh", "\(tmpFileURL.path)"]
launchedTask.standardOutput = taskPipe
try launchedTask.run()
var pipeHandle = taskPipe.fileHandleForReading
var taskResultData = pipeHandle.readDataToEndOfFile()
var taskResult = String(data: taskResultData, encoding: .utf8)
print(taskResult!)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment