Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Introduce modularization #5

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion .github/workflows/test-macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ jobs:
- name: Checkout
uses: actions/checkout@v3
- name: Test
run: swift test
run: cd "Example" && swift test
8 changes: 8 additions & 0 deletions Example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.DS_Store
/.build
/Packages
xcuserdata/
DerivedData/
.swiftpm/configuration/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
52 changes: 52 additions & 0 deletions Example/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// swift-tools-version: 5.9

import PackageDescription

let package = Package(
name: "macro-toolkit-example",
products: [
.library(
name: "Example",
targets: ["Example"]
),
],
dependencies: [
// For basic functionality you can just depend on `swift-macro-toolkit`
// `swift-syntax` will be added as nested dependency. "SwiftSyntax", "SwiftDiagnostics",
// and "SwiftCompilerPlugin" targets will be importable
.package("../"),

// However `swift-syntax` is needed for testing
// You may consider using https://github.com/pointfreeco/swift-macro-testing instead
.package(
url: "https://github.com/apple/swift-syntax.git",
exact: "509.0.0"
),
],
targets: [
// Declares macros used by `swift-macro-toolkit` tests
.target(
name: "MacroToolkitExample",
dependencies: ["MacroToolkitExamplePlugin"]
),

// Implementations of macros tested by `swift-macro-toolkit` tests
.macro(
name: "MacroToolkitExamplePlugin",
dependencies: [
.product(name: "MacroToolkit", package: "swift-macro-toolkit")
]
),

.testTarget(
name: "MacroToolkitTests",
dependencies: [
"MacroToolkitExample",
.product(name: "MacroToolkit", package: "swift-macro-toolkit")
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
.product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax"),
]
),
]
)
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/SwiftPackageIndex/SPIManifest.git",
"state" : {
"revision" : "59b957c55c274ae08da9aaa36efcaac3fb7a326c",
"version" : "0.19.0"
"revision" : "5569d7aca29b7d7f4f793ba997bfaa72ca99fca7",
"version" : "0.12.0"
}
},
{
Expand Down
47 changes: 27 additions & 20 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,56 @@ let package = Package(
.library(
name: "MacroToolkit",
targets: ["MacroToolkit"]
),
.library(
name: "DiagnosticBuilder",
targets: ["DiagnosticBuilder"]
),
.library(
name: "MacroToolkitTypes",
targets: ["MacroToolkitTypes"]
)
],
dependencies: [
.package(
url: "https://github.com/apple/swift-syntax.git",
exact: "509.0.0"
),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.3.0"),
.package(url: "https://github.com/SwiftPackageIndex/SPIManifest.git", from: "0.12.0"),
.package(
url: "https://github.com/apple/swift-docc-plugin",
from: "1.3.0"
),
.package(
url: "https://github.com/SwiftPackageIndex/SPIManifest.git",
.upToNextMinor(from: "0.12.0")
),
],
targets: [
// Implementations of macros tested by tests
.macro(
name: "MacroToolkitExamplePlugin",
.target(
name: "MacroToolkit",
dependencies: [
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
"MacroToolkit",
.target(name: "DiagnosticBuilder"),
.target(name: "MacroToolkitTypes")
]
),

// Declares macros used by tests
.target(name: "MacroToolkitExample", dependencies: ["MacroToolkitExamplePlugin"]),

.target(
name: "MacroToolkit",
name: "DiagnosticBuilder",
dependencies: [
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
.product(name: "SwiftDiagnostics", package: "swift-syntax"),
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
]
),

.testTarget(
name: "MacroToolkitTests",
.target(
name: "MacroToolkitTypes",
dependencies: [
"MacroToolkitExample",
"MacroToolkit",
.target(name: "DiagnosticBuilder"),
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
.product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax"),
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
]
),
)
]
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,6 @@ import Foundation
import SwiftDiagnostics
import SwiftSyntax

// Taken from: https://github.com/DougGregor/swift-macro-examples/blob/f61ac7cdca8dc3557e53f86e7e03df1353908d3e/MacroExamplesPlugin/Diagnostics.swift
/// A simple diagnostic with a message, id, and severity.
public struct SimpleDiagnosticMessage: DiagnosticMessage, Error {
/// The human-readable message.
public let message: String
/// The unique diagnostic id (should be the same for all diagnostics produced by the same codepath).
public let diagnosticID: MessageID
/// The diagnostic's severity.
public let severity: DiagnosticSeverity

/// Creates a new diagnostic message.
public init(message: String, diagnosticID: MessageID, severity: DiagnosticSeverity) {
self.message = message
self.diagnosticID = diagnosticID
self.severity = severity
}
}

extension SimpleDiagnosticMessage: FixItMessage {
/// The unique fix-it id (should be the same for all fix-its produced by the same codepath).
public var fixItID: MessageID { diagnosticID }
}

/// A generic macro error. If you are making a widely used macro I'd encourage you
/// to instead provide more detailed diagnostics through the diagnostics API that
/// macros have access to.
public struct MacroError: LocalizedError {
let message: String

public init(_ message: String) {
self.message = message
}

public var errorDescription: String? {
message
}
}

/// A way to create rich diagnostics with no unnecessary boilerplate code. Only provide the
/// important details and the rest will be given sensible defaults.
public struct DiagnosticBuilder {
Expand Down
16 changes: 16 additions & 0 deletions Sources/DiagnosticBuilder/MacroError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Foundation

/// A generic macro error. If you are making a widely used macro I'd encourage you
/// to instead provide more detailed diagnostics through the diagnostics API that
/// macros have access to.
public struct MacroError: LocalizedError {
let message: String

public init(_ message: String) {
self.message = message
}

public var errorDescription: String? {
message
}
}
24 changes: 24 additions & 0 deletions Sources/DiagnosticBuilder/SimpleDiagnosticMessage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import SwiftDiagnostics

// Taken from: https://github.com/DougGregor/swift-macro-examples/blob/f61ac7cdca8dc3557e53f86e7e03df1353908d3e/MacroExamplesPlugin/Diagnostics.swift
/// A simple diagnostic with a message, id, and severity.
public struct SimpleDiagnosticMessage: DiagnosticMessage, Error {
/// The human-readable message.
public let message: String
/// The unique diagnostic id (should be the same for all diagnostics produced by the same codepath).
public let diagnosticID: MessageID
/// The diagnostic's severity.
public let severity: DiagnosticSeverity

/// Creates a new diagnostic message.
public init(message: String, diagnosticID: MessageID, severity: DiagnosticSeverity) {
self.message = message
self.diagnosticID = diagnosticID
self.severity = severity
}
}

extension SimpleDiagnosticMessage: FixItMessage {
/// The unique fix-it id (should be the same for all fix-its produced by the same codepath).
public var fixItID: MessageID { diagnosticID }
}
2 changes: 2 additions & 0 deletions Sources/MacroToolkit/Exports.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@_exported import DiagnosticBuilder
@_exported import MacroToolkitTypes
File renamed without changes.
1 change: 1 addition & 0 deletions Sources/MacroToolkitTypes/Diagnostics.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

File renamed without changes.
File renamed without changes.
File renamed without changes.