Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --use-script-input-files #193

Merged
merged 1 commit into from
Nov 11, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions Source/swiftlint/LintCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ struct LintCommand: CommandType {
strict: options.strict)
}
return .Failure(CommandantError<()>.CommandError())
} else if options.useScriptInputFiles {
return scriptInputFiles().flatMap { paths in
let files = paths.flatMap(File.init)
return lint(files, configuration: configuration, strict: options.strict)
}
}

// Otherwise parse path.
Expand Down Expand Up @@ -113,18 +118,56 @@ struct LintCommand: CommandType {
}
return []
}

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 { return getEnvironmentVariable("SCRIPT_INPUT_FILE_\($0)") }
.flatMap { path -> String? in
switch path {
case let .Success(path):
return path
case let .Failure(error):
fputs("\(error)\n", stderr)
return nil
}
}
return Result(variables)
}
}
}

struct LintOptions: OptionsType {
let path: String
let useSTDIN: Bool
let configurationFile: String
let strict: Bool
let useScriptInputFiles: Bool

static func create(path: String)(useSTDIN: Bool)(configurationFile: String)(strict: Bool)
-> LintOptions {
(useScriptInputFiles: Bool) -> LintOptions {
return LintOptions(path: path, useSTDIN: useSTDIN, configurationFile: configurationFile,
strict: strict)
strict: strict, useScriptInputFiles: useScriptInputFiles)
}

static func evaluate(mode: CommandMode) -> Result<LintOptions, CommandantError<()>> {
Expand All @@ -141,5 +184,8 @@ struct LintOptions: OptionsType {
<*> mode <| Option(key: "strict",
defaultValue: false,
usage: "fail on warnings")
<*> mode <| Option(key: "use-script-input-files",
defaultValue: false,
usage: "read SCRIPT_INPUT_FILE* environment variables as files")
}
}