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

re-introduce use-script-input-files option for lint & autocorrect #265

Merged
merged 1 commit into from
Dec 14, 2015
Merged
Show file tree
Hide file tree
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
16 changes: 15 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
## Master

##### Breaking

* None.

##### Enhancements

* None.

##### Bug Fixes

* Make linting faster than 0.5.0, but slower than 0.4.0
[Norio Nomura](https://github.com/norio-nomura)
[#119](https://github.com/jpsim/SourceKitten/issues/119)

* Re-introduce `--use-script-input-files` option for `lint` & `autocorrect`
commands. Should also fix some issues when running SwiftLint from an Xcode
build phase.
[JP Simard](https://github.com/jpsim)
[#264](https://github.com/realm/SwiftLint/issues/264)

## 0.5.0: Downy™

##### Breaking
Expand Down Expand Up @@ -72,7 +86,7 @@
[JP Simard](https://github.com/jpsim)

* Support linting from Input Files provided by Run Script Phase of Xcode with
`--use-script-input-files`.
`--use-script-input-files`.
[Norio Nomura](https://github.com/norio-nomura)
[#193](https://github.com/realm/SwiftLint/pull/193)

Expand Down
7 changes: 6 additions & 1 deletion Source/swiftlint/Commands/AutoCorrectCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ struct AutoCorrectCommand: CommandType {
func run(mode: CommandMode) -> Result<(), CommandantError<()>> {
return AutoCorrectOptions.evaluate(mode).flatMap { options in
let configuration = Configuration(commandLinePath: options.configurationFile)
return configuration.visitLintableFiles(options.path, action: "Correcting") { linter in
return configuration.visitLintableFiles(options.path, action: "Correcting",
useScriptInputFiles: options.useScriptInputFiles) { linter in
let corrections = linter.correct()
if !corrections.isEmpty {
let correctionLogs = corrections.map({ $0.consoleDescription })
Expand All @@ -37,6 +38,7 @@ struct AutoCorrectCommand: CommandType {
struct AutoCorrectOptions: OptionsType {
let path: String
let configurationFile: String
let useScriptInputFiles: Bool

static func evaluate(mode: CommandMode) -> Result<AutoCorrectOptions, CommandantError<()>> {
return curry(self.init)
Expand All @@ -46,5 +48,8 @@ struct AutoCorrectOptions: OptionsType {
<*> mode <| Option(key: "config",
defaultValue: ".swiftlint.yml",
usage: "the path to SwiftLint's configuration file")
<*> mode <| Option(key: "use-script-input-files",
defaultValue: false,
usage: "read SCRIPT_INPUT_FILE* environment variables as files")
}
}
10 changes: 8 additions & 2 deletions Source/swiftlint/Commands/LintCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ struct LintCommand: CommandType {
var reporter: Reporter.Type!
let configuration = Configuration(commandLinePath: options.configurationFile)
return configuration.visitLintableFiles(options.path, action: "Linting",
useSTDIN: options.useSTDIN) { linter in
useSTDIN: options.useSTDIN,
useScriptInputFiles: options.useScriptInputFiles) { linter in
let currentViolations = linter.styleViolations
violations += currentViolations
if reporter == nil { reporter = linter.reporter }
Expand Down Expand Up @@ -61,9 +62,11 @@ struct LintOptions: OptionsType {
let useSTDIN: Bool
let configurationFile: String
let strict: Bool
let useScriptInputFiles: Bool

static func evaluate(mode: CommandMode) -> Result<LintOptions, CommandantError<()>> {
return curry(self.init)
let curriedInitializer = curry(self.init)
return curriedInitializer
<*> mode <| Option(key: "path",
defaultValue: "",
usage: "the path to the file or directory to lint")
Expand All @@ -76,5 +79,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")
}
}
15 changes: 8 additions & 7 deletions Source/swiftlint/Extensions/Configuration+CommandLine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import Result
import SourceKittenFramework
import SwiftLintFramework

private let inputFileKey = "SCRIPT_INPUT_FILE_COUNT"

private func scriptInputFiles() -> Result<[String], CommandantError<()>> {
func getEnvironmentVariable(variable: String) -> Result<String, CommandantError<()>> {
let environment = NSProcessInfo.processInfo().environment
Expand All @@ -24,6 +22,7 @@ private func scriptInputFiles() -> Result<[String], CommandantError<()>> {
}

let count: Result<Int, CommandantError<()>> = {
let inputFileKey = "SCRIPT_INPUT_FILE_COUNT"
guard let countString = NSProcessInfo.processInfo().environment[inputFileKey] else {
return .Failure(.UsageError(description: "\(inputFileKey) variable not set"))
}
Expand Down Expand Up @@ -64,8 +63,10 @@ extension Configuration {
}

func visitLintableFiles(path: String, action: String, useSTDIN: Bool = false,
visitorBlock: (Linter) -> ()) -> Result<[File], CommandantError<()>> {
return getFiles(path, action: action, useSTDIN: useSTDIN)
useScriptInputFiles: Bool, visitorBlock: (Linter) -> ()) ->
Result<[File], CommandantError<()>> {
return getFiles(path, action: action, useSTDIN: useSTDIN,
useScriptInputFiles: useScriptInputFiles)
.flatMap { files -> Result<[File], CommandantError<()>> in
if files.isEmpty {
let errorMessage = "No lintable files found at path '\(path)'"
Expand All @@ -85,8 +86,8 @@ extension Configuration {
}
}

private func getFiles(path: String, action: String, useSTDIN: Bool) ->
Result<[File], CommandantError<()>> {
private func getFiles(path: String, action: String, useSTDIN: Bool,
useScriptInputFiles: Bool) -> Result<[File], CommandantError<()>> {
if useSTDIN {
let standardInput = NSFileHandle.fileHandleWithStandardInput()
let stdinData = standardInput.readDataToEndOfFile()
Expand All @@ -95,7 +96,7 @@ extension Configuration {
return .Success([File(contents: stdinString)])
}
return .Failure(.UsageError(description: "stdin isn't a UTF8-encoded string"))
} else if NSProcessInfo.processInfo().environment.keys.contains(inputFileKey) {
} else if useScriptInputFiles {
return scriptInputFiles().map { $0.flatMap(File.maybeSwiftFile) }
}
queuedPrintError(
Expand Down