Skip to content

Commit

Permalink
Add option subpathsToIgnore to make the ignored subpaths explicit
Browse files Browse the repository at this point in the history
Related to #239
  • Loading branch information
FlineDevPublic committed Jan 21, 2022
1 parent 7c1a445 commit 773bdbf
Show file tree
Hide file tree
Showing 19 changed files with 121 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Toml

struct LintOptions {
let paths: [String]
let subpathsToIgnore: [String]
let duplicateKeys: Bool
let emptyValues: Bool
}
Expand All @@ -14,6 +15,7 @@ extension LintOptions: TomlCodable {

return LintOptions(
paths: toml.filePaths(lint, singularKey: "path", pluralKey: "paths"),
subpathsToIgnore: toml.array(lint, "subpathsToIgnore") ?? Constants.defaultSubpathsToIgnore,
duplicateKeys: toml.bool(lint, "duplicateKeys") ?? true,
emptyValues: toml.bool(lint, "emptyValues") ?? true
)
Expand All @@ -23,6 +25,7 @@ extension LintOptions: TomlCodable {
var lines: [String] = ["[lint]"]

lines.append("paths = \(paths)")
lines.append("subpathsToIgnore = \(subpathsToIgnore)")
lines.append("duplicateKeys = \(duplicateKeys)")
lines.append("emptyValues = \(emptyValues)")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Toml

struct CodeOptions {
let codePaths: [String]
let subpathsToIgnore: [String]
let localizablePaths: [String]
let defaultToKeys: Bool
let additive: Bool
Expand All @@ -20,6 +21,7 @@ extension CodeOptions: TomlCodable {

return CodeOptions(
codePaths: toml.filePaths(update, code, singularKey: "codePath", pluralKey: "codePaths"),
subpathsToIgnore: toml.array(update, code, "subpathsToIgnore") ?? Constants.defaultSubpathsToIgnore,
localizablePaths: toml.filePaths(update, code, singularKey: "localizablePath", pluralKey: "localizablePaths"),
defaultToKeys: toml.bool(update, code, "defaultToKeys") ?? false,
additive: toml.bool(update, code, "additive") ?? true,
Expand All @@ -35,6 +37,7 @@ extension CodeOptions: TomlCodable {
var lines: [String] = ["[update.code]"]

lines.append("codePaths = \(codePaths)")
lines.append("subpathsToIgnore = \(subpathsToIgnore)")
lines.append("localizablePaths = \(localizablePaths)")
lines.append("defaultToKeys = \(defaultToKeys)")
lines.append("additive = \(additive)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Toml

struct InterfacesOptions {
let paths: [String]
let subpathsToIgnore: [String]
let defaultToBase: Bool
let ignoreEmptyStrings: Bool
let unstripped: Bool
Expand All @@ -16,6 +17,7 @@ extension InterfacesOptions: TomlCodable {

return InterfacesOptions(
paths: toml.filePaths(update, interfaces, singularKey: "path", pluralKey: "paths"),
subpathsToIgnore: toml.array(update, interfaces, "subpathsToIgnore") ?? Constants.defaultSubpathsToIgnore,
defaultToBase: toml.bool(update, interfaces, "defaultToBase") ?? false,
ignoreEmptyStrings: toml.bool(update, interfaces, "ignoreEmptyStrings") ?? false,
unstripped: toml.bool(update, interfaces, "unstripped") ?? false,
Expand All @@ -27,10 +29,11 @@ extension InterfacesOptions: TomlCodable {
var lines: [String] = ["[update.interfaces]"]

lines.append("paths = \(paths)")
lines.append("subpathsToIgnore = \(subpathsToIgnore)")
lines.append("defaultToBase = \(defaultToBase)")
lines.append("ignoreEmptyStrings = \(ignoreEmptyStrings)")
lines.append("unstripped = \(unstripped)")
lines.append("ignoreKeys = \(Constants.defaultIgnoreKeys)")
lines.append("ignoreKeys = \(ignoreKeys)")

return lines.joined(separator: "\n")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Toml

struct NormalizeOptions {
let paths: [String]
let subpathsToIgnore: [String]
let sourceLocale: String
let harmonizeWithSource: Bool
let sortByKeys: Bool
Expand All @@ -15,6 +16,7 @@ extension NormalizeOptions: TomlCodable {

return NormalizeOptions(
paths: toml.filePaths(update, normalize, singularKey: "path", pluralKey: "paths"),
subpathsToIgnore: toml.array(update, normalize, "subpathsToIgnore") ?? Constants.defaultSubpathsToIgnore,
sourceLocale: toml.string(update, normalize, "sourceLocale") ?? "en",
harmonizeWithSource: toml.bool(update, normalize, "harmonizeWithSource") ?? true,
sortByKeys: toml.bool(update, normalize, "sortByKeys") ?? true
Expand All @@ -25,6 +27,7 @@ extension NormalizeOptions: TomlCodable {
var lines: [String] = ["[update.normalize]"]

lines.append("paths = \(paths)")
lines.append("subpathsToIgnore = \(subpathsToIgnore)")
lines.append("sourceLocale = \"\(sourceLocale)\"")
lines.append("harmonizeWithSource = \(harmonizeWithSource)")
lines.append("sortByKeys = \(sortByKeys)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Toml

struct TransformOptions {
let codePaths: [String]
let subpathsToIgnore: [String]
let localizablePaths: [String]
let transformer: Transformer
let supportedLanguageEnumPath: String
Expand All @@ -26,6 +27,7 @@ extension TransformOptions: TomlCodable {

return TransformOptions(
codePaths: toml.filePaths(update, transform, singularKey: "codePath", pluralKey: "codePaths"),
subpathsToIgnore: toml.array(update, transform, "subpathsToIgnore") ?? Constants.defaultSubpathsToIgnore,
localizablePaths: toml.filePaths(update, transform, singularKey: "localizablePath", pluralKey: "localizablePaths"),
transformer: transformer,
supportedLanguageEnumPath: toml.string(update, transform, "supportedLanguageEnumPath") ?? ".",
Expand All @@ -39,6 +41,7 @@ extension TransformOptions: TomlCodable {
var lines: [String] = ["[update.transform]"]

lines.append("codePaths = \(codePaths)")
lines.append("subpathsToIgnore = \(subpathsToIgnore)")
lines.append("localizablePaths = \(localizablePaths)")
lines.append("transformer = \"\(transformer.rawValue)\"")
lines.append("supportedLanguageEnumPath = \"\(supportedLanguageEnumPath)\"")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ enum Translator: String {

struct TranslateOptions {
let paths: [String]
let subpathsToIgnore: [String]
let secret: Secret
let sourceLocale: String
}
Expand All @@ -21,6 +22,7 @@ extension TranslateOptions: TomlCodable {
if let secretString: String = toml.string(update, translate, "secret") {
let translator = toml.string(update, translate, "translator") ?? ""
let paths = toml.filePaths(update, translate, singularKey: "path", pluralKey: "paths")
let subpathsToIgnore = toml.array(update, translate, "subpathsToIgnore") ?? Constants.defaultSubpathsToIgnore
let sourceLocale: String = toml.string(update, translate, "sourceLocale") ?? "en"
let secret: Secret
switch Translator(rawValue: translator) {
Expand All @@ -31,7 +33,12 @@ extension TranslateOptions: TomlCodable {
secret = .deepL(secret: secretString)
}

return TranslateOptions(paths: paths, secret: secret, sourceLocale: sourceLocale)
return TranslateOptions(
paths: paths,
subpathsToIgnore: subpathsToIgnore,
secret: secret,
sourceLocale: sourceLocale
)
} else {
throw MungoError(source: .invalidUserInput, message: "Incomplete [update.translate] options provided, ignoring them all.")
}
Expand All @@ -41,6 +48,7 @@ extension TranslateOptions: TomlCodable {
var lines: [String] = ["[update.translate]"]

lines.append("paths = \(paths)")
lines.append("subpathsToIgnore = \(subpathsToIgnore)")
switch secret {
case let .deepL(secret):
lines.append("secret = \"\(secret)\"")
Expand Down
14 changes: 6 additions & 8 deletions Sources/BartyCrouchKit/FileHandling/CodeFilesSearch.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import Foundation

final class CodeFilesSearch: FilesSearchable {
#warning("couple of ignored directories need to be turned into a configuration option")
static let dirsToIgnore = Set([".git", "carthage", "pods", "build", ".build", "docs"])

private let baseDirectoryPath: String
private let basePathComponents: [String]

Expand All @@ -12,18 +9,19 @@ final class CodeFilesSearch: FilesSearchable {
self.basePathComponents = URL(fileURLWithPath: baseDirectoryPath).pathComponents
}

func shouldSkipFile(at url: URL) -> Bool {
Set(url.pathComponents).subtracting(basePathComponents).contains { component in
Self.dirsToIgnore.contains(component.lowercased())
func shouldSkipFile(at url: URL, subpathsToIgnore: [String]) -> Bool {
#warning("TODO: write tests to make sure this works even when user provides ignore path 'Hello/World'")
return Set(url.pathComponents).subtracting(basePathComponents).contains { component in
subpathsToIgnore.contains(component.lowercased())
}
}

func findCodeFiles() -> [String] {
func findCodeFiles(subpathsToIgnore: [String]) -> [String] {
guard FileManager.default.fileExists(atPath: baseDirectoryPath) else { return [] }
guard !baseDirectoryPath.hasSuffix(".string") else { return [baseDirectoryPath] }

let codeFileRegex = try! NSRegularExpression(pattern: "\\.swift\\z", options: .caseInsensitive) // swiftlint:disable:this force_try
let codeFiles: [String] = findAllFilePaths(inDirectoryPath: baseDirectoryPath, matching: codeFileRegex)
let codeFiles: [String] = findAllFilePaths(inDirectoryPath: baseDirectoryPath, matching: codeFileRegex, subpathsToIgnore: subpathsToIgnore)
return codeFiles
}
}
12 changes: 9 additions & 3 deletions Sources/BartyCrouchKit/FileHandling/FilesSearchable.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import Foundation

protocol FilesSearchable {
func findAllFilePaths(inDirectoryPath baseDirectoryPath: String, matching regularExpression: NSRegularExpression, ignoreSuffixes: Set<String>) -> [String]
func findAllFilePaths(
inDirectoryPath baseDirectoryPath: String,
matching regularExpression: NSRegularExpression,
ignoreSuffixes: Set<String>,
subpathsToIgnore: [String]
) -> [String]
}

extension FilesSearchable {
func findAllFilePaths(
inDirectoryPath baseDirectoryPath: String,
matching regularExpression: NSRegularExpression,
ignoreSuffixes: Set<String> = []
ignoreSuffixes: Set<String> = [],
subpathsToIgnore: [String]
) -> [String] {
let baseDirectoryURL = URL(fileURLWithPath: baseDirectoryPath)
guard let enumerator = FileManager.default.enumerator(at: baseDirectoryURL, includingPropertiesForKeys: nil) else { return [] }
Expand All @@ -18,7 +24,7 @@ extension FilesSearchable {
let codeFilesSearch = CodeFilesSearch(baseDirectoryPath: baseDirectoryAbsolutePath)

for case let url as URL in enumerator {
if codeFilesSearch.shouldSkipFile(at: url) {
if codeFilesSearch.shouldSkipFile(at: url, subpathsToIgnore: subpathsToIgnore) {
enumerator.skipDescendants()
continue
}
Expand Down
19 changes: 11 additions & 8 deletions Sources/BartyCrouchKit/FileHandling/StringsFilesSearch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,42 @@ public final class StringsFilesSearch: FilesSearchable {
private static let denyListedStringFileNames: Set<String> = ["InfoPlist.strings"]

// MARK: - Instance Methods
public func findAllIBFiles(within baseDirectoryPath: String, withLocale locale: String = "Base") -> [String] {
public func findAllIBFiles(within baseDirectoryPath: String, withLocale locale: String = "Base", subpathsToIgnore: [String]) -> [String] {
// swiftlint:disable:next force_try
let ibFileRegex = try! NSRegularExpression(pattern: "^(.*\\/)?\(locale).lproj.*\\.(storyboard|xib)\\z", options: .caseInsensitive)
return self.findAllFilePaths(inDirectoryPath: baseDirectoryPath, matching: ibFileRegex)
return self.findAllFilePaths(inDirectoryPath: baseDirectoryPath, matching: ibFileRegex, subpathsToIgnore: subpathsToIgnore)
}

public func findAllStringsFiles(within baseDirectoryPath: String, withLocale locale: String) -> [String] {
public func findAllStringsFiles(within baseDirectoryPath: String, withLocale locale: String, subpathsToIgnore: [String]) -> [String] {
// swiftlint:disable:next force_try
let stringsFileRegex = try! NSRegularExpression(pattern: "^(.*\\/)?\(locale).lproj.*\\.strings\\z", options: .caseInsensitive)
return self.findAllFilePaths(
inDirectoryPath: baseDirectoryPath,
matching: stringsFileRegex,
ignoreSuffixes: StringsFilesSearch.denyListedStringFileNames
ignoreSuffixes: StringsFilesSearch.denyListedStringFileNames,
subpathsToIgnore: subpathsToIgnore
)
}

public func findAllStringsFiles(within baseDirectoryPath: String, withFileName fileName: String) -> [String] {
public func findAllStringsFiles(within baseDirectoryPath: String, withFileName fileName: String, subpathsToIgnore: [String]) -> [String] {
// swiftlint:disable:next force_try
let stringsFileRegex = try! NSRegularExpression(pattern: ".*\\.lproj/\(fileName)\\.strings\\z", options: .caseInsensitive)
return self.findAllFilePaths(
inDirectoryPath: baseDirectoryPath,
matching: stringsFileRegex,
ignoreSuffixes: StringsFilesSearch.denyListedStringFileNames
ignoreSuffixes: StringsFilesSearch.denyListedStringFileNames,
subpathsToIgnore: subpathsToIgnore
)
}

public func findAllStringsFiles(within baseDirectoryPath: String) -> [String] {
public func findAllStringsFiles(within baseDirectoryPath: String, subpathsToIgnore: [String]) -> [String] {
// swiftlint:disable:next force_try
let stringsFileRegex = try! NSRegularExpression(pattern: ".*\\.lproj/.+\\.strings\\z", options: .caseInsensitive)
return self.findAllFilePaths(
inDirectoryPath: baseDirectoryPath,
matching: stringsFileRegex,
ignoreSuffixes: StringsFilesSearch.denyListedStringFileNames
ignoreSuffixes: StringsFilesSearch.denyListedStringFileNames,
subpathsToIgnore: subpathsToIgnore
)
}

Expand Down
3 changes: 3 additions & 0 deletions Sources/BartyCrouchKit/Globals/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ import Foundation
enum Constants {
/// These keys can be places in the 'Comment for Localizer' in Interface Builder files to signal that they should be ignored from adding to Strings files.
static let defaultIgnoreKeys: [String] = ["#bartycrouch-ignore!", "#bc-ignore!", "#i!"]

/// Paths to be ignored while searching for code files to consider as sources for new translation keys added to the project.
static let defaultSubpathsToIgnore: [String] = [".git", "carthage", "pods", "build", ".build", "docs"]
}
9 changes: 5 additions & 4 deletions Sources/BartyCrouchKit/OldCommandLine/CodeCommander.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ public final class CodeCommander {
stringsFilesToPath stringsFilePath: String,
fromCodeInDirectoryPath codeDirectoryPath: String,
customFunction: String?,
usePlistArguments: Bool
usePlistArguments: Bool,
subpathsToIgnore: [String]
) throws {
let files = try findFiles(in: codeDirectoryPath)
let files = try findFiles(in: codeDirectoryPath, subpathsToIgnore: subpathsToIgnore)
let customFunctionArgs = customFunction != nil ? ["-s", "\(customFunction!)"] : []

let argumentsWithoutTheFiles = ["extractLocStrings"] + ["-o", stringsFilePath] + customFunctionArgs + ["-q"]
Expand All @@ -37,7 +38,7 @@ public final class CodeCommander {
try Task.run("/usr/bin/xcrun", arguments: arguments)
}

func findFiles(in codeDirectoryPath: String) throws -> [String] {
func findFiles(in codeDirectoryPath: String, subpathsToIgnore: [String]) throws -> [String] {
let fileManager = FileManager.default

var isDirectory: ObjCBool = false
Expand All @@ -58,7 +59,7 @@ public final class CodeCommander {

while let anURL = enumerator.nextObject() as? URL {
if CodeCommanderConstants.sourceCodeExtensions.contains(anURL.pathExtension) &&
!codeFilesSearch.shouldSkipFile(at: anURL) {
!codeFilesSearch.shouldSkipFile(at: anURL, subpathsToIgnore: subpathsToIgnore) {
matchedFiles.append(anURL.path)
}
}
Expand Down
Loading

0 comments on commit 773bdbf

Please sign in to comment.