Skip to content

Commit

Permalink
Simplify launch conditionals
Browse files Browse the repository at this point in the history
Remove command line argument duplication in favour of the configuration file (for now)
  • Loading branch information
nicorichard committed Jun 14, 2024
1 parent 12c2333 commit 9eababd
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 101 deletions.
10 changes: 0 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,6 @@ swift run xcstringslint --config .xcstringslint.yaml \
Sources/StringCatalogValidator/Resources/Localizable.xcstrings
```

Using command line arguments

```bash
swift run xcstringslint \
--require-extraction-state automatic \
--require-locale en fr \
--require-localization-state translated \
Sources/StringCatalogValidator/Resources/Localizable.xcstrings
```

### GitHub Actions

See [this repository's actions for an example](.github/workflows/lint.yaml)
Expand Down
95 changes: 4 additions & 91 deletions Sources/XCStringsLint/Command.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,8 @@ struct Command: ParsableCommand {
private var paths: [String]

@Option(name: .customLong("config"))
private var config: String?

@Option(name: .customLong(Rules.RequireExtractionState.name))
private var requireExtractionState: String?

@Option(name: .customLong(Rules.RejectExtractionState.name))
private var rejectExtractionState: String?

@Option(name: .customLong(Rules.RequireLocale.name), parsing: .upToNextOption)
private var requireLocales: [String] = []

@Option(name: .customLong(Rules.RequireLocalizationState.name), parsing: .upToNextOption)
private var requireLocalizationStates: [String] = []

@Option(name: .customLong(Rules.RejectLocalizationState.name), parsing: .upToNextOption)
private var rejectLocalizationStates: [String] = []
// TODO: if null, find the file in the current directory
private var configPath: String

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

func run(path: String) throws {
let catalog = try StringCatalog.load(from: path)

let rules = if let config {
try buildRules(using: try Config.load(from: config))
} else {
buildRules()
}
let config = try Config.load(from: configPath)
let rules = try config.toDomain()

let results = Validator(rules: rules, ignores: Ignore.default)
.validate(catalog: catalog)

try reporter.build(path: path)
.report(results: results)
}

func buildRules() -> [Rule] {
var rules: [Rule] = []

if let requireExtractionState {
rules.append(
Rules.RequireExtractionState(state: requireExtractionState)
)
}

if let rejectExtractionState {
rules.append(
Rules.RejectExtractionState(state: rejectExtractionState)
)
}

if !requireLocales.isEmpty {
rules.append(
Rules.RequireLocale(in: requireLocales)
)
}

if !requireLocalizationStates.isEmpty {
rules.append(
Rules.RequireLocalizationState(in: requireLocalizationStates)
)
}

if !rejectLocalizationStates.isEmpty {
rules.append(
Rules.RejectLocalizationState(in: rejectLocalizationStates)
)
}

return rules
}

func buildRules(using config: Config) throws -> [Rule] {
var rules = try config.rules.compactMap { (ruleName, rule) -> Rule? in
switch ruleName {
case Rules.RequireExtractionState.name:
var domainRule = Rules.RequireExtractionState(in: rule.values)
domainRule.severity = rule.severity.toDomain()
return domainRule
case Rules.RejectExtractionState.name:
var domainRule = Rules.RejectExtractionState(in: rule.values)
domainRule.severity = rule.severity.toDomain()
return domainRule
case Rules.RequireLocale.name:
var domainRule = Rules.RequireLocale(in: rule.values)
domainRule.severity = rule.severity.toDomain()
return domainRule
case Rules.RequireLocalizationState.name:
var domainRule = Rules.RequireLocalizationState(in: rule.values)
domainRule.severity = rule.severity.toDomain()
return domainRule
case Rules.RejectLocalizationState.name:
var domainRule = Rules.RejectLocalizationState(in: rule.values)
domainRule.severity = rule.severity.toDomain()
return domainRule
default:
throw ValidationError("Unknown rule: \(ruleName)")
}
}

rules.append(contentsOf: buildRules())

return rules
}
}
33 changes: 33 additions & 0 deletions Sources/XCStringsLint/helpers/Config+toDomain.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,37 @@
import StringCatalogValidator
import ArgumentParser

extension Config {
func toDomain() throws -> [StringCatalogValidator.Rule] {
try rules.compactMap { (ruleName, rule) -> StringCatalogValidator.Rule? in
switch ruleName {
case Rules.RequireExtractionState.name:
var domainRule = Rules.RequireExtractionState(in: rule.values)
domainRule.severity = rule.severity.toDomain()
return domainRule
case Rules.RejectExtractionState.name:
var domainRule = Rules.RejectExtractionState(in: rule.values)
domainRule.severity = rule.severity.toDomain()
return domainRule
case Rules.RequireLocale.name:
var domainRule = Rules.RequireLocale(in: rule.values)
domainRule.severity = rule.severity.toDomain()
return domainRule
case Rules.RequireLocalizationState.name:
var domainRule = Rules.RequireLocalizationState(in: rule.values)
domainRule.severity = rule.severity.toDomain()
return domainRule
case Rules.RejectLocalizationState.name:
var domainRule = Rules.RejectLocalizationState(in: rule.values)
domainRule.severity = rule.severity.toDomain()
return domainRule
default:
throw ValidationError("Unknown rule: \(ruleName)")
}
}
}
}


extension Config.Rule.Severity {
func toDomain() -> Severity {
Expand Down

0 comments on commit 9eababd

Please sign in to comment.