move scriptInputFiles() to Configuration+CommandLine.swift

This commit is contained in:
JP Simard
2015-12-05 19:44:45 -08:00
parent 8f534df33d
commit a17bc283d9
2 changed files with 39 additions and 37 deletions
@@ -99,43 +99,6 @@ struct LintCommand: CommandType {
}
return .Failure(CommandantError<()>.CommandError())
}
private func scriptInputFiles() -> Result<[String], CommandantError<()>> {
func getEnvironmentVariable(variable: String) -> Result<String, CommandantError<()>> {
let environment = NSProcessInfo.processInfo().environment
if let value = environment[variable] {
return .Success(value)
} else {
return .Failure(.UsageError(description: "Environment variable not set:" +
" \(variable)"))
}
}
let count: Result<Int, CommandantError<()>> = getEnvironmentVariable(
"SCRIPT_INPUT_FILE_COUNT").flatMap { count in
if let i = Int(count) {
return .Success(i)
} else {
return .Failure(.UsageError(description: "SCRIPT_INPUT_FILE_COUNT did not specify" +
" a number"))
}
}
return count.flatMap { count in
let variables = (0..<count)
.map { getEnvironmentVariable("SCRIPT_INPUT_FILE_\($0)") }
.flatMap { path -> String? in
switch path {
case let .Success(path):
return path
case let .Failure(error):
queuedPrintError(String(error))
return nil
}
}
return Result(variables)
}
}
}
struct LintOptions: OptionsType {
@@ -6,10 +6,49 @@
// Copyright © 2015 Realm. All rights reserved.
//
import Commandant
import Foundation
import Result
import SourceKittenFramework
import SwiftLintFramework
private let inputFileKey = "SCRIPT_INPUT_FILE_COUNT"
func scriptInputFiles() -> Result<[String], CommandantError<()>> {
func getEnvironmentVariable(variable: String) -> Result<String, CommandantError<()>> {
let environment = NSProcessInfo.processInfo().environment
if let value = environment[variable] {
return .Success(value)
}
return .Failure(.UsageError(description: "Environment variable not set: \(variable)"))
}
let count: Result<Int, CommandantError<()>> = {
guard let countString = NSProcessInfo.processInfo().environment[inputFileKey] else {
return .Failure(.UsageError(description: "\(inputFileKey) variable not set"))
}
if let count = Int(countString) {
return .Success(count)
}
return .Failure(.UsageError(description: "\(inputFileKey) did not specify a number"))
}()
return count.flatMap { count in
let inputFiles = (0..<count)
.map { getEnvironmentVariable("SCRIPT_INPUT_FILE_\($0)") }
.flatMap { path -> String? in
switch path {
case let .Success(path):
return path
case let .Failure(error):
queuedPrintError(String(error))
return nil
}
}
return Result(inputFiles)
}
}
extension File {
private static func maybeSwiftFile(path: String) -> File? {
if let file = File(path: path) where path.isSwiftFile() {