Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slightly improved heuristic to find the target root #24

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
22 changes: 21 additions & 1 deletion Plugins/Enlighter/XcodeSPMCompat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,30 @@ extension XcodeTarget {
if let matching = directories.first(where: { $0.string.hasSuffix(match) }) {
return matching
}
if let root = findRootForTarget(named: name, in: directories) {
return root
}
// give up and use first :-)
print("Could not infer target root, using an arbitrary directory")
return directories[0]
}


func findRootForTarget(named name: String, in directories: [Path]) -> Path? {
// find the longest path containing the target name
let innerMatch = "/" + name + "/"
guard let path = directories
.filter({ $0.string.contains(innerMatch) })
.max(by: { $0.string.count > $1.string.count })
else { return nil }

// drop everything after the last path component that matches the target name
let components = path.string.split(separator: "/")
let index = components.distance(from: components.startIndex,
to: components.reversed().firstIndex(where: { $0 == name })!)
let targetPathComponents = components.prefix(components.count - index)
return Path("/").appending(targetPathComponents.map({ String($0) }))
}

func doesRecursivelyDependOnTarget(named name: String) -> Bool {
for dep in self.dependencies {
switch dep {
Expand Down
Loading