Skip to content

Modularize Package.swift #27

New issue

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

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

Already on GitHub? # to your account

Merged
merged 13 commits into from
Oct 11, 2024
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
18 changes: 18 additions & 0 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 84 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,102 @@ let package = Package(
platforms: [
.macOS(.v13)
],
products: [
.executable(
name: "public-api-diff",
targets: ["public-api-diff"]
),
.library(
name: "SwiftInterfaceDiff",
targets: [
"PADSwiftInterfaceDiff",
"PADOutputGenerator"
]
),
.library(
name: "PublicApiDiff",
targets: [
"PADProjectBuilder",
"PADSwiftInterfaceDiff",
"PADOutputGenerator"
]
)
],
dependencies: [
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.5.0"),
.package(url: "https://github.com/swiftlang/swift-syntax", from: "600.0.0"),
.package(url: "https://github.com/nicklockwood/SwiftFormat", from: "0.54.6")
.package(url: "https://github.com/nicklockwood/SwiftFormat", from: "0.54.6"),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0")
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.

// MARK: - Executable Targets

.executableTarget(
name: "public-api-diff",
dependencies: [
.product(name: "ArgumentParser", package: "swift-argument-parser"),
"PADProjectBuilder",
"PADSwiftInterfaceDiff",
"PADOutputGenerator",
.product(name: "ArgumentParser", package: "swift-argument-parser")
],
path: "Sources/ExecutableTargets/CommandLineTool"
),

// MARK: - Public Modules

.target(
name: "PADSwiftInterfaceDiff",
dependencies: [
"PADCore",
"FileHandlingModule",
"PADLogging",
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftParser", package: "swift-syntax"),
],
path: "Sources"
path: "Sources/PublicModules/PADSwiftInterfaceDiff"
),
.target(
name: "PADProjectBuilder",
dependencies: [
"PADCore",
"FileHandlingModule",
"PADLogging",
"ShellModule"
],
path: "Sources/PublicModules/PADProjectBuilder"
),
.target(
name: "PADOutputGenerator",
dependencies: ["PADCore"],
path: "Sources/PublicModules/PADOutputGenerator"
),

// MARK: - Shared/Public

.target(
name: "PADCore",
path: "Sources/Shared/Public/PADCore"
),
.target(
name: "PADLogging",
dependencies: ["FileHandlingModule"],
path: "Sources/Shared/Public/PADLogging"
),

// MARK: - Shared/Package

.target(
name: "FileHandlingModule",
path: "Sources/Shared/Package/FileHandlingModule"
),
.target(
name: "ShellModule",
path: "Sources/Shared/Package/ShellModule"
),

// MARK: - Test Targets

.testTarget(
name: "UnitTests",
dependencies: [
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ The `ChangeConsolidator` takes 2 independent changes (`addition` & `removal`) an

### OutputGenerator

Receives a dictionary of `[{SCOPE_NAME}: [Change]]` and processes them into a human readable format.
Receives a dictionary of `[{SCOPE_NAME}: [PADChange]]` and processes them into a human readable format.

# Alternatives
- `xcrun swift-api-digester -dump-sdk` & `xcrun swift-api-digester -diagnose-sdk`
Expand Down
131 changes: 131 additions & 0 deletions Sources/ExecutableTargets/CommandLineTool/public-api-diff.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
//
// Copyright (c) 2024 Adyen N.V.
//
// This file is open source and available under the MIT license. See the LICENSE file for more info.
//

import ArgumentParser
import Foundation

import PADCore
import PADLogging

import PADSwiftInterfaceDiff
import PADProjectBuilder
import PADOutputGenerator

@main
struct PublicApiDiff: AsyncParsableCommand {

@Option(help: "Specify the updated version to compare to")
public var new: String

@Option(help: "Specify the old version to compare to")
public var old: String

@Option(help: "Where to output the result (File path)")
public var output: String?

@Option(help: "Where to output the logs (File path)")
public var logOutput: String?

@Option(help: "Which scheme to build (Needed when comparing 2 swift frameworks)")
public var scheme: String?

public func run() async throws {

let logLevel: PADLogLevel = .debug
let projectType: PADProjectType = { // Only needed when we have to produce the .swiftinterface files
if let scheme { return .xcodeProject(scheme: scheme) }
return .swiftPackage
}()
let swiftInterfaceType: PADSwiftInterfaceType = .public // Only needed when we have to produce the .swiftinterface files

let logger = Self.logger(with: logLevel, logOutputFilePath: logOutput)

do {
var warnings = [String]()
var changes = [String: [PADChange]]()

// MARK: - Producing .swiftinterface files

let oldSource: PADProjectSource = try .from(old)
let newSource: PADProjectSource = try .from(new)

let oldVersionName = oldSource.description
let newVersionName = newSource.description

let projectBuilder = PADProjectBuilder(
projectType: projectType,
swiftInterfaceType: swiftInterfaceType,
logger: logger
)

let projectBuilderResult = try await projectBuilder.build(
oldSource: oldSource,
newSource: newSource
)

warnings += projectBuilderResult.warnings
if !projectBuilderResult.packageFileChanges.isEmpty {
changes["Package.swift"] = projectBuilderResult.packageFileChanges
}

// MARK: - Analyze .swiftinterface files

let pipeline = PADSwiftInterfaceDiff(logger: logger)

let pipelineOutput = try await pipeline.run(
with: projectBuilderResult.swiftInterfaceFiles
)

// Merging pipeline output into existing changes - making sure we're not overriding any keys
pipelineOutput.forEach { key, value in
var keyToUse = key
if changes[key] != nil {
keyToUse = "\(key) (\(UUID().uuidString))"
}
changes[keyToUse] = value
}

// MARK: - Generate Output

let outputGenerator: any PADOutputGenerating = PADMarkdownOutputGenerator()

let generatedOutput = try outputGenerator.generate(
from: changes,
allTargets: projectBuilderResult.swiftInterfaceFiles.map(\.name).sorted(),
oldVersionName: oldVersionName,
newVersionName: newVersionName,
warnings: warnings
)

if let output {
try FileManager.default.write(generatedOutput, to: output)
} else {
// We're not using a logger here as we always want to have it printed if no output was specified
print(generatedOutput)
}

logger.log("✅ Success", from: "Main")
} catch {
logger.log("💥 \(error.localizedDescription)", from: "Main")
}
}
}

private extension PublicApiDiff {

static func logger(
with logLevel: PADLogLevel,
logOutputFilePath: String?
) -> any PADLogging {
var loggers = [any PADLogging]()
if let logOutputFilePath {
loggers += [PADLogFileLogger(outputFilePath: logOutputFilePath)]
}
loggers += [PADSystemLogger().withLogLevel(logLevel)]

return PADLoggingGroup(with: loggers)
}
}
18 changes: 0 additions & 18 deletions Sources/Helpers/Logging/LoggingGroup.swift

This file was deleted.

12 changes: 0 additions & 12 deletions Sources/Helpers/Models/Constants.swift

This file was deleted.

25 changes: 0 additions & 25 deletions Sources/Helpers/SwiftInterfaceType.swift

This file was deleted.

62 changes: 0 additions & 62 deletions Sources/Pipeline/Pipeline+Protocols.swift

This file was deleted.

Loading
Loading