Skip to content

Commit

Permalink
Fixed a bug that rule-path was not reflected
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryu0118 committed May 23, 2023
1 parent 74f267c commit c3c339b
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ release:

.PHONY: sorter
sorter:
mint run sorter -p .
mint run sorter -p . --rule-path ./sorter_config
7 changes: 7 additions & 0 deletions Sources/Sorter/Sorter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,26 @@ struct Sorter: ParsableCommand {
else if let file, URL(fileURLWithPath: file).hasDirectoryPath {
throw SorterError.notFile(path: file)
}
else if let rulePath, !FileManager.default.fileExists(atPath: rulePath) {
throw SorterError.ruleFileNotFound(path: rulePath)
}
}
}

enum SorterError: LocalizedError {
case notDirectory(path: String)
case notFile(path: String)
case projectNotFound(path: String)
case ruleFileNotFound(path: String)

var errorDescription: String? {
switch self {
case .projectNotFound(let path):
return "\(path) could not be found"

case .ruleFileNotFound(let path):
return "\(path) could not be found"

case .notDirectory(let path):
return "\(path) is not a directory"

Expand Down
File renamed without changes.
9 changes: 6 additions & 3 deletions Sources/SorterCore/RewriterProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ enum RewriterProvider {
}

private static func loadRuleFile(directory: URL) throws -> Rule {
if FileManager.default.fileExists(atPath: directory.absoluteString) {
if FileManager.default.fileExists(atPath: directory.pathComponents.joined(separator: "/")) {
let content = try Data(contentsOf: directory)
let string = String(data: content, encoding: .utf8) ?? ""

return Rule(enabled: string.components(separatedBy: "\n"))
let ruleNames = string
.components(separatedBy: "\n")
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
.filter { !$0.isEmpty }
return Rule(enabled: ruleNames)
} else {
return Rule(enabled: allRewriters.map { $0.ruleName })
}
Expand Down
3 changes: 3 additions & 0 deletions Sources/SorterCore/Rule.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
struct Rule {
let enabled: [String]
}
6 changes: 1 addition & 5 deletions Sources/SorterCore/Sorter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public enum Sorter {
}

for case let fileURL as URL in enumerator {
let fileAttributes = try fileURL.resourceValues(forKeys:[.isRegularFileKey])
let fileAttributes = try fileURL.resourceValues(forKeys: [.isRegularFileKey])
if fileAttributes.isRegularFile!, fileURL.pathExtension == "swift" {
try sort(fileURL: fileURL, rewriter: rewriters)
}
Expand All @@ -66,10 +66,6 @@ public enum Sorter {
}
}

struct Rule {
let enabled: [String]
}

enum SorterError: LocalizedError {
case fileEnumeratorNotFound
}
2 changes: 2 additions & 0 deletions sorter_config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import
enum_case

0 comments on commit c3c339b

Please sign in to comment.