<commit_before>//
//  DocCommand.swift
//  SourceKitten
//
//  Created by <NAME> on 2015-01-07.
//  Copyright (c) 2015 SourceKitten. All rights reserved.
//

import Commandant
import Foundation
import LlamaKit
import SourceKittenFramework

public struct DocCommand: CommandType {
    public let verb = "doc"
    public let function = "Print Swift docs as JSON or Objective-C docs as XML"

    public func run(mode: CommandMode) -> Result<()> {
        return DocOptions.evaluate(mode).flatMap { options in
            let args = Process.arguments
            if options.objc {
                let startIndex = options.singleFile ? 4 : 3
                let (headerFiles, xcodebuildArguments) = parseHeaderFilesAndXcodebuildArguments(Array<String>(args[startIndex..<args.count]))
                if let translationUnit = ClangTranslationUnit(headerFiles: headerFiles, xcodeBuildArguments: xcodebuildArguments) {
                    println(translationUnit)
                    return success(())
                }
                return failure(NSError(domain: "com.sourcekitten.SourceKitten", code: 4, userInfo: [NSLocalizedDescriptionKey: "could not generate docs"]))
            }
            if options.singleFile {
                if args.count < 5 {
                    return failure(NSError(domain: "com.sourcekitten.SourceKitten", code: 0, userInfo: [NSLocalizedDescriptionKey: "must have at least 5 arguments to call `doc --single-file`"]))
                }
                let sourcekitdArguments = Array<String>(args[4..<args.count])
                if let file = File(path: args[3]) {
                    let docs = SwiftDocs(file: file, arguments: sourcekitdArguments)
                    println(docs)
                    return success(())
                }
                return failure(NSError(domain: "com.sourcekitten.SourceKitten", code: 1, userInfo: [NSLocalizedDescriptionKey: "file could not be read"]))
            }
            let moduleName: String? = countElements(options.moduleName) > 0 ? options.moduleName : nil
            let xcodeBuildArgumentsStart = (moduleName != nil) ? 4 : 2
            let xcodeBuildArguments = Array<String>(args[xcodeBuildArgumentsStart..<args.count])
            if let docs = Module(xcodeBuildArguments: xcodeBuildArguments, name: moduleName)?.docs {
                println(docs)
                return success(())
            }
            return failure(NSError(domain: "com.sourcekitten.SourceKitten", code: 2, userInfo: [NSLocalizedDescriptionKey: "could not generate docs"]))
        }
    }
}

public struct DocOptions: OptionsType {
    public let singleFile: Bool
    public let moduleName: String
    public let objc: Bool

    public static func create(singleFile: Bool)(moduleName: String)(objc: Bool) -> DocOptions {
        return self(singleFile: singleFile, moduleName: moduleName, objc: objc)
    }

    public static func evaluate(m: CommandMode) -> Result<DocOptions> {
        return create
            <*> m <| Option(key: "single-file", defaultValue: false, usage: "only document one file")
            <*> m <| Option(key: "module-name", defaultValue: "", usage: "name of module to document (can't be used with `--single-file` or `--objc`)")
            <*> m <| Option(key: "objc", defaultValue: false, usage: "document Objective-C headers")
    }
}<commit_msg>split up DocCommand.run(_:)<commit_after>//
//  DocCommand.swift
//  SourceKitten
//
//  Created by <NAME> on 2015-01-07.
//  Copyright (c) 2015 SourceKitten. All rights reserved.
//

import Commandant
import Foundation
import LlamaKit
import SourceKittenFramework

public struct DocCommand: CommandType {
    public let verb = "doc"
    public let function = "Print Swift docs as JSON or Objective-C docs as XML"

    public func run(mode: CommandMode) -> Result<()> {
        return DocOptions.evaluate(mode).flatMap { options in
            let args = Process.arguments
            if options.objc {
                return DocCommand.runObjC(options, args: args)
            }
            if options.singleFile {
                return DocCommand.runSwiftSingleFile(args)
            }
            let moduleName: String? = countElements(options.moduleName) > 0 ? options.moduleName : nil
            return DocCommand.runSwiftModule(moduleName, args: args)
        }
    }

    public static func runSwiftModule(moduleName: String?, args: [String]) -> Result<()> {
        let xcodeBuildArgumentsStart = (moduleName != nil) ? 4 : 2
        let xcodeBuildArguments = Array<String>(args[xcodeBuildArgumentsStart..<args.count])
        if let docs = Module(xcodeBuildArguments: xcodeBuildArguments, name: moduleName)?.docs {
            println(docs)
            return success(())
        }
        return failure(NSError(domain: "com.sourcekitten.SourceKitten", code: 2, userInfo: [NSLocalizedDescriptionKey: "could not generate docs"]))
    }

    public static func runSwiftSingleFile(args: [String]) -> Result<()> {
        if args.count < 5 {
            return failure(NSError(domain: "com.sourcekitten.SourceKitten", code: 0, userInfo: [NSLocalizedDescriptionKey: "must have at least 5 arguments to call `doc --single-file`"]))
        }
        let sourcekitdArguments = Array<String>(args[4..<args.count])
        if let file = File(path: args[3]) {
            let docs = SwiftDocs(file: file, arguments: sourcekitdArguments)
            println(docs)
            return success(())
        }
        return failure(NSError(domain: "com.sourcekitten.SourceKitten", code: 1, userInfo: [NSLocalizedDescriptionKey: "file could not be read"]))
    }

    public static func runObjC(options: DocOptions, args: [String]) -> Result<()> {
        let startIndex = options.singleFile ? 4 : 3
        let (headerFiles, xcodebuildArguments) = parseHeaderFilesAndXcodebuildArguments(Array<String>(args[startIndex..<args.count]))
        if let translationUnit = ClangTranslationUnit(headerFiles: headerFiles, xcodeBuildArguments: xcodebuildArguments) {
            println(translationUnit)
            return success(())
        }
        return failure(NSError(domain: "com.sourcekitten.SourceKitten", code: 4, userInfo: [NSLocalizedDescriptionKey: "could not generate docs"]))
    }
}

public struct DocOptions: OptionsType {
    public let singleFile: Bool
    public let moduleName: String
    public let objc: Bool

    public static func create(singleFile: Bool)(moduleName: String)(objc: Bool) -> DocOptions {
        return self(singleFile: singleFile, moduleName: moduleName, objc: objc)
    }

    public static func evaluate(m: CommandMode) -> Result<DocOptions> {
        return create
            <*> m <| Option(key: "single-file", defaultValue: false, usage: "only document one file")
            <*> m <| Option(key: "module-name", defaultValue: "", usage: "name of module to document (can't be used with `--single-file` or `--objc`)")
            <*> m <| Option(key: "objc", defaultValue: false, usage: "document Objective-C headers")
    }
}
{'fraction_non_alphanumeric': 0.10914538450563561, 'fraction_numerical': 0.007133685261806249, 'mean_word_length': 2.748663101604278, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 35, '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': '3282753', 'n_tokens_mistral': 2015, 'n_tokens_neox': 1886, 'n_words': 536}