From 115f2b0af5b4090fa67bde8a928dd7dadce8d8e7 Mon Sep 17 00:00:00 2001 From: Gorbenko Roman <45801227+rofle100lvl@users.noreply.github.com> Date: Mon, 30 Sep 2024 17:11:59 +0200 Subject: [PATCH 1/2] Added public api --- Package.swift | 20 +++++++- Sources/Frontend/Commands/ScanCommand.swift | 2 + Sources/Frontend/GuidedSetup.swift | 1 + .../Project.swift | 49 ++++++++++++------- Sources/{Frontend => Scan}/Scan.swift | 16 ++++-- Sources/SourceGraph/SourceGraph.swift | 2 +- 6 files changed, 68 insertions(+), 22 deletions(-) rename Sources/{Frontend => ProjectDrivers}/Project.swift (77%) rename Sources/{Frontend => Scan}/Scan.swift (88%) diff --git a/Package.swift b/Package.swift index 3f4c684e5..2a74ece86 100644 --- a/Package.swift +++ b/Package.swift @@ -34,6 +34,7 @@ var targets: [PackageDescription.Target] = [ .executableTarget( name: "Frontend", dependencies: [ + .target(name: "Scan"), .target(name: "Shared"), .target(name: "Configuration"), .target(name: "SourceGraph"), @@ -114,6 +115,18 @@ var targets: [PackageDescription.Target] = [ .product(name: "FilenameMatcher", package: "swift-filename-matcher"), ] ), + .target( + name: "Scan", + dependencies: [ + .target(name: "Configuration"), + .target(name: "Indexer"), + .target(name: "PeripheryKit"), + .target(name: "ProjectDrivers"), + .target(name: "Shared"), + .target(name: "SourceGraph"), + .target(name: "Logger"), + ] + ), .target( name: "TestShared", dependencies: [ @@ -176,7 +189,12 @@ let package = Package( platforms: [.macOS(.v13)], products: [ .executable(name: "periphery", targets: ["Frontend"]), - .library(name: "PeripheryKit", targets: ["PeripheryKit"]), + .library(name: "PeripheryKit", targets: [ + "SourceGraph", + "Configuration", + "ProjectDrivers", + "Scan", + ]), ], dependencies: dependencies, targets: targets, diff --git a/Sources/Frontend/Commands/ScanCommand.swift b/Sources/Frontend/Commands/ScanCommand.swift index f0468b888..8dc85d65d 100644 --- a/Sources/Frontend/Commands/ScanCommand.swift +++ b/Sources/Frontend/Commands/ScanCommand.swift @@ -3,6 +3,8 @@ import Configuration import Foundation import Logger import PeripheryKit +import ProjectDrivers +import Scan import Shared import SystemPackage diff --git a/Sources/Frontend/GuidedSetup.swift b/Sources/Frontend/GuidedSetup.swift index 7a1ead2e1..ebdeeeee5 100644 --- a/Sources/Frontend/GuidedSetup.swift +++ b/Sources/Frontend/GuidedSetup.swift @@ -1,6 +1,7 @@ import Configuration import Foundation import Logger +import ProjectDrivers import Shared #if canImport(XcodeSupport) diff --git a/Sources/Frontend/Project.swift b/Sources/ProjectDrivers/Project.swift similarity index 77% rename from Sources/Frontend/Project.swift rename to Sources/ProjectDrivers/Project.swift index 1f4ba1b0b..dd69e9d28 100644 --- a/Sources/Frontend/Project.swift +++ b/Sources/ProjectDrivers/Project.swift @@ -1,22 +1,49 @@ import Configuration import Foundation import Logger -import ProjectDrivers import Shared import SystemPackage -final class Project { +public final class Project { let kind: ProjectKind private let configuration: Configuration private let shell: Shell private let logger: Logger - convenience init( + public convenience init( configuration: Configuration, shell: Shell, logger: Logger ) throws { + try self.init( + kind: Self.detectKind(configuration: configuration), + configuration: configuration, + shell: shell, + logger: logger + ) + } + + public init(configuration: Configuration) throws { + self.configuration = configuration + logger = Logger() + shell = Shell(logger: logger) + kind = try Self.detectKind(configuration: configuration) + } + + public init( + kind: ProjectKind, + configuration: Configuration, + shell: Shell, + logger: Logger + ) { + self.kind = kind + self.configuration = configuration + self.shell = shell + self.logger = logger + } + + static func detectKind(configuration: Configuration) throws -> ProjectKind { var kind: ProjectKind? if let path = configuration.project { @@ -33,22 +60,10 @@ final class Project { throw PeripheryError.usageError("Failed to identify project in the current directory. For Xcode projects use the '--project' option, and for SPM projects change to the directory containing the Package.swift.") } - self.init(kind: kind, configuration: configuration, shell: shell, logger: logger) - } - - init( - kind: ProjectKind, - configuration: Configuration, - shell: Shell, - logger: Logger - ) { - self.kind = kind - self.configuration = configuration - self.shell = shell - self.logger = logger + return kind } - func driver() throws -> ProjectDriver { + public func driver() throws -> ProjectDriver { switch kind { case let .xcode(projectPath): #if canImport(XcodeSupport) diff --git a/Sources/Frontend/Scan.swift b/Sources/Scan/Scan.swift similarity index 88% rename from Sources/Frontend/Scan.swift rename to Sources/Scan/Scan.swift index cfeee64f8..9cfe06fdf 100644 --- a/Sources/Frontend/Scan.swift +++ b/Sources/Scan/Scan.swift @@ -7,20 +7,30 @@ import ProjectDrivers import Shared import SourceGraph -final class Scan { +public final class Scan { private let configuration: Configuration private let logger: Logger private let graph: SourceGraph private let swiftVersion: SwiftVersion - required init(configuration: Configuration, logger: Logger, swiftVersion: SwiftVersion) { + public required init(configuration: Configuration, logger: Logger, swiftVersion: SwiftVersion) { self.configuration = configuration self.logger = logger self.swiftVersion = swiftVersion graph = SourceGraph(configuration: configuration) } - func perform(project: Project) throws -> [ScanResult] { + public init( + configuration: Configuration, + sourceGraph: SourceGraph + ) { + self.configuration = configuration + logger = Logger() + swiftVersion = .init(shell: Shell(logger: logger)) + graph = sourceGraph + } + + public func perform(project: Project) throws -> [ScanResult] { if !configuration.indexStorePath.isEmpty { logger.warn("When using the '--index-store-path' option please ensure that Xcode is not running. False-positives can occur if Xcode writes to the index store while Periphery is running.") diff --git a/Sources/SourceGraph/SourceGraph.swift b/Sources/SourceGraph/SourceGraph.swift index 65ecd535d..2f5b5654a 100644 --- a/Sources/SourceGraph/SourceGraph.swift +++ b/Sources/SourceGraph/SourceGraph.swift @@ -22,7 +22,7 @@ public final class SourceGraph { public private(set) var extensions: [Declaration: Set] = [:] private var allDeclarationsByKind: [Declaration.Kind: Set] = [:] - private var allExplicitDeclarationsByUsr: [String: Declaration] = [:] + public private(set) var allExplicitDeclarationsByUsr: [String: Declaration] = [:] private var moduleToExportingModules: [String: Set] = [:] private let configuration: Configuration From 1424b59317cee32fdf4d8002e54f31430c57a216 Mon Sep 17 00:00:00 2001 From: Gorbenko Roman <45801227+rofle100lvl@users.noreply.github.com> Date: Mon, 30 Sep 2024 17:24:13 +0200 Subject: [PATCH 2/2] Fixed periphery scan --- Sources/ProjectDrivers/BazelProjectDriver.swift | 10 +++++----- Sources/ProjectDrivers/GenericProjectDriver.swift | 4 ++-- Sources/ProjectDrivers/Project.swift | 1 + Sources/Scan/Scan.swift | 1 + 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/Sources/ProjectDrivers/BazelProjectDriver.swift b/Sources/ProjectDrivers/BazelProjectDriver.swift index 640eab1de..161cc3f15 100644 --- a/Sources/ProjectDrivers/BazelProjectDriver.swift +++ b/Sources/ProjectDrivers/BazelProjectDriver.swift @@ -4,18 +4,18 @@ import Logger import Shared import SystemPackage -public class BazelProjectDriver: ProjectDriver { - public static var isSupported: Bool { +class BazelProjectDriver: ProjectDriver { + static var isSupported: Bool { FilePath("MODULE.bazel").exists || FilePath("WORKSPACE").exists } - public static func build(configuration: Configuration, shell: Shell, logger: Logger) throws -> Self { + static func build(configuration: Configuration, shell: Shell, logger: Logger) throws -> Self { configuration.bazel = false // Generic project mode is used for the actual scan. configuration.reportExclude.append("**/bazel-out/**/*") return self.init(configuration: configuration, shell: shell, logger: logger) } - private static let topLevelKinds = [ + static let topLevelKinds = [ // rules_apple, iOS "ios_app_clip", "ios_application", @@ -81,7 +81,7 @@ public class BazelProjectDriver: ProjectDriver { self.fileManager = fileManager } - public func build() throws { + func build() throws { guard let executablePath = Bundle.main.executablePath else { fatalError("Expected executable path.") } diff --git a/Sources/ProjectDrivers/GenericProjectDriver.swift b/Sources/ProjectDrivers/GenericProjectDriver.swift index 6807e3643..1ab4be218 100644 --- a/Sources/ProjectDrivers/GenericProjectDriver.swift +++ b/Sources/ProjectDrivers/GenericProjectDriver.swift @@ -6,7 +6,7 @@ import Shared import SwiftIndexStore import SystemPackage -public final class GenericProjectDriver { +final class GenericProjectDriver { struct GenericConfig: Decodable { let indexstores: Set let plists: Set @@ -24,7 +24,7 @@ public final class GenericProjectDriver { private let testTargets: Set private let configuration: Configuration - public convenience init(genericProjectConfig: FilePath, configuration: Configuration) throws { + convenience init(genericProjectConfig: FilePath, configuration: Configuration) throws { guard genericProjectConfig.exists else { throw PeripheryError.pathDoesNotExist(path: genericProjectConfig.string) } diff --git a/Sources/ProjectDrivers/Project.swift b/Sources/ProjectDrivers/Project.swift index dd69e9d28..c7610dce6 100644 --- a/Sources/ProjectDrivers/Project.swift +++ b/Sources/ProjectDrivers/Project.swift @@ -24,6 +24,7 @@ public final class Project { ) } + // periphery:ignore public init(configuration: Configuration) throws { self.configuration = configuration logger = Logger() diff --git a/Sources/Scan/Scan.swift b/Sources/Scan/Scan.swift index 9cfe06fdf..9d6376abf 100644 --- a/Sources/Scan/Scan.swift +++ b/Sources/Scan/Scan.swift @@ -20,6 +20,7 @@ public final class Scan { graph = SourceGraph(configuration: configuration) } + // periphery:ignore public init( configuration: Configuration, sourceGraph: SourceGraph