diff --git a/Makefile b/Makefile index 909f55f..26df0f9 100644 --- a/Makefile +++ b/Makefile @@ -42,6 +42,5 @@ graph: .PHONY: single single: leakDetect \ - --targetType singleFile \ --sdk macosx \ - --file temp.swift --verbose + --file fixture/temp.swift diff --git a/Package.swift b/Package.swift index 86aa3ac..e087dab 100644 --- a/Package.swift +++ b/Package.swift @@ -40,6 +40,7 @@ let package = Package( .product(name: "SourceKittenFramework", package: "SourceKitten"), .product(name: "SKClient", package: "TypeFill"), "LeakDetectKit", + "PathKit", ] ), diff --git a/README.md b/README.md index 40e730d..6d0e401 100644 --- a/README.md +++ b/README.md @@ -64,8 +64,4 @@ leakDetect \ --module LeakDetectorDemo \ --targetType xcworkspace \ --file LeakDetectorDemo.xcworkspace - -# Mode: -# * assign: detecting assign instance function `x = self.func` or `y(self.func)`. -# * capture: detecting capture instance in closure. ``` diff --git a/Sources/LeakDetect/Arguments+Ex.swift b/Sources/LeakDetect/Arguments+Ex.swift index f88f1a6..cc3ed0d 100644 --- a/Sources/LeakDetect/Arguments+Ex.swift +++ b/Sources/LeakDetect/Arguments+Ex.swift @@ -8,6 +8,7 @@ import ArgumentParser import Foundation import SKClient +import PathKit extension Reporter: ExpressibleByArgument { static let all: String = Reporter @@ -24,10 +25,37 @@ public enum TargetType: String, CaseIterable, ExpressibleByArgument { .map(\.rawValue) .joined(separator: "|") + case auto case xcodeproj case xcworkspace case singleFile case spm + + func detect(_ path: String) -> TargetType { + guard case .auto = self else { + return self + } + + if path.hasSuffix(".xcodeproj") { + return .xcodeproj + } + + if path.hasSuffix(".xcworkspace") { + return .xcworkspace + } + + if path.hasSuffix(".swift") && !path.hasSuffix("Package.swift") { + return .singleFile + } + + let path = Path(path) + let package = path + "Package.swift" + if path.isDirectory && package.exists { + return .spm + } + + return .singleFile + } } enum Mode: String, CaseIterable, ExpressibleByArgument { diff --git a/Sources/LeakDetect/Command.swift b/Sources/LeakDetect/Command.swift index 0d618f4..1ea950f 100644 --- a/Sources/LeakDetect/Command.swift +++ b/Sources/LeakDetect/Command.swift @@ -23,10 +23,6 @@ struct Command: ParsableCommand { --module LeakDetectorDemo \ --targetType xcworkspace \ --file LeakDetectorDemo.xcworkspace - - # Mode: - # * assign: detecting assign instance function `x = self.func` or `y(self.func)`. - # * capture: detecting capture instance in closure. """, version: "0.0.3" ) @@ -41,7 +37,7 @@ struct Command: ParsableCommand { var sdk: SDK = .iphonesimulator @Option(name: [.customLong("targetType", withSingleDash: false)], help: "[\(Reporter.all)]") - var targetType: TargetType = .xcodeproj + var targetType: TargetType = .auto @Option(name: [.customLong("module", withSingleDash: false)], help: "Name of Swift module to document (can't be used with `--single-file`)") var moduleName = "" @@ -60,8 +56,8 @@ struct Command: ParsableCommand { private var module: Module? { let moduleName = self.moduleName.isEmpty ? nil : self.moduleName - - switch targetType { + + switch targetType.detect(path) { case .spm: return Module(spmArguments: arguments, spmName: moduleName) case .singleFile: @@ -82,11 +78,13 @@ struct Command: ParsableCommand { self.moduleName, ] return Module(xcodeBuildArguments: arguments + newArgs, name: moduleName) + case .auto: + return nil } } mutating func run() throws { - if case .singleFile = targetType { + if case .singleFile = targetType.detect(path) { try SingleFilePipeline(path, arguments + [path] + sdk.pathArgs) .detect(reporter, verbose) return diff --git a/Sources/LeakDetectKit/Pipeline/Pipeline.swift b/Sources/LeakDetectKit/Pipeline/Pipeline.swift index fca3372..ba439ed 100644 --- a/Sources/LeakDetectKit/Pipeline/Pipeline.swift +++ b/Sources/LeakDetectKit/Pipeline/Pipeline.swift @@ -69,18 +69,6 @@ public struct SingleFilePipeline { let visitors = state2(client) try client.editorOpen() let count = try visitors.detect(client, reporter, isVerbose) - print("Found blocks: \(visitors.capture.subVisitors.count)") -// let ids = visitors.capture.leakVisitors -// .flatMap(\.ids) -// .map { -// $0.withoutTrivia().description -// } -// .joined(separator: "\n") -// -// print(""" -// Found blocks: -// \(ids) -// """) try client.editorClose() summery(count) } diff --git a/temp.swift b/fixture/temp.swift similarity index 100% rename from temp.swift rename to fixture/temp.swift