-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic infrastructure for XCLinter
- Loading branch information
Showing
10 changed files
with
150 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import Foundation | ||
import XcodeProj | ||
import PathKit | ||
|
||
public final class Configuration { | ||
public let project: XcodeProj | ||
public let projectText: String | ||
public let projectRoot: Path | ||
|
||
public init(project: XcodeProj, projectText: String, projectRoot: Path) { | ||
self.project = project | ||
self.projectText = projectText | ||
self.projectRoot = projectRoot | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
public protocol Rule { | ||
init(configuration: Configuration) | ||
func run() throws -> [Violation] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
public enum Status: Hashable { | ||
case passed | ||
case invalidInput | ||
case failed | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import XcodeProj | ||
|
||
public struct Violation { | ||
public var message: String | ||
public var objects: [PBXObject] | ||
|
||
public init(_ message: String, objects: [PBXObject] = []) { | ||
self.message = message | ||
self.objects = objects | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
public enum XCLintError: Error { | ||
case noProjectFileSpecified | ||
case projectFileNotFound(String) | ||
case badProjectFile(Error) | ||
|
||
public var localizedDescription: String { | ||
switch self { | ||
case .noProjectFileSpecified: | ||
return "Project file was not specified." | ||
case let .projectFileNotFound(path): | ||
return "Project file not found at '\(path)'." | ||
case let .badProjectFile(error): | ||
return "Bad project file: \(error.localizedDescription)." | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import Foundation | ||
import XcodeProj | ||
import PathKit | ||
|
||
public struct XCLinter { | ||
public var configuration: Configuration | ||
public var rules: [Rule.Type] | ||
|
||
public init(projectPath: String) throws { | ||
guard !projectPath.isEmpty else { | ||
throw XCLintError.noProjectFileSpecified | ||
} | ||
|
||
let path = Path(projectPath) | ||
guard path.isDirectory else { | ||
throw XCLintError.projectFileNotFound(projectPath) | ||
} | ||
|
||
let xcodeproj: XcodeProj | ||
let projectText: String | ||
do { | ||
xcodeproj = try XcodeProj(path: path) | ||
projectText = try String(contentsOf: URL(fileURLWithPath: projectPath).appendingPathComponent("project.pbxproj")) | ||
} catch { | ||
throw XCLintError.badProjectFile(error) | ||
} | ||
|
||
self.init(configuration: Configuration(project: xcodeproj, projectText: projectText, projectRoot: path), rules: []) | ||
} | ||
|
||
public init(configuration: Configuration, rules: [Rule.Type]) { | ||
self.configuration = configuration | ||
self.rules = rules | ||
} | ||
|
||
public func run() throws -> [Violation] { | ||
var violations = [Violation]() | ||
for ruleType in rules { | ||
let rule = ruleType.init(configuration: configuration) | ||
violations.append(contentsOf: try rule.run()) | ||
} | ||
return violations | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import XCTest | ||
import XCLinting | ||
|
||
final class XCLinterTests: XCTestCase { | ||
|
||
func testEmptyProjectPathThrowsError() throws { | ||
do { | ||
_ = try XCLinter(projectPath: "") | ||
XCTFail() | ||
} catch XCLintError.noProjectFileSpecified { | ||
} catch { | ||
XCTFail("wrong error: \(error)") | ||
} | ||
} | ||
|
||
|
||
func testMissingProjectFileThrowsError() throws { | ||
do { | ||
_ = try XCLinter(projectPath: "/dev/null") | ||
XCTFail() | ||
} catch XCLintError.projectFileNotFound { | ||
} catch { | ||
XCTFail("wrong error: \(error)") | ||
} | ||
} | ||
} |