<commit_before>/*
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import Foundation

class FoundationTask: Task {
    var command: [String] = []
    var input = ""
    var environment: [String: String] = [:]

    var monitor: ProcessMonitor?
    func run(completion: (output: String, ok: Bool) -> Void) {
        precondition(self.monitor == nil, "\(self) is a one-shot task runner, and \(__FUNCTION__) has already been called once!")

        guard let inputData = input.dataUsingEncoding(NSUTF8StringEncoding) else {
            NSLog("\(self): failed to convert input string to UTF-8–encoded data")
            completion(output: "", ok: false)
            return
        }

        let task = NSTask()
        task.launchPath = command.first
        task.arguments = Array(command[1..<command.count])

        let inPipe = NSPipe()
        task.standardInput = inPipe
        /// (jws/2015-12-07)XXX: This is a blocking write.
        /// If we write over a page-size, I suspect we'll freeze.
        /// Converting to the writeability handler would likely fix it.
        inPipe.fileHandleForWriting.writeData(inputData)
        inPipe.fileHandleForWriting.closeFile()

        let outPipe = NSPipe()
        task.standardOutput = outPipe

        let stdout = outPipe.fileHandleForReading
        let monitor = ProcessMonitor(task: task, output: stdout, whenDone: { (output, task) -> Void in
            let string = self.decodeUTF8(output as! NSData)
            let ok = task.terminationStatus == 0
            completion(output: string, ok: ok)
        })
        monitor.begin()
        self.monitor = monitor

        task.launch()
    }


    private func decodeUTF8(data: NSData) -> String {
        let output: String<commit_msg>Shift blocking write out of the way<commit_after>/*
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import Foundation

class FoundationTask: Task {
    var command: [String] = []
    var input = ""
    var environment: [String: String] = [:]

    var monitor: ProcessMonitor?
    func run(completion: (output: String, ok: Bool) -> Void) {
        precondition(self.monitor == nil, "\(self) is a one-shot task runner, and \(__FUNCTION__) has already been called once!")

        guard let inputData = input.dataUsingEncoding(NSUTF8StringEncoding) else {
            NSLog("\(self): failed to convert input string to UTF-8–encoded data")
            completion(output: "", ok: false)
            return
        }

        let task = NSTask()
        task.launchPath = command.first
        task.arguments = Array(command[1..<command.count])

        let inPipe = NSPipe()
        task.standardInput = inPipe
        inPipe.fileHandleForWriting.writeabilityHandler = { fileHandle in
            fileHandle.writeData(inputData)
            fileHandle.closeFile()
        }

        let outPipe = NSPipe()
        task.standardOutput = outPipe

        let stdout = outPipe.fileHandleForReading
        let monitor = ProcessMonitor(task: task, output: stdout, whenDone: { (output, task) -> Void in
            let string = self.decodeUTF8(output as! NSData)
            let ok = task.terminationStatus == 0
            completion(output: string, ok: ok)
        })
        monitor.begin()
        self.monitor = monitor

        task.launch()
    }


    private func decodeUTF8(data: NSData) -> String {
        let output: String
{'fraction_non_alphanumeric': 0.10150068212824011, 'fraction_numerical': 0.007639836289222374, 'mean_word_length': 2.403899721448468, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4119701', 'n_tokens_mistral': 1064, 'n_tokens_neox': 1004, 'n_words': 333}