Skip to content

Commit

Permalink
Support automatic config resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
nicorichard committed Jun 14, 2024
1 parent 9eababd commit 6212eb2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let package = Package(
name: "XCStringsLint",
defaultLocalization: "en",
platforms: [
.macOS(.v12)
.macOS(.v13)
],
products: [
.executable(
Expand Down
46 changes: 43 additions & 3 deletions Sources/XCStringsLint/Command.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ struct Command: ParsableCommand {
private var paths: [String]

@Option(name: .customLong("config"))
// TODO: if null, find the file in the current directory
private var configPath: String
private var configPath: String?

@Option
private var reporter: ReporterFactory = .xcode
Expand All @@ -24,7 +23,7 @@ struct Command: ParsableCommand {

func run(path: String) throws {
let catalog = try StringCatalog.load(from: path)
let config = try Config.load(from: configPath)
let config = try Config.load(from: resolveConfigFilePath())
let rules = try config.toDomain()

let results = Validator(rules: rules, ignores: Ignore.default)
Expand All @@ -34,3 +33,44 @@ struct Command: ParsableCommand {
.report(results: results)
}
}

// MARK: - Config Resolving

extension Command {
private func findConfig(atPath path: String) throws -> String? {
try FileManager.default
.contentsOfDirectory(atPath: path)
.filter({ $0.contains(configRegex) })
.first
}

func resolveConfigFilePath() throws -> String {
let found: String?

if let configPath {
if FileManager.default.isDirectory(atPath: configPath) {
found = try findConfig(atPath: configPath)
} else {
found = configPath
}
} else {
found = try findConfig(atPath: FileManager.default.currentDirectoryPath)
}

guard let found else {
throw ValidationError("No xcstringslint config file could be found")
}

return found
}
}

extension FileManager {
func isDirectory(atPath path: String) -> Bool {
var fileIsDirectory: ObjCBool = false
let exists = FileManager.default.fileExists(atPath: path, isDirectory: &fileIsDirectory)
return exists && fileIsDirectory.boolValue
}
}

private let configRegex = try! Regex("\\.?xcstringslint\\.ya?ml")

0 comments on commit 6212eb2

Please sign in to comment.