Skip to content

Commit

Permalink
Slightly improved heuristic to find the target root
Browse files Browse the repository at this point in the history
Use the longest path where one of the components is the target name
  • Loading branch information
micampe committed Oct 7, 2023
1 parent 7350642 commit 1d2ecfe
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
8 changes: 4 additions & 4 deletions Plugins/Enlighter/Enlighter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ struct Enlighter: BuildToolPlugin {
configuration: configuration
)
guard !groups.isEmpty else {
print("Target contains not matching files:", target.name)
debugLog("Target contains not matching files:", target.name)
print("Could not find matching files in", target.directory)
debugLog("Could not find matching files in", target.directory)
return []
}

Expand Down Expand Up @@ -340,8 +340,8 @@ extension Enlighter: XcodeBuildToolPlugin {
configuration: configuration
)
guard !groups.isEmpty else {
print("Target contains not matching files:", target.name)
debugLog("Target contains not matching files:", target.name)
print("Could not find matching files in", target.directory)
debugLog("Could not find matching files in", target.directory)
return []
}

Expand Down
26 changes: 26 additions & 0 deletions Plugins/Enlighter/XcodeSPMCompat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,26 @@ extension XcodeProject {
}
}

extension Collection where Element == Path {
func longestPathPrefix() -> Path {
let sorted = sorted() { $0.string.count < $1.string.count }
guard let short = sorted.first,
let long = sorted.last
else { return first ?? Path("/tmp/") }

let shortComponents = short.string.split(separator: "/")
let longComponents = long.string.split(separator: "/")

for (i, c) in shortComponents.enumerated() {
if c != longComponents[i] {
return Path("/").appending(shortComponents.prefix(i).map({ String($0) }))
}
}

return short
}
}

extension XcodeTarget {

var name : String { product?.name ?? displayName }
Expand All @@ -54,7 +74,13 @@ extension XcodeTarget {
if let matching = directories.first(where: { $0.string.hasSuffix(match) }) {
return matching
}
let innerMatch = "/" + name + "/"
let matching = directories.filter({ $0.string.contains(innerMatch) })
if matching.count > 0 {
return matching.longestPathPrefix()
}
// give up and use first :-)
print("Could not infer target root, using an arbitrary directory")
return directories[0]
}

Expand Down

0 comments on commit 1d2ecfe

Please sign in to comment.