Skip to content

Commit

Permalink
Clean up logging in PIFSupport, use product name for target node values
Browse files Browse the repository at this point in the history
  • Loading branch information
NinjaLikesCheez committed May 14, 2024
1 parent c7ea905 commit 9d9a335
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 27 deletions.
6 changes: 2 additions & 4 deletions PIF/Sources/PIFSupport/PIF.swift
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,7 @@ public enum PIF {
case FileReference.type:
return try childrenContainer.decode(FileReference.self)
default:
// TODO: use logger here
print("unknown reference type: \(child.type ?? "<nil>")")
logger.debug("unknown reference type: \(child.type ?? "<nil>")")
return nil
}
}
Expand Down Expand Up @@ -508,8 +507,7 @@ public enum PIF {
case ResourcesBuildPhase.type:
return try container.decode(ResourcesBuildPhase.self)
default:
// TODO: replace with logger
print("unknown build phase: \(type)")
logger.debug("unknown build phase: \(type)")
return nil
// TODO: we should probably handle these:
/*
Expand Down
6 changes: 5 additions & 1 deletion PIF/Sources/PIFSupport/PIFSupport.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import Foundation
import Logging

var logger: Logger!

public class PIFParser {
private let cachePath: URL
Expand All @@ -8,7 +11,8 @@ public class PIFParser {
case workspaceNotFound
}

public init(cachePath: URL) throws {
public init(cachePath: URL, logger log: Logger) throws {
logger = log
self.cachePath = cachePath

let data = try Data(contentsOf: try Self.workspacePath(in: cachePath))
Expand Down
34 changes: 14 additions & 20 deletions Sources/GenIR/Dependency Graph/DependencyGraph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,6 @@ class DependencyGraph<Value: NodeValue> {
return depthFirstSearch(startingAt: node)
}

func toDot(_ path: String) throws {
var contents = "digraph DependencyGraph {\n"

for node in nodes.values {
for edge in node.edges.filter({ $0.relationship == .dependency }) {
func dotSanitized(for name: String) -> String {
name
.replacingOccurrences(of: "-", with: "_")
.replacingOccurrences(of: ".", with: "_")
}
contents.append("\(dotSanitized(for: node.valueName)) -> \(dotSanitized(for: edge.to.valueName))\n")
}
}

contents.append("}")
try contents.write(toFile: path, atomically: true, encoding: .utf8)
}

/// Perform a depth-first search starting at the provided node
/// - Parameter node: the node whose children to search through
/// - Returns: an array of nodes ordered by a depth-first search approach
Expand All @@ -76,9 +58,8 @@ class DependencyGraph<Value: NodeValue> {
logger.debug("visited: \(visited)")

for edge in node.edges where edge.relationship == .dependency {
logger.debug("edge to: \(edge.to)")
if visited.insert(edge.to).inserted {
logger.debug("inserted, recursing")
logger.debug("edge to: \(edge.to)")
depthFirst(node: edge.to)
} else {
logger.debug("edge already in visited: \(visited)")
Expand All @@ -92,6 +73,19 @@ class DependencyGraph<Value: NodeValue> {
depthFirst(node: node)
return chain
}

func toDot(_ path: String) throws {
var contents = "digraph DependencyGraph {\n"

for node in nodes.values {
for edge in node.edges.filter({ $0.relationship == .dependency }) {
contents.append("\"\(node.valueName)\" -> \"\(edge.to.valueName)\"\n")
}
}

contents.append("}")
try contents.write(toFile: path, atomically: true, encoding: .utf8)
}
}

extension DependencyGraph: CustomStringConvertible {
Expand Down
2 changes: 1 addition & 1 deletion Sources/GenIR/PIFCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class PIFCache {
self.pifCachePath = try Self.pifCachePath(in: buildCache)

do {
let cache = try PIFParser(cachePath: pifCachePath)
let cache = try PIFParser(cachePath: pifCachePath, logger: logger)
workspace = cache.workspace
} catch {
throw Error.pifError(error)
Expand Down
2 changes: 1 addition & 1 deletion Sources/GenIR/Target.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ extension Target {

extension Target: NodeValue {
var value: Self { self }
var valueName: String { name }
var valueName: String { productName }
}

extension Target: Hashable {
Expand Down

0 comments on commit 9d9a335

Please sign in to comment.