<commit_before>//===----- PrebuiltModulesJob.swift - Swit prebuilt module Planning -------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import TSCBasic
import SwiftOptions

@_spi(Testing) public func isIosMacInterface(_ path: VirtualPath) throws -> Bool {
  let data = try localFileSystem.readFileContents(path).cString
  let myStrings = data.components(separatedBy: .newlines)
  let prefix = "// swift-module-flags: "
  if let argLine = myStrings.first(where: { $0.hasPrefix(prefix) }) {
    let args = argLine.dropFirst(prefix.count).components(separatedBy: " ")
    if let idx = args.firstIndex(of: "-target"), idx + 1 < args.count {
      return args[idx + 1].contains("macabi")
    }
  }
  return false
}

func isIosMac(_ path: VirtualPath) -> Bool {
  // Infer macabi interfaces by the file name.
  // FIXME: more robust way to do this.
  return path.basenameWithoutExt.contains("macabi")
}

fileprivate func getLastInputPath(_ job: Job) -> AbsolutePath {
  return job.inputs.last!.file.absolutePath!
}

fileprivate func logOutput(_ job: Job, _ result: ProcessResult, _ logPath: AbsolutePath?, _ stdout: Bool) throws {
  guard let logPath = logPath else {
    return
  }
  let content = stdout ? try result.utf8Output() : try result.utf8stderrOutput()
  guard !content.isEmpty else {
    return
  }
  if !localFileSystem.exists(logPath) {
    try localFileSystem.createDirectory(logPath, recursive: true)
  }
  let interfaceBase = getLastInputPath(job).basename
  let fileName = "\(job.moduleName)-\(interfaceBase)-\(stdout ? "out" : "err").txt"
  try localFileSystem.writeFileContents(logPath.appending(component: fileName)) {
    $0 <<< content
  }
}

func printJobInfo(_ job: Job, _ start: Bool, _ verbose: Bool) {
  guard verbose else {
    return
  }
  Driver.stdErrQueue.sync {
    stderrStream <<< (start ? "started: " : "finished: ")<commit_msg>PrebuiltModuleGen: only use -err in log file names that contain fatal errors<commit_after>//===----- PrebuiltModulesJob.swift - Swit prebuilt module Planning -------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import TSCBasic
import SwiftOptions

@_spi(Testing) public func isIosMacInterface(_ path: VirtualPath) throws -> Bool {
  let data = try localFileSystem.readFileContents(path).cString
  let myStrings = data.components(separatedBy: .newlines)
  let prefix = "// swift-module-flags: "
  if let argLine = myStrings.first(where: { $0.hasPrefix(prefix) }) {
    let args = argLine.dropFirst(prefix.count).components(separatedBy: " ")
    if let idx = args.firstIndex(of: "-target"), idx + 1 < args.count {
      return args[idx + 1].contains("macabi")
    }
  }
  return false
}

fileprivate func getErrKind(_ content: String) -> String {
  if content.contains("error: ") {
    return "err"
  } else if content.contains("warning: "){
    return "warn"
  } else {
    return "note"
  }
}

func isIosMac(_ path: VirtualPath) -> Bool {
  // Infer macabi interfaces by the file name.
  // FIXME: more robust way to do this.
  return path.basenameWithoutExt.contains("macabi")
}

fileprivate func getLastInputPath(_ job: Job) -> AbsolutePath {
  return job.inputs.last!.file.absolutePath!
}

fileprivate func logOutput(_ job: Job, _ result: ProcessResult, _ logPath: AbsolutePath?, _ stdout: Bool) throws {
  guard let logPath = logPath else {
    return
  }
  let content = stdout ? try result.utf8Output() : try result.utf8stderrOutput()
  guard !content.isEmpty else {
    return
  }
  if !localFileSystem.exists(logPath) {
    try localFileSystem.createDirectory(logPath, recursive: true)
  }
  let interfaceBase = getLastInputPath(job).basename
  let fileName = "\(job.moduleName)-\(interfaceBase)-\(stdout ? "out" : getErrKind(content)).txt"
  try localFileSystem.writeFileContents(logPath.appending(component: fileName)) {
    $0 <<< content
  }
}

func printJobInfo(_ job: Job, _ start: Bool, _ verbose: Bool) {
  guard verbose else {
    return
  }
  Driver.stdErrQueue.sync {
    stderrStream <<< (start ? "started: " : "finished: ")
{'fraction_non_alphanumeric': 0.1548239216503438, 'fraction_numerical': 0.006668055844967702, 'mean_word_length': 3.25155004428698, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 10, 'https://': 4, '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': '128264', 'n_tokens_mistral': 1472, 'n_tokens_neox': 1409, 'n_words': 407}