Skip to content

Commit

Permalink
Allow root directory configuration as fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
nicorichard committed Jun 7, 2024
1 parent 5294a16 commit 20af868
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
58 changes: 37 additions & 21 deletions Plugins/StringCatalogLinterPlugin/StringCatalogLinterPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ let configRegex = try! Regex("\\.?xcstringslint\\.ya?ml")
@main
struct StringCatalogLinterPlugin: BuildToolPlugin {

let fileManager = FileManager()

enum Error: Swift.Error, CustomStringConvertible {
case incorrectTargetType
case missingConfigFile
Expand All @@ -32,20 +34,37 @@ struct StringCatalogLinterPlugin: BuildToolPlugin {
return try commandsForTarget(context: context, target: target)
}

private func commandsForTarget(context: PluginContext, target: Target) throws -> [Command] {
let toolPath = try context.tool(named: toolName).path
let displayName = "Running String Catalog linter for \(target.name)"
private func configPath(rootPath: Path, targetFiles: FileList?) throws -> String {
let rootConfigs = try fileManager
.contentsOfDirectory(atPath: rootPath.string)
.filter({ $0.contains(configRegex) })

let config = target.sourceModule?.sourceFiles.filter {
$0.path.lastComponent.contains(configRegex)
} ?? []
let targetConfigs = targetFiles?
.filter({ $0.path.lastComponent.contains(configRegex) })
.map(\.path.string) ?? []

if config.isEmpty {
if !targetConfigs.isEmpty && !rootConfigs.isEmpty {
Diagnostics.remark("Found target config. Overriding root config.")
}

let config = (targetConfigs + rootConfigs).first

if config == nil {
throw Error.missingConfigFile
} else if config.count > 1 {
} else if targetConfigs.count > 1 || rootConfigs.count > 1 {
throw Error.multipleConfigFiles
}

Diagnostics.remark("Running xcstringslint using config file found at: \(config!)")
return config!
}

private func commandsForTarget(context: PluginContext, target: Target) throws -> [Command] {
let toolPath = try context.tool(named: toolName).path
let displayName = "Running String Catalog linter for \(target.name)"

let config = try configPath(rootPath: context.package.directory, targetFiles: target.sourceModule?.sourceFiles)

let catalogs = target.sourceModule?.sourceFiles.filter {
$0.path.lastComponent.hasSuffix(".xcstrings")
} ?? []
Expand All @@ -56,15 +75,16 @@ struct StringCatalogLinterPlugin: BuildToolPlugin {
}

let arguments: [CustomStringConvertible] = [
"--config", config.first!.path
"--config", config
] + catalogs.map(\.path)

return [
.buildCommand(
displayName: displayName,
executable: toolPath,
arguments: arguments,
inputFiles: catalogs.map(\.path)
inputFiles: catalogs.map(\.path),
outputFiles: []
)
]
}
Expand All @@ -81,15 +101,10 @@ extension StringCatalogLinterPlugin: XcodeBuildToolPlugin {

let displayName = "Running String Catalog linter for \(target.displayName)"

let config = target.inputFiles.filter {
$0.path.lastComponent.contains(configRegex)
}

if config.isEmpty {
throw Error.missingConfigFile
} else if config.count > 1 {
throw Error.multipleConfigFiles
}
let config = try configPath(
rootPath: context.xcodeProject.directory,
targetFiles: target.inputFiles
)

let catalogs = target.inputFiles.filter {
$0.path.lastComponent.hasSuffix(".xcstrings")
Expand All @@ -101,15 +116,16 @@ extension StringCatalogLinterPlugin: XcodeBuildToolPlugin {
}

let arguments: [CustomStringConvertible] = [
"--config", config.first!.path
"--config", config
] + catalogs.map(\.path)

return [
.buildCommand(
displayName: displayName,
executable: toolPath,
arguments: arguments,
inputFiles: catalogs.map(\.path)
inputFiles: catalogs.map(\.path),
outputFiles: []
)
]
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/XCStringsLint/helpers/Config+load.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ extension Config {
let fileManager = FileManager.default

guard fileManager.fileExists(atPath: path) else {
throw ValidationError("Could not find xcstrings catalog at path: \(path)")
throw ValidationError("No xcstringslint config file could be found at path: \(path)")
}

let data = try Data(contentsOf: URL(fileURLWithPath: path))
Expand Down

0 comments on commit 20af868

Please sign in to comment.