Skip to content

Commit

Permalink
Separate Project Drivers to separate module (#785)
Browse files Browse the repository at this point in the history
  • Loading branch information
rofle100lvl authored Aug 18, 2024
1 parent 0690ce7 commit 1476ff3
Show file tree
Hide file tree
Showing 16 changed files with 67 additions and 54 deletions.
31 changes: 21 additions & 10 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,28 @@ dependencies.append(
)
#endif

var frontendDependencies: [PackageDescription.Target.Dependency] = [
.target(name: "Shared"),
.target(name: "SourceGraph"),
.target(name: "PeripheryKit"),
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "FilenameMatcher", package: "swift-filename-matcher")
var projectDriverDependencies: [PackageDescription.Target.Dependency] = [
.target(name: "SourceGraph"),
.target(name: "Shared"),
.target(name: "Indexer"),
]

#if os(macOS)
frontendDependencies.append(.target(name: "XcodeSupport"))
projectDriverDependencies.append(.target(name: "XcodeSupport"))
#endif


var targets: [PackageDescription.Target] = [
.executableTarget(
name: "Frontend",
dependencies: frontendDependencies
dependencies: [
.target(name: "Shared"),
.target(name: "SourceGraph"),
.target(name: "PeripheryKit"),
.target(name: "ProjectDrivers"),
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "FilenameMatcher", package: "swift-filename-matcher")
]
),
.target(
name: "PeripheryKit",
Expand All @@ -59,6 +65,10 @@ var targets: [PackageDescription.Target] = [
.product(name: "SwiftIndexStore", package: "swift-indexstore")
]
),
.target(
name: "ProjectDrivers",
dependencies: projectDriverDependencies
),
.target(
name: "SyntaxAnalysis",
dependencies: [
Expand All @@ -85,7 +95,8 @@ var targets: [PackageDescription.Target] = [
.target(
name: "TestShared",
dependencies: [
.target(name: "PeripheryKit")
.target(name: "PeripheryKit"),
.target(name: "ProjectDrivers")
],
path: "Tests/Shared"
),
Expand Down Expand Up @@ -128,9 +139,9 @@ targets.append(contentsOf: [
.testTarget(
name: "XcodeTests",
dependencies: [
.target(name: "ProjectDrivers"),
.target(name: "TestShared"),
.target(name: "PeripheryKit"),
.target(name: "XcodeSupport")
],
exclude: ["UIKitProject", "SwiftUIProject"]
)
Expand Down
6 changes: 1 addition & 5 deletions Sources/Frontend/Project.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import Foundation
import PeripheryKit
import ProjectDrivers
import Shared
import SystemPackage

#if canImport(XcodeSupport)
import XcodeSupport
#endif

final class Project {
static func identify() throws -> Self {
let configuration = Configuration.shared
Expand Down
1 change: 0 additions & 1 deletion Sources/Frontend/SPMProjectSetupGuide.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Foundation
import PeripheryKit
import Shared
import SystemPackage

Expand Down
1 change: 1 addition & 0 deletions Sources/Frontend/Scan.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import ProjectDrivers
import Indexer
import PeripheryKit
import Shared
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#if os(macOS)
import Foundation
import XcodeSupport
import Indexer
import PeripheryKit
import Shared
import SourceGraph
import SystemPackage
Expand Down Expand Up @@ -123,3 +124,4 @@ extension XcodeProjectDriver: ProjectDriver {
)
}
}
#endif
11 changes: 6 additions & 5 deletions Sources/PeripheryKit/SPM/SPM.swift → Sources/Shared/SPM.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Foundation
import Shared
import SystemPackage

public enum SPM {
Expand All @@ -10,22 +9,24 @@ public enum SPM {
}

public struct Package {
let path: FilePath = .current
public let path: FilePath = .current
let configuration: Configuration = .shared

public init() { }

var exists: Bool {
path.appending(packageFile).exists
}

func clean() throws {
public func clean() throws {
try Shell.shared.exec(["swift", "package", "clean"])
}

func build(additionalArguments: [String]) throws {
public func build(additionalArguments: [String]) throws {
try Shell.shared.exec(["swift", "build", "--build-tests"] + additionalArguments)
}

func testTargetNames() throws -> Set<String> {
public func testTargetNames() throws -> Set<String> {
let description = try load()
return description.targets.filter(\.isTestTarget).mapSet(\.name)
}
Expand Down
24 changes: 13 additions & 11 deletions Sources/XcodeSupport/XcodeProject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import Foundation
import Shared
import SystemPackage
import XcodeProj
final class XcodeProject: XcodeProjectlike {

public final class XcodeProject: XcodeProjectlike {
private static var cache: [FilePath: XcodeProject] = [:]

static func build(path: FilePath, referencedBy refPath: FilePath) throws -> XcodeProject? {
Expand All @@ -23,17 +24,18 @@ final class XcodeProject: XcodeProjectlike {
return try self.init(path: path)
}

let type: String = "project"
let path: FilePath
let sourceRoot: FilePath
let xcodeProject: XcodeProj
let name: String
public let type: String = "project"
public let path: FilePath
public let sourceRoot: FilePath
public let name: String
public private(set) var targets: Set<XcodeTarget> = []

let xcodeProject: XcodeProj

private let xcodebuild: Xcodebuild

private(set) var targets: Set<XcodeTarget> = []

required init(path: FilePath, xcodebuild: Xcodebuild = .init(), logger: Logger = .init()) throws {
public required init(path: FilePath, xcodebuild: Xcodebuild = .init(), logger: Logger = .init()) throws {
logger.contextualized(with: "xcode:project").debug("Loading \(path)")

self.path = path
Expand Down Expand Up @@ -65,19 +67,19 @@ final class XcodeProject: XcodeProjectlike {
.union(subProjects.flatMapSet { $0.targets })
}

func schemes(additionalArguments: [String]) throws -> Set<String> {
public func schemes(additionalArguments: [String]) throws -> Set<String> {
try xcodebuild.schemes(project: self, additionalArguments: additionalArguments)
}
}

extension XcodeProject: Hashable {
func hash(into hasher: inout Hasher) {
public func hash(into hasher: inout Hasher) {
hasher.combine(path.lexicallyNormalized().string)
}
}

extension XcodeProject: Equatable {
static func == (lhs: XcodeProject, rhs: XcodeProject) -> Bool {
public static func == (lhs: XcodeProject, rhs: XcodeProject) -> Bool {
lhs.path == rhs.path
}
}
4 changes: 2 additions & 2 deletions Sources/XcodeSupport/XcodeProjectlike.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation
import SystemPackage

protocol XcodeProjectlike: AnyObject {
public protocol XcodeProjectlike: AnyObject {
var path: FilePath { get }
var targets: Set<XcodeTarget> { get }
var type: String { get }
Expand All @@ -11,7 +11,7 @@ protocol XcodeProjectlike: AnyObject {
func schemes(additionalArguments: [String]) throws -> Set<String>
}

extension XcodeProjectlike {
public extension XcodeProjectlike {
var name: String {
path.lastComponent?.stem ?? ""
}
Expand Down
14 changes: 7 additions & 7 deletions Sources/XcodeSupport/XcodeTarget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import SourceGraph
import SystemPackage
import XcodeProj

final class XcodeTarget {
public final class XcodeTarget {
let project: XcodeProject

private let target: PBXTarget
Expand All @@ -14,15 +14,15 @@ final class XcodeTarget {
self.target = target
}

var isTestTarget: Bool {
public var isTestTarget: Bool {
target.productType?.rawValue.contains("test") ?? false
}

var name: String {
public var name: String {
target.name
}

func identifyFiles() throws {
public func identifyFiles() throws {
let sourcesBuildPhases = project.xcodeProject.pbxproj.sourcesBuildPhases
let resourcesBuildPhases = project.xcodeProject.pbxproj.resourcesBuildPhases

Expand All @@ -32,7 +32,7 @@ final class XcodeTarget {
try identifyInfoPlistFiles()
}

func files(kind: ProjectFileKind) -> Set<FilePath> {
public func files(kind: ProjectFileKind) -> Set<FilePath> {
files[kind, default: []]
}

Expand Down Expand Up @@ -75,13 +75,13 @@ final class XcodeTarget {
}

extension XcodeTarget: Hashable {
func hash(into hasher: inout Hasher) {
public func hash(into hasher: inout Hasher) {
hasher.combine(target.name)
}
}

extension XcodeTarget: Equatable {
static func == (lhs: XcodeTarget, rhs: XcodeTarget) -> Bool {
public static func == (lhs: XcodeTarget, rhs: XcodeTarget) -> Bool {
lhs.name == rhs.name
}
}
15 changes: 8 additions & 7 deletions Sources/XcodeSupport/XcodeWorkspace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ import Foundation
import Shared
import SystemPackage
import XcodeProj
final class XcodeWorkspace: XcodeProjectlike {
let type: String = "workspace"
let path: FilePath
let sourceRoot: FilePath

public final class XcodeWorkspace: XcodeProjectlike {
public let type: String = "workspace"
public let path: FilePath
public let sourceRoot: FilePath

private let xcodebuild: Xcodebuild
private let configuration: Configuration
private let xcworkspace: XCWorkspace

private(set) var targets: Set<XcodeTarget> = []
public private(set) var targets: Set<XcodeTarget> = []

required init(path: FilePath, xcodebuild: Xcodebuild = .init(), configuration: Configuration = .shared, logger: Logger = .init()) throws {
public required init(path: FilePath, xcodebuild: Xcodebuild = .init(), configuration: Configuration = .shared, logger: Logger = .init()) throws {
logger.contextualized(with: "xcode:workspace").debug("Loading \(path)")

self.path = path
Expand All @@ -35,7 +36,7 @@ final class XcodeWorkspace: XcodeProjectlike {
}
}

func schemes(additionalArguments: [String]) throws -> Set<String> {
public func schemes(additionalArguments: [String]) throws -> Set<String> {
try xcodebuild.schemes(project: self, additionalArguments: additionalArguments)
}

Expand Down
6 changes: 3 additions & 3 deletions Sources/XcodeSupport/Xcodebuild.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public final class Xcodebuild {
}

@discardableResult
func build(project: XcodeProjectlike, scheme: String, allSchemes: [String], additionalArguments: [String] = []) throws -> String {
public func build(project: XcodeProjectlike, scheme: String, allSchemes: [String], additionalArguments: [String] = []) throws -> String {
let args = [
"-\(project.type)", "'\(project.path.lexicallyNormalized().string)'",
"-scheme", "'\(scheme)'",
Expand All @@ -54,11 +54,11 @@ public final class Xcodebuild {
return try shell.exec(["/bin/sh", "-c", xcodebuild])
}

func removeDerivedData(for project: XcodeProjectlike, allSchemes: [String]) throws {
public func removeDerivedData(for project: XcodeProjectlike, allSchemes: [String]) throws {
try shell.exec(["rm", "-rf", try derivedDataPath(for: project, schemes: allSchemes).string])
}

func indexStorePath(project: XcodeProjectlike, schemes: [String]) throws -> FilePath {
public func indexStorePath(project: XcodeProjectlike, schemes: [String]) throws -> FilePath {
let derivedDataPath = try derivedDataPath(for: project, schemes: schemes)
let pathsToTry = ["Index.noindex/DataStore", "Index/DataStore"]
.map { derivedDataPath.appending($0) }
Expand Down
2 changes: 1 addition & 1 deletion Tests/Shared/SPMSourceGraphTestCase.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation
import PeripheryKit
import Shared
import ProjectDrivers
import SystemPackage

class SPMSourceGraphTestCase: SourceGraphTestCase {
Expand Down
2 changes: 1 addition & 1 deletion Tests/XcodeTests/XcodeSourceGraphTestCase.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Foundation
import Shared
import ProjectDrivers
import SystemPackage
@testable import TestShared
import XcodeSupport

class XcodeSourceGraphTestCase: SourceGraphTestCase {
static func build(projectPath: FilePath) {
Expand Down

0 comments on commit 1476ff3

Please sign in to comment.