Skip to content

Commit

Permalink
Merge pull request #44 from larslockefeer/master
Browse files Browse the repository at this point in the history
Added a command line option to specify a path to lint
  • Loading branch information
jpsim committed May 29, 2015
2 parents f150414 + ea0bd86 commit e338fcc
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 27 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

##### Enhancements

* Added a command line option `--path` to specify a path to lint.
[Lars Lockefeer](https://github.com/larslockefeer)
[#16](https://github.com/realm/SwiftLint/issues/16)

* The following rules now conform to `ASTRule`:
FunctionBodyLength, Nesting, TypeBodyLength, TypeName, VariableName.
[JP Simard](https://github.com/jpsim)
Expand Down
97 changes: 70 additions & 27 deletions Source/swiftlint/LintCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,79 @@ struct LintCommand: CommandType {
"(default command)"

func run(mode: CommandMode) -> Result<(), CommandantError<()>> {
println("Finding Swift files in current directory...")
let files = fileManager.allFilesRecursively(
directory: fileManager.currentDirectoryPath
).filter { $0.isSwiftFile() }
var numberOfViolations = 0, numberOfSeriousViolations = 0
for (index, file) in enumerate(files) {
println("Linting '\(file.lastPathComponent)' (\(index + 1)/\(files.count))")
for violation in Linter(file: File(path: file)!).styleViolations {
println(violation)
numberOfViolations++
if violation.severity.isError {
numberOfSeriousViolations++
return LintOptions.evaluate(mode).flatMap { options in
return self.lint(options.path)
}
}

private func lint(path: String) -> Result<(), CommandantError<()>> {
let filesToLint = filesToLintAtPath(path)
if filesToLint.count > 0 {

if path == "" {
println("Linting Swift files in current working directory")
} else {
println("Linting Swift files at path \(path)")
}

var numberOfViolations = 0, numberOfSeriousViolations = 0
for (index, file) in enumerate(filesToLint) {
println("Linting '\(file.lastPathComponent)' (\(index + 1)/\(filesToLint.count))")
for violation in Linter(file: File(path: file)!).styleViolations {
println(violation)
numberOfViolations++
if violation.severity.isError {
numberOfSeriousViolations++
}
}
}
let violationSuffix = (numberOfViolations != 1 ? "s" : "")
let filesSuffix = (filesToLint.count != 1 ? "s." : ".")
println(
"Done linting!" +
" Found \(numberOfViolations) violation\(violationSuffix)," +
" \(numberOfSeriousViolations) serious" +
" in \(filesToLint.count) file\(filesSuffix)"
)
if numberOfSeriousViolations <= 0 {
return success()
} else {
// This represents failure of the content (i.e. violations in the files linted)
// and not failure of the scanning process itself. The current command architecture
// doesn't discriminate between these types.
return failure(CommandantError<()>.CommandError(Box()))
}
}
let violationSuffix = (numberOfViolations != 1 ? "s" : "")
let filesSuffix = (files.count != 1 ? "s." : ".")
println(
"Done linting!" +
" Found \(numberOfViolations) violation\(violationSuffix)," +
" \(numberOfSeriousViolations) serious" +
" in \(files.count) file\(filesSuffix)"
)
if numberOfSeriousViolations <= 0 {
return success()
} else {
// This represents failure of the content (i.e. violations in the files linted)
// and not failure of the scanning process itself. The current command architecture
// doesn't discriminate between these types.
return failure(CommandantError<()>.CommandError(Box()))
return failure(CommandantError<()>.UsageError(description: "No lintable files found at" +
" path \(path)"))
}

private func filesToLintAtPath(path: String) -> [String] {
let absolutePath = path.absolutePathRepresentation()
var isDirectory: ObjCBool = false
if fileManager.fileExistsAtPath(absolutePath, isDirectory: &isDirectory) {
if isDirectory {
return fileManager.allFilesRecursively(directory: absolutePath).filter {
$0.isSwiftFile()
}
} else if absolutePath.isSwiftFile() {
return [absolutePath]
}
}
return []
}
}

struct LintOptions: OptionsType {
let path: String

static func create(path: String) -> LintOptions {
return LintOptions(path: path)
}

static func evaluate(m: CommandMode) -> Result<LintOptions, CommandantError<()>> {
return create
<*> m <| Option(key: "path", defaultValue: "", usage: "the path to the file or" +
" directory to lint")
}
}

0 comments on commit e338fcc

Please sign in to comment.